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