blob: 595d4196884098784bcfa68a853506bcfc077fcc [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 *
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
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
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
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 */
Timothy Warren1ed59952012-03-19 18:25:43 -040046 public function _create_database()
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000058 * @param string the database name
59 * @return bool
60 */
Timothy Warren1ed59952012-03-19 18:25:43 -040061 protected function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000062 {
63 if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
64 {
65 if ($this->db->db_debug)
66 {
67 return $this->db->display_error('db_unable_to_drop');
68 }
69 return FALSE;
70 }
71 return TRUE;
72 }
73 // --------------------------------------------------------------------
74
75 /**
76 * Create Table
77 *
Derek Allard2067d1a2008-11-13 22:59:24 +000078 * @param string the table name
79 * @param array the fields
80 * @param mixed primary key(s)
81 * @param mixed key(s)
82 * @param boolean should 'IF NOT EXISTS' be added to the SQL
83 * @return bool
84 */
Timothy Warren1ed59952012-03-19 18:25:43 -040085 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // IF NOT EXISTS added to SQLite in 3.3.0
Andrey Andreev08856b82012-03-03 03:19:28 +020090 if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 $sql .= 'IF NOT EXISTS ';
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 $sql .= $this->db->_escape_identifiers($table)."(";
96 $current_field_count = 0;
97
98 foreach ($fields as $field=>$attributes)
99 {
100 // Numeric field names aren't allowed in databases, so if the key is
101 // numeric, we know it was assigned by PHP and the developer manually
102 // entered the field information, so we'll simply add it to the list
103 if (is_numeric($field))
104 {
105 $sql .= "\n\t$attributes";
106 }
107 else
108 {
109 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200110
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200111 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Jones37f4b9c2011-07-01 17:56:50 -0500113 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if (array_key_exists('CONSTRAINT', $attributes))
116 {
117 $sql .= '('.$attributes['CONSTRAINT'].')';
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
121 {
122 $sql .= ' UNSIGNED';
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if (array_key_exists('DEFAULT', $attributes))
126 {
127 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
131 {
132 $sql .= ' NULL';
133 }
134 else
135 {
Barry Mienydd671972010-10-04 16:33:58 +0200136 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
140 {
141 $sql .= ' AUTO_INCREMENT';
142 }
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // don't add a comma on the end of the last field
146 if (++$current_field_count < count($fields))
147 {
148 $sql .= ',';
149 }
150 }
151
152 if (count($primary_keys) > 0)
153 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200154 $primary_keys = $this->db->protect_identifiers($primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
156 }
157
158 if (is_array($keys) && count($keys) > 0)
159 {
160 foreach ($keys as $key)
161 {
162 if (is_array($key))
163 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200164 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166 else
167 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200168 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
172 }
173 }
174
175 $sql .= "\n)";
176
177 return $sql;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Drop Table
184 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500185 * Unsupported feature in SQLite
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return bool
188 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400189 protected function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 if ($this->db->db_debug)
192 {
193 return $this->db->display_error('db_unsuported_feature');
194 }
195 return array();
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Alter table query
202 *
203 * Generates a platform-specific query so that a table can be altered
204 * Called by add_column(), drop_column(), and column_alter(),
205 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @param string the ALTER type (ADD, DROP, CHANGE)
207 * @param string the column name
208 * @param string the table name
209 * @param string the column definition
210 * @param string the default value
211 * @param boolean should 'NOT NULL' be added
212 * @param string the field after which we should add the new field
213 * @return object
214 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400215 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200217 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
219 // DROP has everything it needs now.
220 if ($alter_type == 'DROP')
221 {
222 // SQLite does not support dropping columns
223 // http://www.sqlite.org/omitted.html
224 // http://www.sqlite.org/faq.html#q11
225 return FALSE;
226 }
227
228 $sql .= " $column_definition";
229
230 if ($default_value != '')
231 {
232 $sql .= " DEFAULT \"$default_value\"";
233 }
234
235 if ($null === NULL)
236 {
237 $sql .= ' NULL';
238 }
239 else
240 {
241 $sql .= ' NOT NULL';
242 }
243
244 if ($after_field != '')
245 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200246 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Rename a table
257 *
258 * Generates a platform-specific query so that a table can be renamed
259 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 * @param string the old table name
261 * @param string the new table name
262 * @return string
263 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400264 protected function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200266 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 +0000267 }
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269}
270
271/* End of file sqlite_forge.php */
Timothy Warren1ed59952012-03-19 18:25:43 -0400272/* Location: ./system/database/drivers/sqlite/sqlite_forge.php */