blob: a527e51cd24114d7b4f084bb99820b8e747497d7 [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
63 // --------------------------------------------------------------------
64
65 /**
66 * Class constructor
67 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020068 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020069 * @return void
70 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020071 public function __construct(&$db)
Andrey Andreeva287a342012-11-05 23:19:59 +020072 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020073 parent::__construct($db);
Andrey Andreeva287a342012-11-05 23:19:59 +020074
75 if (version_compare($this->db->version(), '3.3', '<'))
76 {
77 $this->create_table_if = FALSE;
78 }
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +020084 * Create database
85 *
Andrey Andreeva287a342012-11-05 23:19:59 +020086 * @param string $db_name
Andrey Andreev8ae24c52012-01-16 13:05:23 +020087 * @return bool
88 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030089 public function create_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020090 {
91 // In SQLite, a database is created when you connect to the database.
92 // We'll return TRUE so that an error isn't generated
93 return TRUE;
94 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Drop database
100 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200101 * @param string $db_name (ignored)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200102 * @return bool
103 */
Andrey Andreevd947eba2012-04-09 14:58:28 +0300104 public function drop_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200105 {
106 // In SQLite, a database is dropped when we delete a file
Andrey Andreev382b5132014-02-26 18:41:59 +0200107 if (file_exists($this->db->database))
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200108 {
109 // We need to close the pseudo-connection first
110 $this->db->close();
111 if ( ! @unlink($this->db->database))
112 {
113 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
114 }
Andrey Andreev5d281762012-06-11 22:05:40 +0300115 elseif ( ! empty($this->db->data_cache['db_names']))
116 {
117 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
118 if ($key !== FALSE)
119 {
120 unset($this->db->data_cache['db_names'][$key]);
121 }
122 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200123
124 return TRUE;
125 }
126
127 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200133 * ALTER TABLE
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200134 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200135 * @todo implement drop_column(), modify_column()
136 * @param string $alter_type ALTER type
137 * @param string $table Table name
138 * @param mixed $field Column definition
139 * @return string|string[]
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200140 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200141 protected function _alter_table($alter_type, $table, $field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200142 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200143 if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200144 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200145 // drop_column():
146 // BEGIN TRANSACTION;
147 // CREATE TEMPORARY TABLE t1_backup(a,b);
148 // INSERT INTO t1_backup SELECT a,b FROM t1;
149 // DROP TABLE t1;
150 // CREATE TABLE t1(a,b);
151 // INSERT INTO t1 SELECT a,b FROM t1_backup;
152 // DROP TABLE t1_backup;
153 // COMMIT;
154
155 return FALSE;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200156 }
157
Andrey Andreeva287a342012-11-05 23:19:59 +0200158 return parent::_alter_table($alter_type, $table, $field);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200159 }
160
161 // --------------------------------------------------------------------
162
163 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200164 * Process column
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200165 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200166 * @param array $field
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200167 * @return string
168 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200169 protected function _process_column($field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200170 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200171 return $this->db->escape_identifiers($field['name'])
172 .' '.$field['type']
173 .$field['auto_increment']
174 .$field['null']
175 .$field['unique']
176 .$field['default'];
177 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200178
Andrey Andreeva287a342012-11-05 23:19:59 +0200179 // --------------------------------------------------------------------
180
181 /**
182 * Field attribute TYPE
183 *
184 * Performs a data type mapping between different databases.
185 *
186 * @param array &$attributes
187 * @return void
188 */
189 protected function _attr_type(&$attributes)
190 {
191 switch (strtoupper($attributes['TYPE']))
192 {
193 case 'ENUM':
194 case 'SET':
195 $attributes['TYPE'] = 'TEXT';
196 return;
197 default: return;
198 }
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Field attribute AUTO_INCREMENT
205 *
206 * @param array &$attributes
207 * @param array &$field
208 * @return void
209 */
210 protected function _attr_auto_increment(&$attributes, &$field)
211 {
212 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
213 {
214 $field['type'] = 'INTEGER PRIMARY KEY';
215 $field['default'] = '';
216 $field['null'] = '';
217 $field['unique'] = '';
218 $field['auto_increment'] = ' AUTOINCREMENT';
219
220 $this->primary_keys = array();
221 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200222 }
223
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200224}
225
226/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200227/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */