blob: a519575f019eed6adfcdc3c51de4e7a967148261 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
Derek Jonesf4a4bd82011-10-20 12:18:42 -05003 * CodeIgniter
Derek Allard2067d1a2008-11-13 22:59:24 +00004 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Andrey Andreevd947eba2012-04-09 14:58:28 +030029 * Database Forge Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
Timothy Warren833d5042012-03-19 16:12:03 -040035abstract class CI_DB_forge {
Derek Allard2067d1a2008-11-13 22:59:24 +000036
Andrey Andreev24276a32012-01-08 02:44:38 +020037 public $fields = array();
38 public $keys = array();
39 public $primary_keys = array();
40 public $db_char_set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000041
Andrey Andreevd947eba2012-04-09 14:58:28 +030042 // Platform specific SQL strings
43 protected $_create_database = 'CREATE DATABASE %s';
44 protected $_drop_database = 'DROP DATABASE %s';
45 protected $_drop_table = 'DROP TABLE IF EXISTS %s';
46 protected $_rename_table = 'ALTER TABLE %s RENAME TO %s';
47
Andrey Andreev24276a32012-01-08 02:44:38 +020048 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
50 // Assign the main database object to $this->db
51 $CI =& get_instance();
52 $this->db =& $CI->db;
Andrey Andreev24276a32012-01-08 02:44:38 +020053 log_message('debug', 'Database Forge Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000054 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Create database
60 *
Derek Allard2067d1a2008-11-13 22:59:24 +000061 * @param string the database name
62 * @return bool
63 */
Andrey Andreev24276a32012-01-08 02:44:38 +020064 public function create_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030066 if ($this->_create_database === FALSE)
67 {
68 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
69 }
70 elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat)))
71 {
72 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
73 }
74
75 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Drop database
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @param string the database name
84 * @return bool
85 */
Andrey Andreev24276a32012-01-08 02:44:38 +020086 public function drop_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030088 if ($db_name == '')
89 {
90 show_error('A table name is required for that operation.');
91 return FALSE;
92 }
93 elseif ($this->_drop_database === FALSE)
94 {
95 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
96 }
97 elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name)))
98 {
99 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
100 }
101
102 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Add Key
109 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 * @param string key
111 * @param string type
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000112 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000114 public function add_key($key = '', $primary = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 if (is_array($key))
117 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500118 foreach ($key as $one)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
120 $this->add_key($one, $primary);
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return;
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if ($key == '')
127 {
128 show_error('Key information is required for that operation.');
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 if ($primary === TRUE)
132 {
133 $this->primary_keys[] = $key;
134 }
135 else
136 {
137 $this->keys[] = $key;
138 }
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000139
140 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Add Field
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @param string collation
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000149 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000151 public function add_field($field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
153 if ($field == '')
154 {
155 show_error('Field information is required.');
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 if (is_string($field))
159 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200160 if ($field === 'id')
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 $this->add_field(array(
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000163 'id' => array(
164 'type' => 'INT',
165 'constraint' => 9,
166 'auto_increment' => TRUE
167 )
168 ));
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 $this->add_key('id', TRUE);
170 }
171 else
172 {
173 if (strpos($field, ' ') === FALSE)
174 {
175 show_error('Field information is required for that operation.');
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 $this->fields[] = $field;
179 }
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 if (is_array($field))
183 {
184 $this->fields = array_merge($this->fields, $field);
185 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200186
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000187 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Create Table
194 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @param string the table name
196 * @return bool
197 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000198 public function create_table($table = '', $if_not_exists = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200199 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 if ($table == '')
201 {
202 show_error('A table name is required for that operation.');
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Andrey Andreev24276a32012-01-08 02:44:38 +0200205 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200206 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 show_error('Field information is required.');
208 }
209
210 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 $this->_reset();
Andrey Andreev24276a32012-01-08 02:44:38 +0200212 return is_bool($sql) ? $sql : $this->db->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Drop Table
219 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @param string the table name
221 * @return bool
222 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000223 public function drop_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreevd947eba2012-04-09 14:58:28 +0300225 if ($table_name == '')
226 {
227 return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE;
228 }
229 elseif ($this->_drop_table === FALSE)
230 {
231 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
232 }
233
234 return $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Rename Table
241 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @param string the old table name
243 * @param string the new table name
244 * @return bool
245 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000246 public function rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 if ($table_name == '' OR $new_table_name == '')
249 {
250 show_error('A table name is required for that operation.');
Andrey Andreevd947eba2012-04-09 14:58:28 +0300251 return FALSE;
252 }
253 elseif ($this->_rename_table === FALSE)
254 {
255 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrey Andreevd947eba2012-04-09 14:58:28 +0300258 return $this->db->query(sprintf($this->_rename_table,
259 $this->db->escape_identifiers($this->db->dbprefix.$table_name),
260 $this->db->escape_identifiers($this->db->dbprefix.$new_table_name))
261 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Column Add
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param string the table name
270 * @param string the column name
271 * @param string the column definition
272 * @return bool
273 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000274 public function add_column($table = '', $field = array(), $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if ($table == '')
277 {
278 show_error('A table name is required for that operation.');
279 }
280
281 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000282 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200283 foreach (array_keys($field) as $k)
Barry Mienydd671972010-10-04 16:33:58 +0200284 {
285 $this->add_field(array($k => $field[$k]));
Robin Sowell8a54ef22009-03-04 14:49:53 +0000286
287 if (count($this->fields) == 0)
Barry Mienydd671972010-10-04 16:33:58 +0200288 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000289 show_error('Field information is required.');
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Robin Sowell8a54ef22009-03-04 14:49:53 +0000292 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000293 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200294
Robin Sowell8a54ef22009-03-04 14:49:53 +0000295 if ($this->db->query($sql) === FALSE)
296 {
297 return FALSE;
298 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Robin Sowell8a54ef22009-03-04 14:49:53 +0000301 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Column Drop
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @param string the table name
310 * @param string the column name
311 * @return bool
312 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000313 public function drop_column($table = '', $column_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 if ($table == '')
316 {
317 show_error('A table name is required for that operation.');
318 }
319
320 if ($column_name == '')
321 {
322 show_error('A column name is required for that operation.');
323 }
324
Andrey Andreev24276a32012-01-08 02:44:38 +0200325 return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Column Modify
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string the table name
334 * @param string the column name
335 * @param string the column definition
336 * @return bool
337 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000338 public function modify_column($table = '', $field = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 if ($table == '')
341 {
342 show_error('A table name is required for that operation.');
343 }
344
345 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000346 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200347 foreach (array_keys($field) as $k)
Robin Sowell8a54ef22009-03-04 14:49:53 +0000348 {
Phil Sturgeona58ecae2010-12-15 10:32:10 +0000349 // If no name provided, use the current name
350 if ( ! isset($field[$k]['name']))
351 {
352 $field[$k]['name'] = $k;
353 }
354
Robin Sowell8a54ef22009-03-04 14:49:53 +0000355 $this->add_field(array($k => $field[$k]));
Andrey Andreev24276a32012-01-08 02:44:38 +0200356 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200357 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000358 show_error('Field information is required.');
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Robin Sowell8a54ef22009-03-04 14:49:53 +0000361 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000362 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200363
Robin Sowell8a54ef22009-03-04 14:49:53 +0000364 if ($this->db->query($sql) === FALSE)
365 {
366 return FALSE;
367 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Robin Sowell8a54ef22009-03-04 14:49:53 +0000370 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * Reset
377 *
378 * Resets table creation vars
379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @return void
381 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000382 protected function _reset()
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200384 $this->fields = $this->keys = $this->primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387}
388
389/* End of file DB_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400390/* Location: ./system/database/DB_forge.php */