1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, 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 we cycle through
262 foreach ($field as $k => $v)
264 $this->add_field(array($k => $field[$k]));
266 if (count($this->fields) == 0)
268 show_error('Field information is required.');
271 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
275 if ($this->db->query($sql) === FALSE)
285 // --------------------------------------------------------------------
291 * @param string the table name
292 * @param string the column name
295 function drop_column($table = '', $column_name = '')
300 show_error('A table name is required for that operation.');
303 if ($column_name == '')
305 show_error('A column name is required for that operation.');
308 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
310 return $this->db->query($sql);
313 // --------------------------------------------------------------------
319 * @param string the table name
320 * @param string the column name
321 * @param string the column definition
324 function modify_column($table = '', $field = array())
328 show_error('A table name is required for that operation.');
331 // add field info into field array, but we can only do one at a time
332 // so we cycle through
334 foreach ($field as $k => $v)
336 // If no name provided, use the current name
337 if ( ! isset($field[$k]['name']))
339 $field[$k]['name'] = $k;
342 $this->add_field(array($k => $field[$k]));
344 if (count($this->fields) == 0)
346 show_error('Field information is required.');
349 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
353 if ($this->db->query($sql) === FALSE)
362 // --------------------------------------------------------------------
367 * Resets table creation vars
374 $this->fields = array();
375 $this->keys = array();
376 $this->primary_keys = array();
381 /* End of file DB_forge.php */
382 /* Location: ./system/database/DB_forge.php */