blob: 068a556ed15e265b1402a73509a091a41fc1f660 [file] [log] [blame]
Andrey Andreev8480f7c2012-03-20 16:55:05 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev8480f7c2012-03-20 16:55:05 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev8480f7c2012-03-20 16:55:05 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * SQLite Forge Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_sqlite_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @param string the database name
41 * @return bool
42 */
Andrey Andreev8480f7c2012-03-20 16:55:05 +020043 public function _create_database()
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
45 // In SQLite, a database is created when you connect to the database.
46 // We'll return TRUE so that an error isn't generated
47 return TRUE;
48 }
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Drop database
54 *
Derek Allard2067d1a2008-11-13 22:59:24 +000055 * @param string the database name
56 * @return bool
57 */
Andrey Andreev8480f7c2012-03-20 16:55:05 +020058 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
60 if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
61 {
62 if ($this->db->db_debug)
63 {
64 return $this->db->display_error('db_unable_to_drop');
65 }
66 return FALSE;
67 }
68 return TRUE;
69 }
Andrey Andreev8480f7c2012-03-20 16:55:05 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // --------------------------------------------------------------------
72
73 /**
74 * Create Table
75 *
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @param string the table name
77 * @param array the fields
78 * @param mixed primary key(s)
79 * @param mixed key(s)
Andrey Andreev8480f7c2012-03-20 16:55:05 +020080 * @param bool should 'IF NOT EXISTS' be added to the SQL
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @return bool
82 */
Andrey Andreev8480f7c2012-03-20 16:55:05 +020083 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
85 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // IF NOT EXISTS added to SQLite in 3.3.0
Andrey Andreev08856b82012-03-03 03:19:28 +020088 if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 $sql .= 'IF NOT EXISTS ';
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 $sql .= $this->db->_escape_identifiers($table)."(";
94 $current_field_count = 0;
95
96 foreach ($fields as $field=>$attributes)
97 {
98 // Numeric field names aren't allowed in databases, so if the key is
99 // numeric, we know it was assigned by PHP and the developer manually
100 // entered the field information, so we'll simply add it to the list
101 if (is_numeric($field))
102 {
103 $sql .= "\n\t$attributes";
104 }
105 else
106 {
107 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200108
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200109 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Jones37f4b9c2011-07-01 17:56:50 -0500111 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 if (array_key_exists('CONSTRAINT', $attributes))
114 {
115 $sql .= '('.$attributes['CONSTRAINT'].')';
116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
119 {
120 $sql .= ' UNSIGNED';
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 if (array_key_exists('DEFAULT', $attributes))
124 {
125 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
129 {
130 $sql .= ' NULL';
131 }
132 else
133 {
Barry Mienydd671972010-10-04 16:33:58 +0200134 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
138 {
139 $sql .= ' AUTO_INCREMENT';
140 }
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // don't add a comma on the end of the last field
144 if (++$current_field_count < count($fields))
145 {
146 $sql .= ',';
147 }
148 }
149
150 if (count($primary_keys) > 0)
151 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200152 $primary_keys = $this->db->protect_identifiers($primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
154 }
155
156 if (is_array($keys) && count($keys) > 0)
157 {
158 foreach ($keys as $key)
159 {
160 if (is_array($key))
161 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200162 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164 else
165 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200166 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
170 }
171 }
172
173 $sql .= "\n)";
174
175 return $sql;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Drop Table
182 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * @return bool
184 */
Andrey Andreev8480f7c2012-03-20 16:55:05 +0200185 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
187 if ($this->db->db_debug)
188 {
189 return $this->db->display_error('db_unsuported_feature');
190 }
191 return array();
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Alter table query
198 *
199 * Generates a platform-specific query so that a table can be altered
200 * Called by add_column(), drop_column(), and column_alter(),
201 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * @param string the ALTER type (ADD, DROP, CHANGE)
203 * @param string the column name
204 * @param string the table name
205 * @param string the column definition
206 * @param string the default value
Andrey Andreev8480f7c2012-03-20 16:55:05 +0200207 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * @param string the field after which we should add the new field
Andrey Andreev8480f7c2012-03-20 16:55:05 +0200209 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 */
Andrey Andreev8480f7c2012-03-20 16:55:05 +0200211 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200213 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
215 // DROP has everything it needs now.
216 if ($alter_type == 'DROP')
217 {
218 // SQLite does not support dropping columns
219 // http://www.sqlite.org/omitted.html
220 // http://www.sqlite.org/faq.html#q11
221 return FALSE;
222 }
223
224 $sql .= " $column_definition";
225
226 if ($default_value != '')
227 {
228 $sql .= " DEFAULT \"$default_value\"";
229 }
230
231 if ($null === NULL)
232 {
233 $sql .= ' NULL';
234 }
235 else
236 {
237 $sql .= ' NOT NULL';
238 }
239
240 if ($after_field != '')
241 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200242 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Rename a table
253 *
254 * Generates a platform-specific query so that a table can be renamed
255 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @param string the old table name
257 * @param string the new table name
258 * @return string
259 */
Andrey Andreev8480f7c2012-03-20 16:55:05 +0200260 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200262 return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265}
266
267/* End of file sqlite_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400268/* Location: ./system/database/drivers/sqlite/sqlite_forge.php */