Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | 37b3ecf | 2008-09-12 23:34:18 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * SQLite Forge Class
|
| 20 | *
|
| 21 | * @category Database
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 22 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 24 | */
|
| 25 | class CI_DB_sqlite_forge extends CI_DB_forge {
|
| 26 |
|
| 27 | /**
|
| 28 | * Create database
|
| 29 | *
|
| 30 | * @access public
|
| 31 | * @param string the database name
|
| 32 | * @return bool
|
| 33 | */
|
| 34 | function _create_database()
|
| 35 | {
|
| 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
|
| 38 | return TRUE;
|
| 39 | }
|
| 40 |
|
| 41 | // --------------------------------------------------------------------
|
| 42 |
|
| 43 | /**
|
| 44 | * Drop database
|
| 45 | *
|
| 46 | * @access private
|
| 47 | * @param string the database name
|
| 48 | * @return bool
|
| 49 | */
|
| 50 | function _drop_database($name)
|
| 51 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 52 | if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 53 | {
|
| 54 | if ($this->db->db_debug)
|
| 55 | {
|
| 56 | return $this->db->display_error('db_unable_to_drop');
|
| 57 | }
|
| 58 | return FALSE;
|
| 59 | }
|
| 60 | return TRUE;
|
| 61 | }
|
| 62 | // --------------------------------------------------------------------
|
| 63 |
|
| 64 | /**
|
| 65 | * Create Table
|
| 66 | *
|
| 67 | * @access private
|
| 68 | * @param string the table name
|
| 69 | * @param array the fields
|
| 70 | * @param mixed primary key(s)
|
| 71 | * @param mixed key(s)
|
| 72 | * @param boolean should 'IF NOT EXISTS' be added to the SQL
|
| 73 | * @return bool
|
| 74 | */
|
| 75 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
|
| 76 | {
|
| 77 | $sql = 'CREATE TABLE ';
|
| 78 |
|
Derek Jones | 5b4d532 | 2008-02-13 04:34:10 +0000 | [diff] [blame] | 79 | // IF NOT EXISTS added to SQLite in 3.3.0
|
| 80 | if ($if_not_exists === TRUE && version_compare($this->_version(), '3.3.0', '>=') === TRUE)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 81 | {
|
| 82 | $sql .= 'IF NOT EXISTS ';
|
| 83 | }
|
| 84 |
|
Derek Jones | 5b4d532 | 2008-02-13 04:34:10 +0000 | [diff] [blame] | 85 | $sql .= $this->db->_escape_table($table)."(";
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 86 | $current_field_count = 0;
|
| 87 |
|
| 88 | foreach ($fields as $field=>$attributes)
|
| 89 | {
|
| 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))
|
| 94 | {
|
| 95 | $sql .= "\n\t$attributes";
|
| 96 | }
|
| 97 | else
|
| 98 | {
|
| 99 | $attributes = array_change_key_case($attributes, CASE_UPPER);
|
| 100 |
|
| 101 | $sql .= "\n\t".$this->db->_protect_identifiers($field);
|
| 102 |
|
| 103 | $sql .= ' '.$attributes['TYPE'];
|
| 104 |
|
| 105 | if (array_key_exists('CONSTRAINT', $attributes))
|
| 106 | {
|
| 107 | $sql .= '('.$attributes['CONSTRAINT'].')';
|
| 108 | }
|
| 109 |
|
| 110 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
|
| 111 | {
|
| 112 | $sql .= ' UNSIGNED';
|
| 113 | }
|
| 114 |
|
| 115 | if (array_key_exists('DEFAULT', $attributes))
|
| 116 | {
|
| 117 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
|
| 118 | }
|
| 119 |
|
| 120 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
|
| 121 | {
|
| 122 | $sql .= ' NULL';
|
| 123 | }
|
| 124 | else
|
| 125 | {
|
| 126 | $sql .= ' NOT NULL';
|
| 127 | }
|
| 128 |
|
| 129 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
|
| 130 | {
|
| 131 | $sql .= ' AUTO_INCREMENT';
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | // don't add a comma on the end of the last field
|
| 136 | if (++$current_field_count < count($fields))
|
| 137 | {
|
| 138 | $sql .= ',';
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | if (count($primary_keys) > 0)
|
| 143 | {
|
| 144 | $primary_keys = $this->db->_protect_identifiers($primary_keys);
|
| 145 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
|
| 146 | }
|
Rick Ellis | ff73401 | 2008-09-30 20:38:12 +0000 | [diff] [blame] | 147 |
|
Derek Jones | bd44009 | 2008-05-29 17:52:11 +0000 | [diff] [blame] | 148 | if (is_array($keys) && count($keys) > 0)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 149 | {
|
Derek Jones | bd44009 | 2008-05-29 17:52:11 +0000 | [diff] [blame] | 150 | foreach ($keys as $key)
|
| 151 | {
|
| 152 | if (is_array($key))
|
| 153 | {
|
| 154 | $key = $this->db->_protect_identifiers($key);
|
| 155 | }
|
| 156 | else
|
| 157 | {
|
| 158 | $key = array($this->db->_protect_identifiers($key));
|
| 159 | }
|
| 160 |
|
| 161 | $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
|
| 162 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 163 | }
|
Rick Ellis | ff73401 | 2008-09-30 20:38:12 +0000 | [diff] [blame] | 164 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 165 | $sql .= "\n)";
|
| 166 |
|
| 167 | return $sql;
|
| 168 | }
|
| 169 |
|
| 170 | // --------------------------------------------------------------------
|
| 171 |
|
| 172 | /**
|
| 173 | * Drop Table
|
| 174 | *
|
| 175 | * Unsupported feature in SQLite
|
| 176 | *
|
| 177 | * @access private
|
| 178 | * @return bool
|
| 179 | */
|
| 180 | function _drop_table($table)
|
| 181 | {
|
| 182 | if ($this->db->db_debug)
|
| 183 | {
|
| 184 | return $this->db->display_error('db_unsuported_feature');
|
| 185 | }
|
| 186 | return array();
|
| 187 | }
|
| 188 |
|
| 189 | // --------------------------------------------------------------------
|
| 190 |
|
| 191 | /**
|
| 192 | * Alter table query
|
| 193 | *
|
| 194 | * Generates a platform-specific query so that a table can be altered
|
| 195 | * Called by add_column(), drop_column(), and column_alter(),
|
| 196 | *
|
| 197 | * @access private
|
| 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
|
| 205 | * @return object
|
| 206 | */
|
| 207 | function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
|
| 208 | {
|
| 209 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
|
| 210 |
|
| 211 | // DROP has everything it needs now.
|
| 212 | if ($alter_type == 'DROP')
|
| 213 | {
|
| 214 | // SQLite does not support dropping columns
|
| 215 | // http://www.sqlite.org/omitted.html
|
| 216 | // http://www.sqlite.org/faq.html#q11
|
| 217 | return FALSE;
|
| 218 | }
|
| 219 |
|
| 220 | $sql .= " $column_definition";
|
| 221 |
|
| 222 | if ($default_value != '')
|
| 223 | {
|
| 224 | $sql .= " DEFAULT \"$default_value\"";
|
| 225 | }
|
| 226 |
|
| 227 | if ($null === NULL)
|
| 228 | {
|
| 229 | $sql .= ' NULL';
|
| 230 | }
|
| 231 | else
|
| 232 | {
|
| 233 | $sql .= ' NOT NULL';
|
| 234 | }
|
| 235 |
|
| 236 | if ($after_field != '')
|
| 237 | {
|
| 238 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
|
| 239 | }
|
| 240 |
|
| 241 | return $sql;
|
| 242 |
|
| 243 | }
|
Rick Ellis | ff73401 | 2008-09-30 20:38:12 +0000 | [diff] [blame] | 244 |
|
| 245 | // --------------------------------------------------------------------
|
| 246 |
|
| 247 | /**
|
| 248 | * Rename a table
|
| 249 | *
|
| 250 | * Generates a platform-specific query so that a table can be renamed
|
| 251 | *
|
| 252 | * @access private
|
| 253 | * @param string the old table name
|
| 254 | * @param string the new table name
|
| 255 | * @return string
|
| 256 | */
|
| 257 | function _rename_table($table_name, $new_table_name)
|
| 258 | {
|
| 259 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
|
| 260 | return $sql;
|
| 261 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 262 | }
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 263 |
|
| 264 | /* End of file sqlite_forge.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 265 | /* Location: ./system/database/drivers/sqlite/sqlite_forge.php */ |