blob: df05548693835b3bc55aeedd413f4e69a28e04c0 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Andrey Andreev8ae24c52012-01-16 13:05:23 +02002/**
3 * CodeIgniter
4 *
vlakofffe832492012-07-13 20:40:06 +02005 * An open source application development framework for PHP 5.2.4 or newer
Andrey Andreev8ae24c52012-01-16 13:05:23 +02006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev8ae24c52012-01-16 13:05:23 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev8ae24c52012-01-16 13:05:23 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Andreev8ae24c52012-01-16 13:05:23 +020017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 3.0.0
Andrey Andreev8ae24c52012-01-16 13:05:23 +020036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Andrey Andreev8ae24c52012-01-16 13:05:23 +020039
40/**
41 * SQLite3 Forge Class
42 *
43 * @category Database
44 * @author Andrey Andreev
45 * @link http://codeigniter.com/user_guide/database/
46 */
47class CI_DB_sqlite3_forge extends CI_DB_forge {
48
49 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020050 * 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
Zachary Flower9b512282014-11-02 21:51:19 -070063 /**
64 * COMMENT value representation in CREATE/ALTER TABLE statements
65 *
66 * @var string
67 */
68 protected $_comment = FALSE;
69
Andrey Andreeva287a342012-11-05 23:19:59 +020070 // --------------------------------------------------------------------
71
72 /**
73 * Class constructor
74 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020075 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020076 * @return void
77 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020078 public function __construct(&$db)
Andrey Andreeva287a342012-11-05 23:19:59 +020079 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020080 parent::__construct($db);
Andrey Andreeva287a342012-11-05 23:19:59 +020081
82 if (version_compare($this->db->version(), '3.3', '<'))
83 {
84 $this->create_table_if = FALSE;
85 }
86 }
87
88 // --------------------------------------------------------------------
89
90 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +020091 * Create database
92 *
Andrey Andreeva287a342012-11-05 23:19:59 +020093 * @param string $db_name
Andrey Andreev8ae24c52012-01-16 13:05:23 +020094 * @return bool
95 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030096 public function create_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020097 {
98 // In SQLite, a database is created when you connect to the database.
99 // We'll return TRUE so that an error isn't generated
100 return TRUE;
101 }
102
103 // --------------------------------------------------------------------
104
105 /**
106 * Drop database
107 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200108 * @param string $db_name (ignored)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200109 * @return bool
110 */
Andrey Andreevd947eba2012-04-09 14:58:28 +0300111 public function drop_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200112 {
113 // In SQLite, a database is dropped when we delete a file
Andrey Andreev382b5132014-02-26 18:41:59 +0200114 if (file_exists($this->db->database))
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200115 {
116 // We need to close the pseudo-connection first
117 $this->db->close();
118 if ( ! @unlink($this->db->database))
119 {
120 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
121 }
Andrey Andreev5d281762012-06-11 22:05:40 +0300122 elseif ( ! empty($this->db->data_cache['db_names']))
123 {
124 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
125 if ($key !== FALSE)
126 {
127 unset($this->db->data_cache['db_names'][$key]);
128 }
129 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200130
131 return TRUE;
132 }
133
134 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200140 * ALTER TABLE
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200141 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200142 * @todo implement drop_column(), modify_column()
143 * @param string $alter_type ALTER type
144 * @param string $table Table name
145 * @param mixed $field Column definition
146 * @return string|string[]
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200147 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200148 protected function _alter_table($alter_type, $table, $field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200149 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200150 if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200151 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200152 // drop_column():
153 // BEGIN TRANSACTION;
154 // CREATE TEMPORARY TABLE t1_backup(a,b);
155 // INSERT INTO t1_backup SELECT a,b FROM t1;
156 // DROP TABLE t1;
157 // CREATE TABLE t1(a,b);
158 // INSERT INTO t1 SELECT a,b FROM t1_backup;
159 // DROP TABLE t1_backup;
160 // COMMIT;
161
162 return FALSE;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200163 }
164
Andrey Andreeva287a342012-11-05 23:19:59 +0200165 return parent::_alter_table($alter_type, $table, $field);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200166 }
167
168 // --------------------------------------------------------------------
169
170 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200171 * Process column
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200172 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200173 * @param array $field
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200174 * @return string
175 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200176 protected function _process_column($field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200177 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200178 return $this->db->escape_identifiers($field['name'])
179 .' '.$field['type']
180 .$field['auto_increment']
181 .$field['null']
182 .$field['unique']
183 .$field['default'];
184 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200185
Andrey Andreeva287a342012-11-05 23:19:59 +0200186 // --------------------------------------------------------------------
187
188 /**
189 * Field attribute TYPE
190 *
191 * Performs a data type mapping between different databases.
192 *
193 * @param array &$attributes
194 * @return void
195 */
196 protected function _attr_type(&$attributes)
197 {
198 switch (strtoupper($attributes['TYPE']))
199 {
200 case 'ENUM':
201 case 'SET':
202 $attributes['TYPE'] = 'TEXT';
203 return;
204 default: return;
205 }
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Field attribute AUTO_INCREMENT
212 *
213 * @param array &$attributes
214 * @param array &$field
215 * @return void
216 */
217 protected function _attr_auto_increment(&$attributes, &$field)
218 {
219 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
220 {
221 $field['type'] = 'INTEGER PRIMARY KEY';
222 $field['default'] = '';
223 $field['null'] = '';
224 $field['unique'] = '';
225 $field['auto_increment'] = ' AUTOINCREMENT';
226
227 $this->primary_keys = array();
228 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200229 }
230
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200231}
232
233/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200234/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */