blob: 50b55d60e1064b6aa4abe81685431439f0def0cc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Andrey Andreevd947eba2012-04-09 14:58:28 +030030 * Database Forge Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
Timothy Warren833d5042012-03-19 16:12:03 -040036abstract class CI_DB_forge {
Derek Allard2067d1a2008-11-13 22:59:24 +000037
Andrey Andreev24276a32012-01-08 02:44:38 +020038 public $fields = array();
39 public $keys = array();
40 public $primary_keys = array();
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030041 public $db_char_set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreevd947eba2012-04-09 14:58:28 +030043 // Platform specific SQL strings
44 protected $_create_database = 'CREATE DATABASE %s';
45 protected $_drop_database = 'DROP DATABASE %s';
46 protected $_drop_table = 'DROP TABLE IF EXISTS %s';
47 protected $_rename_table = 'ALTER TABLE %s RENAME TO %s';
48
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030049 /**
50 * Constructor
51 *
52 * @return void
53 */
Andrey Andreev24276a32012-01-08 02:44:38 +020054 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
56 // Assign the main database object to $this->db
57 $CI =& get_instance();
58 $this->db =& $CI->db;
Andrey Andreev24276a32012-01-08 02:44:38 +020059 log_message('debug', 'Database Forge Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
61
62 // --------------------------------------------------------------------
63
64 /**
65 * Create database
66 *
Derek Allard2067d1a2008-11-13 22:59:24 +000067 * @param string the database name
68 * @return bool
69 */
Andrey Andreev24276a32012-01-08 02:44:38 +020070 public function create_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030072 if ($this->_create_database === FALSE)
73 {
74 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
75 }
76 elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat)))
77 {
78 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
79 }
80
Andrey Andreev5d281762012-06-11 22:05:40 +030081 if ( ! empty($this->db->data_cache['db_names']))
82 {
83 $this->db->data_cache['db_names'][] = $db_name;
84 }
85
Andrey Andreevd947eba2012-04-09 14:58:28 +030086 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
88
89 // --------------------------------------------------------------------
90
91 /**
92 * Drop database
93 *
Derek Allard2067d1a2008-11-13 22:59:24 +000094 * @param string the database name
95 * @return bool
96 */
Andrey Andreev24276a32012-01-08 02:44:38 +020097 public function drop_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +010099 if ($db_name === '')
Andrey Andreevd947eba2012-04-09 14:58:28 +0300100 {
101 show_error('A table name is required for that operation.');
102 return FALSE;
103 }
104 elseif ($this->_drop_database === FALSE)
105 {
106 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
107 }
108 elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name)))
109 {
110 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
111 }
112
Andrey Andreev5d281762012-06-11 22:05:40 +0300113 if ( ! empty($this->db->data_cache['db_names']))
114 {
115 $key = array_search(strtolower($db_name), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
116 if ($key !== FALSE)
117 {
118 unset($this->db->data_cache['db_names'][$key]);
119 }
120 }
121
Andrey Andreevd947eba2012-04-09 14:58:28 +0300122 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
124
125 // --------------------------------------------------------------------
126
127 /**
128 * Add Key
129 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * @param string key
131 * @param string type
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000132 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000134 public function add_key($key = '', $primary = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 if (is_array($key))
137 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500138 foreach ($key as $one)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 $this->add_key($one, $primary);
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 return;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100146 if ($key === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 show_error('Key information is required for that operation.');
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 if ($primary === TRUE)
152 {
153 $this->primary_keys[] = $key;
154 }
155 else
156 {
157 $this->keys[] = $key;
158 }
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000159
160 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Add Field
167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @param string collation
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000169 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000171 public function add_field($field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100173 if ($field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 show_error('Field information is required.');
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if (is_string($field))
179 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200180 if ($field === 'id')
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 $this->add_field(array(
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000183 'id' => array(
184 'type' => 'INT',
185 'constraint' => 9,
186 'auto_increment' => TRUE
187 )
188 ));
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $this->add_key('id', TRUE);
190 }
191 else
192 {
193 if (strpos($field, ' ') === FALSE)
194 {
195 show_error('Field information is required for that operation.');
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $this->fields[] = $field;
199 }
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 if (is_array($field))
203 {
204 $this->fields = array_merge($this->fields, $field);
205 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200206
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000207 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Create Table
214 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300215 * @param string $table = ''
216 * @param bool $if_not_exists = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @return bool
218 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000219 public function create_table($table = '', $if_not_exists = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100221 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 show_error('A table name is required for that operation.');
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Andrey Andreev24276a32012-01-08 02:44:38 +0200226 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 show_error('Field information is required.');
229 }
230
231 $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 +0000232 $this->_reset();
Andrey Andreev5d281762012-06-11 22:05:40 +0300233
234 if (is_bool($sql))
235 {
236 return $sql;
237 }
238
239 if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names']))
240 {
Phil Sturgeonf68db392012-06-14 17:14:11 -0500241 $this->db->data_cache['table_names'][] = $this->db->dbprefix.$table;
Andrey Andreev5d281762012-06-11 22:05:40 +0300242 }
243
244 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Drop Table
251 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @param string the table name
253 * @return bool
254 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000255 public function drop_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100257 if ($table_name === '')
Andrey Andreevd947eba2012-04-09 14:58:28 +0300258 {
259 return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE;
260 }
261 elseif ($this->_drop_table === FALSE)
262 {
263 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
264 }
265
Andrey Andreev5d281762012-06-11 22:05:40 +0300266 $result = $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name)));
267
268 // Update table list cache
269 if ($result && ! empty($this->db->data_cache['table_names']))
270 {
271 $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
272 if ($key !== FALSE)
273 {
274 unset($this->db->data_cache['table_names'][$key]);
275 }
276 }
277
278 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Rename Table
285 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param string the old table name
287 * @param string the new table name
288 * @return bool
289 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000290 public function rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100292 if ($table_name === '' OR $new_table_name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 show_error('A table name is required for that operation.');
Andrey Andreevd947eba2012-04-09 14:58:28 +0300295 return FALSE;
296 }
297 elseif ($this->_rename_table === FALSE)
298 {
299 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Andrey Andreev5d281762012-06-11 22:05:40 +0300302 $result = $this->db->query(sprintf($this->_rename_table,
Andrey Andreevd947eba2012-04-09 14:58:28 +0300303 $this->db->escape_identifiers($this->db->dbprefix.$table_name),
304 $this->db->escape_identifiers($this->db->dbprefix.$new_table_name))
305 );
Andrey Andreev5d281762012-06-11 22:05:40 +0300306
307 if ($result && ! empty($this->db->data_cache['table_names']))
308 {
309 $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE);
310 if ($key !== FALSE)
311 {
312 $this->db->data_cache['table_names'][$key] = $this->db->dbprefix.$new_table_name;
313 }
314 }
315
316 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * Column Add
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string the table name
325 * @param string the column name
326 * @param string the column definition
327 * @return bool
328 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000329 public function add_column($table = '', $field = array(), $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100331 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 show_error('A table name is required for that operation.');
334 }
335
336 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000337 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200338 foreach (array_keys($field) as $k)
Barry Mienydd671972010-10-04 16:33:58 +0200339 {
340 $this->add_field(array($k => $field[$k]));
Robin Sowell8a54ef22009-03-04 14:49:53 +0000341
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100342 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200343 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000344 show_error('Field information is required.');
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Robin Sowell8a54ef22009-03-04 14:49:53 +0000347 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000348 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200349
Robin Sowell8a54ef22009-03-04 14:49:53 +0000350 if ($this->db->query($sql) === FALSE)
351 {
352 return FALSE;
353 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Robin Sowell8a54ef22009-03-04 14:49:53 +0000356 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Column Drop
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param string the table name
365 * @param string the column name
366 * @return bool
367 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000368 public function drop_column($table = '', $column_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100370 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 show_error('A table name is required for that operation.');
373 }
374
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100375 if ($column_name === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 show_error('A column name is required for that operation.');
378 }
379
Andrey Andreev24276a32012-01-08 02:44:38 +0200380 return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Column Modify
387 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300388 * @param string $table = ''
389 * @param string $field = array() column definition
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 * @return bool
391 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000392 public function modify_column($table = '', $field = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100394 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 show_error('A table name is required for that operation.');
397 }
398
399 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000400 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200401 foreach (array_keys($field) as $k)
Robin Sowell8a54ef22009-03-04 14:49:53 +0000402 {
Phil Sturgeona58ecae2010-12-15 10:32:10 +0000403 // If no name provided, use the current name
404 if ( ! isset($field[$k]['name']))
405 {
406 $field[$k]['name'] = $k;
407 }
408
Robin Sowell8a54ef22009-03-04 14:49:53 +0000409 $this->add_field(array($k => $field[$k]));
Andrey Andreev24276a32012-01-08 02:44:38 +0200410 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200411 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000412 show_error('Field information is required.');
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Robin Sowell8a54ef22009-03-04 14:49:53 +0000415 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000416 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200417
Robin Sowell8a54ef22009-03-04 14:49:53 +0000418 if ($this->db->query($sql) === FALSE)
419 {
420 return FALSE;
421 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Robin Sowell8a54ef22009-03-04 14:49:53 +0000424 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Reset
431 *
432 * Resets table creation vars
433 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @return void
435 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000436 protected function _reset()
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200438 $this->fields = $this->keys = $this->primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441}
442
443/* End of file DB_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400444/* Location: ./system/database/DB_forge.php */