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