blob: 1fd2a2bd42846a6f979ae5dee15cba18a93e9370 [file] [log] [blame]
Derek Allard39b622d2008-01-16 21:10:09 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allard39b622d2008-01-16 21:10:09 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard39b622d2008-01-16 21:10:09 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * SQLite Forge Class
20 *
21 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000023 * @link http://codeigniter.com/user_guide/database/
Derek Allard39b622d2008-01-16 21:10:09 +000024 */
25class CI_DB_sqlite_forge extends CI_DB_forge {
26
27 /**
28 * Create database
29 *
30 * @access public
31 * @param string the database name
32 * @return bool
33 */
34 function _create_database()
35 {
36 // In SQLite, a database is created when you connect to the database.
37 // We'll return TRUE so that an error isn't generated
38 return TRUE;
39 }
40
41 // --------------------------------------------------------------------
42
43 /**
44 * Drop database
45 *
46 * @access private
47 * @param string the database name
48 * @return bool
49 */
50 function _drop_database($name)
51 {
52 if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
53 {
54 if ($this->db->db_debug)
55 {
56 return $this->db->display_error('db_unable_to_drop');
57 }
58 return FALSE;
59 }
60 return TRUE;
61 }
62 // --------------------------------------------------------------------
63
64 /**
65 * Create Table
66 *
67 * @access private
68 * @param string the table name
69 * @param array the fields
70 * @param mixed primary key(s)
71 * @param mixed key(s)
72 * @param boolean should 'IF NOT EXISTS' be added to the SQL
73 * @return bool
74 */
75 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
76 {
77 $sql = 'CREATE TABLE ';
78
Derek Jones5b4d5322008-02-13 04:34:10 +000079 // IF NOT EXISTS added to SQLite in 3.3.0
80 if ($if_not_exists === TRUE && version_compare($this->_version(), '3.3.0', '>=') === TRUE)
Derek Allard39b622d2008-01-16 21:10:09 +000081 {
82 $sql .= 'IF NOT EXISTS ';
83 }
84
Derek Jones5b4d5322008-02-13 04:34:10 +000085 $sql .= $this->db->_escape_table($table)."(";
Derek Allard39b622d2008-01-16 21:10:09 +000086 $current_field_count = 0;
87
88 foreach ($fields as $field=>$attributes)
89 {
90 // Numeric field names aren't allowed in databases, so if the key is
91 // numeric, we know it was assigned by PHP and the developer manually
92 // entered the field information, so we'll simply add it to the list
93 if (is_numeric($field))
94 {
95 $sql .= "\n\t$attributes";
96 }
97 else
98 {
99 $attributes = array_change_key_case($attributes, CASE_UPPER);
100
101 $sql .= "\n\t".$this->db->_protect_identifiers($field);
102
103 $sql .= ' '.$attributes['TYPE'];
104
105 if (array_key_exists('CONSTRAINT', $attributes))
106 {
107 $sql .= '('.$attributes['CONSTRAINT'].')';
108 }
109
110 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
111 {
112 $sql .= ' UNSIGNED';
113 }
114
115 if (array_key_exists('DEFAULT', $attributes))
116 {
117 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
118 }
119
120 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
121 {
122 $sql .= ' NULL';
123 }
124 else
125 {
126 $sql .= ' NOT NULL';
127 }
128
129 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
130 {
131 $sql .= ' AUTO_INCREMENT';
132 }
133 }
134
135 // don't add a comma on the end of the last field
136 if (++$current_field_count < count($fields))
137 {
138 $sql .= ',';
139 }
140 }
141
142 if (count($primary_keys) > 0)
143 {
144 $primary_keys = $this->db->_protect_identifiers($primary_keys);
145 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
146 }
147
148 if (count($keys) > 0)
149 {
150 $keys = $this->db->_protect_identifiers($keys);
151 $sql .= ",\n\tUNIQUE (" . implode(', ', $keys) . ")";
152 }
153
154 $sql .= "\n)";
155
156 return $sql;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Drop Table
163 *
164 * Unsupported feature in SQLite
165 *
166 * @access private
167 * @return bool
168 */
169 function _drop_table($table)
170 {
171 if ($this->db->db_debug)
172 {
173 return $this->db->display_error('db_unsuported_feature');
174 }
175 return array();
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Alter table query
182 *
183 * Generates a platform-specific query so that a table can be altered
184 * Called by add_column(), drop_column(), and column_alter(),
185 *
186 * @access private
187 * @param string the ALTER type (ADD, DROP, CHANGE)
188 * @param string the column name
189 * @param string the table name
190 * @param string the column definition
191 * @param string the default value
192 * @param boolean should 'NOT NULL' be added
193 * @param string the field after which we should add the new field
194 * @return object
195 */
196 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
197 {
198 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
199
200 // DROP has everything it needs now.
201 if ($alter_type == 'DROP')
202 {
203 // SQLite does not support dropping columns
204 // http://www.sqlite.org/omitted.html
205 // http://www.sqlite.org/faq.html#q11
206 return FALSE;
207 }
208
209 $sql .= " $column_definition";
210
211 if ($default_value != '')
212 {
213 $sql .= " DEFAULT \"$default_value\"";
214 }
215
216 if ($null === NULL)
217 {
218 $sql .= ' NULL';
219 }
220 else
221 {
222 $sql .= ' NOT NULL';
223 }
224
225 if ($after_field != '')
226 {
227 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
228 }
229
230 return $sql;
231
232 }
233}
234?>