DB_forge.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 class CI_DB_forge {
00026
00027 var $fields = array();
00028 var $keys = array();
00029 var $primary_keys = array();
00030 var $db_char_set = '';
00031
00032
00033
00034
00035
00036
00037
00038 function CI_DB_forge()
00039 {
00040
00041 $CI =& get_instance();
00042 $this->db =& $CI->db;
00043 log_message('debug', "Database Forge Class Initialized");
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 function create_database($db_name)
00056 {
00057 $sql = $this->_create_database($db_name);
00058
00059 if (is_bool($sql))
00060 {
00061 return $sql;
00062 }
00063
00064 return $this->db->query($sql);
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 function drop_database($db_name)
00077 {
00078 $sql = $this->_drop_database($db_name);
00079
00080 if (is_bool($sql))
00081 {
00082 return $sql;
00083 }
00084
00085 return $this->db->query($sql);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 function add_key($key = '', $primary = FALSE)
00099 {
00100 if ($key == '')
00101 {
00102 show_error('Key information is required for that operation.');
00103 }
00104
00105 if ($primary === TRUE)
00106 {
00107 $this->primary_keys[] = $key;
00108 }
00109 else
00110 {
00111 $this->keys[] = $key;
00112 }
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 function add_field($field = '')
00125 {
00126 if ($field == '')
00127 {
00128 show_error('Field information is required.');
00129 }
00130
00131 if (is_string($field))
00132 {
00133 if ($field == 'id')
00134 {
00135 $this->add_field(array(
00136 'id' => array(
00137 'type' => 'INT',
00138 'constraint' => 9,
00139 'auto_increment' => TRUE
00140 )
00141 ));
00142 $this->add_key('id', TRUE);
00143 }
00144 else
00145 {
00146 if (strpos($field, ' ') === FALSE)
00147 {
00148 show_error('Field information is required for that operation.');
00149 }
00150
00151 $this->fields[] = $field;
00152 }
00153 }
00154
00155 if (is_array($field))
00156 {
00157 $this->fields = array_merge($this->fields, $field);
00158 }
00159
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 function create_table($table = '', $if_not_exists = FALSE)
00172 {
00173 if ($table == '')
00174 {
00175 show_error('A table name is required for that operation.');
00176 }
00177
00178 if (count($this->fields) == 0)
00179 {
00180 show_error('Field information is required.');
00181 }
00182
00183 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
00184
00185 $this->_reset();
00186 return $this->db->query($sql);
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 function drop_table($table_name)
00199 {
00200 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
00201
00202 if (is_bool($sql))
00203 {
00204 return $sql;
00205 }
00206
00207 return $this->db->query($sql);
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 function rename_table($table_name, $new_table_name)
00221 {
00222 if ($table_name == '' OR $new_table_name == '')
00223 {
00224 show_error('A table name is required for that operation.');
00225 }
00226
00227 $sql = $this->_rename_table($table_name, $new_table_name);
00228 return $this->db->query($sql);
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 function add_column($table = '', $field = array(), $after_field = '')
00243 {
00244 if ($table == '')
00245 {
00246 show_error('A table name is required for that operation.');
00247 }
00248
00249
00250
00251 $this->add_field(array_slice($field, 0, 1));
00252
00253 if (count($this->fields) == 0)
00254 {
00255 show_error('Field information is required.');
00256 }
00257
00258 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
00259
00260 $this->_reset();
00261 return $this->db->query($sql);
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 function drop_column($table = '', $column_name = '')
00275 {
00276
00277 if ($table == '')
00278 {
00279 show_error('A table name is required for that operation.');
00280 }
00281
00282 if ($column_name == '')
00283 {
00284 show_error('A column name is required for that operation.');
00285 }
00286
00287 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
00288
00289 return $this->db->query($sql);
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 function modify_column($table = '', $field = array())
00304 {
00305
00306 if ($table == '')
00307 {
00308 show_error('A table name is required for that operation.');
00309 }
00310
00311
00312
00313 $this->add_field(array_slice($field, 0, 1));
00314
00315 if (count($this->fields) == 0)
00316 {
00317 show_error('Field information is required.');
00318 }
00319
00320 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
00321
00322 $this->_reset();
00323 return $this->db->query($sql);
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 function _reset()
00337 {
00338 $this->fields = array();
00339 $this->keys = array();
00340 $this->primary_keys = array();
00341 }
00342
00343 }
00344
00345
00346