blob: cd468bc6a779fb12017d73686a85e0a1e44a0159 [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 /**
212 * Column Add
213 *
214 * @access public
215 * @param string the table name
216 * @param string the column name
217 * @param string the column definition
218 * @return bool
219 */
220 function add_column($table = '', $field = array(), $after_field = '')
221 {
222 if ($table == '')
223 {
224 show_error('A table name is required for that operation.');
225 }
226
227 // add field info into field array, but we can only do one at a time
228 // so only grab the first field in the event there are more then one
229 $this->add_field(array_slice($field, 0, 1));
230
231 if (count($this->fields) == 0)
232 {
233 show_error('Field information is required.');
234 }
235
Derek Allarddf264422008-01-21 16:48:11 +0000236 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
Derek Allard39b622d2008-01-16 21:10:09 +0000237
238 $this->_reset();
239 return $this->db->query($sql);
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Column Drop
246 *
247 * @access public
248 * @param string the table name
249 * @param string the column name
250 * @return bool
251 */
252 function drop_column($table = '', $column_name = '')
253 {
254
255 if ($table == '')
256 {
257 show_error('A table name is required for that operation.');
258 }
259
260 if ($column_name == '')
261 {
262 show_error('A column name is required for that operation.');
263 }
264
Derek Allarddf264422008-01-21 16:48:11 +0000265 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
Derek Allard39b622d2008-01-16 21:10:09 +0000266
267 return $this->db->query($sql);
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Column Modify
274 *
275 * @access public
276 * @param string the table name
277 * @param string the column name
278 * @param string the column definition
279 * @return bool
280 */
281 function modify_column($table = '', $field = array())
282 {
283
284 if ($table == '')
285 {
286 show_error('A table name is required for that operation.');
287 }
288
289 // add field info into field array, but we can only do one at a time
290 // so only grab the first field in the event there are more then one
291 $this->add_field(array_slice($field, 0, 1));
292
293 if (count($this->fields) == 0)
294 {
295 show_error('Field information is required.');
296 }
297
Derek Allarddf264422008-01-21 16:48:11 +0000298 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
Derek Allard39b622d2008-01-16 21:10:09 +0000299
300 $this->_reset();
301 return $this->db->query($sql);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Reset
308 *
309 * Resets table creation vars
310 *
311 * @access private
312 * @return void
313 */
314 function _reset()
315 {
316 $this->fields = array();
317 $this->keys = array();
318 $this->primary_keys = array();
319 }
320
321}
322?>