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) 2006, 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 ($key == '')
00101                 {
00102                         show_error('Key information is required for that operation.');
00103                 }
00104                 
00105                 if ($primary === TRUE)
00106                 {
00107                         $this->primary_keys[] = $key;
00108                 }
00109                 else
00110                 {
00111                         $this->keys[] = $key;
00112                 }
00113         }
00114 
00115         // --------------------------------------------------------------------
00116 
00117         /**
00118          * Add Field
00119          *
00120          * @access      public
00121          * @param       string  collation
00122          * @return      void
00123          */
00124         function add_field($field = '')
00125         {
00126                 if ($field == '')
00127                 {
00128                         show_error('Field information is required.');
00129                 }
00130                 
00131                 if (is_string($field))
00132                 {
00133                         if ($field == 'id')
00134                         {
00135                                 $this->add_field(array(
00136                                                                                 'id' => array(
00137                                                                                                         'type' => 'INT',
00138                                                                                                         'constraint' => 9,
00139                                                                                                         'auto_increment' => TRUE
00140                                                                                                         )
00141                                                                 ));
00142                                 $this->add_key('id', TRUE);
00143                         }
00144                         else
00145                         {
00146                                 if (strpos($field, ' ') === FALSE)
00147                                 {
00148                                         show_error('Field information is required for that operation.');
00149                                 }
00150                                 
00151                                 $this->fields[] = $field;
00152                         }
00153                 }
00154                 
00155                 if (is_array($field))
00156                 {
00157                         $this->fields = array_merge($this->fields, $field);
00158                 }
00159                 
00160         }
00161 
00162         // --------------------------------------------------------------------
00163 
00164         /**
00165          * Create Table
00166          *
00167          * @access      public
00168          * @param       string  the table name
00169          * @return      bool
00170          */
00171         function create_table($table = '', $if_not_exists = FALSE)
00172         {       
00173                 if ($table == '')
00174                 {
00175                         show_error('A table name is required for that operation.');
00176                 }
00177                         
00178                 if (count($this->fields) == 0)
00179                 {       
00180                         show_error('Field information is required.');
00181                 }
00182 
00183                 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
00184 
00185                 $this->_reset();
00186                 return $this->db->query($sql);
00187         }
00188 
00189         // --------------------------------------------------------------------
00190 
00191         /**
00192          * Drop Table
00193          *
00194          * @access      public
00195          * @param       string  the table name
00196          * @return      bool
00197          */
00198         function drop_table($table_name)
00199         {
00200                 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
00201                 
00202                 if (is_bool($sql))
00203                 {
00204                         return $sql;
00205                 }
00206         
00207                 return $this->db->query($sql);
00208         }
00209 
00210         // --------------------------------------------------------------------
00211 
00212         /**
00213          * Rename Table
00214          *
00215          * @access      public
00216          * @param       string  the old table name
00217          * @param       string  the new table name
00218          * @return      bool
00219          */
00220         function rename_table($table_name, $new_table_name)
00221         {
00222                 if ($table_name == '' OR $new_table_name == '')
00223                 {
00224                         show_error('A table name is required for that operation.');
00225                 }
00226                         
00227                 $sql = $this->_rename_table($table_name, $new_table_name);
00228                 return $this->db->query($sql);
00229         }
00230 
00231         // --------------------------------------------------------------------
00232 
00233         /**
00234          * Column Add
00235          *
00236          * @access      public
00237          * @param       string  the table name
00238          * @param       string  the column name
00239          * @param       string  the column definition
00240          * @return      bool
00241          */
00242         function add_column($table = '', $field = array(), $after_field = '')
00243         {
00244                 if ($table == '')
00245                 {
00246                                 show_error('A table name is required for that operation.');
00247                 }
00248 
00249                 // add field info into field array, but we can only do one at a time
00250                 // so only grab the first field in the event there are more then one
00251                 $this->add_field(array_slice($field, 0, 1));
00252 
00253                 if (count($this->fields) == 0)
00254                 {       
00255                         show_error('Field information is required.');
00256                 }
00257 
00258                 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
00259 
00260                 $this->_reset();
00261                 return $this->db->query($sql);
00262         }
00263 
00264         // --------------------------------------------------------------------
00265 
00266         /**
00267          * Column Drop
00268          *
00269          * @access      public
00270          * @param       string  the table name
00271          * @param       string  the column name
00272          * @return      bool
00273          */
00274         function drop_column($table = '', $column_name = '')
00275         {
00276         
00277                 if ($table == '')
00278                 {
00279                                 show_error('A table name is required for that operation.');
00280                 }
00281 
00282                 if ($column_name == '')
00283                 {
00284                                 show_error('A column name is required for that operation.');
00285                 }
00286 
00287                 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
00288         
00289                 return $this->db->query($sql);
00290         }
00291 
00292         // --------------------------------------------------------------------
00293 
00294         /**
00295          * Column Modify
00296          *
00297          * @access      public
00298          * @param       string  the table name
00299          * @param       string  the column name
00300          * @param       string  the column definition
00301          * @return      bool
00302          */
00303         function modify_column($table = '', $field = array())
00304         {
00305         
00306                 if ($table == '')
00307                 {
00308                                 show_error('A table name is required for that operation.');
00309                 }
00310 
00311                 // add field info into field array, but we can only do one at a time
00312                 // so only grab the first field in the event there are more then one
00313                 $this->add_field(array_slice($field, 0, 1));
00314 
00315                 if (count($this->fields) == 0)
00316                 {       
00317                         show_error('Field information is required.');
00318                 }
00319 
00320                 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
00321 
00322                 $this->_reset();
00323                 return $this->db->query($sql);
00324         }
00325 
00326         // --------------------------------------------------------------------
00327 
00328         /**
00329          * Reset
00330          *
00331          * Resets table creation vars
00332          *
00333          * @access      private
00334          * @return      void
00335          */
00336         function _reset()
00337         {
00338                 $this->fields           = array();
00339                 $this->keys                     = array();
00340                 $this->primary_keys     = array();
00341         }
00342 
00343 }
00344 
00345 /* End of file DB_forge.php */
00346 /* Location: ./system/database/DB_forge.php */