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 | * |
vlakoff | fe83249 | 2012-07-13 20:40:06 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 |
Andrey Andreev | f20fb98 | 2012-01-24 15:26:01 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 | * |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 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 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * SQLite3 Forge Class |
| 31 | * |
| 32 | * @category Database |
| 33 | * @author Andrey Andreev |
| 34 | * @link http://codeigniter.com/user_guide/database/ |
| 35 | */ |
| 36 | class CI_DB_sqlite3_forge extends CI_DB_forge { |
| 37 | |
| 38 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 39 | * UNSIGNED support |
| 40 | * |
| 41 | * @var bool|array |
| 42 | */ |
| 43 | protected $_unsigned = FALSE; |
| 44 | |
| 45 | /** |
| 46 | * NULL value representation in CREATE/ALTER TABLE statements |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $_null = 'NULL'; |
| 51 | |
| 52 | // -------------------------------------------------------------------- |
| 53 | |
| 54 | /** |
| 55 | * Class constructor |
| 56 | * |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 57 | * @param object &$db Database object |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 58 | * @return void |
| 59 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 60 | public function __construct(&$db) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 61 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 62 | parent::__construct($db); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 63 | |
| 64 | if (version_compare($this->db->version(), '3.3', '<')) |
| 65 | { |
| 66 | $this->create_table_if = FALSE; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // -------------------------------------------------------------------- |
| 71 | |
| 72 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 73 | * Create database |
| 74 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 75 | * @param string $db_name |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 76 | * @return bool |
| 77 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 78 | public function create_database($db_name = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 79 | { |
| 80 | // In SQLite, a database is created when you connect to the database. |
| 81 | // We'll return TRUE so that an error isn't generated |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | /** |
| 88 | * Drop database |
| 89 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 90 | * @param string $db_name (ignored) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 91 | * @return bool |
| 92 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 93 | public function drop_database($db_name = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 94 | { |
| 95 | // In SQLite, a database is dropped when we delete a file |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame^] | 96 | if (file_exists($this->db->database)) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 97 | { |
| 98 | // We need to close the pseudo-connection first |
| 99 | $this->db->close(); |
| 100 | if ( ! @unlink($this->db->database)) |
| 101 | { |
| 102 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 103 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 104 | elseif ( ! empty($this->db->data_cache['db_names'])) |
| 105 | { |
| 106 | $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); |
| 107 | if ($key !== FALSE) |
| 108 | { |
| 109 | unset($this->db->data_cache['db_names'][$key]); |
| 110 | } |
| 111 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 112 | |
| 113 | return TRUE; |
| 114 | } |
| 115 | |
| 116 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 117 | } |
| 118 | |
| 119 | // -------------------------------------------------------------------- |
| 120 | |
| 121 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 122 | * ALTER TABLE |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 123 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 124 | * @todo implement drop_column(), modify_column() |
| 125 | * @param string $alter_type ALTER type |
| 126 | * @param string $table Table name |
| 127 | * @param mixed $field Column definition |
| 128 | * @return string|string[] |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 129 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 130 | protected function _alter_table($alter_type, $table, $field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 131 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 132 | if ($alter_type === 'DROP' OR $alter_type === 'CHANGE') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 133 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 134 | // drop_column(): |
| 135 | // BEGIN TRANSACTION; |
| 136 | // CREATE TEMPORARY TABLE t1_backup(a,b); |
| 137 | // INSERT INTO t1_backup SELECT a,b FROM t1; |
| 138 | // DROP TABLE t1; |
| 139 | // CREATE TABLE t1(a,b); |
| 140 | // INSERT INTO t1 SELECT a,b FROM t1_backup; |
| 141 | // DROP TABLE t1_backup; |
| 142 | // COMMIT; |
| 143 | |
| 144 | return FALSE; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 147 | return parent::_alter_table($alter_type, $table, $field); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // -------------------------------------------------------------------- |
| 151 | |
| 152 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 153 | * Process column |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 154 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 155 | * @param array $field |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 156 | * @return string |
| 157 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 158 | protected function _process_column($field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 159 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 160 | return $this->db->escape_identifiers($field['name']) |
| 161 | .' '.$field['type'] |
| 162 | .$field['auto_increment'] |
| 163 | .$field['null'] |
| 164 | .$field['unique'] |
| 165 | .$field['default']; |
| 166 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 167 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Field attribute TYPE |
| 172 | * |
| 173 | * Performs a data type mapping between different databases. |
| 174 | * |
| 175 | * @param array &$attributes |
| 176 | * @return void |
| 177 | */ |
| 178 | protected function _attr_type(&$attributes) |
| 179 | { |
| 180 | switch (strtoupper($attributes['TYPE'])) |
| 181 | { |
| 182 | case 'ENUM': |
| 183 | case 'SET': |
| 184 | $attributes['TYPE'] = 'TEXT'; |
| 185 | return; |
| 186 | default: return; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Field attribute AUTO_INCREMENT |
| 194 | * |
| 195 | * @param array &$attributes |
| 196 | * @param array &$field |
| 197 | * @return void |
| 198 | */ |
| 199 | protected function _attr_auto_increment(&$attributes, &$field) |
| 200 | { |
| 201 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) |
| 202 | { |
| 203 | $field['type'] = 'INTEGER PRIMARY KEY'; |
| 204 | $field['default'] = ''; |
| 205 | $field['null'] = ''; |
| 206 | $field['unique'] = ''; |
| 207 | $field['auto_increment'] = ' AUTOINCREMENT'; |
| 208 | |
| 209 | $this->primary_keys = array(); |
| 210 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* End of file sqlite3_forge.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 216 | /* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */ |