Changeset 396

Show
Ignore:
Timestamp:
10/14/07 07:11:37 (1 year ago)
Author:
kaste
Message:

basic implementation of a database adapter in the middle of AR and ADODb

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/kaste/framework/lib/Ak.php

    r383 r396  
    6464    function &db($dsn = null, $connection_id = null) 
    6565    { 
     66        require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkDbAdapter.php'); 
     67        return AkDbAdapter::getConnection(); 
    6668        static $db, $default_connection_id; 
    6769 
  • branches/kaste/framework/lib/AkActiveRecord.php

    r395 r396  
    2020 
    2121require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkAssociatedActiveRecord.php'); 
     22require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkDbAdapter.php'); 
    2223 
    2324/**#@+ 
     
    189190    var $_tableName; 
    190191    var $_db; 
     192    //var $_db_settings = ; 
    191193    var $_newRecord; 
    192194    var $_freeze; 
     
    24622464    function setConnection($dns = null, $connection_id = null) 
    24632465    { 
    2464         $this->_db =& Ak::db($dns, $connection_id); 
     2466        //$this->_db =& Ak::db($dns, $connection_id); 
     2467        $this->_db =& AkDbAdapter::getConnection(); 
    24652468    } 
    24662469     
     
    24702473    function _getDatabaseType() 
    24712474    { 
    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(); 
    24812476    } 
    24822477    /*/Database connection*/ 
     
    27122707        if(empty($this->_columnsSettings) || !AK_ACTIVE_RECORD_ENABLE_PERSISTENCE){ 
    27132708            if(empty($this->_dataDictionary)){ 
    2714                 $this->_dataDictionary =& NewDataDictionary($this->_db); 
     2709                $this->_dataDictionary =& NewDataDictionary($this->_db->connection); 
    27152710            } 
    27162711 
     
    47884783        $resulting_array = array(); 
    47894784        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           
    47984793        } 
    47994794        return $resulting_array; 
  • branches/kaste/framework/lib/AkActiveRecord/AkActsAsList.php

    r374 r396  
    137137    } 
    138138 
     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     
    139161    function moveLower() 
    140162    { 
     
    167189    } 
    168190 
    169  
    170191    function moveToBottom() 
    171192    { 
     
    182203        return false; 
    183204    } 
    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     
    227206    function moveToTop() 
    228207    { 
     
    239218        return false; 
    240219    } 
     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    } 
    241273 
    242274    /** 
     
    251283        return false; 
    252284    } 
    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     
    260299    function removeFromList() 
    261300    { 
     
    269308    } 
    270309 
    271  
    272310    function incrementPosition() 
    273311    { 
     
    317355        return false; 
    318356    } 
    319  
    320357 
    321358    function addToListTop() 
     
    336373        // True condition in case we don't have a scope 
    337374        }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'; 
    339376        }elseif (!empty($this->scope)){ 
    340377            $this->setScopeCondition(join(' AND ',array_map(array(&$this,'getScopedColumn'),(array)$this->scope))); 
     
    364401        } 
    365402    } 
    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 list 
    393     */ 
    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     } 
    412403} 
    413404 
  • branches/kaste/framework/lib/AkActiveRecord/AkActsAsNestedSet.php

    r374 r396  
    158158            // True condition in case we don't have a scope 
    159159        }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'; 
    161161        }elseif (!empty($this->scope)){ 
    162162            $this->setScopeCondition(join(' AND ',array_map(array(&$this,'getScopedColumn'),(array)$this->scope))); 
  • branches/kaste/framework/lib/AkActiveRecord/AkActsAsTree.php

    r374 r396  
    166166        // True condition in case we don't have a scope 
    167167        }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'; 
    169169        }elseif (!empty($this->scope)){ 
    170170            $this->setScopeCondition(join(' AND ',array_diff(array_map(array(&$this,'getScopedColumn'),(array)$this->scope),array('')))); 
  • branches/kaste/framework/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r336 r396  
    603603        $options = $this->getOptions($this->association_id); 
    604604        if(empty($options['finder_sql'])){ 
    605             $sqlite = substr($this->Owner->_db->databaseType,0,6) == 'sqlite'; 
     605            $is_sqlite = $this->Owner->_db->type() == 'sqlite'; 
    606606            $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 data 
    610             $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'].' ' : ''; 
    612612        } 
    613613        if(empty($options['counter_sql'])){ 
  • branches/kaste/framework/lib/AkDbManager.php

    r285 r396  
    4040        } 
    4141         
    42         $dict = NewDataDictionary($db); 
     42        $dict = NewDataDictionary($db->connection); 
    4343        $sqlarray = $dict->CreateTableSQL($table_name, $table_fields, $table_options); 
    4444        $dict->ExecuteSQLArray($sqlarray); 
  • branches/kaste/framework/lib/AkInstaller.php

    r350 r396  
    3636    { 
    3737        if(empty($db_connection)){ 
    38             $this->db =& Ak::db(); 
     38            $this->db =& AkDbAdapter::getConnection(); 
    3939        }else { 
    4040            $this->db =& $db_connection; 
    4141        } 
    4242 
     43 
     44        $this->db->connection->debug = $this->debug; 
     45        //$this->db->debug = $this->debug; 
     46 
     47        $this->data_dictionary = NewDataDictionary($this->db->connection); 
    4348        $this->available_tables = $this->getAvailableTables(); 
    44  
    45         $this->db->debug =& $this->debug; 
    46  
    47         $this->data_dictionary = NewDataDictionary($this->db); 
    4849    } 
    4950 
     
    7778            $this->vervose = false; 
    7879        }elseif(!empty($this->vervose) && AK_ENVIRONMENT == 'development'){ 
    79             $this->db->debug = true; 
     80            $this->db->connection->debug = true; 
    8081        } 
    8182 
     
    233234    function renameColumn($table_name, $old_column_name, $new_column_name) 
    234235    { 
    235         if(!strstr($this->db->databaseType,'mysql')){ 
     236        if($this->db->type() != 'mysql'){ 
    236237            trigger_error(Ak::t('Column renaming is only supported when using MySQL databases'), E_USER_ERROR); 
    237238        } 
     
    510511    function _requiresSequenceTable($column_string) 
    511512    { 
    512         if(preg_match('/mysql|postgres/', $this->db->databaseType)){ 
     513        if(in_array($this->db->type(),array('mysql','postgre'))){ 
    513514            return false; 
    514515        } 
  • branches/kaste/framework/lib/constants.php

    r370 r396  
    2222if(AK_ENVIRONMENT != 'setup'){ 
    2323    $akdb = $database_settings[strtolower(AK_ENVIRONMENT)]; 
     24    $GLOBALS['default_database_settings'] = $database_settings[strtolower(AK_ENVIRONMENT)]; 
    2425    $dsn = $akdb['type'] == 'sqlite' ? 
    2526    'sqlite://'.urlencode($akdb['database_file']).'/?persist' : 
     
    112113@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"))); 
    113114defined('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!'); 
    114116 
    115117if(!AK_CLI && AK_WEB_REQUEST){ 
  • branches/kaste/framework/test/fixtures/config/config.php

    r335 r396  
    4343    include_once(AK_LIB_DIR.DS.'Ak.php'); 
    4444    Ak::db(&$dsn); 
     45    //require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkDbAdapter.php'); 
     46    //AkDbAdapter::getConnection($GLOBALS['default_database_settings']); 
    4547} 
    4648 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsList.php

    r394 r396  
    100100        } 
    101101 
    102         $dict = NewDataDictionary($db); 
     102        $dict = NewDataDictionary($db->connection); 
    103103        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    104104        $dict->ExecuteSQLArray($sqlarray); 
     
    171171    { 
    172172        $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'); 
    174174        $TodoItems->list->setScopeCondition('true'); 
    175175        $this->assertEqual($TodoItems->list->getScopeCondition(), 'true'); 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsNestedSet.php

    r374 r396  
    111111        } 
    112112 
    113         $dict = NewDataDictionary($db); 
     113        $dict = NewDataDictionary($db->connection); 
    114114        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    115115        $dict->ExecuteSQLArray($sqlarray); 
     
    183183    { 
    184184        $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'); 
    186186        $Categories->nested_set->setScopeCondition('true'); 
    187187        $this->assertEqual($Categories->nested_set->getScopeCondition(), 'true'); 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/AkActsAsTree.php

    r374 r396  
    124124        } 
    125125 
    126         $dict = NewDataDictionary($db); 
     126        $dict = NewDataDictionary($db->connection); 
    127127        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    128128        $dict->ExecuteSQLArray($sqlarray); 
     
    194194    { 
    195195        $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'); 
    197197        $Categories->tree->setScopeCondition('true'); 
    198198        $this->assertEqual($Categories->tree->getScopeCondition(), 'true'); 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/AkObserver.php

    r217 r396  
    225225        } 
    226226 
    227         $dict = NewDataDictionary($db); 
     227        $dict = NewDataDictionary($db->connection); 
    228228        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    229229        $dict->ExecuteSQLArray($sqlarray); 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/AkValidation.php

    r395 r396  
    138138        } 
    139139 
    140         $dict = NewDataDictionary($db); 
     140        $dict = NewDataDictionary($db->connection); 
    141141        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    142142        $dict->ExecuteSQLArray($sqlarray); 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/_AkActiveRecord_1.php

    r323 r396  
    209209        } 
    210210 
    211         $dict = NewDataDictionary($db); 
     211        $dict = NewDataDictionary($db->connection); 
    212212        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    213213        $dict->ExecuteSQLArray($sqlarray); 
     
    217217        } 
    218218         
    219         strstr($db->databaseType,'sqlite') ? $db->CreateSequence('seq_'.$table['table_name']) : null; 
     219        ($db->type() == 'sqlite') ? $db->CreateSequence('seq_'.$table['table_name']) : null; 
    220220 
    221221        $this->_testing_model_databases_to_delete[] = $table_name; 
     
    233233        foreach ($this->_testing_model_databases_to_delete as $table_name){ 
    234234            $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; 
    236236        } 
    237237    } 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/_AkActiveRecord_locking.php

    r217 r396  
    107107        } 
    108108 
    109         $dict = NewDataDictionary($db); 
     109        $dict = NewDataDictionary($db->connection); 
    110110        $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); 
    111111        $dict->ExecuteSQLArray($sqlarray); 
  • branches/kaste/framework/test/unit/lib/AkInstaller.php

    r323 r396  
    2626    function Test_setup_expected_returns() 
    2727    { 
    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(); 
    3529 
    3630        switch ($db_type) {