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