blob: efc94fbf11ba7b75b6e1b2da45ca1cdcc61afa7b [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.
10 * @license http://www.codeigniter.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @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 Allard39b622d2008-01-16 21:10:09 +000023 * @link http://www.codeigniter.com/user_guide/database/
24 */
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
79 if ($if_not_exists === TRUE)
80 {
81 $sql .= 'IF NOT EXISTS ';
82 }
83
84 $sql .= $this->db->_escape_table($table)." (";
85 $current_field_count = 0;
86
87 foreach ($fields as $field=>$attributes)
88 {
89 // Numeric field names aren't allowed in databases, so if the key is
90 // numeric, we know it was assigned by PHP and the developer manually
91 // entered the field information, so we'll simply add it to the list
92 if (is_numeric($field))
93 {
94 $sql .= "\n\t$attributes";
95 }
96 else
97 {
98 $attributes = array_change_key_case($attributes, CASE_UPPER);
99
100 $sql .= "\n\t".$this->db->_protect_identifiers($field);
101
102 $sql .= ' '.$attributes['TYPE'];
103
104 if (array_key_exists('CONSTRAINT', $attributes))
105 {
106 $sql .= '('.$attributes['CONSTRAINT'].')';
107 }
108
109 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
110 {
111 $sql .= ' UNSIGNED';
112 }
113
114 if (array_key_exists('DEFAULT', $attributes))
115 {
116 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
117 }
118
119 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
120 {
121 $sql .= ' NULL';
122 }
123 else
124 {
125 $sql .= ' NOT NULL';
126 }
127
128 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
129 {
130 $sql .= ' AUTO_INCREMENT';
131 }
132 }
133
134 // don't add a comma on the end of the last field
135 if (++$current_field_count < count($fields))
136 {
137 $sql .= ',';
138 }
139 }
140
141 if (count($primary_keys) > 0)
142 {
143 $primary_keys = $this->db->_protect_identifiers($primary_keys);
144 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
145 }
146
147 if (count($keys) > 0)
148 {
149 $keys = $this->db->_protect_identifiers($keys);
150 $sql .= ",\n\tUNIQUE (" . implode(', ', $keys) . ")";
151 }
152
153 $sql .= "\n)";
154
155 return $sql;
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Drop Table
162 *
163 * Unsupported feature in SQLite
164 *
165 * @access private
166 * @return bool
167 */
168 function _drop_table($table)
169 {
170 if ($this->db->db_debug)
171 {
172 return $this->db->display_error('db_unsuported_feature');
173 }
174 return array();
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Alter table query
181 *
182 * Generates a platform-specific query so that a table can be altered
183 * Called by add_column(), drop_column(), and column_alter(),
184 *
185 * @access private
186 * @param string the ALTER type (ADD, DROP, CHANGE)
187 * @param string the column name
188 * @param string the table name
189 * @param string the column definition
190 * @param string the default value
191 * @param boolean should 'NOT NULL' be added
192 * @param string the field after which we should add the new field
193 * @return object
194 */
195 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
196 {
197 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
198
199 // DROP has everything it needs now.
200 if ($alter_type == 'DROP')
201 {
202 // SQLite does not support dropping columns
203 // http://www.sqlite.org/omitted.html
204 // http://www.sqlite.org/faq.html#q11
205 return FALSE;
206 }
207
208 $sql .= " $column_definition";
209
210 if ($default_value != '')
211 {
212 $sql .= " DEFAULT \"$default_value\"";
213 }
214
215 if ($null === NULL)
216 {
217 $sql .= ' NULL';
218 }
219 else
220 {
221 $sql .= ' NOT NULL';
222 }
223
224 if ($after_field != '')
225 {
226 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
227 }
228
229 return $sql;
230
231 }
232}
233?>