Changeset 396
- Timestamp:
- 10/14/07 07:11:37 (1 year ago)
- Files:
-
- branches/kaste/framework/lib/Ak.php (modified) (1 diff)
- branches/kaste/framework/lib/AkActiveRecord.php (modified) (6 diffs)
- branches/kaste/framework/lib/AkActiveRecord/AkActsAsList.php (modified) (9 diffs)
- branches/kaste/framework/lib/AkActiveRecord/AkActsAsNestedSet.php (modified) (1 diff)
- branches/kaste/framework/lib/AkActiveRecord/AkActsAsTree.php (modified) (1 diff)
- branches/kaste/framework/lib/AkActiveRecord/AkDbAdapter.php (added)
- branches/kaste/framework/lib/AkActiveRecord/AkDbAdapter_mysql.php (added)
- branches/kaste/framework/lib/AkActiveRecord/AkDbAdapter_pgsql.php (added)
- branches/kaste/framework/lib/AkActiveRecord/AkDbAdapter_sqlite.php (added)
- branches/kaste/framework/lib/AkActiveRecord/AkHasAndBelongsToMany.php (modified) (1 diff)
- branches/kaste/framework/lib/AkDbManager.php (modified) (1 diff)
- branches/kaste/framework/lib/AkInstaller.php (modified) (4 diffs)
- branches/kaste/framework/lib/constants.php (modified) (2 diffs)
- branches/kaste/framework/test/fixtures/config/config.php (modified) (1 diff)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsList.php (modified) (2 diffs)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsNestedSet.php (modified) (2 diffs)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsTree.php (modified) (2 diffs)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/AkObserver.php (modified) (1 diff)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/AkValidation.php (modified) (1 diff)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/_AkActiveRecord_1.php (modified) (3 diffs)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/_AkActiveRecord_locking.php (modified) (1 diff)
- branches/kaste/framework/test/unit/lib/AkInstaller.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/kaste/framework/lib/Ak.php
r383 r396 64 64 function &db($dsn = null, $connection_id = null) 65 65 { 66 require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkDbAdapter.php'); 67 return AkDbAdapter::getConnection(); 66 68 static $db, $default_connection_id; 67 69 branches/kaste/framework/lib/AkActiveRecord.php
r395 r396 20 20 21 21 require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkAssociatedActiveRecord.php'); 22 require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkDbAdapter.php'); 22 23 23 24 /**#@+ … … 189 190 var $_tableName; 190 191 var $_db; 192 //var $_db_settings = ; 191 193 var $_newRecord; 192 194 var $_freeze; … … 2462 2464 function setConnection($dns = null, $connection_id = null) 2463 2465 { 2464 $this->_db =& Ak::db($dns, $connection_id); 2466 //$this->_db =& Ak::db($dns, $connection_id); 2467 $this->_db =& AkDbAdapter::getConnection(); 2465 2468 } 2466 2469 … … 2470 2473 function _getDatabaseType() 2471 2474 { 2472 if(strstr($this->_db->databaseType,'mysql')){ 2473 return 'mysql'; 2474 }elseif(strstr($this->_db->databaseType,'sqlite')){ 2475 return 'sqlite'; 2476 }elseif(strstr($this->_db->databaseType,'post')){ 2477 return 'postgre'; 2478 }else{ 2479 return 'unknown'; 2480 } 2475 return $this->_db->type(); 2481 2476 } 2482 2477 /*/Database connection*/ … … 2712 2707 if(empty($this->_columnsSettings) || !AK_ACTIVE_RECORD_ENABLE_PERSISTENCE){ 2713 2708 if(empty($this->_dataDictionary)){ 2714 $this->_dataDictionary =& NewDataDictionary($this->_db );2709 $this->_dataDictionary =& NewDataDictionary($this->_db->connection); 2715 2710 } 2716 2711 … … 4788 4783 $resulting_array = array(); 4789 4784 if(!empty($source_array) && is_array($source_array) && func_num_args() > 1) { 4790 (array)$args = array_filter(array_slice(func_get_args(),1),array($this,'hasColumn'));4791 foreach ($source_array as $source_item){4792 $item_fields = array();4793 foreach ($args as $arg){4794 $item_fields[$arg] =& $source_item->get($arg);4795 }4796 $resulting_array[] =& $item_fields;4797 }4785 (array)$args = array_filter(array_slice(func_get_args(),1),array($this,'hasColumn')); 4786 foreach ($source_array as $source_item){ 4787 $item_fields = array(); 4788 foreach ($args as $arg){ 4789 $item_fields[$arg] =& $source_item->get($arg); 4790 } 4791 $resulting_array[] =& $item_fields; 4792 } 4798 4793 } 4799 4794 return $resulting_array; branches/kaste/framework/lib/AkActiveRecord/AkActsAsList.php
r374 r396 137 137 } 138 138 139 /** 140 * This function saves the object using save() before inserting it into the list 141 */ 142 function insertAtPosition($position) 143 { 144 $this->_ActiveRecordInstance->transactionStart(); 145 if($this->_ActiveRecordInstance->isNewRecord()){ 146 $this->_ActiveRecordInstance->save(); 147 } 148 $this->removeFromList(); 149 $this->incrementPositionsOnLowerItems($position); 150 151 $this->_ActiveRecordInstance->updateAttribute($this->column, $position); 152 if($this->_ActiveRecordInstance->transactionHasFailed()){ 153 $this->_ActiveRecordInstance->transactionComplete(); 154 return false; 155 } 156 157 $this->_ActiveRecordInstance->transactionComplete(); 158 return true; 159 } 160 139 161 function moveLower() 140 162 { … … 167 189 } 168 190 169 170 191 function moveToBottom() 171 192 { … … 182 203 return false; 183 204 } 184 185 /** 186 * This has the effect of moving all the lower items up one. 187 */ 188 function decrementPositionsOnLowerItems() 189 { 190 if($this->isInList()){ 191 $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} - 1)", $this->getScopeCondition()." AND {$this->column} > ".$this->_ActiveRecordInstance->getAttribute($this->column)); 192 return true; 193 } 194 return false; 195 } 196 197 function assumeBottomPosition() 198 { 199 return $this->_ActiveRecordInstance->updateAttribute($this->column, $this->getBottomPosition($this->_ActiveRecordInstance->getId()) + 1); 200 } 201 202 function getBottomPosition($except = null) 203 { 204 return ($item = $this->getBottomItem($except)) ? $item->getAttribute($this->column) : 0; 205 } 206 207 /** 208 * Returns an instance of the item that's on the very bottom of the list. Returns false if there's none 209 */ 210 function getBottomItem($except = null) 211 { 212 $conditions = $this->getScopeCondition(); 213 214 if(isset($except)){ 215 $conditions .= " AND id != $except"; 216 } 217 return $this->_ActiveRecordInstance->find('first', array('conditions' => $conditions, 'order' => "{$this->column} DESC")); 218 } 219 220 221 function isInList() 222 { 223 return !empty($this->_ActiveRecordInstance->{$this->column}); 224 } 225 226 205 227 206 function moveToTop() 228 207 { … … 239 218 return false; 240 219 } 220 221 function assumeBottomPosition() 222 { 223 return $this->_ActiveRecordInstance->updateAttribute($this->column, $this->getBottomPosition($this->_ActiveRecordInstance->getId()) + 1); 224 } 225 226 function assumeTopPosition() 227 { 228 return $this->_ActiveRecordInstance->updateAttribute($this->column, 1); 229 } 230 231 function getBottomPosition($except = null) 232 { 233 return ($item = $this->getBottomItem($except)) ? $item->getAttribute($this->column) : 0; 234 } 235 236 /** 237 * Returns an instance of the item that's on the very bottom of the list. Returns false if there's none 238 */ 239 function getBottomItem($except = null) 240 { 241 $conditions = $this->getScopeCondition(); 242 243 if(isset($except)){ 244 $conditions .= " AND id != $except"; 245 } 246 return $this->_ActiveRecordInstance->find('first', array('conditions' => $conditions, 'order' => "{$this->column} DESC")); 247 } 248 249 function isInList() 250 { 251 return !empty($this->_ActiveRecordInstance->{$this->column}); 252 } 253 254 /** 255 * This has the effect of moving all the higher items up one. 256 */ 257 function decrementPositionsOnHigherItems($position) 258 { 259 return $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} - 1)", $this->getScopeCondition()." AND {$this->column} <= $position"); 260 } 261 262 /** 263 * This has the effect of moving all the lower items up one. 264 */ 265 function decrementPositionsOnLowerItems() 266 { 267 if($this->isInList()){ 268 $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} - 1)", $this->getScopeCondition()." AND {$this->column} > ".$this->_ActiveRecordInstance->getAttribute($this->column)); 269 return true; 270 } 271 return false; 272 } 241 273 242 274 /** … … 251 283 return false; 252 284 } 253 254 function assumeTopPosition() 255 { 256 return $this->_ActiveRecordInstance->updateAttribute($this->column, 1); 257 } 258 259 285 286 /** 287 * This has the effect of moving all the lower items down one. 288 */ 289 function incrementPositionsOnLowerItems($position) 290 { 291 return $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} + 1)", $this->getScopeCondition()." AND {$this->column} >= $position"); 292 } 293 294 function incrementPositionsOnAllItems() 295 { 296 return $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} + 1)", $this->getScopeCondition()); 297 } 298 260 299 function removeFromList() 261 300 { … … 269 308 } 270 309 271 272 310 function incrementPosition() 273 311 { … … 317 355 return false; 318 356 } 319 320 357 321 358 function addToListTop() … … 336 373 // True condition in case we don't have a scope 337 374 }elseif(empty($this->scope_condition) && empty($this->scope)){ 338 $this->scope_condition = ( substr($this->_ActiveRecordInstance->_db->databaseType,0,4) == 'post') ? 'true' : '1';375 $this->scope_condition = ($this->_ActiveRecordInstance->_db->type() == 'postgre') ? 'true' : '1'; 339 376 }elseif (!empty($this->scope)){ 340 377 $this->setScopeCondition(join(' AND ',array_map(array(&$this,'getScopedColumn'),(array)$this->scope))); … … 364 401 } 365 402 } 366 367 368 /**369 * This has the effect of moving all the higher items up one.370 */371 function decrementPositionsOnHigherItems($position)372 {373 return $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} - 1)", $this->getScopeCondition()." AND {$this->column} <= $position");374 }375 376 /**377 * This has the effect of moving all the lower items down one.378 */379 function incrementPositionsOnLowerItems($position)380 {381 return $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} + 1)", $this->getScopeCondition()." AND {$this->column} >= $position");382 }383 384 function incrementPositionsOnAllItems()385 {386 return $this->_ActiveRecordInstance->updateAll("{$this->column} = ({$this->column} + 1)", $this->getScopeCondition());387 }388 389 390 391 /**392 * This function saves the object using save() before inserting it into the list393 */394 function insertAtPosition($position)395 {396 $this->_ActiveRecordInstance->transactionStart();397 if($this->_ActiveRecordInstance->isNewRecord()){398 $this->_ActiveRecordInstance->save();399 }400 $this->removeFromList();401 $this->incrementPositionsOnLowerItems($position);402 403 $this->_ActiveRecordInstance->updateAttribute($this->column, $position);404 if($this->_ActiveRecordInstance->transactionHasFailed()){405 $this->_ActiveRecordInstance->transactionComplete();406 return false;407 }408 409 $this->_ActiveRecordInstance->transactionComplete();410 return true;411 }412 403 } 413 404 branches/kaste/framework/lib/AkActiveRecord/AkActsAsNestedSet.php
r374 r396 158 158 // True condition in case we don't have a scope 159 159 }elseif(empty($this->scope_condition) && empty($this->scope)){ 160 $this->scope_condition = ( substr($this->_ActiveRecordInstance->_db->databaseType,0,4) == 'post') ? 'true' : '1';160 $this->scope_condition = ($this->_ActiveRecordInstance->_db->type() == 'postgre') ? 'true' : '1'; 161 161 }elseif (!empty($this->scope)){ 162 162 $this->setScopeCondition(join(' AND ',array_map(array(&$this,'getScopedColumn'),(array)$this->scope))); branches/kaste/framework/lib/AkActiveRecord/AkActsAsTree.php
r374 r396 166 166 // True condition in case we don't have a scope 167 167 }elseif(empty($this->scope_condition) && empty($this->scope)){ 168 $this->scope_condition = ( substr($this->_ActiveRecordInstance->_db->databaseType,0,4) == 'post') ? 'true' : '1';168 $this->scope_condition = ($this->_ActiveRecordInstance->_db->type() == 'postgre') ? 'true' : '1'; 169 169 }elseif (!empty($this->scope)){ 170 170 $this->setScopeCondition(join(' AND ',array_diff(array_map(array(&$this,'getScopedColumn'),(array)$this->scope),array('')))); branches/kaste/framework/lib/AkActiveRecord/AkHasAndBelongsToMany.php
r336 r396 603 603 $options = $this->getOptions($this->association_id); 604 604 if(empty($options['finder_sql'])){ 605 $ sqlite = substr($this->Owner->_db->databaseType,0,6) == 'sqlite';605 $is_sqlite = $this->Owner->_db->type() == 'sqlite'; 606 606 $options['finder_sql'] = "SELECT {$options['table_name']}.* FROM {$options['table_name']} ". 607 $this->associationJoin().608 "WHERE ".$this->Owner->getTableName().'.'.$this->Owner->getPrimaryKey()." ".609 ($sqlite ? ' LIKE ' : ' = ').' '.$this->Owner->quotedId(); // (HACK FOR SQLITE) Otherwise returns wrong data610 $options['finder_sql'] .= !empty($options['conditions']) ? ' AND '.$options['conditions'].' ' : '';611 $options['finder_sql'] .= !empty($options['conditions']) ? ' AND '.$options['conditions'].' ' : '';607 $this->associationJoin(). 608 "WHERE ".$this->Owner->getTableName().'.'.$this->Owner->getPrimaryKey()." ". 609 ($is_sqlite ? ' LIKE ' : ' = ').' '.$this->Owner->quotedId(); // (HACK FOR SQLITE) Otherwise returns wrong data 610 $options['finder_sql'] .= !empty($options['conditions']) ? ' AND '.$options['conditions'].' ' : ''; 611 $options['finder_sql'] .= !empty($options['conditions']) ? ' AND '.$options['conditions'].' ' : ''; 612 612 } 613 613 if(empty($options['counter_sql'])){ branches/kaste/framework/lib/AkDbManager.php
r285 r396 40 40 } 41 41 42 $dict = NewDataDictionary($db );42 $dict = NewDataDictionary($db->connection); 43 43 $sqlarray = $dict->CreateTableSQL($table_name, $table_fields, $table_options); 44 44 $dict->ExecuteSQLArray($sqlarray); branches/kaste/framework/lib/AkInstaller.php
r350 r396 36 36 { 37 37 if(empty($db_connection)){ 38 $this->db =& Ak ::db();38 $this->db =& AkDbAdapter::getConnection(); 39 39 }else { 40 40 $this->db =& $db_connection; 41 41 } 42 42 43 44 $this->db->connection->debug = $this->debug; 45 //$this->db->debug = $this->debug; 46 47 $this->data_dictionary = NewDataDictionary($this->db->connection); 43 48 $this->available_tables = $this->getAvailableTables(); 44 45 $this->db->debug =& $this->debug;46 47 $this->data_dictionary = NewDataDictionary($this->db);48 49 } 49 50 … … 77 78 $this->vervose = false; 78 79 }elseif(!empty($this->vervose) && AK_ENVIRONMENT == 'development'){ 79 $this->db-> debug = true;80 $this->db->connection->debug = true; 80 81 } 81 82 … … 233 234 function renameColumn($table_name, $old_column_name, $new_column_name) 234 235 { 235 if( !strstr($this->db->databaseType,'mysql')){236 if($this->db->type() != 'mysql'){ 236 237 trigger_error(Ak::t('Column renaming is only supported when using MySQL databases'), E_USER_ERROR); 237 238 } … … 510 511 function _requiresSequenceTable($column_string) 511 512 { 512 if( preg_match('/mysql|postgres/', $this->db->databaseType)){513 if(in_array($this->db->type(),array('mysql','postgre'))){ 513 514 return false; 514 515 } branches/kaste/framework/lib/constants.php
r370 r396 22 22 if(AK_ENVIRONMENT != 'setup'){ 23 23 $akdb = $database_settings[strtolower(AK_ENVIRONMENT)]; 24 $GLOBALS['default_database_settings'] = $database_settings[strtolower(AK_ENVIRONMENT)]; 24 25 $dsn = $akdb['type'] == 'sqlite' ? 25 26 'sqlite://'.urlencode($akdb['database_file']).'/?persist' : … … 112 113 @ini_set("include_path",(AK_LIB_DIR.PATH_SEPARATOR.AK_MODELS_DIR.PATH_SEPARATOR.AK_CONTRIB_DIR.DS.'pear'.PATH_SEPARATOR.ini_get("include_path"))); 113 114 defined('AK_PHP5') ? null : define('AK_PHP5', version_compare(PHP_VERSION, '5', '>=') == 1 ? true : false); 115 /* +++++++++++++ PHP5 ++++++++++++ */ if (!AK_PHP5) die('This branch is PHP5 only right now!'); 114 116 115 117 if(!AK_CLI && AK_WEB_REQUEST){ branches/kaste/framework/test/fixtures/config/config.php
r335 r396 43 43 include_once(AK_LIB_DIR.DS.'Ak.php'); 44 44 Ak::db(&$dsn); 45 //require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkDbAdapter.php'); 46 //AkDbAdapter::getConnection($GLOBALS['default_database_settings']); 45 47 } 46 48 branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsList.php
r394 r396 100 100 } 101 101 102 $dict = NewDataDictionary($db );102 $dict = NewDataDictionary($db->connection); 103 103 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 104 104 $dict->ExecuteSQLArray($sqlarray); … … 171 171 { 172 172 $TodoItems =& new AkTestTodoItem(); 173 $this->assertEqual($TodoItems->list->getScopeCondition(), ( substr($TodoItems->_db->databaseType,0,4) == 'post') ? 'true' : '1');173 $this->assertEqual($TodoItems->list->getScopeCondition(), ($TodoItems->_db->type() == 'postgre') ? 'true' : '1'); 174 174 $TodoItems->list->setScopeCondition('true'); 175 175 $this->assertEqual($TodoItems->list->getScopeCondition(), 'true'); branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsNestedSet.php
r374 r396 111 111 } 112 112 113 $dict = NewDataDictionary($db );113 $dict = NewDataDictionary($db->connection); 114 114 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 115 115 $dict->ExecuteSQLArray($sqlarray); … … 183 183 { 184 184 $Categories =& new AkTestNestedCategory(); 185 $this->assertEqual($Categories->nested_set->getScopeCondition(), ( substr($Categories->_db->databaseType,0,4) == 'post') ? 'true' : '1');185 $this->assertEqual($Categories->nested_set->getScopeCondition(), ($Categories->_db->type() == 'postgre') ? 'true' : '1'); 186 186 $Categories->nested_set->setScopeCondition('true'); 187 187 $this->assertEqual($Categories->nested_set->getScopeCondition(), 'true'); branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsTree.php
r374 r396 124 124 } 125 125 126 $dict = NewDataDictionary($db );126 $dict = NewDataDictionary($db->connection); 127 127 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 128 128 $dict->ExecuteSQLArray($sqlarray); … … 194 194 { 195 195 $Categories =& new AkTestCategory(); 196 $this->assertEqual($Categories->tree->getScopeCondition(), ( substr($Categories->_db->databaseType,0,4) == 'post') ? 'true' : '1');196 $this->assertEqual($Categories->tree->getScopeCondition(), ($Categories->_db->type() == 'postgre') ? 'true' : '1'); 197 197 $Categories->tree->setScopeCondition('true'); 198 198 $this->assertEqual($Categories->tree->getScopeCondition(), 'true'); branches/kaste/framework/test/unit/lib/AkActiveRecord/AkObserver.php
r217 r396 225 225 } 226 226 227 $dict = NewDataDictionary($db );227 $dict = NewDataDictionary($db->connection); 228 228 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 229 229 $dict->ExecuteSQLArray($sqlarray); branches/kaste/framework/test/unit/lib/AkActiveRecord/AkValidation.php
r395 r396 138 138 } 139 139 140 $dict = NewDataDictionary($db );140 $dict = NewDataDictionary($db->connection); 141 141 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 142 142 $dict->ExecuteSQLArray($sqlarray); branches/kaste/framework/test/unit/lib/AkActiveRecord/_AkActiveRecord_1.php
r323 r396 209 209 } 210 210 211 $dict = NewDataDictionary($db );211 $dict = NewDataDictionary($db->connection); 212 212 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 213 213 $dict->ExecuteSQLArray($sqlarray); … … 217 217 } 218 218 219 strstr($db->databaseType,'sqlite') ? $db->CreateSequence('seq_'.$table['table_name']) : null;219 ($db->type() == 'sqlite') ? $db->CreateSequence('seq_'.$table['table_name']) : null; 220 220 221 221 $this->_testing_model_databases_to_delete[] = $table_name; … … 233 233 foreach ($this->_testing_model_databases_to_delete as $table_name){ 234 234 $db->Execute('DROP TABLE '.$table_name); 235 strstr($db->databaseType,'sqlite') ? $db->DropSequence('seq_'.$table_name) : null;235 ($db->type() == 'sqlite') ? $db->DropSequence('seq_'.$table_name) : null; 236 236 } 237 237 } branches/kaste/framework/test/unit/lib/AkActiveRecord/_AkActiveRecord_locking.php
r217 r396 107 107 } 108 108 109 $dict = NewDataDictionary($db );109 $dict = NewDataDictionary($db->connection); 110 110 $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 111 111 $dict->ExecuteSQLArray($sqlarray); branches/kaste/framework/test/unit/lib/AkInstaller.php
r323 r396 26 26 function Test_setup_expected_returns() 27 27 { 28 if(strstr($this->Installer->db->databaseType, 'mysql')){ 29 $db_type = 'mysql'; 30 }elseif(strstr($this->Installer->db->databaseType, 'sqlite')){ 31 $db_type = 'sqlite'; 32 }elseif(strstr($this->Installer->db->databaseType, 'postgres')){ 33 $db_type = 'postgres'; 34 } 28 $db_type = $db->type(); 35 29 36 30 switch ($db_type) {
