Take two:
[www-register-wizard.git] / database / drivers / mysqli / mysqli_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  * MySQLi 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_mysqli_forge extends CI_DB_forge {\r
26         \r
27         /**\r
28          * Create database\r
29          *\r
30          * @access      private\r
31          * @param       string  the database name\r
32          * @return      bool\r
33          */\r
34         function _create_database($name)\r
35         {\r
36                 return "CREATE DATABASE ".$name;\r
37         }\r
38 \r
39         // --------------------------------------------------------------------\r
40 \r
41         /**\r
42          * Drop database\r
43          *\r
44          * @access      private\r
45          * @param       string  the database name\r
46          * @return      bool\r
47          */\r
48         function _drop_database($name)\r
49         {\r
50                 return "DROP DATABASE ".$name;\r
51         }\r
52 \r
53         // --------------------------------------------------------------------\r
54 \r
55         /**\r
56          * Process Fields\r
57          *\r
58          * @access      private\r
59          * @param       mixed   the fields\r
60          * @return      string\r
61          */\r
62         function _process_fields($fields)\r
63         {\r
64                 $current_field_count = 0;\r
65                 $sql = '';\r
66 \r
67                 foreach ($fields as $field=>$attributes)\r
68                 {\r
69                         // Numeric field names aren't allowed in databases, so if the key is\r
70                         // numeric, we know it was assigned by PHP and the developer manually\r
71                         // entered the field information, so we'll simply add it to the list\r
72                         if (is_numeric($field))\r
73                         {\r
74                                 $sql .= "\n\t$attributes";\r
75                         }\r
76                         else\r
77                         {\r
78                                 $attributes = array_change_key_case($attributes, CASE_UPPER);\r
79                                 \r
80                                 $sql .= "\n\t".$this->db->_protect_identifiers($field);\r
81 \r
82                                 if (array_key_exists('NAME', $attributes))\r
83                                 {\r
84                                         $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';\r
85                                 }\r
86                                 \r
87                                 if (array_key_exists('TYPE', $attributes))\r
88                                 {\r
89                                         $sql .=  ' '.$attributes['TYPE'];\r
90                                 }\r
91         \r
92                                 if (array_key_exists('CONSTRAINT', $attributes))\r
93                                 {\r
94                                         $sql .= '('.$attributes['CONSTRAINT'].')';\r
95                                 }\r
96         \r
97                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)\r
98                                 {\r
99                                         $sql .= ' UNSIGNED';\r
100                                 }\r
101         \r
102                                 if (array_key_exists('DEFAULT', $attributes))\r
103                                 {\r
104                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';\r
105                                 }\r
106         \r
107                                 if (array_key_exists('NULL', $attributes))\r
108                                 {\r
109                                         $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';\r
110                                 }\r
111         \r
112                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)\r
113                                 {\r
114                                         $sql .= ' AUTO_INCREMENT';\r
115                                 }\r
116                         }\r
117                         \r
118                         // don't add a comma on the end of the last field\r
119                         if (++$current_field_count < count($fields))\r
120                         {\r
121                                 $sql .= ',';\r
122                         }\r
123                 }\r
124                 \r
125                 return $sql;\r
126         }\r
127 \r
128         // --------------------------------------------------------------------\r
129 \r
130         /**\r
131          * Create Table\r
132          *\r
133          * @access      private\r
134          * @param       string  the table name\r
135          * @param       mixed   the fields\r
136          * @param       mixed   primary key(s)\r
137          * @param       mixed   key(s)\r
138          * @param       boolean should 'IF NOT EXISTS' be added to the SQL\r
139          * @return      bool\r
140          */\r
141         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)\r
142         {\r
143                 $sql = 'CREATE TABLE ';\r
144                 \r
145                 if ($if_not_exists === TRUE)\r
146                 {\r
147                         $sql .= 'IF NOT EXISTS ';\r
148                 }\r
149                 \r
150                 $sql .= $this->db->_escape_identifiers($table)." (";\r
151 \r
152                 $sql .= $this->_process_fields($fields);\r
153 \r
154                 if (count($primary_keys) > 0)\r
155                 {\r
156                         $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));\r
157                         $primary_keys = $this->db->_protect_identifiers($primary_keys);\r
158                         $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";\r
159                 }\r
160 \r
161                 if (is_array($keys) && count($keys) > 0)\r
162                 {\r
163                         foreach ($keys as $key)\r
164                         {\r
165                                 if (is_array($key))\r
166                                 {\r
167                                         $key_name = $this->db->_protect_identifiers(implode('_', $key));\r
168                                         $key = $this->db->_protect_identifiers($key);   \r
169                                 }\r
170                                 else\r
171                                 {\r
172                                         $key_name = $this->db->_protect_identifiers($key);\r
173                                         $key = array($key_name);\r
174                                 }\r
175                                 \r
176                                 $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";\r
177                         }\r
178                 }\r
179 \r
180                 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";\r
181 \r
182                 return $sql;\r
183         }\r
184 \r
185         // --------------------------------------------------------------------\r
186 \r
187         /**\r
188          * Drop Table\r
189          *\r
190          * @access      private\r
191          * @return      string\r
192          */\r
193         function _drop_table($table)\r
194         {\r
195                 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);\r
196         }\r
197 \r
198         // --------------------------------------------------------------------\r
199 \r
200         /**\r
201          * Alter table query\r
202          *\r
203          * Generates a platform-specific query so that a table can be altered\r
204          * Called by add_column(), drop_column(), and column_alter(),\r
205          *\r
206          * @access      private\r
207          * @param       string  the ALTER type (ADD, DROP, CHANGE)\r
208          * @param       string  the column name\r
209          * @param       array   fields\r
210          * @param       string  the field after which we should add the new field\r
211          * @return      object\r
212          */\r
213         function _alter_table($alter_type, $table, $fields, $after_field = '')\r
214         {\r
215                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";\r
216 \r
217                 // DROP has everything it needs now.\r
218                 if ($alter_type == 'DROP')\r
219                 {\r
220                         return $sql.$this->db->_protect_identifiers($fields);\r
221                 }\r
222 \r
223                 $sql .= $this->_process_fields($fields);\r
224 \r
225                 if ($after_field != '')\r
226                 {\r
227                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);\r
228                 }\r
229                 \r
230                 return $sql;\r
231         }\r
232 \r
233         // --------------------------------------------------------------------\r
234 \r
235         /**\r
236          * Rename a table\r
237          *\r
238          * Generates a platform-specific query so that a table can be renamed\r
239          *\r
240          * @access      private\r
241          * @param       string  the old table name\r
242          * @param       string  the new table name\r
243          * @return      string\r
244          */\r
245         function _rename_table($table_name, $new_table_name)\r
246         {\r
247                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);\r
248                 return $sql;\r
249         }\r
250 \r
251 }\r
252 \r
253 /* End of file mysqli_forge.php */\r
254 /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */