Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * SQLite Forge Class |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/database/ |
| 36 | */ |
| 37 | class CI_DB_sqlite_forge extends CI_DB_forge { |
| 38 | |
| 39 | /** |
| 40 | * Create database |
| 41 | * |
| 42 | * @access public |
| 43 | * @param string the database name |
| 44 | * @return bool |
| 45 | */ |
| 46 | function _create_database() |
| 47 | { |
| 48 | // In SQLite, a database is created when you connect to the database. |
| 49 | // We'll return TRUE so that an error isn't generated |
| 50 | return TRUE; |
| 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * Drop database |
| 57 | * |
| 58 | * @access private |
| 59 | * @param string the database name |
| 60 | * @return bool |
| 61 | */ |
| 62 | function _drop_database($name) |
| 63 | { |
| 64 | if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database)) |
| 65 | { |
| 66 | if ($this->db->db_debug) |
| 67 | { |
| 68 | return $this->db->display_error('db_unable_to_drop'); |
| 69 | } |
| 70 | return FALSE; |
| 71 | } |
| 72 | return TRUE; |
| 73 | } |
| 74 | // -------------------------------------------------------------------- |
| 75 | |
| 76 | /** |
| 77 | * Create Table |
| 78 | * |
| 79 | * @access private |
| 80 | * @param string the table name |
| 81 | * @param array the fields |
| 82 | * @param mixed primary key(s) |
| 83 | * @param mixed key(s) |
| 84 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 85 | * @return bool |
| 86 | */ |
| 87 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 88 | { |
| 89 | $sql = 'CREATE TABLE '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | // IF NOT EXISTS added to SQLite in 3.3.0 |
Derek Jones | 4bde110 | 2010-03-30 08:56:53 -0500 | [diff] [blame] | 92 | if ($if_not_exists === TRUE && version_compare($this->db->_version(), '3.3.0', '>=') === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | { |
| 94 | $sql .= 'IF NOT EXISTS '; |
| 95 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | $sql .= $this->db->_escape_identifiers($table)."("; |
| 98 | $current_field_count = 0; |
| 99 | |
| 100 | foreach ($fields as $field=>$attributes) |
| 101 | { |
| 102 | // Numeric field names aren't allowed in databases, so if the key is |
| 103 | // numeric, we know it was assigned by PHP and the developer manually |
| 104 | // entered the field information, so we'll simply add it to the list |
| 105 | if (is_numeric($field)) |
| 106 | { |
| 107 | $sql .= "\n\t$attributes"; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | $sql .= "\n\t".$this->db->_protect_identifiers($field); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 115 | $sql .= ' '.$attributes['TYPE']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 118 | { |
| 119 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 120 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 123 | { |
| 124 | $sql .= ' UNSIGNED'; |
| 125 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | if (array_key_exists('DEFAULT', $attributes)) |
| 128 | { |
| 129 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 130 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 133 | { |
| 134 | $sql .= ' NULL'; |
| 135 | } |
| 136 | else |
| 137 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 142 | { |
| 143 | $sql .= ' AUTO_INCREMENT'; |
| 144 | } |
| 145 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | // don't add a comma on the end of the last field |
| 148 | if (++$current_field_count < count($fields)) |
| 149 | { |
| 150 | $sql .= ','; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (count($primary_keys) > 0) |
| 155 | { |
| 156 | $primary_keys = $this->db->_protect_identifiers($primary_keys); |
| 157 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 158 | } |
| 159 | |
| 160 | if (is_array($keys) && count($keys) > 0) |
| 161 | { |
| 162 | foreach ($keys as $key) |
| 163 | { |
| 164 | if (is_array($key)) |
| 165 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | $key = $this->db->_protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { |
| 170 | $key = array($this->db->_protect_identifiers($key)); |
| 171 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")"; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | $sql .= "\n)"; |
| 178 | |
| 179 | return $sql; |
| 180 | } |
| 181 | |
| 182 | // -------------------------------------------------------------------- |
| 183 | |
| 184 | /** |
| 185 | * Drop Table |
| 186 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 187 | * Unsupported feature in SQLite |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | * |
| 189 | * @access private |
| 190 | * @return bool |
| 191 | */ |
| 192 | function _drop_table($table) |
| 193 | { |
| 194 | if ($this->db->db_debug) |
| 195 | { |
| 196 | return $this->db->display_error('db_unsuported_feature'); |
| 197 | } |
| 198 | return array(); |
| 199 | } |
| 200 | |
| 201 | // -------------------------------------------------------------------- |
| 202 | |
| 203 | /** |
| 204 | * Alter table query |
| 205 | * |
| 206 | * Generates a platform-specific query so that a table can be altered |
| 207 | * Called by add_column(), drop_column(), and column_alter(), |
| 208 | * |
| 209 | * @access private |
| 210 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 211 | * @param string the column name |
| 212 | * @param string the table name |
| 213 | * @param string the column definition |
| 214 | * @param string the default value |
| 215 | * @param boolean should 'NOT NULL' be added |
| 216 | * @param string the field after which we should add the new field |
| 217 | * @return object |
| 218 | */ |
| 219 | function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
| 220 | { |
| 221 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name); |
| 222 | |
| 223 | // DROP has everything it needs now. |
| 224 | if ($alter_type == 'DROP') |
| 225 | { |
| 226 | // SQLite does not support dropping columns |
| 227 | // http://www.sqlite.org/omitted.html |
| 228 | // http://www.sqlite.org/faq.html#q11 |
| 229 | return FALSE; |
| 230 | } |
| 231 | |
| 232 | $sql .= " $column_definition"; |
| 233 | |
| 234 | if ($default_value != '') |
| 235 | { |
| 236 | $sql .= " DEFAULT \"$default_value\""; |
| 237 | } |
| 238 | |
| 239 | if ($null === NULL) |
| 240 | { |
| 241 | $sql .= ' NULL'; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | $sql .= ' NOT NULL'; |
| 246 | } |
| 247 | |
| 248 | if ($after_field != '') |
| 249 | { |
| 250 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 251 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 252 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | return $sql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 254 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
| 260 | * Rename a table |
| 261 | * |
| 262 | * Generates a platform-specific query so that a table can be renamed |
| 263 | * |
| 264 | * @access private |
| 265 | * @param string the old table name |
| 266 | * @param string the new table name |
| 267 | * @return string |
| 268 | */ |
| 269 | function _rename_table($table_name, $new_table_name) |
| 270 | { |
| 271 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); |
| 272 | return $sql; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* End of file sqlite_forge.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 277 | /* Location: ./system/database/drivers/sqlite/sqlite_forge.php */ |