blob: 5f25ebedc507ccff7b0a49c39612d8025b6e1530 [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 {
135 $this->fields[] = array('id' => array(
136 'type' => 'INT',
137 'constraint' => 9,
138 'auto_increment' => TRUE
139 )
140 );
141 $this->add_key('id', TRUE);
142 }
143 else
144 {
145 if (strpos($field, ' ') === FALSE)
146 {
147 show_error('Field information is required for that operation.');
148 }
149
150 $this->fields[] = $field;
151 }
152 }
153
154 if (is_array($field))
155 {
156 $this->fields = array_merge($this->fields, $field);
157 }
158
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Create Table
165 *
166 * @access public
167 * @param string the table name
168 * @return bool
169 */
170 function create_table($table = '', $if_not_exists = FALSE)
171 {
172 if ($table == '')
173 {
174 show_error('A table name is required for that operation.');
175 }
176
177 if (count($this->fields) == 0)
178 {
179 show_error('Field information is required.');
180 }
181
Derek Allarddf264422008-01-21 16:48:11 +0000182 $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 +0000183
184 $this->_reset();
185 return $this->db->query($sql);
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Drop Table
192 *
193 * @access public
194 * @param string the table name
195 * @return bool
196 */
197 function drop_table($table_name)
198 {
Derek Allarddf264422008-01-21 16:48:11 +0000199 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
Derek Allard39b622d2008-01-16 21:10:09 +0000200
201 if (is_bool($sql))
202 {
203 return $sql;
204 }
205
206 return $this->db->query($sql);
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
Derek Allard2385d302008-04-07 14:01:01 +0000212 * Rename Table
213 *
214 * @access public
215 * @param string the old table name
216 * @param string the new table name
217 * @return bool
218 */
219 function rename_table($table_name, $new_table_name)
220 {
221 if ($table_name == '' OR $new_table_name == '')
222 {
223 show_error('A table name is required for that operation.');
224 }
225
226 $sql = $this->_rename_table($table_name, $new_table_name);
227 return $this->db->query($sql);
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000233 * Column Add
234 *
235 * @access public
236 * @param string the table name
237 * @param string the column name
238 * @param string the column definition
239 * @return bool
240 */
241 function add_column($table = '', $field = array(), $after_field = '')
242 {
243 if ($table == '')
244 {
245 show_error('A table name is required for that operation.');
246 }
247
248 // add field info into field array, but we can only do one at a time
249 // so only grab the first field in the event there are more then one
250 $this->add_field(array_slice($field, 0, 1));
251
252 if (count($this->fields) == 0)
253 {
254 show_error('Field information is required.');
255 }
256
Derek Allarddf264422008-01-21 16:48:11 +0000257 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Derek Allard39b622d2008-01-16 21:10:09 +0000258
259 $this->_reset();
260 return $this->db->query($sql);
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Column Drop
267 *
268 * @access public
269 * @param string the table name
270 * @param string the column name
271 * @return bool
272 */
273 function drop_column($table = '', $column_name = '')
274 {
275
276 if ($table == '')
277 {
278 show_error('A table name is required for that operation.');
279 }
280
281 if ($column_name == '')
282 {
283 show_error('A column name is required for that operation.');
284 }
285
Derek Allarddf264422008-01-21 16:48:11 +0000286 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
Derek Allard39b622d2008-01-16 21:10:09 +0000287
288 return $this->db->query($sql);
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Column Modify
295 *
296 * @access public
297 * @param string the table name
298 * @param string the column name
299 * @param string the column definition
300 * @return bool
301 */
302 function modify_column($table = '', $field = array())
303 {
304
305 if ($table == '')
306 {
307 show_error('A table name is required for that operation.');
308 }
309
310 // add field info into field array, but we can only do one at a time
311 // so only grab the first field in the event there are more then one
312 $this->add_field(array_slice($field, 0, 1));
313
314 if (count($this->fields) == 0)
315 {
316 show_error('Field information is required.');
317 }
318
Derek Allarddf264422008-01-21 16:48:11 +0000319 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Derek Allard39b622d2008-01-16 21:10:09 +0000320
321 $this->_reset();
322 return $this->db->query($sql);
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Reset
329 *
330 * Resets table creation vars
331 *
332 * @access private
333 * @return void
334 */
335 function _reset()
336 {
337 $this->fields = array();
338 $this->keys = array();
339 $this->primary_keys = array();
340 }
341
342}
343?>