Changeset 436

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

removed AkActiveRecord::sqlSelect* (now in AkDbAdapter?)
removed AkBaseModel::_*Sql* (unused)
finally all ADODb::Execute-calls throughout the framework point to AkDbAdapter::sqlexecute
(Note: sqlexecute will be renamed to execute later)

Files:

Legend:

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

    r430 r436  
    579579        } 
    580580         
    581         AK_LOG_EVENTS ? ($this->Logger->message($this->getModelName().' executing SQL: '.$sql)) : null; 
    582         $rs = $this->_db->Execute($sql); 
    583  
    584         return @(integer)$rs->fields[0]; 
     581        return (integer)$this->_db->selectValue($sql); 
    585582    } 
    586583    /*/Counting Records*/ 
     
    654651        */ 
    655652        $sql = 'UPDATE '.$this->getTableName().' SET '.$updates; 
    656         $sql  .= isset($conditions) ? ' WHERE '.$conditions : ''; 
    657         $this->_executeSql($sql); 
    658         return $this->_db->Affected_Rows(); 
     653        $this->addConditions($sql, $conditions); 
     654        return $this->_db->update($sql, $this->getModelName().' Update All'); 
    659655    } 
    660656 
     
    740736        */ 
    741737        $sql = 'DELETE FROM '.$this->getTableName(); 
    742  
    743         $sql  .= isset($conditions) ? ' WHERE '.$conditions : ($this->_getDatabaseType() == 'sqlite' ? ' WHERE 1' : ''); // (HACK) If where clause is not included sqlite_changes will not get the right result 
    744         $this->_executeSql($sql); 
    745         return $this->_db->Affected_Rows() > 0; 
     738        $this->addConditions($sql,$conditions); 
     739        return $this->_db->delete($sql,$this->getModelName().' Delete All'); 
    746740    } 
    747741 
     
    759753        } 
    760754 
    761         $this->transactionStart(); 
    762755        $id = func_num_args() > 1 ? func_get_args() : $id; 
    763756 
    764757        if(isset($id)){ 
     758            $this->transactionStart(); 
    765759            $id_arr = is_array($id) ? $id : array($id); 
    766760            if($objects = $this->find($id_arr)){ 
     
    780774        }else{ 
    781775            if(!$this->isNewRecord()){ 
    782                 if($this->beforeDestroy()){ 
    783                     $this->notifyObservers('beforeDestroy'); 
     776                $this->transactionStart(); 
     777                if($this->beforeDestroy() && $this->notifyObservers('beforeDestroy')){ 
    784778 
    785779                    $sql = 'DELETE FROM '.$this->getTableName().' WHERE '.$this->getPrimaryKey().' = '.$this->_db->qstr($this->getId()); 
    786                     $this->_executeSql($sql); 
    787                     $had_success = ($this->_db->Affected_Rows() > 0); 
    788                     if(!$had_success || ($had_success && !$this->afterDestroy())){ 
     780                    $had_success = $this->_db->delete($sql,$this->getModelName().' Destroy'); 
     781                    if(!$had_success || !$this->afterDestroy() || !$this->notifyObservers('afterDestroy')){ 
    789782                        $this->transactionFail(); 
    790                         $had_success = false; 
    791                     }else{ 
    792                         $had_success = $this->notifyObservers('afterDestroy') === false ? false : true; 
    793                     } 
    794                     $this->transactionComplete(); 
    795                     $this->freeze(); 
    796                     return  $had_success; 
     783                    } else $this->freeze(); 
    797784                }else { 
    798785                    $this->transactionFail(); 
    799                     $this->transactionComplete(); 
    800                     return false; 
    801786                } 
    802             } 
    803         } 
    804  
    805         if(!$this->afterDestroy()){ 
    806             $this->transactionFail(); 
    807         }else{ 
    808             $this->notifyObservers('afterDestroy'); 
    809         } 
    810  
    811         $this->transactionComplete(); 
    812  
    813         return false; 
     787                $this->transactionComplete(); 
     788                return !$this->transactionHasFailed();// && $this->freeze(); 
     789            } 
     790        } 
    814791    } 
    815792 
     
    13671344    { 
    13681345        $concat = empty($sql) ? '' : ' WHERE '; 
     1346        if (empty($conditions) && $this->_getDatabaseType() == 'sqlite') $conditions = '1';  // sqlite HACK 
    13691347        if(!empty($conditions)){ 
    13701348            $sql  .= $concat.$conditions; 
     
    13781356        return $sql; 
    13791357    } 
    1380  
    13811358 
    13821359    /** 
     
    48404817    /*/Utilities*/ 
    48414818     
    4842      
    4843     /**  
    4844                         Database statements 
    4845     ==================================================================== 
    4846     */ 
    4847  
    4848     /** 
    4849      * Returns a record array with the column names as keys and column values 
    4850      * as values. 
    4851      */ 
    4852     function sqlSelectOne($sql) 
    4853     { 
    4854         $result = $this->sqlSelect($sql); 
    4855         return  !is_null($result) ? array_shift($result) : null; 
    4856     } 
    4857  
    4858     /** 
    4859     * Returns a single value from a record 
    4860     */ 
    4861     function sqlSelectValue($sql) 
    4862     { 
    4863         $result = $this->sqlSelectOne($sql); 
    4864         return !is_null($result) ? array_shift($result) : null; 
    4865     } 
    4866  
    4867     /** 
    4868      * Returns an array of the values of the first column in a select: 
    4869      *   sqlSelectValues("SELECT id FROM companies LIMIT 3") => array(1,2,3) 
    4870      */ 
    4871     function sqlSelectValues($sql) 
    4872     { 
    4873         $values = array(); 
    4874         if($results = $this->sqlSelectAll($sql)){ 
    4875             foreach ($results as $result){ 
    4876                 $values[] = array_slice(array_values($result),0,1); 
    4877             } 
    4878         } 
    4879     } 
    4880  
    4881     /** 
    4882     * Returns an array of record hashes with the column names as keys and 
    4883     * column values as values. 
    4884     */ 
    4885     function sqlSelectAll($sql) 
    4886     { 
    4887         return $this->sqlSelect($sql); 
    4888     } 
    4889  
    4890     function sqlSelect($sql) 
    4891     { 
    4892         //$previous_fetch_mode = $GLOBALS['ADODB_FETCH_MODE']; 
    4893         //$GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_ASSOC; 
    4894         $result = $this->_db->sqlexecute($sql,'selecting'); 
    4895         if (!$result) return array();      
    4896         //$GLOBALS['ADODB_FETCH_MODE'] = $previous_fetch_mode; 
    4897  
    4898         $records = array(); 
    4899         while ($record = $result->FetchRow()) { 
    4900             $records[] = $record; 
    4901         } 
    4902         return $records; 
    4903     } 
    4904      
    49054819 
    49064820    function getAttributeCondition($argument) 
     
    49144828        } 
    49154829    } 
    4916     /*/Database statements*/ 
    4917  
    4918  
     4830 
     4831     
    49194832 /**  
    49204833                     Calculations 
     
    51205033    function _executeSimpleCalculation($operation, $column_name, $column, $options) 
    51215034    { 
    5122         $value = $this->sqlSelectValue($this->_constructCalculationSql($operation, $column_name, $options)); 
     5035        $value = $this->_db->selectValue($this->_constructCalculationSql($operation, $column_name, $options)); 
    51235036        return $this->_typeCastCalculatedValue($value, $column, $operation); 
    51245037    } 
     
    51345047        $options = array_merge(array('group_field' => $group_field, 'group_alias' => $group_alias),$options); 
    51355048        $sql = $this->_constructCalculationSql($operation, $column_name, $options); 
    5136         $calculated_data = $this->sqlSelectAll($sql); 
     5049        $calculated_data = $this->_db->select($sql); 
    51375050        $aggregate_alias = $this->_getColumnAliasFor($operation, $column_name); 
    51385051 
  • branches/kaste/framework/lib/AkBaseModel.php

    r285 r436  
    145145        return $models; 
    146146    } 
    147      
    148     function _executeSql($sql, $trigger_error = true) 
    149     { 
    150         AK_LOG_EVENTS ? ($this->Logger->message($this->getModelName().' executing SQL: '.$sql)) : null; 
    151         $result = $this->_db->Execute($sql); 
    152         if(!$result && AK_DEBUG){ 
    153             AK_LOG_EVENTS ? ($this->Logger->error($this->getModelName().': '.$this->_db->ErrorMsg())) : null; 
    154             $trigger_error ? trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE) : false; 
    155         } 
    156         return $result; 
    157     } 
    158      
    159     function _startSqlBlockLog() 
    160     { 
    161         $this->__original_dbug = $this->_db->debug; 
    162         $this->_db->debug = true; 
    163         ob_start(); 
    164     } 
    165      
    166     function _endSqlBlockLog() 
    167     { 
    168         $sql_debug = ob_get_clean(); 
    169         $this->Logger->message($this->Logger->formatText($this->getModelName(),'bold').' executing SQL: '. 
    170         $this->Logger->formatText(preg_replace('/^\([a-z]+\): /','',trim(Ak::html_entity_decode(strip_tags($sql_debug)),"\n- ")),'blue')); 
    171         if($this->__original_dbug){ 
    172             echo $sql_debug; 
    173         } 
    174         $this->_db->debug = $this->__original_dbug; 
    175     } 
    176147} 
    177148 
  • branches/kaste/framework/lib/AkDbSession.php

    r285 r436  
    5656class AkDbSession extends AkObject 
    5757{ 
    58     // {{{ properties 
    59  
    60  
    61     // --- Public properties --- // 
    62  
    63  
    6458    /** 
    6559    * Secconds for the session to expire. 
     
    7064    */ 
    7165    var $sessionLife = AK_SESSION_EXPIRE; 
    72  
    73  
    74     // --- Protected properties --- // 
    75  
    7666 
    7767    /** 
     
    8575    var $_db; 
    8676 
    87     // }}} 
    88  
    89  
    9077    /** 
    9178    * Original session value for avoiding hitting the database in case nothing has changed 
     
    9582    */ 
    9683    var $_original_sess_value = ''; 
    97  
    98     // }}} 
    99  
    100  
    101     // ------ CLASS METHODS ------ // 
    102  
    103  
    104  
    105     // ---- Setters ---- // 
    106  
    107  
    108     // {{{ setSessionLife() 
    10984 
    11085    /** 
     
    125100    } 
    126101 
    127     // }}} 
    128  
    129  
    130102    // ---- Protected methods ---- // 
    131  
    132  
    133     // {{{ _open() 
    134103 
    135104    /** 
     
    144113        return true; 
    145114    } 
    146  
    147     // }}} 
    148     // {{{ _close() 
    149115 
    150116    /** 
     
    164130    } 
    165131 
    166     // }}} 
    167     // {{{ _read() 
    168  
    169132    /** 
    170133    * Session read handler 
     
    176139    function _read($id) 
    177140    { 
    178         $query_result = $this->_db->Execute("SELECT value FROM sessions WHERE id = ".$this->_db->qstr($id)); 
    179         if(!$query_result && AK_DEBUG){ 
    180             trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE); 
    181         }else{ 
    182             $this->_original_sess_value = (string)$query_result->fields[0]; 
    183             return $this->_original_sess_value; 
    184         } 
    185         return ''; 
     141        $result = $this->_db->selectValue("SELECT value FROM sessions WHERE id = ".$this->_db->qstr($id)); 
     142        return is_null($result) ? '' : (string)$result; 
    186143    } 
    187  
    188     // }}} 
    189     // {{{ _write() 
    190144 
    191145    /** 
     
    201155        // We don't want to hit the db if nothing has changed 
    202156        if($this->_original_sess_value != $data){ 
    203             $ret = $this->_db->Replace('sessions', array('id'=>$this->_db->qstr($id),'expire'=>$this->_db->DBTimeStamp(time()),'value'=>$this->_db->qstr($data)), 'id'); 
     157            // TODO: replace with dbAdapter-method 
     158            $ret = $this->_db->connection->Replace('sessions', array('id'=>$this->_db->qstr($id),'expire'=>$this->_db->DBTimeStamp(time()),'value'=>$this->_db->qstr($data)), 'id'); 
    204159            if($ret == 0){ 
    205160                return false; 
     
    212167    } 
    213168 
    214     // }}} 
    215     // {{{ _destroy() 
    216  
    217169    /** 
    218170    * Session destroy handler 
     
    224176    function _destroy($id) 
    225177    { 
    226         if(!$this->_db->Execute('DELETE FROM sessions WHERE id = '.$this->_db->qstr($id)) && AK_DEBUG){ 
    227             trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE); 
    228         } 
    229         return (bool)@$this->_db->Affected_Rows(); 
     178        return (bool)$this->_db->delete('DELETE FROM sessions WHERE id = '.$this->_db->qstr($id)); 
    230179    } 
    231  
    232     // }}} 
    233     // {{{ _gc() 
    234180 
    235181    /** 
     
    241187    function _gc() 
    242188    { 
    243         if(!$this->_db->Execute('DELETE FROM sessions WHERE expire < '.$this->_db->DBTimeStamp(time()-$this->sessionLife)) && AK_DEBUG){ 
    244             trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE); 
    245         } 
    246         return (bool)$this->_db->Affected_Rows(); 
     189        return (bool)$this->_db->delete('DELETE FROM sessions WHERE expire < '.$this->_db->DBTimeStamp(time()-$this->sessionLife)); 
    247190    } 
    248  
    249     // }}} 
    250191 
    251192 
  • branches/kaste/framework/lib/AkInstaller.php

    r435 r436  
    301301    function dropTable($table_name, $options = array()) 
    302302    { 
    303         $result = $this->tableExists($table_name) ? $this->db->Execute('DROP TABLE '.$table_name) : true; 
     303        $result = $this->tableExists($table_name) ? $this->db->sqlexecute('DROP TABLE '.$table_name) : true; 
    304304        if($result){ 
    305305            unset($this->available_tables[array_search($table_name, $this->available_tables)]); 
     
    626626    function execute($sql) 
    627627    { 
    628         return $this->db->Execute($sql); 
     628        return $this->db->sqlexecute($sql); 
    629629    } 
    630630 
  • branches/kaste/framework/lib/AkLogger.php

    r285 r436  
    167167        " VALUES (0, ".$db->qstr($type).", ".$db->qstr($message).', '.($this->mode & AK_MODE_DIE ? 100 : 0).', '. 
    168168        $db->qstr(AK_CURRENT_URL).', '.$db->qstr($_SERVER['REMOTE_ADDR']).', '.$db->qstr(Ak::getTimestamp()).');'; 
    169         if ($db->Execute($sql) === false) { 
     169        if ($db->sqlexecute($sql) === false) { 
    170170            die($this->internalError($this->t('Error inserting: ').$db->ErrorMsg(),__FILE__,__LINE__)); 
    171171        } 
  • branches/kaste/framework/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r416 r436  
    209209        $this->assertTrue($Property->destroy()); 
    210210 
    211         $RecordSet = $PropertyInAltea->_db->Execute('SELECT * FROM properties_property_types WHERE property_id = '.$property_id); 
     211        $RecordSet = $PropertyInAltea->_db->sqlexecute('SELECT * FROM properties_property_types WHERE property_id = '.$property_id); 
    212212        $this->assertEqual($RecordSet->RecordCount(), 0); 
    213213 
  • branches/kaste/framework/test/unit/lib/AkDbSession.php

    r217 r436  
    1717    var $sessionLife = NULL; 
    1818    
     19    function test_install_db_tables() 
     20    { 
     21        require_once(dirname(__FILE__).'/../../fixtures/app/installers/framework_installer.php'); 
     22        $installer =& new FrameworkInstaller(); 
     23        $installer->uninstall(); 
     24        $installer->install(); 
     25         
     26    } 
     27 
    1928    function setUp() 
    2029    {    
     
    4049        $this->get("$this->_test_script?key=test_key");         
    4150        $this->assertWantedText($expected,'Session is not storing values on database correctly when calling '. 
    42         $this->_test_script.'?key=test_key'); 
     51            $this->_test_script.'?key=test_key'); 
    4352    } 
    4453