Take two:
[www-register-wizard.git] / database / DB_forge.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * Code Igniter\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  * Database Utility 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_forge {\r
26 \r
27         var $fields                     = array();\r
28         var $keys                       = array();\r
29         var $primary_keys       = array();\r
30         var $db_char_set        =       '';\r
31 \r
32         /**\r
33          * Constructor\r
34          *\r
35          * Grabs the CI super object instance so we can access it.\r
36          *\r
37          */     \r
38         function CI_DB_forge()\r
39         {\r
40                 // Assign the main database object to $this->db\r
41                 $CI =& get_instance();\r
42                 $this->db =& $CI->db;\r
43                 log_message('debug', "Database Forge Class Initialized");\r
44         }\r
45 \r
46         // --------------------------------------------------------------------\r
47 \r
48         /**\r
49          * Create database\r
50          *\r
51          * @access      public\r
52          * @param       string  the database name\r
53          * @return      bool\r
54          */\r
55         function create_database($db_name)\r
56         {\r
57                 $sql = $this->_create_database($db_name);\r
58                 \r
59                 if (is_bool($sql))\r
60                 {\r
61                         return $sql;\r
62                 }\r
63         \r
64                 return $this->db->query($sql);\r
65         }\r
66 \r
67         // --------------------------------------------------------------------\r
68 \r
69         /**\r
70          * Drop database\r
71          *\r
72          * @access      public\r
73          * @param       string  the database name\r
74          * @return      bool\r
75          */\r
76         function drop_database($db_name)\r
77         {\r
78                 $sql = $this->_drop_database($db_name);\r
79                 \r
80                 if (is_bool($sql))\r
81                 {\r
82                         return $sql;\r
83                 }\r
84         \r
85                 return $this->db->query($sql);\r
86         }\r
87 \r
88         // --------------------------------------------------------------------\r
89 \r
90         /**\r
91          * Add Key\r
92          *\r
93          * @access      public\r
94          * @param       string  key\r
95          * @param       string  type\r
96          * @return      void\r
97          */\r
98         function add_key($key = '', $primary = FALSE)\r
99         {\r
100                 if (is_array($key))\r
101                 {\r
102                         foreach($key as $one)\r
103                         {\r
104                                 $this->add_key($one, $primary);\r
105                         }\r
106                         \r
107                         return;\r
108                 }\r
109         \r
110                 if ($key == '')\r
111                 {\r
112                         show_error('Key information is required for that operation.');\r
113                 }\r
114                 \r
115                 if ($primary === TRUE)\r
116                 {\r
117                         $this->primary_keys[] = $key;\r
118                 }\r
119                 else\r
120                 {\r
121                         $this->keys[] = $key;\r
122                 }\r
123         }\r
124 \r
125         // --------------------------------------------------------------------\r
126 \r
127         /**\r
128          * Add Field\r
129          *\r
130          * @access      public\r
131          * @param       string  collation\r
132          * @return      void\r
133          */\r
134         function add_field($field = '')\r
135         {\r
136                 if ($field == '')\r
137                 {\r
138                         show_error('Field information is required.');\r
139                 }\r
140                 \r
141                 if (is_string($field))\r
142                 {\r
143                         if ($field == 'id')\r
144                         {\r
145                                 $this->add_field(array(\r
146                                                                                 'id' => array(\r
147                                                                                                         'type' => 'INT',\r
148                                                                                                         'constraint' => 9,\r
149                                                                                                         'auto_increment' => TRUE\r
150                                                                                                         )\r
151                                                                 ));\r
152                                 $this->add_key('id', TRUE);\r
153                         }\r
154                         else\r
155                         {\r
156                                 if (strpos($field, ' ') === FALSE)\r
157                                 {\r
158                                         show_error('Field information is required for that operation.');\r
159                                 }\r
160                                 \r
161                                 $this->fields[] = $field;\r
162                         }\r
163                 }\r
164                 \r
165                 if (is_array($field))\r
166                 {\r
167                         $this->fields = array_merge($this->fields, $field);\r
168                 }\r
169                 \r
170         }\r
171 \r
172         // --------------------------------------------------------------------\r
173 \r
174         /**\r
175          * Create Table\r
176          *\r
177          * @access      public\r
178          * @param       string  the table name\r
179          * @return      bool\r
180          */\r
181         function create_table($table = '', $if_not_exists = FALSE)\r
182         {       \r
183                 if ($table == '')\r
184                 {\r
185                         show_error('A table name is required for that operation.');\r
186                 }\r
187                         \r
188                 if (count($this->fields) == 0)\r
189                 {       \r
190                         show_error('Field information is required.');\r
191                 }\r
192 \r
193                 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);\r
194                 \r
195                 $this->_reset();\r
196                 return $this->db->query($sql);\r
197         }\r
198 \r
199         // --------------------------------------------------------------------\r
200 \r
201         /**\r
202          * Drop Table\r
203          *\r
204          * @access      public\r
205          * @param       string  the table name\r
206          * @return      bool\r
207          */\r
208         function drop_table($table_name)\r
209         {\r
210                 $sql = $this->_drop_table($this->db->dbprefix.$table_name);\r
211                 \r
212                 if (is_bool($sql))\r
213                 {\r
214                         return $sql;\r
215                 }\r
216         \r
217                 return $this->db->query($sql);\r
218         }\r
219 \r
220         // --------------------------------------------------------------------\r
221 \r
222         /**\r
223          * Rename Table\r
224          *\r
225          * @access      public\r
226          * @param       string  the old table name\r
227          * @param       string  the new table name\r
228          * @return      bool\r
229          */\r
230         function rename_table($table_name, $new_table_name)\r
231         {\r
232                 if ($table_name == '' OR $new_table_name == '')\r
233                 {\r
234                         show_error('A table name is required for that operation.');\r
235                 }\r
236                         \r
237                 $sql = $this->_rename_table($table_name, $new_table_name);\r
238                 return $this->db->query($sql);\r
239         }\r
240 \r
241         // --------------------------------------------------------------------\r
242 \r
243         /**\r
244          * Column Add\r
245          *\r
246          * @access      public\r
247          * @param       string  the table name\r
248          * @param       string  the column name\r
249          * @param       string  the column definition\r
250          * @return      bool\r
251          */\r
252         function add_column($table = '', $field = array(), $after_field = '')\r
253         {\r
254                 if ($table == '')\r
255                 {\r
256                         show_error('A table name is required for that operation.');\r
257                 }\r
258 \r
259                 // add field info into field array, but we can only do one at a time\r
260                 // so only grab the first field in the event there are more then one\r
261                 $this->add_field(array_slice($field, 0, 1));\r
262 \r
263                 if (count($this->fields) == 0)\r
264                 {       \r
265                         show_error('Field information is required.');\r
266                 }\r
267 \r
268                 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);\r
269 \r
270                 $this->_reset();\r
271                 return $this->db->query($sql);\r
272         }\r
273 \r
274         // --------------------------------------------------------------------\r
275 \r
276         /**\r
277          * Column Drop\r
278          *\r
279          * @access      public\r
280          * @param       string  the table name\r
281          * @param       string  the column name\r
282          * @return      bool\r
283          */\r
284         function drop_column($table = '', $column_name = '')\r
285         {\r
286         \r
287                 if ($table == '')\r
288                 {\r
289                         show_error('A table name is required for that operation.');\r
290                 }\r
291 \r
292                 if ($column_name == '')\r
293                 {\r
294                         show_error('A column name is required for that operation.');\r
295                 }\r
296 \r
297                 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);\r
298         \r
299                 return $this->db->query($sql);\r
300         }\r
301 \r
302         // --------------------------------------------------------------------\r
303 \r
304         /**\r
305          * Column Modify\r
306          *\r
307          * @access      public\r
308          * @param       string  the table name\r
309          * @param       string  the column name\r
310          * @param       string  the column definition\r
311          * @return      bool\r
312          */\r
313         function modify_column($table = '', $field = array())\r
314         {\r
315                 if ($table == '')\r
316                 {\r
317                         show_error('A table name is required for that operation.');\r
318                 }\r
319 \r
320                 // add field info into field array, but we can only do one at a time\r
321                 // so only grab the first field in the event there are more then one\r
322                 $this->add_field(array_slice($field, 0, 1));\r
323 \r
324                 if (count($this->fields) == 0)\r
325                 {       \r
326                         show_error('Field information is required.');\r
327                 }\r
328 \r
329                 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);\r
330 \r
331                 $this->_reset();\r
332                 return $this->db->query($sql);\r
333         }\r
334 \r
335         // --------------------------------------------------------------------\r
336 \r
337         /**\r
338          * Reset\r
339          *\r
340          * Resets table creation vars\r
341          *\r
342          * @access      private\r
343          * @return      void\r
344          */\r
345         function _reset()\r
346         {\r
347                 $this->fields           = array();\r
348                 $this->keys                     = array();\r
349                 $this->primary_keys     = array();\r
350         }\r
351 \r
352 }\r
353 \r
354 /* End of file DB_forge.php */\r
355 /* Location: ./system/database/DB_forge.php */