Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?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 ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Database Utility Class |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author ExpressionEngine Dev Team |
| 23 | * @link http://codeigniter.com/user_guide/database/ |
| 24 | */ |
| 25 | class CI_DB_forge { |
| 26 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 27 | var $fields = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | var $keys = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 29 | var $primary_keys = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 30 | var $db_char_set = ''; |
| 31 | |
| 32 | /** |
| 33 | * Constructor |
| 34 | * |
| 35 | * Grabs the CI super object instance so we can access it. |
| 36 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 37 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | 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); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 58 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | if (is_bool($sql)) |
| 60 | { |
| 61 | return $sql; |
| 62 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 79 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | if (is_bool($sql)) |
| 81 | { |
| 82 | return $sql; |
| 83 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 84 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 (is_array($key)) |
| 101 | { |
| 102 | foreach($key as $one) |
| 103 | { |
| 104 | $this->add_key($one, $primary); |
| 105 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 106 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | return; |
| 108 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 109 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | if ($key == '') |
| 111 | { |
| 112 | show_error('Key information is required for that operation.'); |
| 113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | if ($primary === TRUE) |
| 116 | { |
| 117 | $this->primary_keys[] = $key; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | $this->keys[] = $key; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // -------------------------------------------------------------------- |
| 126 | |
| 127 | /** |
| 128 | * Add Field |
| 129 | * |
| 130 | * @access public |
| 131 | * @param string collation |
| 132 | * @return void |
| 133 | */ |
| 134 | function add_field($field = '') |
| 135 | { |
| 136 | if ($field == '') |
| 137 | { |
| 138 | show_error('Field information is required.'); |
| 139 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 140 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | if (is_string($field)) |
| 142 | { |
| 143 | if ($field == 'id') |
| 144 | { |
| 145 | $this->add_field(array( |
| 146 | 'id' => array( |
| 147 | 'type' => 'INT', |
| 148 | 'constraint' => 9, |
| 149 | 'auto_increment' => TRUE |
| 150 | ) |
| 151 | )); |
| 152 | $this->add_key('id', TRUE); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | if (strpos($field, ' ') === FALSE) |
| 157 | { |
| 158 | show_error('Field information is required for that operation.'); |
| 159 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | $this->fields[] = $field; |
| 162 | } |
| 163 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | if (is_array($field)) |
| 166 | { |
| 167 | $this->fields = array_merge($this->fields, $field); |
| 168 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 169 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Create Table |
| 176 | * |
| 177 | * @access public |
| 178 | * @param string the table name |
| 179 | * @return bool |
| 180 | */ |
| 181 | function create_table($table = '', $if_not_exists = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 182 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | if ($table == '') |
| 184 | { |
| 185 | show_error('A table name is required for that operation.'); |
| 186 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 187 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | if (count($this->fields) == 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 189 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | show_error('Field information is required.'); |
| 191 | } |
| 192 | |
| 193 | $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | $this->_reset(); |
| 196 | return $this->db->query($sql); |
| 197 | } |
| 198 | |
| 199 | // -------------------------------------------------------------------- |
| 200 | |
| 201 | /** |
| 202 | * Drop Table |
| 203 | * |
| 204 | * @access public |
| 205 | * @param string the table name |
| 206 | * @return bool |
| 207 | */ |
| 208 | function drop_table($table_name) |
| 209 | { |
| 210 | $sql = $this->_drop_table($this->db->dbprefix.$table_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | if (is_bool($sql)) |
| 213 | { |
| 214 | return $sql; |
| 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | return $this->db->query($sql); |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Rename Table |
| 224 | * |
| 225 | * @access public |
| 226 | * @param string the old table name |
| 227 | * @param string the new table name |
| 228 | * @return bool |
| 229 | */ |
| 230 | function rename_table($table_name, $new_table_name) |
| 231 | { |
| 232 | if ($table_name == '' OR $new_table_name == '') |
| 233 | { |
| 234 | show_error('A table name is required for that operation.'); |
| 235 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | $sql = $this->_rename_table($table_name, $new_table_name); |
| 238 | return $this->db->query($sql); |
| 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * Column Add |
| 245 | * |
| 246 | * @access public |
| 247 | * @param string the table name |
| 248 | * @param string the column name |
| 249 | * @param string the column definition |
| 250 | * @return bool |
| 251 | */ |
| 252 | function add_column($table = '', $field = array(), $after_field = '') |
| 253 | { |
| 254 | if ($table == '') |
| 255 | { |
| 256 | show_error('A table name is required for that operation.'); |
| 257 | } |
| 258 | |
| 259 | // add field info into field array, but we can only do one at a time |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 260 | // so we cycle through |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 262 | foreach ($field as $k => $v) |
| 263 | { |
| 264 | $this->add_field(array($k => $field[$k])); |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 265 | |
| 266 | if (count($this->fields) == 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 267 | { |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 268 | show_error('Field information is required.'); |
| 269 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 270 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 271 | $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field); |
| 272 | |
| 273 | $this->_reset(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 274 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 275 | if ($this->db->query($sql) === FALSE) |
| 276 | { |
| 277 | return FALSE; |
| 278 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 280 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 281 | return TRUE; |
Derek Jones | 30b35eb | 2010-03-02 17:25:41 -0600 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
| 288 | * Column Drop |
| 289 | * |
| 290 | * @access public |
| 291 | * @param string the table name |
| 292 | * @param string the column name |
| 293 | * @return bool |
| 294 | */ |
| 295 | function drop_column($table = '', $column_name = '') |
| 296 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 297 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | if ($table == '') |
| 299 | { |
| 300 | show_error('A table name is required for that operation.'); |
| 301 | } |
| 302 | |
| 303 | if ($column_name == '') |
| 304 | { |
| 305 | show_error('A column name is required for that operation.'); |
| 306 | } |
| 307 | |
| 308 | $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | return $this->db->query($sql); |
| 311 | } |
| 312 | |
| 313 | // -------------------------------------------------------------------- |
| 314 | |
| 315 | /** |
| 316 | * Column Modify |
| 317 | * |
| 318 | * @access public |
| 319 | * @param string the table name |
| 320 | * @param string the column name |
| 321 | * @param string the column definition |
| 322 | * @return bool |
| 323 | */ |
| 324 | function modify_column($table = '', $field = array()) |
| 325 | { |
| 326 | if ($table == '') |
| 327 | { |
| 328 | show_error('A table name is required for that operation.'); |
| 329 | } |
| 330 | |
| 331 | // add field info into field array, but we can only do one at a time |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 332 | // so we cycle through |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 334 | foreach ($field as $k => $v) |
| 335 | { |
| 336 | $this->add_field(array($k => $field[$k])); |
| 337 | |
| 338 | if (count($this->fields) == 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 339 | { |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 340 | show_error('Field information is required.'); |
| 341 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 342 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 343 | $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields); |
| 344 | |
| 345 | $this->_reset(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 346 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 347 | if ($this->db->query($sql) === FALSE) |
| 348 | { |
| 349 | return FALSE; |
| 350 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 352 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 353 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // -------------------------------------------------------------------- |
| 357 | |
| 358 | /** |
| 359 | * Reset |
| 360 | * |
| 361 | * Resets table creation vars |
| 362 | * |
| 363 | * @access private |
| 364 | * @return void |
| 365 | */ |
| 366 | function _reset() |
| 367 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 368 | $this->fields = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | $this->keys = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 370 | $this->primary_keys = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | } |
| 374 | |
| 375 | /* End of file DB_forge.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 376 | /* Location: ./system/database/DB_forge.php */ |