Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
10 changes: 6 additions & 4 deletions Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class Wave_Auth {

public static $_is_loaded = false;

public static function registerHandler($class){
public static function registerHandler($class, $autoload = true){
if(!class_implements($class))
throw new Wave_Exception('Auth Handler class ('.$class.') must implement Wave_IAuthable');

$class::loadPersistentAuth();
if($autoload) $class::loadPersistentAuth();

self::$_handler = $class;

Expand Down Expand Up @@ -70,7 +70,7 @@ public static function registerIdentity($identity){
return Wave_Registry::store('__wave_identity', $identity);
}

public static function deregisterIdentity($identity){
public static function deregisterIdentity(){
return Wave_Registry::destroy('__wave_identity');
}

Expand All @@ -88,7 +88,9 @@ public static function persistIdentity($identity, $type = null, $expires = null)
$identity,
strtotime($expires),
$config->cookie->path,
$config->cookie->domain
$config->cookie->domain,
isset($config->cookie->secure) ? $config->cookie->secure : false,
isset($config->cookie->httponly) ? $config->cookie->httponly : true
);
}

Expand Down
7 changes: 4 additions & 3 deletions Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function register(){
static public function autoload($class){
$search_paths = array();
$skip_app = false;

if (substr($class, 0, 5) === 'Wave_') {
$filename = substr($class, 5);
$search_paths[] = WAVE_CORE_PATH . strtr($filename, '_', DS).'.php';
Expand Down Expand Up @@ -50,8 +50,9 @@ static public function autoload($class){
}

//if still not found, try with alias for model
if(Wave_DB::get() !== null){
$alias_class = Wave_DB::get()->getNamespace().Wave_DB::NS_SEPARATOR.$class;
if(Wave_DB::getDefaultNamespace() !== null){
$alias_class = Wave_DB::getDefaultNamespace().Wave_DB::NS_SEPARATOR.$class;

$filename = Wave_Config::get('wave')->path->models . strtr($alias_class, '_', DS) . '.php';
if(file_exists($filename) && include_once($filename)){
Wave_Debug::getInstance()->addUsedFile($filename, __FUNCTION__);
Expand Down
10 changes: 5 additions & 5 deletions Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

class Wave_Core {

const MODE_TEST = 0;
const MODE_DEVELOPMENT = 1;
const MODE_PRODUCTION = 2;

static $_MODE = self::MODE_PRODUCTION;
const MODE_TEST = 'test';
const MODE_DEVELOPMENT = 'development';
const MODE_PRODUCTION = 'production';

static $_MODE = self::MODE_PRODUCTION;

public static function bootstrap($mode = null){

if($mode == null)
Expand Down
201 changes: 126 additions & 75 deletions DB.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function __construct($config){

$this->connection = new Wave_DB_Connection($config);
$this->config = $config;
if(Wave_Core::$_MODE == Wave_Core::MODE_DEVELOPMENT)

if(in_array(Wave_Core::$_MODE, array(Wave_Core::MODE_DEVELOPMENT, Wave_Core::MODE_TEST)))
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

Expand All @@ -37,36 +37,33 @@ public static function save($object){
}

}

//CRUDE IMPLEMENTATION TO BE REPLACED vvvvvvvv (euphemism?)
public static function insert($object){

$schema = $object::_getSchemaName();
$db = self::get($schema)->getConnection();


public static function insert($object){

$table = $object::_getTableName();
$data = $object->_getDataArray();

$values = '';
foreach($data as $key => $value){
if($value === null || (is_string($value) && $value === ''))
$values .= 'NULL,';
elseif($value instanceof DateTime)
$values .= '"'.$value->format('Y-m-d H:i:s').'",';
else
$values .= '"'.addslashes($value).'",';
$data = array();
$object_data = $object->_getDataArray();
foreach(array_keys($object->_getFields()) as $field){
if(isset($object_data[$field]))
$data[$field] = $object_data[$field];
}

$fields = '`'.implode('`,`', array_keys($data)).'`';
$values = trim($values,',');

$sql = "INSERT INTO `$table` ($fields) VALUES ($values)";

$schema = $object::_getSchemaName();
$conn = self::get($schema)->getConnection();

// WHAT THE HELL, SOMEONE PUT A GROSS ON HERE
$params = array();
$values = array_map(array($conn->getDriverClass(), 'convertValueForSQL'), array_values($data));

$fields = implode('`,`', array_keys($data));
$placeholders = implode(',', array_fill(0, count($values), '?'));

$db->exec($sql);
$sql = sprintf('INSERT INTO `%s` (`%s`) VALUES (%s)', $table, $fields, $placeholders);

$conn->prepare($sql)->execute($values);

$liid = $db->lastInsertId();
$liid = intval($conn->lastInsertId());
if($liid !== 0){
$keys = $object::_getKeys(Wave_DB_Column::INDEX_PRIMARY);
if(count($keys) === 1){
Expand All @@ -75,67 +72,90 @@ public static function insert($object){
}
}

return true;
return $object->_isLoaded();
}


public static function update($object){

$schema = $object::_getSchemaName();
$db = self::get($schema)->getConnection();


$keys = $object::_getKeys(Wave_DB_Column::INDEX_PRIMARY);
$table = $object::_getTableName();
$data = $object->_getDataArray();
$schema = $object::_getSchemaName();

if(count($keys) === 0)
throw new Wave_Exception("No primary key defined for $schema.");

$dirty = $object->_getDirtyArray();
$keys = $object::_getKeys(Wave_DB_Column::INDEX_PRIMARY);
$data = $object->_getDataArray();
$fields = $object->_getFields();

$sql = "UPDATE $table SET ";
$conn = self::get($schema)->getConnection();

$updates = array();
$params = array();
$driver_class = $conn->getDriverClass();
foreach($dirty as $key => $value){
if(!array_key_exists($key, $data)) continue;
if(!isset($fields[$key])) continue;

if($data[$key] === null || (is_string($data[$key]) && $data[$key] === ''))
$updates[] = "`$key` = NULL";
elseif($data[$key] instanceof DateTime)
$updates[] = "`$key` = '".$data[$key]->format('Y-m-d H:i:s')."'";
elseif(!is_object($data[$key]) && !is_array($data[$key]))
$updates[] = "`$key` = '".addslashes($data[$key])."'";
$updates[] = "`$key` = ?";
$params[] = $driver_class::convertValueForSQL($data[$key]);
}

$where = array();
foreach($keys as $key){
$where[] = "`$key` = ?";
$params[] = $object->$key;
}

if(!isset($updates[0])) return false;

$sql = sprintf('UPDATE `%s` SET %s WHERE %s LIMIT 1;', $table, implode(',', $updates), implode(' AND ', $where));
$conn->prepare($sql)->execute($params);

return true;
}

if(!isset($updates[0]))
return true;

//remove comma
$sql .= implode(', ', $updates);
public static function delete(&$object){

$keys = $object::_getKeys(Wave_DB_Column::INDEX_PRIMARY);
$table = $object::_getTableName();
$schema = $object::_getSchemaName();

if(count($keys) === 0)
throw new Wave_Exception("No primary key defined for $schema.");

$sql .= " WHERE ";
$conn = self::get($schema)->getConnection();

$_keys = array();
$params = array();
$where = array();
foreach($keys as $key){
$_keys[] = "`$key` = '{$object->$key}'";
$where[] = "`$key` = ?";
$params[] = $object->$key;
}
$sql .= implode(' AND ', $_keys);

return $db->exec($sql);
$sql = sprintf('DELETE FROM `%s` WHERE %s LIMIT 1;', $table, implode(' AND ', $where));

$conn->prepare($sql)->execute($params);

$object->_setLoaded(false);

return true;
}




public function rawQuery($sql){
$db = $this->getConnection();
$db->exec($sql);
}

//FOR BOTH OF THESE ^^^^^^^^^^


/**
* Function to return the results of a basic query
*/
public function basicQuery($sql, $params = array()){


$statement = $this->basicStatement($sql, $params);

return $statement->fetchAll();
}

Expand Down Expand Up @@ -167,13 +187,10 @@ public function getNamespace(){

public function isDefault(){
return isset($database->default) && $database->default == true;
}


}

public static function init($database){


$installed_drivers = Wave_DB_Connection::getAvailableDrivers();

$driver_class = self::getDriverClass($database->driver);
Expand All @@ -195,25 +212,59 @@ public static function init($database){

self::$num_databases++;


return self::$instances[$database->namespace];

}

public static function get($namespace = null){
public static function get($namespace = null, $mode = null){

$databases = Wave_Config::get('db')->databases;

//if no db spec, return default
if($namespace === null)
$namespace = isset(self::$default) ? self::$default : $databases[0]['namespace'];

if(!isset(self::$instances[$namespace])){

foreach($databases as $database){
if($database->namespace === $namespace)
self::init($database);
}

if($namespace === null){
if(isset(self::$default))
$namespace = self::$default;
else {
$namespace = self::getDefaultNamespace();
}
}

if(!isset($databases[$namespace])){
throw new Wave_Exception("There is no database configuration for {$namespace}");
}

if($mode === null)
$mode = isset($databases[$namespace][Wave_Core::$_MODE])
? Wave_Core::$_MODE
: Wave_Core::MODE_PRODUCTION;

if(!isset($databases[$namespace][$mode])){
throw new Wave_Exception('There must be at least a PRODUCTION database defined');
}
else {
$databases[$namespace][$mode]->namespace = $namespace;
$databases[$namespace][$mode]->mode = $mode;

return isset(self::$instances[$namespace])
? self::$instances[$namespace]
: self::init($databases[$namespace][$mode]);
}
}

public static function set($namespace, Wave_DB_Connection $connection){
if(isset(self::$instances[$namespace])){
self::$instances[$namespace]->connection = $connection;
return self::$instances[$namespace];
}
return null;
}

public static function getDefaultNamespace(){
if(!Wave_Config::get('db')) return null;

foreach(Wave_Config::get('db')->databases as $ns => $database){
return $ns;
}

return isset(self::$instances[$namespace]) ? self::$instances[$namespace] : null;
}

public static function getNumDatabases(){
Expand All @@ -223,8 +274,8 @@ public static function getNumDatabases(){
public static function getAllDatabases(){

$databases = Wave_Config::get('db')->databases;
foreach($databases as $database)
self::init($database);
foreach($databases as $namespace => $modes)
self::get($namespace);

return self::$instances;
}
Expand Down
Empty file modified DB/Column.php
100644 → 100755
Empty file.
15 changes: 15 additions & 0 deletions DB/Driver/MySQL.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static function translateSQLDataType($type){
case 'tinyint':
return Wave_DB_Column::TYPE_BOOL;

case 'datetime':
case 'timestamp':
return Wave_DB_Column::TYPE_TIMESTAMP;

Expand Down Expand Up @@ -97,6 +98,20 @@ public static function translateSQLNullable($nullable){
}
}

public static function convertValueForSQL($value){

switch(true){
case is_null($value):
return null;

case $value instanceof DateTime:
return $value->format('Y-m-d H:i:s');

default:
return $value;
}
}



}
Expand Down
Loading