blob: d7259c08cc2c1e86a119ceed4a5aa8603687e02a [file] [log] [blame]
Derek Allard39b622d2008-01-16 21:10:09 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allard39b622d2008-01-16 21:10:09 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard39b622d2008-01-16 21:10:09 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Database Utility Class
20 *
21 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000023 * @link http://codeigniter.com/user_guide/database/
Derek Allard39b622d2008-01-16 21:10:09 +000024 */
25class CI_DB_forge {
26
27 var $fields = array();
28 var $keys = array();
29 var $primary_keys = array();
30 var $db_char_set = '';
31
32 /**
33 * Constructor
34 *
35 * Grabs the CI super object instance so we can access it.
36 *
37 */
38 function CI_DB_forge()
39 {
40 // Assign the main database object to $this->db
41 $CI =& get_instance();
42 $this->db =& $CI->db;
Derek Allard39b622d2008-01-16 21:10:09 +000043 log_message('debug', "Database Forge Class Initialized");
44 }
45
46 // --------------------------------------------------------------------
47
48 /**
49 * Create database
50 *
51 * @access public
52 * @param string the database name
53 * @return bool
54 */
55 function create_database($db_name)
56 {
57 $sql = $this->_create_database($db_name);
58
59 if (is_bool($sql))
60 {
61 return $sql;
62 }
63
64 return $this->db->query($sql);
65 }
66
67 // --------------------------------------------------------------------
68
69 /**
70 * Drop database
71 *
72 * @access public
73 * @param string the database name
74 * @return bool
75 */
76 function drop_database($db_name)
77 {
78 $sql = $this->_drop_database($db_name);
79
80 if (is_bool($sql))
81 {
82 return $sql;
83 }
84
85 return $this->db->query($sql);
86 }
87
88 // --------------------------------------------------------------------
89
90 /**
91 * Add Key
92 *
93 * @access public
94 * @param string key
95 * @param string type
96 * @return void
97 */
98 function add_key($key = '', $primary = FALSE)
99 {
100 if ($key == '')
101 {
102 show_error('Key information is required for that operation.');
103 }
104
105 if ($primary === TRUE)
106 {
107 $this->primary_keys[] = $key;
108 }
109 else
110 {
111 $this->keys[] = $key;
112 }
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Add Field
119 *
120 * @access public
121 * @param string collation
122 * @return void
123 */
124 function add_field($field = '')
125 {
126 if ($field == '')
127 {
128 show_error('Field information is required.');
129 }
130
131 if (is_string($field))
132 {
133 if ($field == 'id')
134 {
Derek Allard7f309812008-05-06 00:17:10 +0000135 $this->add_field(array(
136 'id' => array(
137 'type' => 'INT',
138 'constraint' => 9,
139 'auto_increment' => TRUE
140 )
141 ));
Derek Allard39b622d2008-01-16 21:10:09 +0000142 $this->add_key('id', TRUE);
143 }
144 else
145 {
146 if (strpos($field, ' ') === FALSE)
147 {
148 show_error('Field information is required for that operation.');
149 }
150
151 $this->fields[] = $field;
152 }
153 }
154
155 if (is_array($field))
156 {
157 $this->fields = array_merge($this->fields, $field);
158 }
159
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Create Table
166 *
167 * @access public
168 * @param string the table name
169 * @return bool
170 */
171 function create_table($table = '', $if_not_exists = FALSE)
172 {
173 if ($table == '')
174 {
175 show_error('A table name is required for that operation.');
176 }
177
178 if (count($this->fields) == 0)
179 {
180 show_error('Field information is required.');
181 }
182
Derek Allarddf264422008-01-21 16:48:11 +0000183 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
Derek Allard39b622d2008-01-16 21:10:09 +0000184
185 $this->_reset();
186 return $this->db->query($sql);
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Drop Table
193 *
194 * @access public
195 * @param string the table name
196 * @return bool
197 */
198 function drop_table($table_name)
199 {
Derek Allarddf264422008-01-21 16:48:11 +0000200 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
Derek Allard39b622d2008-01-16 21:10:09 +0000201
202 if (is_bool($sql))
203 {
204 return $sql;
205 }
206
207 return $this->db->query($sql);
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
Derek Allard2385d302008-04-07 14:01:01 +0000213 * Rename Table
214 *
215 * @access public
216 * @param string the old table name
217 * @param string the new table name
218 * @return bool
219 */
220 function rename_table($table_name, $new_table_name)
221 {
222 if ($table_name == '' OR $new_table_name == '')
223 {
224 show_error('A table name is required for that operation.');
225 }
226
227 $sql = $this->_rename_table($table_name, $new_table_name);
228 return $this->db->query($sql);
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000234 * Column Add
235 *
236 * @access public
237 * @param string the table name
238 * @param string the column name
239 * @param string the column definition
240 * @return bool
241 */
242 function add_column($table = '', $field = array(), $after_field = '')
243 {
244 if ($table == '')
245 {
246 show_error('A table name is required for that operation.');
247 }
248
249 // add field info into field array, but we can only do one at a time
250 // so only grab the first field in the event there are more then one
251 $this->add_field(array_slice($field, 0, 1));
252
253 if (count($this->fields) == 0)
254 {
255 show_error('Field information is required.');
256 }
257
Derek Allarddf264422008-01-21 16:48:11 +0000258 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Derek Allard39b622d2008-01-16 21:10:09 +0000259
260 $this->_reset();
261 return $this->db->query($sql);
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Column Drop
268 *
269 * @access public
270 * @param string the table name
271 * @param string the column name
272 * @return bool
273 */
274 function drop_column($table = '', $column_name = '')
275 {
276
277 if ($table == '')
278 {
279 show_error('A table name is required for that operation.');
280 }
281
282 if ($column_name == '')
283 {
284 show_error('A column name is required for that operation.');
285 }
286
Derek Allarddf264422008-01-21 16:48:11 +0000287 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
Derek Allard39b622d2008-01-16 21:10:09 +0000288
289 return $this->db->query($sql);
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Column Modify
296 *
297 * @access public
298 * @param string the table name
299 * @param string the column name
300 * @param string the column definition
301 * @return bool
302 */
303 function modify_column($table = '', $field = array())
304 {
305
306 if ($table == '')
307 {
308 show_error('A table name is required for that operation.');
309 }
310
311 // add field info into field array, but we can only do one at a time
312 // so only grab the first field in the event there are more then one
313 $this->add_field(array_slice($field, 0, 1));
314
315 if (count($this->fields) == 0)
316 {
317 show_error('Field information is required.');
318 }
319
Derek Allarddf264422008-01-21 16:48:11 +0000320 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Derek Allard39b622d2008-01-16 21:10:09 +0000321
322 $this->_reset();
323 return $this->db->query($sql);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * Reset
330 *
331 * Resets table creation vars
332 *
333 * @access private
334 * @return void
335 */
336 function _reset()
337 {
338 $this->fields = array();
339 $this->keys = array();
340 $this->primary_keys = array();
341 }
342
343}
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000344
345/* End of file DB_forge.php */
346/* Location: ./system/database/DB_forge.php */