blob: 336e9497db37ee89076a13a5335fcecb42aaea30 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
28// ------------------------------------------------------------------------
29
30/**
31 * Database Utility Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_forge {
38
Andrey Andreev24276a32012-01-08 02:44:38 +020039 public $fields = array();
40 public $keys = array();
41 public $primary_keys = array();
42 public $db_char_set = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000043
Andrey Andreev24276a32012-01-08 02:44:38 +020044 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000045 {
46 // Assign the main database object to $this->db
47 $CI =& get_instance();
48 $this->db =& $CI->db;
Andrey Andreev24276a32012-01-08 02:44:38 +020049 log_message('debug', 'Database Forge Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000050 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Create database
56 *
Derek Allard2067d1a2008-11-13 22:59:24 +000057 * @param string the database name
58 * @return bool
59 */
Andrey Andreev24276a32012-01-08 02:44:38 +020060 public function create_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000061 {
62 $sql = $this->_create_database($db_name);
Andrey Andreev24276a32012-01-08 02:44:38 +020063 return is_bool($sql) ? $sql : $this->db->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +000064 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Drop database
70 *
Derek Allard2067d1a2008-11-13 22:59:24 +000071 * @param string the database name
72 * @return bool
73 */
Andrey Andreev24276a32012-01-08 02:44:38 +020074 public function drop_database($db_name)
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 $sql = $this->_drop_database($db_name);
Andrey Andreev24276a32012-01-08 02:44:38 +020077 return is_bool($sql) ? $sql : $this->db->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +000078 }
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Add Key
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string key
86 * @param string type
Phil Sturgeona7de97e2011-12-31 18:41:08 +000087 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +000088 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +000089 public function add_key($key = '', $primary = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
91 if (is_array($key))
92 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -050093 foreach ($key as $one)
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 $this->add_key($one, $primary);
96 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 return;
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 if ($key == '')
102 {
103 show_error('Key information is required for that operation.');
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 if ($primary === TRUE)
107 {
108 $this->primary_keys[] = $key;
109 }
110 else
111 {
112 $this->keys[] = $key;
113 }
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000114
115 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Add Field
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string collation
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000124 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000126 public function add_field($field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 if ($field == '')
129 {
130 show_error('Field information is required.');
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 if (is_string($field))
134 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200135 if ($field === 'id')
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 $this->add_field(array(
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000138 'id' => array(
139 'type' => 'INT',
140 'constraint' => 9,
141 'auto_increment' => TRUE
142 )
143 ));
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 $this->add_key('id', TRUE);
145 }
146 else
147 {
148 if (strpos($field, ' ') === FALSE)
149 {
150 show_error('Field information is required for that operation.');
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 $this->fields[] = $field;
154 }
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if (is_array($field))
158 {
159 $this->fields = array_merge($this->fields, $field);
160 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200161
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000162 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Create Table
169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string the table name
171 * @return bool
172 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000173 public function create_table($table = '', $if_not_exists = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200174 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if ($table == '')
176 {
177 show_error('A table name is required for that operation.');
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Andrey Andreev24276a32012-01-08 02:44:38 +0200180 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200181 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 show_error('Field information is required.');
183 }
184
185 $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 +0000186 $this->_reset();
Andrey Andreev24276a32012-01-08 02:44:38 +0200187 return is_bool($sql) ? $sql : $this->db->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Drop 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 drop_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
Andrey Andreev24276a32012-01-08 02:44:38 +0200201 return is_bool($sql) ? $sql : $this->db->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Rename Table
208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @param string the old table name
210 * @param string the new table name
211 * @return bool
212 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000213 public function rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 if ($table_name == '' OR $new_table_name == '')
216 {
217 show_error('A table name is required for that operation.');
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Andrey Andreev24276a32012-01-08 02:44:38 +0200220 return $this->db->query($this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Column Add
227 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @param string the table name
229 * @param string the column name
230 * @param string the column definition
231 * @return bool
232 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000233 public function add_column($table = '', $field = array(), $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 if ($table == '')
236 {
237 show_error('A table name is required for that operation.');
238 }
239
240 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000241 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200242 foreach (array_keys($field) as $k)
Barry Mienydd671972010-10-04 16:33:58 +0200243 {
244 $this->add_field(array($k => $field[$k]));
Robin Sowell8a54ef22009-03-04 14:49:53 +0000245
246 if (count($this->fields) == 0)
Barry Mienydd671972010-10-04 16:33:58 +0200247 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000248 show_error('Field information is required.');
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Robin Sowell8a54ef22009-03-04 14:49:53 +0000251 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000252 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200253
Robin Sowell8a54ef22009-03-04 14:49:53 +0000254 if ($this->db->query($sql) === FALSE)
255 {
256 return FALSE;
257 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Robin Sowell8a54ef22009-03-04 14:49:53 +0000260 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Column Drop
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string the table name
269 * @param string the column name
270 * @return bool
271 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000272 public function drop_column($table = '', $column_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 if ($table == '')
275 {
276 show_error('A table name is required for that operation.');
277 }
278
279 if ($column_name == '')
280 {
281 show_error('A column name is required for that operation.');
282 }
283
Andrey Andreev24276a32012-01-08 02:44:38 +0200284 return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name));
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Column Modify
291 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * @param string the table name
293 * @param string the column name
294 * @param string the column definition
295 * @return bool
296 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000297 public function modify_column($table = '', $field = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 if ($table == '')
300 {
301 show_error('A table name is required for that operation.');
302 }
303
304 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000305 // so we cycle through
Andrey Andreev24276a32012-01-08 02:44:38 +0200306 foreach (array_keys($field) as $k)
Robin Sowell8a54ef22009-03-04 14:49:53 +0000307 {
Phil Sturgeona58ecae2010-12-15 10:32:10 +0000308 // If no name provided, use the current name
309 if ( ! isset($field[$k]['name']))
310 {
311 $field[$k]['name'] = $k;
312 }
313
Robin Sowell8a54ef22009-03-04 14:49:53 +0000314 $this->add_field(array($k => $field[$k]));
Andrey Andreev24276a32012-01-08 02:44:38 +0200315 if (count($this->fields) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200316 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000317 show_error('Field information is required.');
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Robin Sowell8a54ef22009-03-04 14:49:53 +0000320 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Robin Sowell8a54ef22009-03-04 14:49:53 +0000321 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200322
Robin Sowell8a54ef22009-03-04 14:49:53 +0000323 if ($this->db->query($sql) === FALSE)
324 {
325 return FALSE;
326 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Robin Sowell8a54ef22009-03-04 14:49:53 +0000329 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Reset
336 *
337 * Resets table creation vars
338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @return void
340 */
Phil Sturgeona7de97e2011-12-31 18:41:08 +0000341 protected function _reset()
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200343 $this->fields = $this->keys = $this->primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345
346}
347
348/* End of file DB_forge.php */
Andrey Andreev24276a32012-01-08 02:44:38 +0200349/* Location: ./system/database/DB_forge.php */