blob: 119d78d3821d30b0b36181bc2f47b96c0d3e4f1a [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();
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030040 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 Andreev5fd3ae82012-10-24 14:55:35 +030048 /**
49 * Constructor
50 *
51 * @return void
52 */
Andrey Andreev24276a32012-01-08 02:44:38 +020053 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
55 // Assign the main database object to $this->db
56 $CI =& get_instance();
57 $this->db =& $CI->db;
Andrey Andreev24276a32012-01-08 02:44:38 +020058 log_message('debug', 'Database Forge Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Create database
65 *
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * @param string the database name
67 * @return bool
68 */
Andrey Andreev24276a32012-01-08 02:44:38 +020069 public function create_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030071 if ($this->_create_database === FALSE)
72 {
73 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
74 }
75 elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat)))
76 {
77 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
78 }
79
Andrey Andreev5d281762012-06-11 22:05:40 +030080 if ( ! empty($this->db->data_cache['db_names']))
81 {
82 $this->db->data_cache['db_names'][] = $db_name;
83 }
84
Andrey Andreevd947eba2012-04-09 14:58:28 +030085 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
87
88 // --------------------------------------------------------------------
89
90 /**
91 * Drop database
92 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @param string the database name
94 * @return bool
95 */
Andrey Andreev24276a32012-01-08 02:44:38 +020096 public function drop_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010098 if ($db_name === '')
Andrey Andreevd947eba2012-04-09 14:58:28 +030099 {
100 show_error('A table name is required for that operation.');
101 return FALSE;
102 }
103 elseif ($this->_drop_database === FALSE)
104 {
105 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
106 }
107 elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name)))
108 {
109 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
110 }
111
Andrey Andreev5d281762012-06-11 22:05:40 +0300112 if ( ! empty($this->db->data_cache['db_names']))
113 {
114 $key = array_search(strtolower($db_name), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
115 if ($key !== FALSE)
116 {
117 unset($this->db->data_cache['db_names'][$key]);
118 }
119 }
120
Andrey Andreevd947eba2012-04-09 14:58:28 +0300121 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Add Key
128 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * @param string key
130 * @param string type
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000131 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000133 public function add_key($key = '', $primary = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 if (is_array($key))
136 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500137 foreach ($key as $one)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 $this->add_key($one, $primary);
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 return;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100145 if ($key === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 show_error('Key information is required for that operation.');
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 if ($primary === TRUE)
151 {
152 $this->primary_keys[] = $key;
153 }
154 else
155 {
156 $this->keys[] = $key;
157 }
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000158
159 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Add Field
166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @param string collation
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000168 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000170 public function add_field($field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100172 if ($field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 show_error('Field information is required.');
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if (is_string($field))
178 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200179 if ($field === 'id')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 $this->add_field(array(
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000182 'id' => array(
183 'type' => 'INT',
184 'constraint' => 9,
185 'auto_increment' => TRUE
186 )
187 ));
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 $this->add_key('id', TRUE);
189 }
190 else
191 {
192 if (strpos($field, ' ') === FALSE)
193 {
194 show_error('Field information is required for that operation.');
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 $this->fields[] = $field;
198 }
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 if (is_array($field))
202 {
203 $this->fields = array_merge($this->fields, $field);
204 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200205
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000206 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Create Table
213 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300214 * @param string $table = ''
215 * @param bool $if_not_exists = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * @return bool
217 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000218 public function create_table($table = '', $if_not_exists = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200219 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100220 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 show_error('A table name is required for that operation.');
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Andrey Andreev24276a32012-01-08 02:44:38 +0200225 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200226 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 show_error('Field information is required.');
228 }
229
230 $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 +0000231 $this->_reset();
Andrey Andreev5d281762012-06-11 22:05:40 +0300232
233 if (is_bool($sql))
234 {
235 return $sql;
236 }
237
238 if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names']))
239 {
Phil Sturgeonf68db392012-06-14 17:14:11 -0500240 $this->db->data_cache['table_names'][] = $this->db->dbprefix.$table;
Andrey Andreev5d281762012-06-11 22:05:40 +0300241 }
242
243 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Drop Table
250 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 * @param string the table name
252 * @return bool
253 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000254 public function drop_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100256 if ($table_name === '')
Andrey Andreevd947eba2012-04-09 14:58:28 +0300257 {
258 return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE;
259 }
260 elseif ($this->_drop_table === FALSE)
261 {
262 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
263 }
264
Andrey Andreev5d281762012-06-11 22:05:40 +0300265 $result = $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name)));
266
267 // Update table list cache
268 if ($result && ! empty($this->db->data_cache['table_names']))
269 {
270 $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
271 if ($key !== FALSE)
272 {
273 unset($this->db->data_cache['table_names'][$key]);
274 }
275 }
276
277 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Rename Table
284 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @param string the old table name
286 * @param string the new table name
287 * @return bool
288 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000289 public function rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100291 if ($table_name === '' OR $new_table_name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 show_error('A table name is required for that operation.');
Andrey Andreevd947eba2012-04-09 14:58:28 +0300294 return FALSE;
295 }
296 elseif ($this->_rename_table === FALSE)
297 {
298 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Andrey Andreev5d281762012-06-11 22:05:40 +0300301 $result = $this->db->query(sprintf($this->_rename_table,
Andrey Andreevd947eba2012-04-09 14:58:28 +0300302 $this->db->escape_identifiers($this->db->dbprefix.$table_name),
303 $this->db->escape_identifiers($this->db->dbprefix.$new_table_name))
304 );
Andrey Andreev5d281762012-06-11 22:05:40 +0300305
306 if ($result && ! empty($this->db->data_cache['table_names']))
307 {
308 $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
309 if ($key !== FALSE)
310 {
311 $this->db->data_cache['table_names'][$key] = $this->db->dbprefix.$new_table_name;
312 }
313 }
314
315 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Column Add
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @param string the table name
324 * @param string the column name
325 * @param string the column definition
326 * @return bool
327 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000328 public function add_column($table = '', $field = array(), $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100330 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 show_error('A table name is required for that operation.');
333 }
334
335 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000336 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200337 foreach (array_keys($field) as $k)
Barry Mienydd671972010-10-04 16:33:58 +0200338 {
339 $this->add_field(array($k => $field[$k]));
Robin Sowell8a54ef22009-03-04 14:49:53 +0000340
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100341 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200342 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000343 show_error('Field information is required.');
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Robin Sowell8a54ef22009-03-04 14:49:53 +0000346 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000347 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200348
Robin Sowell8a54ef22009-03-04 14:49:53 +0000349 if ($this->db->query($sql) === FALSE)
350 {
351 return FALSE;
352 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Robin Sowell8a54ef22009-03-04 14:49:53 +0000355 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Column Drop
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param string the table name
364 * @param string the column name
365 * @return bool
366 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000367 public function drop_column($table = '', $column_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100369 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 show_error('A table name is required for that operation.');
372 }
373
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100374 if ($column_name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 show_error('A column name is required for that operation.');
377 }
378
Andrey Andreev24276a32012-01-08 02:44:38 +0200379 return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Column Modify
386 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300387 * @param string $table = ''
388 * @param string $field = array() column definition
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @return bool
390 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000391 public function modify_column($table = '', $field = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100393 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 show_error('A table name is required for that operation.');
396 }
397
398 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000399 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200400 foreach (array_keys($field) as $k)
Robin Sowell8a54ef22009-03-04 14:49:53 +0000401 {
Phil Sturgeona58ecae2010-12-15 10:32:10 +0000402 // If no name provided, use the current name
403 if ( ! isset($field[$k]['name']))
404 {
405 $field[$k]['name'] = $k;
406 }
407
Robin Sowell8a54ef22009-03-04 14:49:53 +0000408 $this->add_field(array($k => $field[$k]));
Andrey Andreev24276a32012-01-08 02:44:38 +0200409 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200410 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000411 show_error('Field information is required.');
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Robin Sowell8a54ef22009-03-04 14:49:53 +0000414 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000415 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200416
Robin Sowell8a54ef22009-03-04 14:49:53 +0000417 if ($this->db->query($sql) === FALSE)
418 {
419 return FALSE;
420 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Robin Sowell8a54ef22009-03-04 14:49:53 +0000423 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
429 * Reset
430 *
431 * Resets table creation vars
432 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @return void
434 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000435 protected function _reset()
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200437 $this->fields = $this->keys = $this->primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440}
441
442/* End of file DB_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400443/* Location: ./system/database/DB_forge.php */