| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/dbal/src/query/lmbInsertQuery.class.php'); 10 lmb_require('limb/dbal/src/query/lmbSelectQuery.class.php'); 11 lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php'); 12 lmb_require('limb/dbal/src/query/lmbUpdateQuery.class.php'); 13 lmb_require('limb/dbal/src/query/lmbDeleteQuery.class.php'); 14 lmb_require('limb/dbal/src/drivers/lmbDbTypeInfo.class.php'); 15 lmb_require('limb/dbal/src/drivers/lmbDbCachedInfo.class.php'); 16 17 /** 18 * class lmbTableGateway. 19 * 20 * @package dbal 21 * @version $Id: lmbTableGateway.class.php 5945 2007-06-06 08:31:43Z pachanga $ 22 */ 23 class lmbTableGateway 24 { 25 protected $_db_table_name; 26 protected $_primary_key_name; 27 protected $_table_info; 28 protected $_constraints = array(); 29 protected $_conn; 30 protected $_stmt; 31 protected $_toolkit; 32 33 function __construct($table_name = null, $conn = null) 34 { 35 $this->_toolkit = lmbToolkit :: instance(); 36 37 if(is_object($conn)) 38 $this->_conn = $conn; 39 else 40 $this->_conn = $this->_toolkit->getDefaultDbConnection(); 41 42 if($table_name) 43 $this->_db_table_name = $table_name; 44 elseif(!$this->_db_table_name) 45 $this->_db_table_name = $this->_guessDbTableName(); 46 47 $this->_table_info = $this->_loadTableInfo(); 48 $this->_constraints = $this->_defineConstraints(); 49 $this->_primary_key_name = $this->_definePrimaryKeyName(); 50 } 51 52 protected function _guessDbTableName() 53 { 54 return str_replace('_db_table', '', lmb_under_scores(get_class($this))); 55 } 56 57 protected function _definePrimaryKeyName() 58 { 59 return 'id'; 60 } 61 62 protected function _loadTableInfo() 63 { 64 $db_info = $this->_toolkit->getDbInfo($this->_conn); 65 return $db_info->getTable($this->_db_table_name); 66 } 67 68 protected function _defineConstraints() 69 { 70 return array(); 71 } 72 73 function getTableInfo() 74 { 75 return $this->_table_info; 76 } 77 78 function getColumnInfo($name) 79 { 80 if($this->hasColumn($name)) 81 return $this->_table_info->getColumn($name); 82 } 83 84 function hasColumn($name) 85 { 86 return $this->_table_info->hasColumn($name); 87 } 88 89 function getColumnNames() 90 { 91 return $this->_table_info->getColumnList(); 92 } 93 94 function getConstraints() 95 { 96 return $this->_constraints; 97 } 98 99 function getColumnType($column_name) 100 { 101 if(!$this->hasColumn($column_name)) 102 return false; 103 104 return $this->_table_info->getColumn($column_name)->getType(); 105 } 106 107 function getPrimaryKeyName() 108 { 109 return $this->_primary_key_name; 110 } 111 112 function isAutoIncrement($field) 113 { 114 return $this->_table_info->getColumn($field)->isAutoIncrement(); 115 } 116 117 function getStatement() 118 { 119 return $this->_stmt; 120 } 121 122 function getAffectedRowCount() 123 { 124 if($this->_stmt) 125 return $this->_stmt->getAffectedRowCount(); 126 else 127 return 0; 128 } 129 130 function insert($row) 131 { 132 $filtered_row = $this->_filterRow($row); 133 if(!count($filtered_row)) 134 throw new lmbException('All fields filtered!! Insert statement must contain atleast one field!'); 135 136 $query = new lmbInsertQuery($this->_db_table_name, $this->_conn); 137 $values = array(); 138 $update_sequence = false; 139 foreach($filtered_row as $key => $value) 140 { 141 if(is_null($value) && $this->isAutoIncrement($key)) 142 continue; 143 144 $query->addField($key); 145 $values[$key] = $value; 146 } 147 148 $this->_stmt = $query->getStatement($this->_conn); 149 150 $this->_bindValuesToStatement($this->_stmt, $values); 151 152 return (int)$this->_stmt->insertId($this->_primary_key_name); 153 } 154 155 function update($set, $criteria = null) 156 { 157 if(is_array($set)) 158 $set = $this->_filterRow($set); 159 160 $query = new lmbUpdateQuery($this->_db_table_name, $this->_conn); 161 162 if($criteria) 163 $query->addCriteria($criteria); 164 165 if(is_array($set)) 166 { 167 foreach(array_keys($set) as $key) 168 $query->addField($key); 169 170 $this->_stmt = $query->getStatement(); 171 $this->_bindValuesToStatement($this->_stmt, $set); 172 return $this->_stmt->execute(); 173 } 174 else 175 { 176 $query->addRawField($set); 177 $this->_stmt = $query->getStatement(); 178 return $this->_stmt->execute(); 179 } 180 } 181 182 protected function _bindValuesToStatement($stmt, $values) 183 { 184 $typeinfo = new lmbDbTypeInfo(); 185 $accessors = $typeinfo->getColumnTypeAccessors(); 186 187 foreach($values as $key => $value) 188 { 189 $accessor = $accessors[$this->getColumnInfo($key)->getType()]; 190 $stmt->$accessor($key, $value); 191 } 192 } 193 194 function updateById($id, $data) 195 { 196 return $this->update($data, new lmbSQLFieldCriteria($this->_primary_key_name, $id)); 197 } 198 199 function selectRecordById($id, $fields = array()) 200 { 201 if($id == null) 202 return null; 203 204 $query = $this->getSelectQuery($fields); 205 206 $query->addCriteria(new lmbSQLFieldCriteria($this->_primary_key_name, $id)); 207 $record_set = $query->getRecordSet(); 208 $record_set->rewind(); 209 210 if(!$record_set->valid()) 211 return null; 212 else 213 return $record_set->current(); 214 } 215 216 function select($criteria = null, $sort_params = array(), $fields = array()) 217 { 218 $query = $this->getSelectQuery($fields); 219 220 if($criteria) 221 $query->addCriteria($criteria); 222 223 $rs = $query->getRecordSet(); 224 225 if(count($sort_params)) 226 $rs->sort($sort_params); 227 228 return $rs; 229 } 230 231 function selectFirstRecord($criteria = null, $sort_params = array(), $fields = array()) 232 { 233 $rs = $this->select($criteria, $sort_params, $fields); 234 235 if($sort_params) 236 $rs->sort($sort_params); 237 238 $rs->rewind(); 239 if($rs->valid()) 240 return $rs->current(); 241 } 242 243 function getSelectQuery($fields = array()) 244 { 245 $query = new lmbSelectQuery(null, $this->_conn); 246 $query->addTable($this->_db_table_name); 247 248 if(!$fields) 249 $fields = $this->getColumnsForSelect(); 250 251 foreach($fields as $field) 252 $query->addField($field); 253 254 return $query; 255 } 256 257 function delete($criteria = null) 258 { 259 $query = new lmbDeleteQuery($this->_db_table_name, $this->_conn); 260 261 if($criteria) 262 $query->addCriteria($criteria); 263 264 $this->_stmt = $query->getStatement(); 265 $this->_stmt->execute(); 266 } 267 268 function deleteById($id) 269 { 270 return $this->delete(new lmbSQLFieldCriteria($this->_primary_key_name, $id)); 271 } 272 273 function getTableName() 274 { 275 return $this->_db_table_name; 276 } 277 278 protected function _mapTableNameToClass($table_name) 279 { 280 return lmb_camel_case($table_name); 281 } 282 283 function getColumnsForSelect($table_name = '', $exclude_columns = array(), $prefix = '') 284 { 285 if(!$table_name) 286 $table_name = $this->getTableName(); 287 288 $columns = $this->getColumnNames(); 289 $fields = array(); 290 foreach($columns as $name) 291 { 292 if(!in_array($name, $exclude_columns)) 293 $fields[$table_name . '.' . $name] = $prefix . $name; 294 } 295 296 return $fields; 297 } 298 299 protected function _filterRow($row) 300 { 301 if(!is_array($row)) 302 return array(); 303 304 $filtered = array(); 305 foreach($row as $key => $value) 306 { 307 if(is_integer($key)) 308 $filtered[$key] = $value; 309 elseif($this->hasColumn($key)) 310 $filtered[$key] = $value; 311 } 312 return $filtered; 313 } 314 } 315 316 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Sep 6 04:46:52 2008 | Cross-referenced by PHPXref 0.7 |