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