Changeset 3336

Show
Ignore:
Timestamp:
08/16/2008 11:13:30 AM (4 months ago)
Author:
Shadowhand
Message:

Updated Database/ORM:

  • Combined field_data and list_fields into list_columns
  • Updated MySQL driver to match (pgsql, mssql, and pdosqlite are NOT updated)
  • Updated ORM to match
Location:
trunk/system/libraries
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Database.php

    r3326 r3336  
    10861086 
    10871087        /** 
    1088          * Lists all the tables in the current database. 
    1089          * 
    1090          * @return  array 
    1091          */ 
    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 name 
    1105          * @return  boolean 
    1106          */ 
    1107         public function table_exists($table_name) 
    1108         { 
    1109                 return in_array($this->config['table_prefix'].$table_name, $this->list_tables()); 
    1110         } 
    1111  
    1112         /** 
    11131088         * Combine a SQL statement with the bind values. Used for safe queries. 
    11141089         * 
     
    11401115 
    11411116        /** 
    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         * 
    11451130         * @return  array 
    11461131         */ 
    1147         public function field_data($table = '') 
     1132        public function list_tables() 
    11481133        { 
    11491134                $this->link or $this->connect(); 
    11501135 
    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 
    11581144         * @return  array 
    11591145         */ 
    1160         public function list_fields($table = '') 
     1146        public function list_columns($table, $metadata = FALSE) 
    11611147        { 
    11621148                $this->link or $this->connect(); 
    11631149 
    1164                 return $this->driver->list_fields($this->config['table_prefix'].$table); 
     1150                return $this->driver->list_columns($this->config['table_prefix'].$table, $metadata, $this); 
    11651151        } 
    11661152 
  • trunk/system/libraries/ORM.php

    r3326 r3336  
    11<?php 
    22/** 
    3  * Object Relational Mapping (ORM) is a method of abstracting database 
    4  * access to standard PHP calls. All table rows are represented as a model. 
    5  * 
    6  * @see http://en.wikipedia.org/wiki/Active_record 
    7  * @see http://en.wikipedia.org/wiki/Object-relational_mapping 
     3 * [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. 
    88 * 
    99 * $Id$ 
     
    691691                { 
    692692                        // 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); 
    697694                } 
    698695 
  • trunk/system/libraries/drivers/Database.php

    r3326 r3336  
    338338         * @return  array 
    339339         */ 
    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 
    346347         * @return  array 
    347348         */ 
    348         abstract function list_fields($table); 
     349        abstract public function list_columns($table, $metadata = FALSE, Database $db); 
    349350 
    350351        /** 
     
    354355         */ 
    355356        abstract public function show_error(); 
    356  
    357         /** 
    358          * Returns field data about a table. 
    359          * 
    360          * @param   string  table name 
    361          * @return  array 
    362          */ 
    363         abstract public function field_data($table); 
    364357 
    365358        /** 
     
    397390                empty($sql_types[$type]) and exit 
    398391                ( 
    399                         'Unknown field type: '.$type.'. '. 
     392                        'Unknown database column type: '.$type.'. '. 
    400393                        'Please report this: http://trac.kohanaphp.com/newticket' 
    401394                ); 
  • trunk/system/libraries/drivers/Database/Mysql.php

    r3326 r3336  
    256256        } 
    257257 
    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) 
    278259        { 
    279260                static $tables; 
    280261 
    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) 
    284280                        { 
    285281                                // Make an associative array 
    286                                 $tables[$table][$row->Field] = $this->sql_type($row->Type); 
     282                                $columns[$table][$row->Field] = $this->sql_type($row->Type); 
    287283 
    288284                                if ($row->Key === 'PRI' AND $row->Extra === 'auto_increment') 
    289285                                { 
    290286                                        // For sequenced (AUTO_INCREMENT) tables 
    291                                         $tables[$table][$row->Field]['sequenced'] = TRUE; 
     287                                        $columns[$table][$row->Field]['sequenced'] = TRUE; 
    292288                                } 
    293289 
     
    295291                                { 
    296292                                        // Set NULL status 
    297                                         $tables[$table][$row->Field]['null'] = TRUE; 
     293                                        $columns[$table][$row->Field]['null'] = TRUE; 
    298294                                } 
    299295                        } 
    300296                } 
    301297 
    302                 if (!isset($tables[$table])) 
     298                if ( ! isset($columns[$table])) 
    303299                        throw new Kohana_Database_Exception('database.table_not_found', $table); 
    304300 
    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); 
    324307        } 
    325308 
  • trunk/system/libraries/drivers/Database/Mysqli.php

    r3326 r3336  
    113113        } 
    114114 
    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  
    128115} // End Database_Mysqli_Driver Class 
    129116