1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Database Utility Class
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
27 var $fields = array();
29 var $primary_keys = array();
30 var $db_char_set = '';
35 * Grabs the CI super object instance so we can access it.
38 function CI_DB_forge()
40 // Assign the main database object to $this->db
41 $CI =& get_instance();
43 log_message('debug', "Database Forge Class Initialized");
46 // --------------------------------------------------------------------
52 * @param string the database name
55 function create_database($db_name)
57 $sql = $this->_create_database($db_name);
64 return $this->db->query($sql);
67 // --------------------------------------------------------------------
73 * @param string the database name
76 function drop_database($db_name)
78 $sql = $this->_drop_database($db_name);
85 return $this->db->query($sql);
88 // --------------------------------------------------------------------
98 function add_key($key = '', $primary = FALSE)
102 foreach($key as $one)
104 $this->add_key($one, $primary);
112 show_error('Key information is required for that operation.');
115 if ($primary === TRUE)
117 $this->primary_keys[] = $key;
121 $this->keys[] = $key;
125 // --------------------------------------------------------------------
131 * @param string collation
134 function add_field($field = '')
138 show_error('Field information is required.');
141 if (is_string($field))
145 $this->add_field(array(
149 'auto_increment' => TRUE
152 $this->add_key('id', TRUE);
156 if (strpos($field, ' ') === FALSE)
158 show_error('Field information is required for that operation.');
161 $this->fields[] = $field;
165 if (is_array($field))
167 $this->fields = array_merge($this->fields, $field);
172 // --------------------------------------------------------------------
178 * @param string the table name
181 function create_table($table = '', $if_not_exists = FALSE)
185 show_error('A table name is required for that operation.');
188 if (count($this->fields) == 0)
190 show_error('Field information is required.');
193 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
196 return $this->db->query($sql);
199 // --------------------------------------------------------------------
205 * @param string the table name
208 function drop_table($table_name)
210 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
217 return $this->db->query($sql);
220 // --------------------------------------------------------------------
226 * @param string the old table name
227 * @param string the new table name
230 function rename_table($table_name, $new_table_name)
232 if ($table_name == '' OR $new_table_name == '')
234 show_error('A table name is required for that operation.');
237 $sql = $this->_rename_table($table_name, $new_table_name);
238 return $this->db->query($sql);
241 // --------------------------------------------------------------------
247 * @param string the table name
248 * @param string the column name
249 * @param string the column definition
252 function add_column($table = '', $field = array(), $after_field = '')
256 show_error('A table name is required for that operation.');
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));
263 if (count($this->fields) == 0)
265 show_error('Field information is required.');
268 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
271 return $this->db->query($sql);
274 // --------------------------------------------------------------------
280 * @param string the table name
281 * @param string the column name
284 function drop_column($table = '', $column_name = '')
289 show_error('A table name is required for that operation.');
292 if ($column_name == '')
294 show_error('A column name is required for that operation.');
297 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
299 return $this->db->query($sql);
302 // --------------------------------------------------------------------
308 * @param string the table name
309 * @param string the column name
310 * @param string the column definition
313 function modify_column($table = '', $field = array())
317 show_error('A table name is required for that operation.');
320 // add field info into field array, but we can only do one at a time
321 // so only grab the first field in the event there are more then one
322 $this->add_field(array_slice($field, 0, 1));
324 if (count($this->fields) == 0)
326 show_error('Field information is required.');
329 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
332 return $this->db->query($sql);
335 // --------------------------------------------------------------------
340 * Resets table creation vars
347 $this->fields = array();
348 $this->keys = array();
349 $this->primary_keys = array();
354 /* End of file DB_forge.php */
355 /* Location: ./system/database/DB_forge.php */