Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 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 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * SQLite3 Forge Class |
| 30 | * |
| 31 | * @category Database |
| 32 | * @author Andrey Andreev |
| 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
| 35 | class CI_DB_sqlite3_forge extends CI_DB_forge { |
| 36 | |
| 37 | /** |
| 38 | * Create database |
| 39 | * |
| 40 | * @param string the database name |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function _create_database() |
| 44 | { |
| 45 | // In SQLite, a database is created when you connect to the database. |
| 46 | // We'll return TRUE so that an error isn't generated |
| 47 | return TRUE; |
| 48 | } |
| 49 | |
| 50 | // -------------------------------------------------------------------- |
| 51 | |
| 52 | /** |
| 53 | * Drop database |
| 54 | * |
| 55 | * @param string the database name |
| 56 | * @return bool |
| 57 | */ |
| 58 | public function _drop_database($name) |
| 59 | { |
| 60 | // In SQLite, a database is dropped when we delete a file |
| 61 | if (@file_exists($this->db->database)) |
| 62 | { |
| 63 | // We need to close the pseudo-connection first |
| 64 | $this->db->close(); |
| 65 | if ( ! @unlink($this->db->database)) |
| 66 | { |
| 67 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 68 | } |
| 69 | |
| 70 | return TRUE; |
| 71 | } |
| 72 | |
| 73 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 74 | } |
| 75 | |
| 76 | // -------------------------------------------------------------------- |
| 77 | |
| 78 | /** |
| 79 | * Create Table |
| 80 | * |
| 81 | * @param string the table name |
| 82 | * @param array the fields |
| 83 | * @param mixed primary key(s) |
| 84 | * @param mixed key(s) |
| 85 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
| 86 | * @return bool |
| 87 | */ |
| 88 | public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 89 | { |
| 90 | $sql = 'CREATE TABLE '; |
| 91 | |
| 92 | // IF NOT EXISTS added to SQLite in 3.3.0 |
| 93 | if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE) |
| 94 | { |
| 95 | $sql .= 'IF NOT EXISTS '; |
| 96 | } |
| 97 | |
| 98 | $sql .= $this->db->_escape_identifiers($table).'('; |
| 99 | $current_field_count = 0; |
| 100 | |
| 101 | foreach ($fields as $field=>$attributes) |
| 102 | { |
| 103 | // Numeric field names aren't allowed in databases, so if the key is |
| 104 | // numeric, we know it was assigned by PHP and the developer manually |
| 105 | // entered the field information, so we'll simply add it to the list |
| 106 | if (is_numeric($field)) |
| 107 | { |
| 108 | $sql .= "\n\t".$attributes; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 113 | |
| 114 | $sql .= "\n\t".$this->db->_protect_identifiers($field) |
| 115 | .' '.$attributes['TYPE'] |
| 116 | .(array_key_exists('CONSTRAINT', $attributes) ? '('.$attributes['CONSTRAINT'].')' : '') |
| 117 | .((array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '') |
| 118 | .(array_key_exists('DEFAULT', $attributes) ? ' DEFAULT \''.$attributes['DEFAULT'].'\'' : '') |
| 119 | .((array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') |
| 120 | .((array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : ''); |
| 121 | } |
| 122 | |
| 123 | // don't add a comma on the end of the last field |
| 124 | if (++$current_field_count < count($fields)) |
| 125 | { |
| 126 | $sql .= ','; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (count($primary_keys) > 0) |
| 131 | { |
| 132 | $primary_keys = $this->db->_protect_identifiers($primary_keys); |
| 133 | $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')'; |
| 134 | } |
| 135 | |
| 136 | if (is_array($keys) && count($keys) > 0) |
| 137 | { |
| 138 | foreach ($keys as $key) |
| 139 | { |
| 140 | if (is_array($key)) |
| 141 | { |
| 142 | $key = $this->db->_protect_identifiers($key); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | $key = array($this->db->_protect_identifiers($key)); |
| 147 | } |
| 148 | |
| 149 | $sql .= ",\n\tUNIQUE (".implode(', ', $key).')'; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $sql."\n)"; |
| 154 | } |
| 155 | |
| 156 | // -------------------------------------------------------------------- |
| 157 | |
| 158 | /** |
| 159 | * Drop Table |
| 160 | * |
| 161 | * @param string the table name |
| 162 | * @return string |
| 163 | */ |
| 164 | public function _drop_table($table) |
| 165 | { |
| 166 | return 'DROP TABLE '.$table.' IF EXISTS'; |
| 167 | } |
| 168 | |
| 169 | // -------------------------------------------------------------------- |
| 170 | |
| 171 | /** |
| 172 | * Alter table query |
| 173 | * |
| 174 | * Generates a platform-specific query so that a table can be altered |
| 175 | * Called by add_column(), drop_column(), and column_alter(), |
| 176 | * |
| 177 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 178 | * @param string the column name |
| 179 | * @param string the table name |
| 180 | * @param string the column definition |
| 181 | * @param string the default value |
| 182 | * @param bool should 'NOT NULL' be added |
| 183 | * @param string the field after which we should add the new field |
| 184 | * @return string |
| 185 | */ |
| 186 | public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
| 187 | { |
| 188 | /* SQLite only supports adding new columns and it does |
| 189 | * NOT support the AFTER statement. Each new column will |
| 190 | * be added as the last one in the table. |
| 191 | */ |
| 192 | if ($alter_type !== 'ADD COLUMN') |
| 193 | { |
| 194 | // Not supported |
| 195 | return FALSE; |
| 196 | } |
| 197 | |
| 198 | return 'ALTER TABLE '.$this->db->_protect_identifiers($table).' '.$alter_type.' '.$this->db->_protect_identifiers($column_name) |
| 199 | .' '.$column_definition |
| 200 | .($default_value != '' ? ' DEFAULT '.$default_value : '') |
| 201 | // If NOT NULL is specified, the field must have a DEFAULT value other than NULL |
| 202 | .(($null !== NULL && $default_value !== 'NULL') ? ' NOT NULL' : ' NULL'); |
| 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
| 207 | /** |
| 208 | * Rename a table |
| 209 | * |
| 210 | * Generates a platform-specific query so that a table can be renamed |
| 211 | * |
| 212 | * @param string the old table name |
| 213 | * @param string the new table name |
| 214 | * @return string |
| 215 | */ |
| 216 | public function _rename_table($table_name, $new_table_name) |
| 217 | { |
| 218 | return 'ALTER TABLE '.$this->db->_protect_identifiers($table_name).' RENAME TO '.$this->db->_protect_identifiers($new_table_name); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* End of file sqlite3_forge.php */ |
| 223 | /* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */ |