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 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, 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 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function __construct() |
| 60 | { |
| 61 | parent::__construct(); |
| 62 | |
| 63 | if (version_compare($this->db->version(), '3.3', '<')) |
| 64 | { |
| 65 | $this->create_table_if = FALSE; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // -------------------------------------------------------------------- |
| 70 | |
| 71 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 72 | * Create database |
| 73 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 74 | * @param string $db_name |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 75 | * @return bool |
| 76 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 77 | public function create_database($db_name = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 78 | { |
| 79 | // In SQLite, a database is created when you connect to the database. |
| 80 | // We'll return TRUE so that an error isn't generated |
| 81 | return TRUE; |
| 82 | } |
| 83 | |
| 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * Drop database |
| 88 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 89 | * @param string $db_name (ignored) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 90 | * @return bool |
| 91 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 92 | public function drop_database($db_name = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 93 | { |
| 94 | // In SQLite, a database is dropped when we delete a file |
| 95 | if (@file_exists($this->db->database)) |
| 96 | { |
| 97 | // We need to close the pseudo-connection first |
| 98 | $this->db->close(); |
| 99 | if ( ! @unlink($this->db->database)) |
| 100 | { |
| 101 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 102 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 103 | elseif ( ! empty($this->db->data_cache['db_names'])) |
| 104 | { |
| 105 | $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); |
| 106 | if ($key !== FALSE) |
| 107 | { |
| 108 | unset($this->db->data_cache['db_names'][$key]); |
| 109 | } |
| 110 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 111 | |
| 112 | return TRUE; |
| 113 | } |
| 114 | |
| 115 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 116 | } |
| 117 | |
| 118 | // -------------------------------------------------------------------- |
| 119 | |
| 120 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 121 | * ALTER TABLE |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 122 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 123 | * @todo implement drop_column(), modify_column() |
| 124 | * @param string $alter_type ALTER type |
| 125 | * @param string $table Table name |
| 126 | * @param mixed $field Column definition |
| 127 | * @return string|string[] |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 128 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 129 | protected function _alter_table($alter_type, $table, $field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 130 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 131 | if ($alter_type === 'DROP' OR $alter_type === 'CHANGE') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 132 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 133 | // drop_column(): |
| 134 | // BEGIN TRANSACTION; |
| 135 | // CREATE TEMPORARY TABLE t1_backup(a,b); |
| 136 | // INSERT INTO t1_backup SELECT a,b FROM t1; |
| 137 | // DROP TABLE t1; |
| 138 | // CREATE TABLE t1(a,b); |
| 139 | // INSERT INTO t1 SELECT a,b FROM t1_backup; |
| 140 | // DROP TABLE t1_backup; |
| 141 | // COMMIT; |
| 142 | |
| 143 | return FALSE; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 146 | return parent::_alter_table($alter_type, $table, $field); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 152 | * Process column |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 153 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 154 | * @param array $field |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 155 | * @return string |
| 156 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 157 | protected function _process_column($field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 158 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 159 | return $this->db->escape_identifiers($field['name']) |
| 160 | .' '.$field['type'] |
| 161 | .$field['auto_increment'] |
| 162 | .$field['null'] |
| 163 | .$field['unique'] |
| 164 | .$field['default']; |
| 165 | } |
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 | // -------------------------------------------------------------------- |
| 168 | |
| 169 | /** |
| 170 | * Field attribute TYPE |
| 171 | * |
| 172 | * Performs a data type mapping between different databases. |
| 173 | * |
| 174 | * @param array &$attributes |
| 175 | * @return void |
| 176 | */ |
| 177 | protected function _attr_type(&$attributes) |
| 178 | { |
| 179 | switch (strtoupper($attributes['TYPE'])) |
| 180 | { |
| 181 | case 'ENUM': |
| 182 | case 'SET': |
| 183 | $attributes['TYPE'] = 'TEXT'; |
| 184 | return; |
| 185 | default: return; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // -------------------------------------------------------------------- |
| 190 | |
| 191 | /** |
| 192 | * Field attribute AUTO_INCREMENT |
| 193 | * |
| 194 | * @param array &$attributes |
| 195 | * @param array &$field |
| 196 | * @return void |
| 197 | */ |
| 198 | protected function _attr_auto_increment(&$attributes, &$field) |
| 199 | { |
| 200 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) |
| 201 | { |
| 202 | $field['type'] = 'INTEGER PRIMARY KEY'; |
| 203 | $field['default'] = ''; |
| 204 | $field['null'] = ''; |
| 205 | $field['unique'] = ''; |
| 206 | $field['auto_increment'] = ' AUTOINCREMENT'; |
| 207 | |
| 208 | $this->primary_keys = array(); |
| 209 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* End of file sqlite3_forge.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 215 | /* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */ |