blob: 96a5e0edb61c73a3d5876f4d96afdaf4dbadee4d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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
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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * SQLite Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_sqlite_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
42 * @access public
43 * @param string the database name
44 * @return bool
45 */
46 function _create_database()
47 {
48 // In SQLite, a database is created when you connect to the database.
49 // We'll return TRUE so that an error isn't generated
50 return TRUE;
51 }
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Drop database
57 *
58 * @access private
59 * @param string the database name
60 * @return bool
61 */
62 function _drop_database($name)
63 {
64 if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
65 {
66 if ($this->db->db_debug)
67 {
68 return $this->db->display_error('db_unable_to_drop');
69 }
70 return FALSE;
71 }
72 return TRUE;
73 }
74 // --------------------------------------------------------------------
75
76 /**
77 * Create Table
78 *
79 * @access private
80 * @param string the table name
81 * @param array the fields
82 * @param mixed primary key(s)
83 * @param mixed key(s)
84 * @param boolean should 'IF NOT EXISTS' be added to the SQL
85 * @return bool
86 */
87 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
88 {
89 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // IF NOT EXISTS added to SQLite in 3.3.0
Derek Jones4bde1102010-03-30 08:56:53 -050092 if ($if_not_exists === TRUE && version_compare($this->db->_version(), '3.3.0', '>=') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 $sql .= 'IF NOT EXISTS ';
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 $sql .= $this->db->_escape_identifiers($table)."(";
98 $current_field_count = 0;
99
100 foreach ($fields as $field=>$attributes)
101 {
102 // Numeric field names aren't allowed in databases, so if the key is
103 // numeric, we know it was assigned by PHP and the developer manually
104 // entered the field information, so we'll simply add it to the list
105 if (is_numeric($field))
106 {
107 $sql .= "\n\t$attributes";
108 }
109 else
110 {
111 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Jones37f4b9c2011-07-01 17:56:50 -0500115 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 if (array_key_exists('CONSTRAINT', $attributes))
118 {
119 $sql .= '('.$attributes['CONSTRAINT'].')';
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
123 {
124 $sql .= ' UNSIGNED';
125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if (array_key_exists('DEFAULT', $attributes))
128 {
129 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
133 {
134 $sql .= ' NULL';
135 }
136 else
137 {
Barry Mienydd671972010-10-04 16:33:58 +0200138 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
142 {
143 $sql .= ' AUTO_INCREMENT';
144 }
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // don't add a comma on the end of the last field
148 if (++$current_field_count < count($fields))
149 {
150 $sql .= ',';
151 }
152 }
153
154 if (count($primary_keys) > 0)
155 {
156 $primary_keys = $this->db->_protect_identifiers($primary_keys);
157 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
158 }
159
160 if (is_array($keys) && count($keys) > 0)
161 {
162 foreach ($keys as $key)
163 {
164 if (is_array($key))
165 {
Barry Mienydd671972010-10-04 16:33:58 +0200166 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168 else
169 {
170 $key = array($this->db->_protect_identifiers($key));
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
174 }
175 }
176
177 $sql .= "\n)";
178
179 return $sql;
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Drop Table
186 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500187 * Unsupported feature in SQLite
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 *
189 * @access private
190 * @return bool
191 */
192 function _drop_table($table)
193 {
194 if ($this->db->db_debug)
195 {
196 return $this->db->display_error('db_unsuported_feature');
197 }
198 return array();
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Alter table query
205 *
206 * Generates a platform-specific query so that a table can be altered
207 * Called by add_column(), drop_column(), and column_alter(),
208 *
209 * @access private
210 * @param string the ALTER type (ADD, DROP, CHANGE)
211 * @param string the column name
212 * @param string the table name
213 * @param string the column definition
214 * @param string the default value
215 * @param boolean should 'NOT NULL' be added
216 * @param string the field after which we should add the new field
217 * @return object
218 */
219 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
220 {
221 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
222
223 // DROP has everything it needs now.
224 if ($alter_type == 'DROP')
225 {
226 // SQLite does not support dropping columns
227 // http://www.sqlite.org/omitted.html
228 // http://www.sqlite.org/faq.html#q11
229 return FALSE;
230 }
231
232 $sql .= " $column_definition";
233
234 if ($default_value != '')
235 {
236 $sql .= " DEFAULT \"$default_value\"";
237 }
238
239 if ($null === NULL)
240 {
241 $sql .= ' NULL';
242 }
243 else
244 {
245 $sql .= ' NOT NULL';
246 }
247
248 if ($after_field != '')
249 {
250 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Rename a table
261 *
262 * Generates a platform-specific query so that a table can be renamed
263 *
264 * @access private
265 * @param string the old table name
266 * @param string the new table name
267 * @return string
268 */
269 function _rename_table($table_name, $new_table_name)
270 {
271 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
272 return $sql;
273 }
274}
275
276/* End of file sqlite_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000277/* Location: ./system/database/drivers/sqlite/sqlite_forge.php */