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 (is_array($key))
00101 {
00102 foreach($key as $one)
00103 {
00104 $this->add_key($one, $primary);
00105 }
00106
00107 return;
00108 }
00109
00110 if ($key == '')
00111 {
00112 show_error('Key information is required for that operation.');
00113 }
00114
00115 if ($primary === TRUE)
00116 {
00117 $this->primary_keys[] = $key;
00118 }
00119 else
00120 {
00121 $this->keys[] = $key;
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 function add_field($field = '')
00135 {
00136 if ($field == '')
00137 {
00138 show_error('Field information is required.');
00139 }
00140
00141 if (is_string($field))
00142 {
00143 if ($field == 'id')
00144 {
00145 $this->add_field(array(
00146 'id' => array(
00147 'type' => 'INT',
00148 'constraint' => 9,
00149 'auto_increment' => TRUE
00150 )
00151 ));
00152 $this->add_key('id', TRUE);
00153 }
00154 else
00155 {
00156 if (strpos($field, ' ') === FALSE)
00157 {
00158 show_error('Field information is required for that operation.');
00159 }
00160
00161 $this->fields[] = $field;
00162 }
00163 }
00164
00165 if (is_array($field))
00166 {
00167 $this->fields = array_merge($this->fields, $field);
00168 }
00169
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 function create_table($table = '', $if_not_exists = FALSE)
00182 {
00183 if ($table == '')
00184 {
00185 show_error('A table name is required for that operation.');
00186 }
00187
00188 if (count($this->fields) == 0)
00189 {
00190 show_error('Field information is required.');
00191 }
00192
00193 $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
00194
00195 $this->_reset();
00196 return $this->db->query($sql);
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 function drop_table($table_name)
00209 {
00210 $sql = $this->_drop_table($this->db->dbprefix.$table_name);
00211
00212 if (is_bool($sql))
00213 {
00214 return $sql;
00215 }
00216
00217 return $this->db->query($sql);
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 function rename_table($table_name, $new_table_name)
00231 {
00232 if ($table_name == '' OR $new_table_name == '')
00233 {
00234 show_error('A table name is required for that operation.');
00235 }
00236
00237 $sql = $this->_rename_table($table_name, $new_table_name);
00238 return $this->db->query($sql);
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 function add_column($table = '', $field = array(), $after_field = '')
00253 {
00254 if ($table == '')
00255 {
00256 show_error('A table name is required for that operation.');
00257 }
00258
00259
00260
00261 $this->add_field(array_slice($field, 0, 1));
00262
00263 if (count($this->fields) == 0)
00264 {
00265 show_error('Field information is required.');
00266 }
00267
00268 $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
00269
00270 $this->_reset();
00271 return $this->db->query($sql);
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 function drop_column($table = '', $column_name = '')
00285 {
00286
00287 if ($table == '')
00288 {
00289 show_error('A table name is required for that operation.');
00290 }
00291
00292 if ($column_name == '')
00293 {
00294 show_error('A column name is required for that operation.');
00295 }
00296
00297 $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
00298
00299 return $this->db->query($sql);
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 function modify_column($table = '', $field = array())
00314 {
00315 if ($table == '')
00316 {
00317 show_error('A table name is required for that operation.');
00318 }
00319
00320
00321
00322 $this->add_field(array_slice($field, 0, 1));
00323
00324 if (count($this->fields) == 0)
00325 {
00326 show_error('Field information is required.');
00327 }
00328
00329 $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
00330
00331 $this->_reset();
00332 return $this->db->query($sql);
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 function _reset()
00346 {
00347 $this->fields = array();
00348 $this->keys = array();
00349 $this->primary_keys = array();
00350 }
00351
00352 }
00353
00354
00355