blob: 78bf77a9fa750088929300b05bde1af25b64ab69 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
Barry Mienydd671972010-10-04 16:33:58 +020039 var $fields = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000040 var $keys = array();
Barry Mienydd671972010-10-04 16:33:58 +020041 var $primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000042 var $db_char_set = '';
43
44 /**
45 * Constructor
46 *
47 * Grabs the CI super object instance so we can access it.
48 *
Barry Mienydd671972010-10-04 16:33:58 +020049 */
Timothy Warrena2097a02011-10-10 10:10:46 -040050 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000051 {
52 // Assign the main database object to $this->db
53 $CI =& get_instance();
54 $this->db =& $CI->db;
55 log_message('debug', "Database Forge Class Initialized");
56 }
57
58 // --------------------------------------------------------------------
59
60 /**
61 * Create database
62 *
63 * @access public
64 * @param string the database name
65 * @return bool
66 */
67 function create_database($db_name)
68 {
69 $sql = $this->_create_database($db_name);
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 if (is_bool($sql))
72 {
73 return $sql;
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 return $this->db->query($sql);
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Drop database
83 *
84 * @access public
85 * @param string the database name
86 * @return bool
87 */
88 function drop_database($db_name)
89 {
90 $sql = $this->_drop_database($db_name);
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 if (is_bool($sql))
93 {
94 return $sql;
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 return $this->db->query($sql);
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Add Key
104 *
105 * @access public
106 * @param string key
107 * @param string type
108 * @return void
109 */
110 function add_key($key = '', $primary = FALSE)
111 {
112 if (is_array($key))
113 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500114 foreach ($key as $one)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 $this->add_key($one, $primary);
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 return;
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 if ($key == '')
123 {
124 show_error('Key information is required for that operation.');
125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if ($primary === TRUE)
128 {
129 $this->primary_keys[] = $key;
130 }
131 else
132 {
133 $this->keys[] = $key;
134 }
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Add Field
141 *
142 * @access public
143 * @param string collation
144 * @return void
145 */
146 function add_field($field = '')
147 {
148 if ($field == '')
149 {
150 show_error('Field information is required.');
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 if (is_string($field))
154 {
155 if ($field == 'id')
156 {
157 $this->add_field(array(
158 'id' => array(
159 'type' => 'INT',
160 'constraint' => 9,
161 'auto_increment' => TRUE
162 )
163 ));
164 $this->add_key('id', TRUE);
165 }
166 else
167 {
168 if (strpos($field, ' ') === FALSE)
169 {
170 show_error('Field information is required for that operation.');
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $this->fields[] = $field;
174 }
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if (is_array($field))
178 {
179 $this->fields = array_merge($this->fields, $field);
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Create Table
188 *
189 * @access public
190 * @param string the table name
191 * @return bool
192 */
193 function create_table($table = '', $if_not_exists = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 if ($table == '')
196 {
197 show_error('A table name is required for that operation.');
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 if (count($this->fields) == 0)
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 show_error('Field information is required.');
203 }
204
205 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 $this->_reset();
Tomasz Tb9ec4022011-12-05 15:28:33 +0100208
209 if (is_bool($sql))
210 {
211 return $sql;
212 }
213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 return $this->db->query($sql);
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Drop Table
221 *
222 * @access public
223 * @param string the table name
224 * @return bool
225 */
226 function drop_table($table_name)
227 {
228 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if (is_bool($sql))
231 {
232 return $sql;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 return $this->db->query($sql);
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rename Table
242 *
243 * @access public
244 * @param string the old table name
245 * @param string the new table name
246 * @return bool
247 */
248 function rename_table($table_name, $new_table_name)
249 {
250 if ($table_name == '' OR $new_table_name == '')
251 {
252 show_error('A table name is required for that operation.');
253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Zac Wasielewski0acf65a2011-07-15 22:23:18 -0400255 $sql = $this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 return $this->db->query($sql);
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Column Add
263 *
264 * @access public
265 * @param string the table name
266 * @param string the column name
267 * @param string the column definition
268 * @return bool
269 */
270 function add_column($table = '', $field = array(), $after_field = '')
271 {
272 if ($table == '')
273 {
274 show_error('A table name is required for that operation.');
275 }
276
277 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000278 // so we cycle through
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Barry Mienydd671972010-10-04 16:33:58 +0200280 foreach ($field as $k => $v)
281 {
282 $this->add_field(array($k => $field[$k]));
Robin Sowell8a54ef22009-03-04 14:49:53 +0000283
284 if (count($this->fields) == 0)
Barry Mienydd671972010-10-04 16:33:58 +0200285 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000286 show_error('Field information is required.');
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Robin Sowell8a54ef22009-03-04 14:49:53 +0000289 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
290
291 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200292
Robin Sowell8a54ef22009-03-04 14:49:53 +0000293 if ($this->db->query($sql) === FALSE)
294 {
295 return FALSE;
296 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Robin Sowell8a54ef22009-03-04 14:49:53 +0000299 return TRUE;
Derek Jones30b35eb2010-03-02 17:25:41 -0600300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Column Drop
307 *
308 * @access public
309 * @param string the table name
310 * @param string the column name
311 * @return bool
312 */
313 function drop_column($table = '', $column_name = '')
314 {
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 if ($table == '')
317 {
318 show_error('A table name is required for that operation.');
319 }
320
321 if ($column_name == '')
322 {
323 show_error('A column name is required for that operation.');
324 }
325
326 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 return $this->db->query($sql);
329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * Column Modify
335 *
336 * @access public
337 * @param string the table name
338 * @param string the column name
339 * @param string the column definition
340 * @return bool
341 */
342 function modify_column($table = '', $field = array())
343 {
344 if ($table == '')
345 {
346 show_error('A table name is required for that operation.');
347 }
348
349 // add field info into field array, but we can only do one at a time
Robin Sowell8a54ef22009-03-04 14:49:53 +0000350 // so we cycle through
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
Robin Sowell8a54ef22009-03-04 14:49:53 +0000352 foreach ($field as $k => $v)
353 {
Phil Sturgeona58ecae2010-12-15 10:32:10 +0000354 // If no name provided, use the current name
355 if ( ! isset($field[$k]['name']))
356 {
357 $field[$k]['name'] = $k;
358 }
359
Robin Sowell8a54ef22009-03-04 14:49:53 +0000360 $this->add_field(array($k => $field[$k]));
361
362 if (count($this->fields) == 0)
Barry Mienydd671972010-10-04 16:33:58 +0200363 {
Robin Sowell8a54ef22009-03-04 14:49:53 +0000364 show_error('Field information is required.');
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Robin Sowell8a54ef22009-03-04 14:49:53 +0000367 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
368
369 $this->_reset();
Barry Mienydd671972010-10-04 16:33:58 +0200370
Robin Sowell8a54ef22009-03-04 14:49:53 +0000371 if ($this->db->query($sql) === FALSE)
372 {
373 return FALSE;
374 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Robin Sowell8a54ef22009-03-04 14:49:53 +0000377 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Reset
384 *
385 * Resets table creation vars
386 *
387 * @access private
388 * @return void
389 */
390 function _reset()
391 {
Barry Mienydd671972010-10-04 16:33:58 +0200392 $this->fields = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 $this->keys = array();
Barry Mienydd671972010-10-04 16:33:58 +0200394 $this->primary_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
396
397}
398
399/* End of file DB_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000400/* Location: ./system/database/DB_forge.php */