1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * SQLite Forge Class
\r
21 * @category Database
\r
22 * @author ExpressionEngine Dev Team
\r
23 * @link http://codeigniter.com/user_guide/database/
\r
25 class CI_DB_sqlite_forge extends CI_DB_forge {
\r
31 * @param string the database name
\r
34 function _create_database()
\r
36 // In SQLite, a database is created when you connect to the database.
\r
37 // We'll return TRUE so that an error isn't generated
\r
41 // --------------------------------------------------------------------
\r
47 * @param string the database name
\r
50 function _drop_database($name)
\r
52 if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
\r
54 if ($this->db->db_debug)
\r
56 return $this->db->display_error('db_unable_to_drop');
\r
62 // --------------------------------------------------------------------
\r
68 * @param string the table name
\r
69 * @param array the fields
\r
70 * @param mixed primary key(s)
\r
71 * @param mixed key(s)
\r
72 * @param boolean should 'IF NOT EXISTS' be added to the SQL
\r
75 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
\r
77 $sql = 'CREATE TABLE ';
\r
79 // IF NOT EXISTS added to SQLite in 3.3.0
\r
80 if ($if_not_exists === TRUE && version_compare($this->_version(), '3.3.0', '>=') === TRUE)
\r
82 $sql .= 'IF NOT EXISTS ';
\r
85 $sql .= $this->db->_escape_identifiers($table)."(";
\r
86 $current_field_count = 0;
\r
88 foreach ($fields as $field=>$attributes)
\r
90 // Numeric field names aren't allowed in databases, so if the key is
\r
91 // numeric, we know it was assigned by PHP and the developer manually
\r
92 // entered the field information, so we'll simply add it to the list
\r
93 if (is_numeric($field))
\r
95 $sql .= "\n\t$attributes";
\r
99 $attributes = array_change_key_case($attributes, CASE_UPPER);
\r
101 $sql .= "\n\t".$this->db->_protect_identifiers($field);
\r
103 $sql .= ' '.$attributes['TYPE'];
\r
105 if (array_key_exists('CONSTRAINT', $attributes))
\r
107 $sql .= '('.$attributes['CONSTRAINT'].')';
\r
110 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
\r
112 $sql .= ' UNSIGNED';
\r
115 if (array_key_exists('DEFAULT', $attributes))
\r
117 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
\r
120 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
\r
126 $sql .= ' NOT NULL';
\r
129 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
\r
131 $sql .= ' AUTO_INCREMENT';
\r
135 // don't add a comma on the end of the last field
\r
136 if (++$current_field_count < count($fields))
\r
142 if (count($primary_keys) > 0)
\r
144 $primary_keys = $this->db->_protect_identifiers($primary_keys);
\r
145 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
\r
148 if (is_array($keys) && count($keys) > 0)
\r
150 foreach ($keys as $key)
\r
152 if (is_array($key))
\r
154 $key = $this->db->_protect_identifiers($key);
\r
158 $key = array($this->db->_protect_identifiers($key));
\r
161 $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
\r
170 // --------------------------------------------------------------------
\r
175 * Unsupported feature in SQLite
\r
180 function _drop_table($table)
\r
182 if ($this->db->db_debug)
\r
184 return $this->db->display_error('db_unsuported_feature');
\r
189 // --------------------------------------------------------------------
\r
192 * Alter table query
\r
194 * Generates a platform-specific query so that a table can be altered
\r
195 * Called by add_column(), drop_column(), and column_alter(),
\r
198 * @param string the ALTER type (ADD, DROP, CHANGE)
\r
199 * @param string the column name
\r
200 * @param string the table name
\r
201 * @param string the column definition
\r
202 * @param string the default value
\r
203 * @param boolean should 'NOT NULL' be added
\r
204 * @param string the field after which we should add the new field
\r
207 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
\r
209 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
\r
211 // DROP has everything it needs now.
\r
212 if ($alter_type == 'DROP')
\r
214 // SQLite does not support dropping columns
\r
215 // http://www.sqlite.org/omitted.html
\r
216 // http://www.sqlite.org/faq.html#q11
\r
220 $sql .= " $column_definition";
\r
222 if ($default_value != '')
\r
224 $sql .= " DEFAULT \"$default_value\"";
\r
227 if ($null === NULL)
\r
233 $sql .= ' NOT NULL';
\r
236 if ($after_field != '')
\r
238 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
\r
245 // --------------------------------------------------------------------
\r
250 * Generates a platform-specific query so that a table can be renamed
\r
253 * @param string the old table name
\r
254 * @param string the new table name
\r
257 function _rename_table($table_name, $new_table_name)
\r
259 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
\r
264 /* End of file sqlite_forge.php */
\r
265 /* Location: ./system/database/drivers/sqlite/sqlite_forge.php */