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