Changeset 3336
- Timestamp:
- 08/16/2008 11:13:30 AM (4 months ago)
- Location:
- trunk/system/libraries
- Files:
-
- 5 modified
-
Database.php (modified) (2 diffs)
-
ORM.php (modified) (2 diffs)
-
drivers/Database.php (modified) (3 diffs)
-
drivers/Database/Mysql.php (modified) (2 diffs)
-
drivers/Database/Mysqli.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/libraries/Database.php
r3326 r3336 1086 1086 1087 1087 /** 1088 * Lists all the tables in the current database.1089 *1090 * @return array1091 */1092 public function list_tables()1093 {1094 $this->link or $this->connect();1095 1096 $this->reset_select();1097 1098 return $this->driver->list_tables();1099 }1100 1101 /**1102 * See if a table exists in the database.1103 *1104 * @param string table name1105 * @return boolean1106 */1107 public function table_exists($table_name)1108 {1109 return in_array($this->config['table_prefix'].$table_name, $this->list_tables());1110 }1111 1112 /**1113 1088 * Combine a SQL statement with the bind values. Used for safe queries. 1114 1089 * … … 1140 1115 1141 1116 /** 1142 * Get the field data for a database table, along with the field's attributes. 1143 * 1144 * @param string table name 1117 * See if a table exists in the database. 1118 * 1119 * @param string table name 1120 * @return boolean 1121 */ 1122 public function table_exists($table_name) 1123 { 1124 return in_array($this->config['table_prefix'].$table_name, $this->list_tables()); 1125 } 1126 1127 /** 1128 * Lists all the tables in the current database. 1129 * 1145 1130 * @return array 1146 1131 */ 1147 public function field_data($table = '')1132 public function list_tables() 1148 1133 { 1149 1134 $this->link or $this->connect(); 1150 1135 1151 return $this->driver->field_data($this->config['table_prefix'].$table); 1152 } 1153 1154 /** 1155 * Get the field data for a database table, along with the field's attributes. 1156 * 1157 * @param string table name 1136 return $this->driver->list_tables($this); 1137 } 1138 1139 /** 1140 * Lists all columns in a table, and optionally include column metadata. 1141 * 1142 * @param string table name 1143 * @param boolean include metadata 1158 1144 * @return array 1159 1145 */ 1160 public function list_ fields($table = '')1146 public function list_columns($table, $metadata = FALSE) 1161 1147 { 1162 1148 $this->link or $this->connect(); 1163 1149 1164 return $this->driver->list_ fields($this->config['table_prefix'].$table);1150 return $this->driver->list_columns($this->config['table_prefix'].$table, $metadata, $this); 1165 1151 } 1166 1152 -
trunk/system/libraries/ORM.php
r3326 r3336 1 1 <?php 2 2 /** 3 * Object Relational Mapping (ORM) is a method of abstracting database4 * access to standard PHP calls. All table rows are represented as a model.5 * 6 * @see http://en.wikipedia.org/wiki/Active_record7 * @see http://en.wikipedia.org/wiki/Object-relational_mapping3 * [Object Relational Mapping](http://wikipedia.org/wiki/Object-relational_mapping) 4 * (ORM) is a method of abstracting database access to standard PHP calls. 5 * All table rows are represented as model objects, with object properties 6 * representingrow data. ORM in Kohana generally follows the 7 * [Active Record](http://wikipedia.org/wiki/Active_record) pattern. 8 8 * 9 9 * $Id$ … … 691 691 { 692 692 // Load table columns 693 $this->table_columns = $this->db->list_fields($this->table_name); 694 695 if (empty($this->table_columns)) 696 throw new Kohana_Exception('database.table_not_found', $this->table); 693 $this->table_columns = $this->db->list_columns($this->table_name, TRUE); 697 694 } 698 695 -
trunk/system/libraries/drivers/Database.php
r3326 r3336 338 338 * @return array 339 339 */ 340 abstract public function list_tables(); 341 342 /** 343 * Lists all fields in a table. 344 * 345 * @param string table name 340 abstract public function list_tables(Database $db); 341 342 /** 343 * Lists all columns in a table, and optionally include column metadata. 344 * 345 * @param string table name 346 * @param boolean include metadata 346 347 * @return array 347 348 */ 348 abstract function list_fields($table);349 abstract public function list_columns($table, $metadata = FALSE, Database $db); 349 350 350 351 /** … … 354 355 */ 355 356 abstract public function show_error(); 356 357 /**358 * Returns field data about a table.359 *360 * @param string table name361 * @return array362 */363 abstract public function field_data($table);364 357 365 358 /** … … 397 390 empty($sql_types[$type]) and exit 398 391 ( 399 'Unknown fieldtype: '.$type.'. '.392 'Unknown database column type: '.$type.'. '. 400 393 'Please report this: http://trac.kohanaphp.com/newticket' 401 394 ); -
trunk/system/libraries/drivers/Database/Mysql.php
r3326 r3336 256 256 } 257 257 258 public function list_tables() 259 { 260 $sql = 'SHOW TABLES FROM `'.$this->db_config['connection']['database'].'`'; 261 $result = $this->query($sql)->result(FALSE, MYSQL_ASSOC); 262 263 $retval = array(); 264 foreach ($result as $row) 265 { 266 $retval[] = current($row); 267 } 268 269 return $retval; 270 } 271 272 public function show_error() 273 { 274 return mysql_error($this->link); 275 } 276 277 public function list_fields($table) 258 public function list_tables(Database $db) 278 259 { 279 260 static $tables; 280 261 281 if (empty($tables[$table])) 282 { 283 foreach ($this->field_data($table) as $row) 262 if (empty($tables) AND $query = $db->query('SHOW TABLES FROM '.$this->escape_table($this->db_config['connection']['database']))) 263 { 264 foreach ($query->result(FALSE) as $row) 265 { 266 $tables[] = current($row); 267 } 268 } 269 270 return $tables; 271 } 272 273 public function list_columns($table, $metadata = FALSE, Database $db) 274 { 275 static $columns; 276 277 if (empty($columns[$table]) AND $query = $db->query('SHOW COLUMNS FROM '.$this->escape_table($table))) 278 { 279 foreach ($query->result(TRUE) as $row) 284 280 { 285 281 // Make an associative array 286 $ tables[$table][$row->Field] = $this->sql_type($row->Type);282 $columns[$table][$row->Field] = $this->sql_type($row->Type); 287 283 288 284 if ($row->Key === 'PRI' AND $row->Extra === 'auto_increment') 289 285 { 290 286 // For sequenced (AUTO_INCREMENT) tables 291 $ tables[$table][$row->Field]['sequenced'] = TRUE;287 $columns[$table][$row->Field]['sequenced'] = TRUE; 292 288 } 293 289 … … 295 291 { 296 292 // Set NULL status 297 $ tables[$table][$row->Field]['null'] = TRUE;293 $columns[$table][$row->Field]['null'] = TRUE; 298 294 } 299 295 } 300 296 } 301 297 302 if ( !isset($tables[$table]))298 if ( ! isset($columns[$table])) 303 299 throw new Kohana_Database_Exception('database.table_not_found', $table); 304 300 305 return $tables[$table]; 306 } 307 308 public function field_data($table) 309 { 310 $columns = array(); 311 312 if ($query = mysql_query('SHOW COLUMNS FROM '.$this->escape_table($table), $this->link)) 313 { 314 if (mysql_num_rows($query) > 0) 315 { 316 while ($row = mysql_fetch_object($query)) 317 { 318 $columns[] = $row; 319 } 320 } 321 } 322 323 return $columns; 301 return ($metadata == TRUE) ? $columns[$table] : array_keys($columns[$table]); 302 } 303 304 public function show_error() 305 { 306 return mysql_error($this->link); 324 307 } 325 308 -
trunk/system/libraries/drivers/Database/Mysqli.php
r3326 r3336 113 113 } 114 114 115 public function field_data($table)116 {117 $query = $this->link->query('SHOW COLUMNS FROM '.$this->escape_table($table));118 119 $table = array();120 while ($row = $query->fetch_object())121 {122 $table[] = $row;123 }124 125 return $table;126 }127 128 115 } // End Database_Mysqli_Driver Class 129 116
