Changeset 436
- Timestamp:
- 11/10/07 07:15:04 (1 year ago)
- Files:
-
- branches/kaste/framework/lib/AkActiveRecord.php (modified) (11 diffs)
- branches/kaste/framework/lib/AkBaseModel.php (modified) (1 diff)
- branches/kaste/framework/lib/AkDbSession.php (modified) (12 diffs)
- branches/kaste/framework/lib/AkInstaller.php (modified) (2 diffs)
- branches/kaste/framework/lib/AkLogger.php (modified) (1 diff)
- branches/kaste/framework/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php (modified) (1 diff)
- branches/kaste/framework/test/unit/lib/AkDbSession.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/kaste/framework/lib/AkActiveRecord.php
r430 r436 579 579 } 580 580 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); 585 582 } 586 583 /*/Counting Records*/ … … 654 651 */ 655 652 $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'); 659 655 } 660 656 … … 740 736 */ 741 737 $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'); 746 740 } 747 741 … … 759 753 } 760 754 761 $this->transactionStart();762 755 $id = func_num_args() > 1 ? func_get_args() : $id; 763 756 764 757 if(isset($id)){ 758 $this->transactionStart(); 765 759 $id_arr = is_array($id) ? $id : array($id); 766 760 if($objects = $this->find($id_arr)){ … … 780 774 }else{ 781 775 if(!$this->isNewRecord()){ 782 if($this->beforeDestroy()){783 $this->notifyObservers('beforeDestroy');776 $this->transactionStart(); 777 if($this->beforeDestroy() && $this->notifyObservers('beforeDestroy')){ 784 778 785 779 $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')){ 789 782 $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(); 797 784 }else { 798 785 $this->transactionFail(); 799 $this->transactionComplete();800 return false;801 786 } 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 } 814 791 } 815 792 … … 1367 1344 { 1368 1345 $concat = empty($sql) ? '' : ' WHERE '; 1346 if (empty($conditions) && $this->_getDatabaseType() == 'sqlite') $conditions = '1'; // sqlite HACK 1369 1347 if(!empty($conditions)){ 1370 1348 $sql .= $concat.$conditions; … … 1378 1356 return $sql; 1379 1357 } 1380 1381 1358 1382 1359 /** … … 4840 4817 /*/Utilities*/ 4841 4818 4842 4843 /**4844 Database statements4845 ====================================================================4846 */4847 4848 /**4849 * Returns a record array with the column names as keys and column values4850 * 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 record4860 */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 and4883 * 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 4905 4819 4906 4820 function getAttributeCondition($argument) … … 4914 4828 } 4915 4829 } 4916 /*/Database statements*/ 4917 4918 4830 4831 4919 4832 /** 4920 4833 Calculations … … 5120 5033 function _executeSimpleCalculation($operation, $column_name, $column, $options) 5121 5034 { 5122 $value = $this-> sqlSelectValue($this->_constructCalculationSql($operation, $column_name, $options));5035 $value = $this->_db->selectValue($this->_constructCalculationSql($operation, $column_name, $options)); 5123 5036 return $this->_typeCastCalculatedValue($value, $column, $operation); 5124 5037 } … … 5134 5047 $options = array_merge(array('group_field' => $group_field, 'group_alias' => $group_alias),$options); 5135 5048 $sql = $this->_constructCalculationSql($operation, $column_name, $options); 5136 $calculated_data = $this-> sqlSelectAll($sql);5049 $calculated_data = $this->_db->select($sql); 5137 5050 $aggregate_alias = $this->_getColumnAliasFor($operation, $column_name); 5138 5051 branches/kaste/framework/lib/AkBaseModel.php
r285 r436 145 145 return $models; 146 146 } 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 }176 147 } 177 148 branches/kaste/framework/lib/AkDbSession.php
r285 r436 56 56 class AkDbSession extends AkObject 57 57 { 58 // {{{ properties59 60 61 // --- Public properties --- //62 63 64 58 /** 65 59 * Secconds for the session to expire. … … 70 64 */ 71 65 var $sessionLife = AK_SESSION_EXPIRE; 72 73 74 // --- Protected properties --- //75 76 66 77 67 /** … … 85 75 var $_db; 86 76 87 // }}}88 89 90 77 /** 91 78 * Original session value for avoiding hitting the database in case nothing has changed … … 95 82 */ 96 83 var $_original_sess_value = ''; 97 98 // }}}99 100 101 // ------ CLASS METHODS ------ //102 103 104 105 // ---- Setters ---- //106 107 108 // {{{ setSessionLife()109 84 110 85 /** … … 125 100 } 126 101 127 // }}}128 129 130 102 // ---- Protected methods ---- // 131 132 133 // {{{ _open()134 103 135 104 /** … … 144 113 return true; 145 114 } 146 147 // }}}148 // {{{ _close()149 115 150 116 /** … … 164 130 } 165 131 166 // }}}167 // {{{ _read()168 169 132 /** 170 133 * Session read handler … … 176 139 function _read($id) 177 140 { 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; 186 143 } 187 188 // }}}189 // {{{ _write()190 144 191 145 /** … … 201 155 // We don't want to hit the db if nothing has changed 202 156 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'); 204 159 if($ret == 0){ 205 160 return false; … … 212 167 } 213 168 214 // }}}215 // {{{ _destroy()216 217 169 /** 218 170 * Session destroy handler … … 224 176 function _destroy($id) 225 177 { 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)); 230 179 } 231 232 // }}}233 // {{{ _gc()234 180 235 181 /** … … 241 187 function _gc() 242 188 { 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)); 247 190 } 248 249 // }}}250 191 251 192 branches/kaste/framework/lib/AkInstaller.php
r435 r436 301 301 function dropTable($table_name, $options = array()) 302 302 { 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; 304 304 if($result){ 305 305 unset($this->available_tables[array_search($table_name, $this->available_tables)]); … … 626 626 function execute($sql) 627 627 { 628 return $this->db-> Execute($sql);628 return $this->db->sqlexecute($sql); 629 629 } 630 630 branches/kaste/framework/lib/AkLogger.php
r285 r436 167 167 " VALUES (0, ".$db->qstr($type).", ".$db->qstr($message).', '.($this->mode & AK_MODE_DIE ? 100 : 0).', '. 168 168 $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) { 170 170 die($this->internalError($this->t('Error inserting: ').$db->ErrorMsg(),__FILE__,__LINE__)); 171 171 } branches/kaste/framework/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php
r416 r436 209 209 $this->assertTrue($Property->destroy()); 210 210 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); 212 212 $this->assertEqual($RecordSet->RecordCount(), 0); 213 213 branches/kaste/framework/test/unit/lib/AkDbSession.php
r217 r436 17 17 var $sessionLife = NULL; 18 18 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 19 28 function setUp() 20 29 { … … 40 49 $this->get("$this->_test_script?key=test_key"); 41 50 $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'); 43 52 } 44 53
