Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 3.0.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * SQLite3 Forge Class |
| 42 | * |
| 43 | * @category Database |
| 44 | * @author Andrey Andreev |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 45 | * @link https://codeigniter.com/user_guide/database/ |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 46 | */ |
| 47 | class CI_DB_sqlite3_forge extends CI_DB_forge { |
| 48 | |
| 49 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 50 | * UNSIGNED support |
| 51 | * |
| 52 | * @var bool|array |
| 53 | */ |
| 54 | protected $_unsigned = FALSE; |
| 55 | |
| 56 | /** |
| 57 | * NULL value representation in CREATE/ALTER TABLE statements |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
| 61 | protected $_null = 'NULL'; |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
| 64 | |
| 65 | /** |
| 66 | * Class constructor |
| 67 | * |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 68 | * @param object &$db Database object |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 69 | * @return void |
| 70 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 71 | public function __construct(&$db) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 72 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 73 | parent::__construct($db); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 74 | |
| 75 | if (version_compare($this->db->version(), '3.3', '<')) |
| 76 | { |
Andrey Andreev | 40780c2 | 2015-07-02 11:33:02 +0300 | [diff] [blame] | 77 | $this->_create_table_if = FALSE; |
| 78 | $this->_drop_table_if = FALSE; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // -------------------------------------------------------------------- |
| 83 | |
| 84 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 85 | * Create database |
| 86 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 87 | * @param string $db_name |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 88 | * @return bool |
| 89 | */ |
Andrey Andreev | da270b2 | 2016-10-17 18:22:43 +0300 | [diff] [blame] | 90 | public function create_database($db_name) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 91 | { |
| 92 | // In SQLite, a database is created when you connect to the database. |
| 93 | // We'll return TRUE so that an error isn't generated |
| 94 | return TRUE; |
| 95 | } |
| 96 | |
| 97 | // -------------------------------------------------------------------- |
| 98 | |
| 99 | /** |
| 100 | * Drop database |
| 101 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 102 | * @param string $db_name (ignored) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 103 | * @return bool |
| 104 | */ |
Andrey Andreev | da270b2 | 2016-10-17 18:22:43 +0300 | [diff] [blame] | 105 | public function drop_database($db_name) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 106 | { |
| 107 | // In SQLite, a database is dropped when we delete a file |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 108 | if (file_exists($this->db->database)) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 109 | { |
| 110 | // We need to close the pseudo-connection first |
| 111 | $this->db->close(); |
| 112 | if ( ! @unlink($this->db->database)) |
| 113 | { |
| 114 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 115 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 116 | elseif ( ! empty($this->db->data_cache['db_names'])) |
| 117 | { |
| 118 | $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); |
| 119 | if ($key !== FALSE) |
| 120 | { |
| 121 | unset($this->db->data_cache['db_names'][$key]); |
| 122 | } |
| 123 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 124 | |
| 125 | return TRUE; |
| 126 | } |
| 127 | |
| 128 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 134 | * ALTER TABLE |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 135 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 136 | * @todo implement drop_column(), modify_column() |
| 137 | * @param string $alter_type ALTER type |
| 138 | * @param string $table Table name |
| 139 | * @param mixed $field Column definition |
| 140 | * @return string|string[] |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 141 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 142 | protected function _alter_table($alter_type, $table, $field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 143 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 144 | if ($alter_type === 'DROP' OR $alter_type === 'CHANGE') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 145 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 146 | // drop_column(): |
| 147 | // BEGIN TRANSACTION; |
| 148 | // CREATE TEMPORARY TABLE t1_backup(a,b); |
| 149 | // INSERT INTO t1_backup SELECT a,b FROM t1; |
| 150 | // DROP TABLE t1; |
| 151 | // CREATE TABLE t1(a,b); |
| 152 | // INSERT INTO t1 SELECT a,b FROM t1_backup; |
| 153 | // DROP TABLE t1_backup; |
| 154 | // COMMIT; |
| 155 | |
| 156 | return FALSE; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 159 | return parent::_alter_table($alter_type, $table, $field); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 165 | * Process column |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 166 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 167 | * @param array $field |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 168 | * @return string |
| 169 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 170 | protected function _process_column($field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 171 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 172 | return $this->db->escape_identifiers($field['name']) |
| 173 | .' '.$field['type'] |
| 174 | .$field['auto_increment'] |
| 175 | .$field['null'] |
| 176 | .$field['unique'] |
| 177 | .$field['default']; |
| 178 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 179 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
| 183 | * Field attribute TYPE |
| 184 | * |
| 185 | * Performs a data type mapping between different databases. |
| 186 | * |
| 187 | * @param array &$attributes |
| 188 | * @return void |
| 189 | */ |
| 190 | protected function _attr_type(&$attributes) |
| 191 | { |
| 192 | switch (strtoupper($attributes['TYPE'])) |
| 193 | { |
| 194 | case 'ENUM': |
| 195 | case 'SET': |
| 196 | $attributes['TYPE'] = 'TEXT'; |
| 197 | return; |
| 198 | default: return; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Field attribute AUTO_INCREMENT |
| 206 | * |
| 207 | * @param array &$attributes |
| 208 | * @param array &$field |
| 209 | * @return void |
| 210 | */ |
| 211 | protected function _attr_auto_increment(&$attributes, &$field) |
| 212 | { |
| 213 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) |
| 214 | { |
| 215 | $field['type'] = 'INTEGER PRIMARY KEY'; |
| 216 | $field['default'] = ''; |
| 217 | $field['null'] = ''; |
| 218 | $field['unique'] = ''; |
| 219 | $field['auto_increment'] = ' AUTOINCREMENT'; |
| 220 | |
| 221 | $this->primary_keys = array(); |
| 222 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 225 | } |