blob: d02589457135f1c5161a220ed9ee079cc4a8a721 [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
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @license http://www.codeigniter.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Database Utility Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
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;
43
44 log_message('debug', "Database Forge Class Initialized");
45 }
46
47 // --------------------------------------------------------------------
48
49 /**
50 * Create database
51 *
52 * @access public
53 * @param string the database name
54 * @return bool
55 */
56 function create_database($db_name)
57 {
58 $sql = $this->_create_database($db_name);
59
60 if (is_bool($sql))
61 {
62 return $sql;
63 }
64
65 return $this->db->query($sql);
66 }
67
68 // --------------------------------------------------------------------
69
70 /**
71 * Drop database
72 *
73 * @access public
74 * @param string the database name
75 * @return bool
76 */
77 function drop_database($db_name)
78 {
79 $sql = $this->_drop_database($db_name);
80
81 if (is_bool($sql))
82 {
83 return $sql;
84 }
85
86 return $this->db->query($sql);
87 }
88
89 // --------------------------------------------------------------------
90
91 /**
92 * Add Key
93 *
94 * @access public
95 * @param string key
96 * @param string type
97 * @return void
98 */
99 function add_key($key = '', $primary = FALSE)
100 {
101 if ($key == '')
102 {
103 show_error('Key information is required for that operation.');
104 }
105
106 if ($primary === TRUE)
107 {
108 $this->primary_keys[] = $key;
109 }
110 else
111 {
112 $this->keys[] = $key;
113 }
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Add Field
120 *
121 * @access public
122 * @param string collation
123 * @return void
124 */
125 function add_field($field = '')
126 {
127 if ($field == '')
128 {
129 show_error('Field information is required.');
130 }
131
132 if (is_string($field))
133 {
134 if ($field == 'id')
135 {
136 $this->fields[] = array('id' => array(
137 'type' => 'INT',
138 'constraint' => 9,
139 'auto_increment' => TRUE
140 )
141 );
142 $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
183 $sql = $this->_create_table($table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
184
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 {
200 $sql = $this->_drop_table($table_name);
201
202 if (is_bool($sql))
203 {
204 return $sql;
205 }
206
207 return $this->db->query($sql);
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Column Add
214 *
215 * @access public
216 * @param string the table name
217 * @param string the column name
218 * @param string the column definition
219 * @return bool
220 */
221 function add_column($table = '', $field = array(), $after_field = '')
222 {
223 if ($table == '')
224 {
225 show_error('A table name is required for that operation.');
226 }
227
228 // add field info into field array, but we can only do one at a time
229 // so only grab the first field in the event there are more then one
230 $this->add_field(array_slice($field, 0, 1));
231
232 if (count($this->fields) == 0)
233 {
234 show_error('Field information is required.');
235 }
236
237 $sql = $this->_alter_table('ADD', $table, $this->fields, $after_field);
238
239 $this->_reset();
240 return $this->db->query($sql);
241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Column Drop
247 *
248 * @access public
249 * @param string the table name
250 * @param string the column name
251 * @return bool
252 */
253 function drop_column($table = '', $column_name = '')
254 {
255
256 if ($table == '')
257 {
258 show_error('A table name is required for that operation.');
259 }
260
261 if ($column_name == '')
262 {
263 show_error('A column name is required for that operation.');
264 }
265
266 $sql = $this->_alter_table('DROP', $table, $column_name);
267
268 return $this->db->query($sql);
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Column Modify
275 *
276 * @access public
277 * @param string the table name
278 * @param string the column name
279 * @param string the column definition
280 * @return bool
281 */
282 function modify_column($table = '', $field = array())
283 {
284
285 if ($table == '')
286 {
287 show_error('A table name is required for that operation.');
288 }
289
290 // add field info into field array, but we can only do one at a time
291 // so only grab the first field in the event there are more then one
292 $this->add_field(array_slice($field, 0, 1));
293
294 if (count($this->fields) == 0)
295 {
296 show_error('Field information is required.');
297 }
298
299 $sql = $this->_alter_table('CHANGE', $table, $this->fields);
300
301 $this->_reset();
302 return $this->db->query($sql);
303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Reset
309 *
310 * Resets table creation vars
311 *
312 * @access private
313 * @return void
314 */
315 function _reset()
316 {
317 $this->fields = array();
318 $this->keys = array();
319 $this->primary_keys = array();
320 }
321
322}
323?>