DB_forge.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * Code Igniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Database Utility Class
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/user_guide/database/
00024  */
00025 class CI_DB_forge {
00026 
00027         var $fields                     = array();
00028         var $keys                       = array();
00029         var $primary_keys       = array();
00030         var $db_char_set        =       '';
00031 
00032         /**
00033          * Constructor
00034          *
00035          * Grabs the CI super object instance so we can access it.
00036          *
00037          */     
00038         function CI_DB_forge()
00039         {
00040                 // Assign the main database object to $this->db
00041                 $CI =& get_instance();
00042                 $this->db =& $CI->db;
00043                 log_message('debug', "Database Forge Class Initialized");
00044         }
00045 
00046         // --------------------------------------------------------------------
00047 
00048         /**
00049          * Create database
00050          *
00051          * @access      public
00052          * @param       string  the database name
00053          * @return      bool
00054          */
00055         function create_database($db_name)
00056         {
00057                 $sql = $this->_create_database($db_name);
00058                 
00059                 if (is_bool($sql))
00060                 {
00061                         return $sql;
00062                 }
00063         
00064                 return $this->db->query($sql);
00065         }
00066 
00067         // --------------------------------------------------------------------
00068 
00069         /**
00070          * Drop database
00071          *
00072          * @access      public
00073          * @param       string  the database name
00074          * @return      bool
00075          */
00076         function drop_database($db_name)
00077         {
00078                 $sql = $this->_drop_database($db_name);
00079                 
00080                 if (is_bool($sql))
00081                 {
00082                         return $sql;
00083                 }
00084         
00085                 return $this->db->query($sql);
00086         }
00087 
00088         // --------------------------------------------------------------------
00089 
00090         /**
00091          * Add Key
00092          *
00093          * @access      public
00094          * @param       string  key
00095          * @param       string  type
00096          * @return      void
00097          */
00098         function add_key($key = '', $primary = FALSE)
00099         {
00100                 if (is_array($key))
00101                 {
00102                         foreach($key as $one)
00103                         {
00104                                 $this->add_key($one, $primary);
00105                         }
00106                         
00107                         return;
00108                 }
00109         
00110                 if ($key == '')
00111                 {
00112                         show_error('Key information is required for that operation.');
00113                 }
00114                 
00115                 if ($primary === TRUE)
00116                 {
00117                         $this->primary_keys[] = $key;
00118                 }
00119                 else
00120                 {
00121                         $this->keys[] = $key;
00122                 }
00123         }
00124 
00125         // --------------------------------------------------------------------
00126 
00127         /**
00128          * Add Field
00129          *
00130          * @access      public
00131          * @param       string  collation
00132          * @return      void
00133          */
00134         function add_field($field = '')
00135         {
00136                 if ($field == '')
00137                 {
00138                         show_error('Field information is required.');
00139                 }
00140                 
00141                 if (is_string($field))
00142                 {
00143                         if ($field == 'id')
00144                         {
00145                                 $this->add_field(array(
00146                                                                                 'id' => array(
00147                                                                                                         'type' => 'INT',
00148                                                                                                         'constraint' => 9,
00149                                                                                                         'auto_increment' => TRUE
00150                                                                                                         )
00151                                                                 ));
00152                                 $this->add_key('id', TRUE);
00153                         }
00154                         else
00155                         {
00156                                 if (strpos($field, ' ') === FALSE)
00157                                 {
00158                                         show_error('Field information is required for that operation.');
00159                                 }
00160                                 
00161                                 $this->fields[] = $field;
00162                         }
00163                 }
00164                 
00165                 if (is_array($field))
00166                 {
00167                         $this->fields = array_merge($this->fields, $field);
00168                 }
00169                 
00170         }
00171 
00172         // --------------------------------------------------------------------
00173 
00174         /**
00175          * Create Table
00176          *
00177          * @access      public
00178          * @param       string  the table name
00179          * @return      bool
00180          */
00181         function create_table($table = '', $if_not_exists = FALSE)
00182         {       
00183                 if ($table == '')
00184                 {
00185                         show_error('A table name is required for that operation.');
00186                 }
00187                         
00188                 if (count($this->fields) == 0)
00189                 {       
00190                         show_error('Field information is required.');
00191                 }
00192 
00193                 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
00194                 
00195                 $this->_reset();
00196                 return $this->db->query($sql);
00197         }
00198 
00199         // --------------------------------------------------------------------
00200 
00201         /**
00202          * Drop Table
00203          *
00204          * @access      public
00205          * @param       string  the table name
00206          * @return      bool
00207          */
00208         function drop_table($table_name)
00209         {
00210                 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
00211                 
00212                 if (is_bool($sql))
00213                 {
00214                         return $sql;
00215                 }
00216         
00217                 return $this->db->query($sql);
00218         }
00219 
00220         // --------------------------------------------------------------------
00221 
00222         /**
00223          * Rename Table
00224          *
00225          * @access      public
00226          * @param       string  the old table name
00227          * @param       string  the new table name
00228          * @return      bool
00229          */
00230         function rename_table($table_name, $new_table_name)
00231         {
00232                 if ($table_name == '' OR $new_table_name == '')
00233                 {
00234                         show_error('A table name is required for that operation.');
00235                 }
00236                         
00237                 $sql = $this->_rename_table($table_name, $new_table_name);
00238                 return $this->db->query($sql);
00239         }
00240 
00241         // --------------------------------------------------------------------
00242 
00243         /**
00244          * Column Add
00245          *
00246          * @access      public
00247          * @param       string  the table name
00248          * @param       string  the column name
00249          * @param       string  the column definition
00250          * @return      bool
00251          */
00252         function add_column($table = '', $field = array(), $after_field = '')
00253         {
00254                 if ($table == '')
00255                 {
00256                         show_error('A table name is required for that operation.');
00257                 }
00258 
00259                 // add field info into field array, but we can only do one at a time
00260                 // so only grab the first field in the event there are more then one
00261                 $this->add_field(array_slice($field, 0, 1));
00262 
00263                 if (count($this->fields) == 0)
00264                 {       
00265                         show_error('Field information is required.');
00266                 }
00267 
00268                 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
00269 
00270                 $this->_reset();
00271                 return $this->db->query($sql);
00272         }
00273 
00274         // --------------------------------------------------------------------
00275 
00276         /**
00277          * Column Drop
00278          *
00279          * @access      public
00280          * @param       string  the table name
00281          * @param       string  the column name
00282          * @return      bool
00283          */
00284         function drop_column($table = '', $column_name = '')
00285         {
00286         
00287                 if ($table == '')
00288                 {
00289                         show_error('A table name is required for that operation.');
00290                 }
00291 
00292                 if ($column_name == '')
00293                 {
00294                         show_error('A column name is required for that operation.');
00295                 }
00296 
00297                 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
00298         
00299                 return $this->db->query($sql);
00300         }
00301 
00302         // --------------------------------------------------------------------
00303 
00304         /**
00305          * Column Modify
00306          *
00307          * @access      public
00308          * @param       string  the table name
00309          * @param       string  the column name
00310          * @param       string  the column definition
00311          * @return      bool
00312          */
00313         function modify_column($table = '', $field = array())
00314         {
00315                 if ($table == '')
00316                 {
00317                         show_error('A table name is required for that operation.');
00318                 }
00319 
00320                 // add field info into field array, but we can only do one at a time
00321                 // so only grab the first field in the event there are more then one
00322                 $this->add_field(array_slice($field, 0, 1));
00323 
00324                 if (count($this->fields) == 0)
00325                 {       
00326                         show_error('Field information is required.');
00327                 }
00328 
00329                 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
00330 
00331                 $this->_reset();
00332                 return $this->db->query($sql);
00333         }
00334 
00335         // --------------------------------------------------------------------
00336 
00337         /**
00338          * Reset
00339          *
00340          * Resets table creation vars
00341          *
00342          * @access      private
00343          * @return      void
00344          */
00345         function _reset()
00346         {
00347                 $this->fields           = array();
00348                 $this->keys                     = array();
00349                 $this->primary_keys     = array();
00350         }
00351 
00352 }
00353 
00354 /* End of file DB_forge.php */
00355 /* Location: ./system/database/DB_forge.php */