mysqli_forge.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
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  * MySQLi Forge Class
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/user_guide/database/
00024  */
00025 class CI_DB_mysqli_forge extends CI_DB_forge {
00026         
00027         /**
00028          * Create database
00029          *
00030          * @access      private
00031          * @param       string  the database name
00032          * @return      bool
00033          */
00034         function _create_database($name)
00035         {
00036                 return "CREATE DATABASE ".$name;
00037         }
00038 
00039         // --------------------------------------------------------------------
00040 
00041         /**
00042          * Drop database
00043          *
00044          * @access      private
00045          * @param       string  the database name
00046          * @return      bool
00047          */
00048         function _drop_database($name)
00049         {
00050                 return "DROP DATABASE ".$name;
00051         }
00052 
00053         // --------------------------------------------------------------------
00054 
00055         /**
00056          * Process Fields
00057          *
00058          * @access      private
00059          * @param       mixed   the fields
00060          * @return      string
00061          */
00062         function _process_fields($fields)
00063         {
00064                 $current_field_count = 0;
00065                 $sql = '';
00066 
00067                 foreach ($fields as $field=>$attributes)
00068                 {
00069                         // Numeric field names aren't allowed in databases, so if the key is
00070                         // numeric, we know it was assigned by PHP and the developer manually
00071                         // entered the field information, so we'll simply add it to the list
00072                         if (is_numeric($field))
00073                         {
00074                                 $sql .= "\n\t$attributes";
00075                         }
00076                         else
00077                         {
00078                                 $attributes = array_change_key_case($attributes, CASE_UPPER);
00079                                 
00080                                 $sql .= "\n\t".$this->db->_protect_identifiers($field);
00081 
00082                                 if (array_key_exists('NAME', $attributes))
00083                                 {
00084                                         $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
00085                                 }
00086                                 
00087                                 if (array_key_exists('TYPE', $attributes))
00088                                 {
00089                                         $sql .=  ' '.$attributes['TYPE'];
00090                                 }
00091         
00092                                 if (array_key_exists('CONSTRAINT', $attributes))
00093                                 {
00094                                         $sql .= '('.$attributes['CONSTRAINT'].')';
00095                                 }
00096         
00097                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
00098                                 {
00099                                         $sql .= ' UNSIGNED';
00100                                 }
00101         
00102                                 if (array_key_exists('DEFAULT', $attributes))
00103                                 {
00104                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
00105                                 }
00106         
00107                                 if (array_key_exists('NULL', $attributes))
00108                                 {
00109                                         $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
00110                                 }
00111         
00112                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
00113                                 {
00114                                         $sql .= ' AUTO_INCREMENT';
00115                                 }
00116                         }
00117                         
00118                         // don't add a comma on the end of the last field
00119                         if (++$current_field_count < count($fields))
00120                         {
00121                                 $sql .= ',';
00122                         }
00123                 }
00124                 
00125                 return $sql;
00126         }
00127 
00128         // --------------------------------------------------------------------
00129 
00130         /**
00131          * Create Table
00132          *
00133          * @access      private
00134          * @param       string  the table name
00135          * @param       mixed   the fields
00136          * @param       mixed   primary key(s)
00137          * @param       mixed   key(s)
00138          * @param       boolean should 'IF NOT EXISTS' be added to the SQL
00139          * @return      bool
00140          */
00141         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
00142         {
00143                 $sql = 'CREATE TABLE ';
00144                 
00145                 if ($if_not_exists === TRUE)
00146                 {
00147                         $sql .= 'IF NOT EXISTS ';
00148                 }
00149                 
00150                 $sql .= $this->db->_escape_table($table)." (";
00151 
00152                 $sql .= $this->_process_fields($fields);
00153 
00154                 if (count($primary_keys) > 0)
00155                 {
00156                         $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
00157                         $primary_keys = $this->db->_protect_identifiers($primary_keys);
00158                         $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
00159                 }
00160 
00161                 if (is_array($keys) && count($keys) > 0)
00162                 {
00163                         foreach ($keys as $key)
00164                         {
00165                                 if (is_array($key))
00166                                 {
00167                                         $key_name = $this->db->_protect_identifiers(implode('_', $key));
00168                                         $key = $this->db->_protect_identifiers($key);   
00169                                 }
00170                                 else
00171                                 {
00172                                         $key_name = $this->db->_protect_identifiers($key);
00173                                         $key = array($key_name);
00174                                 }
00175                                 
00176                                 $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
00177                         }
00178                 }
00179 
00180                 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
00181 
00182                 return $sql;
00183         }
00184 
00185         // --------------------------------------------------------------------
00186 
00187         /**
00188          * Drop Table
00189          *
00190          * @access      private
00191          * @return      string
00192          */
00193         function _drop_table($table)
00194         {
00195                 return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
00196         }
00197 
00198         // --------------------------------------------------------------------
00199 
00200         /**
00201          * Alter table query
00202          *
00203          * Generates a platform-specific query so that a table can be altered
00204          * Called by add_column(), drop_column(), and column_alter(),
00205          *
00206          * @access      private
00207          * @param       string  the ALTER type (ADD, DROP, CHANGE)
00208          * @param       string  the column name
00209          * @param       array   fields
00210          * @param       string  the field after which we should add the new field
00211          * @return      object
00212          */
00213         function _alter_table($alter_type, $table, $fields, $after_field = '')
00214         {
00215                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
00216 
00217                 // DROP has everything it needs now.
00218                 if ($alter_type == 'DROP')
00219                 {
00220                         return $sql.$this->db->_protect_identifiers($fields);
00221                 }
00222 
00223                 $sql .= $this->_process_fields($fields);
00224 
00225                 if ($after_field != '')
00226                 {
00227                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
00228                 }
00229                 
00230                 return $sql;
00231         }
00232 
00233         // --------------------------------------------------------------------
00234 
00235         /**
00236          * Rename a table
00237          *
00238          * Generates a platform-specific query so that a table can be renamed
00239          *
00240          * @access      private
00241          * @param       string  the old table name
00242          * @param       string  the new table name
00243          * @return      string
00244          */
00245         function _rename_table($table_name, $new_table_name)
00246         {
00247                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
00248                 return $sql;
00249         }
00250 
00251 }
00252 
00253 /* End of file mysqli_forge.php */
00254 /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */