blob: 9b7639289c00391ee11d864d08a00d0016bd54cc [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
Andrey Andreev5d281762012-06-11 22:05:40 +030075 if ( ! empty($this->db->data_cache['db_names']))
76 {
77 $this->db->data_cache['db_names'][] = $db_name;
78 }
79
Andrey Andreevd947eba2012-04-09 14:58:28 +030080 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Drop database
87 *
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * @param string the database name
89 * @return bool
90 */
Andrey Andreev24276a32012-01-08 02:44:38 +020091 public function drop_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010093 if ($db_name === '')
Andrey Andreevd947eba2012-04-09 14:58:28 +030094 {
95 show_error('A table name is required for that operation.');
96 return FALSE;
97 }
98 elseif ($this->_drop_database === FALSE)
99 {
100 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
101 }
102 elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name)))
103 {
104 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
105 }
106
Andrey Andreev5d281762012-06-11 22:05:40 +0300107 if ( ! empty($this->db->data_cache['db_names']))
108 {
109 $key = array_search(strtolower($db_name), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
110 if ($key !== FALSE)
111 {
112 unset($this->db->data_cache['db_names'][$key]);
113 }
114 }
115
Andrey Andreevd947eba2012-04-09 14:58:28 +0300116 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Add Key
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param string key
125 * @param string type
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000126 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000128 public function add_key($key = '', $primary = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 if (is_array($key))
131 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500132 foreach ($key as $one)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 $this->add_key($one, $primary);
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 return;
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100140 if ($key === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
142 show_error('Key information is required for that operation.');
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 if ($primary === TRUE)
146 {
147 $this->primary_keys[] = $key;
148 }
149 else
150 {
151 $this->keys[] = $key;
152 }
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000153
154 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Add Field
161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @param string collation
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000163 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000165 public function add_field($field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100167 if ($field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 show_error('Field information is required.');
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 if (is_string($field))
173 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200174 if ($field === 'id')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 $this->add_field(array(
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000177 'id' => array(
178 'type' => 'INT',
179 'constraint' => 9,
180 'auto_increment' => TRUE
181 )
182 ));
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $this->add_key('id', TRUE);
184 }
185 else
186 {
187 if (strpos($field, ' ') === FALSE)
188 {
189 show_error('Field information is required for that operation.');
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 $this->fields[] = $field;
193 }
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 if (is_array($field))
197 {
198 $this->fields = array_merge($this->fields, $field);
199 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200200
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000201 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Create Table
208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @param string the table name
210 * @return bool
211 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000212 public function create_table($table = '', $if_not_exists = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200213 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100214 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 show_error('A table name is required for that operation.');
217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Andrey Andreev24276a32012-01-08 02:44:38 +0200219 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 show_error('Field information is required.');
222 }
223
224 $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 +0000225 $this->_reset();
Andrey Andreev5d281762012-06-11 22:05:40 +0300226
227 if (is_bool($sql))
228 {
229 return $sql;
230 }
231
232 if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names']))
233 {
234 $this->db->data_cache['table_names'][] = $$this->db->dbprefix.$table;
235 }
236
237 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Drop Table
244 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * @param string the table name
246 * @return bool
247 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000248 public function drop_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100250 if ($table_name === '')
Andrey Andreevd947eba2012-04-09 14:58:28 +0300251 {
252 return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE;
253 }
254 elseif ($this->_drop_table === FALSE)
255 {
256 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
257 }
258
Andrey Andreev5d281762012-06-11 22:05:40 +0300259 $result = $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name)));
260
261 // Update table list cache
262 if ($result && ! empty($this->db->data_cache['table_names']))
263 {
264 $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
265 if ($key !== FALSE)
266 {
267 unset($this->db->data_cache['table_names'][$key]);
268 }
269 }
270
271 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Rename Table
278 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * @param string the old table name
280 * @param string the new table name
281 * @return bool
282 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000283 public function rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100285 if ($table_name === '' OR $new_table_name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 show_error('A table name is required for that operation.');
Andrey Andreevd947eba2012-04-09 14:58:28 +0300288 return FALSE;
289 }
290 elseif ($this->_rename_table === FALSE)
291 {
292 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreev5d281762012-06-11 22:05:40 +0300295 $result = $this->db->query(sprintf($this->_rename_table,
Andrey Andreevd947eba2012-04-09 14:58:28 +0300296 $this->db->escape_identifiers($this->db->dbprefix.$table_name),
297 $this->db->escape_identifiers($this->db->dbprefix.$new_table_name))
298 );
Andrey Andreev5d281762012-06-11 22:05:40 +0300299
300 if ($result && ! empty($this->db->data_cache['table_names']))
301 {
302 $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
303 if ($key !== FALSE)
304 {
305 $this->db->data_cache['table_names'][$key] = $this->db->dbprefix.$new_table_name;
306 }
307 }
308
309 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Column Add
316 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @param string the table name
318 * @param string the column name
319 * @param string the column definition
320 * @return bool
321 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000322 public function add_column($table = '', $field = array(), $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100324 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 show_error('A table name is required for that operation.');
327 }
328
329 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000330 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200331 foreach (array_keys($field) as $k)
Barry Mienydd671972010-10-04 16:33:58 +0200332 {
333 $this->add_field(array($k => $field[$k]));
Robin Sowell8a54ef22009-03-04 14:49:53 +0000334
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100335 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200336 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000337 show_error('Field information is required.');
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Robin Sowell8a54ef22009-03-04 14:49:53 +0000340 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000341 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200342
Robin Sowell8a54ef22009-03-04 14:49:53 +0000343 if ($this->db->query($sql) === FALSE)
344 {
345 return FALSE;
346 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Robin Sowell8a54ef22009-03-04 14:49:53 +0000349 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Column Drop
356 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * @param string the table name
358 * @param string the column name
359 * @return bool
360 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000361 public function drop_column($table = '', $column_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100363 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
365 show_error('A table name is required for that operation.');
366 }
367
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100368 if ($column_name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 show_error('A column name is required for that operation.');
371 }
372
Andrey Andreev24276a32012-01-08 02:44:38 +0200373 return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Column Modify
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string the table name
382 * @param string the column name
383 * @param string the column definition
384 * @return bool
385 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000386 public function modify_column($table = '', $field = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100388 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 show_error('A table name is required for that operation.');
391 }
392
393 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000394 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200395 foreach (array_keys($field) as $k)
Robin Sowell8a54ef22009-03-04 14:49:53 +0000396 {
Phil Sturgeona58ecae2010-12-15 10:32:10 +0000397 // If no name provided, use the current name
398 if ( ! isset($field[$k]['name']))
399 {
400 $field[$k]['name'] = $k;
401 }
402
Robin Sowell8a54ef22009-03-04 14:49:53 +0000403 $this->add_field(array($k => $field[$k]));
Andrey Andreev24276a32012-01-08 02:44:38 +0200404 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200405 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000406 show_error('Field information is required.');
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Robin Sowell8a54ef22009-03-04 14:49:53 +0000409 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000410 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200411
Robin Sowell8a54ef22009-03-04 14:49:53 +0000412 if ($this->db->query($sql) === FALSE)
413 {
414 return FALSE;
415 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Robin Sowell8a54ef22009-03-04 14:49:53 +0000418 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Reset
425 *
426 * Resets table creation vars
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @return void
429 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000430 protected function _reset()
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200432 $this->fields = $this->keys = $this->primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435}
436
437/* End of file DB_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400438/* Location: ./system/database/DB_forge.php */