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