Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 3 | * CodeIgniter |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 29 | * Database Forge Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 30 | * |
| 31 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 32 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
Timothy Warren | 833d504 | 2012-03-19 16:12:03 -0400 | [diff] [blame] | 35 | abstract class CI_DB_forge { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 37 | public $fields = array(); |
| 38 | public $keys = array(); |
| 39 | public $primary_keys = array(); |
| 40 | public $db_char_set = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 42 | // Platform specific SQL strings |
| 43 | protected $_create_database = 'CREATE DATABASE %s'; |
| 44 | protected $_drop_database = 'DROP DATABASE %s'; |
| 45 | protected $_drop_table = 'DROP TABLE IF EXISTS %s'; |
| 46 | protected $_rename_table = 'ALTER TABLE %s RENAME TO %s'; |
| 47 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 48 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | { |
| 50 | // Assign the main database object to $this->db |
| 51 | $CI =& get_instance(); |
| 52 | $this->db =& $CI->db; |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 53 | log_message('debug', 'Database Forge Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // -------------------------------------------------------------------- |
| 57 | |
| 58 | /** |
| 59 | * Create database |
| 60 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | * @param string the database name |
| 62 | * @return bool |
| 63 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 64 | public function create_database($db_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | { |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 66 | if ($this->_create_database === FALSE) |
| 67 | { |
| 68 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
| 69 | } |
| 70 | elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat))) |
| 71 | { |
| 72 | return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 73 | } |
| 74 | |
| 75 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // -------------------------------------------------------------------- |
| 79 | |
| 80 | /** |
| 81 | * Drop database |
| 82 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | * @param string the database name |
| 84 | * @return bool |
| 85 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 86 | public function drop_database($db_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 88 | if ($db_name === '') |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 89 | { |
| 90 | show_error('A table name is required for that operation.'); |
| 91 | return FALSE; |
| 92 | } |
| 93 | elseif ($this->_drop_database === FALSE) |
| 94 | { |
| 95 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
| 96 | } |
| 97 | elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name))) |
| 98 | { |
| 99 | return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 100 | } |
| 101 | |
| 102 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Add Key |
| 109 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | * @param string key |
| 111 | * @param string type |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 112 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 114 | public function add_key($key = '', $primary = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | { |
| 116 | if (is_array($key)) |
| 117 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 118 | foreach ($key as $one) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | { |
| 120 | $this->add_key($one, $primary); |
| 121 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 126 | if ($key === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | { |
| 128 | show_error('Key information is required for that operation.'); |
| 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | if ($primary === TRUE) |
| 132 | { |
| 133 | $this->primary_keys[] = $key; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | $this->keys[] = $key; |
| 138 | } |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 139 | |
| 140 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
| 146 | * Add Field |
| 147 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | * @param string collation |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 149 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 151 | public function add_field($field = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 153 | if ($field === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | { |
| 155 | show_error('Field information is required.'); |
| 156 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | if (is_string($field)) |
| 159 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 160 | if ($field === 'id') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | { |
| 162 | $this->add_field(array( |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 163 | 'id' => array( |
| 164 | 'type' => 'INT', |
| 165 | 'constraint' => 9, |
| 166 | 'auto_increment' => TRUE |
| 167 | ) |
| 168 | )); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | $this->add_key('id', TRUE); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | if (strpos($field, ' ') === FALSE) |
| 174 | { |
| 175 | show_error('Field information is required for that operation.'); |
| 176 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | $this->fields[] = $field; |
| 179 | } |
| 180 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | if (is_array($field)) |
| 183 | { |
| 184 | $this->fields = array_merge($this->fields, $field); |
| 185 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 186 | |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 187 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Create Table |
| 194 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | * @param string the table name |
| 196 | * @return bool |
| 197 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 198 | public function create_table($table = '', $if_not_exists = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 200 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | show_error('A table name is required for that operation.'); |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 205 | if (count($this->fields) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | show_error('Field information is required.'); |
| 208 | } |
| 209 | |
| 210 | $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | $this->_reset(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 212 | return is_bool($sql) ? $sql : $this->db->query($sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Drop Table |
| 219 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | * @param string the table name |
| 221 | * @return bool |
| 222 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 223 | public function drop_table($table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 225 | if ($table_name === '') |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 226 | { |
| 227 | return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE; |
| 228 | } |
| 229 | elseif ($this->_drop_table === FALSE) |
| 230 | { |
| 231 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
| 232 | } |
| 233 | |
| 234 | return $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
| 240 | * Rename Table |
| 241 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | * @param string the old table name |
| 243 | * @param string the new table name |
| 244 | * @return bool |
| 245 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 246 | public function rename_table($table_name, $new_table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 248 | if ($table_name === '' OR $new_table_name === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | { |
| 250 | show_error('A table name is required for that operation.'); |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 251 | return FALSE; |
| 252 | } |
| 253 | elseif ($this->_rename_table === FALSE) |
| 254 | { |
| 255 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 258 | return $this->db->query(sprintf($this->_rename_table, |
| 259 | $this->db->escape_identifiers($this->db->dbprefix.$table_name), |
| 260 | $this->db->escape_identifiers($this->db->dbprefix.$new_table_name)) |
| 261 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Column Add |
| 268 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | * @param string the table name |
| 270 | * @param string the column name |
| 271 | * @param string the column definition |
| 272 | * @return bool |
| 273 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 274 | public function add_column($table = '', $field = array(), $after_field = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 276 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | { |
| 278 | show_error('A table name is required for that operation.'); |
| 279 | } |
| 280 | |
| 281 | // 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] | 282 | // so we cycle through |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 283 | foreach (array_keys($field) as $k) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | { |
| 285 | $this->add_field(array($k => $field[$k])); |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 286 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 287 | if (count($this->fields) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | { |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 289 | show_error('Field information is required.'); |
| 290 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 292 | $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field); |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 293 | $this->_reset(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 295 | if ($this->db->query($sql) === FALSE) |
| 296 | { |
| 297 | return FALSE; |
| 298 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 300 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 301 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // -------------------------------------------------------------------- |
| 305 | |
| 306 | /** |
| 307 | * Column Drop |
| 308 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | * @param string the table name |
| 310 | * @param string the column name |
| 311 | * @return bool |
| 312 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 313 | public function drop_column($table = '', $column_name = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 315 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | { |
| 317 | show_error('A table name is required for that operation.'); |
| 318 | } |
| 319 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 320 | if ($column_name === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | { |
| 322 | show_error('A column name is required for that operation.'); |
| 323 | } |
| 324 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 325 | return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // -------------------------------------------------------------------- |
| 329 | |
| 330 | /** |
| 331 | * Column Modify |
| 332 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | * @param string the table name |
| 334 | * @param string the column name |
| 335 | * @param string the column definition |
| 336 | * @return bool |
| 337 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 338 | public function modify_column($table = '', $field = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 340 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
| 342 | show_error('A table name is required for that operation.'); |
| 343 | } |
| 344 | |
| 345 | // 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] | 346 | // so we cycle through |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 347 | foreach (array_keys($field) as $k) |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 348 | { |
Phil Sturgeon | a58ecae | 2010-12-15 10:32:10 +0000 | [diff] [blame] | 349 | // If no name provided, use the current name |
| 350 | if ( ! isset($field[$k]['name'])) |
| 351 | { |
| 352 | $field[$k]['name'] = $k; |
| 353 | } |
| 354 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 355 | $this->add_field(array($k => $field[$k])); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 356 | if (count($this->fields) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | { |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 358 | show_error('Field information is required.'); |
| 359 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 361 | $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields); |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 362 | $this->_reset(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 363 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 364 | if ($this->db->query($sql) === FALSE) |
| 365 | { |
| 366 | return FALSE; |
| 367 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 370 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // -------------------------------------------------------------------- |
| 374 | |
| 375 | /** |
| 376 | * Reset |
| 377 | * |
| 378 | * Resets table creation vars |
| 379 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | * @return void |
| 381 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 382 | protected function _reset() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 384 | $this->fields = $this->keys = $this->primary_keys = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | } |
| 388 | |
| 389 | /* End of file DB_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 390 | /* Location: ./system/database/DB_forge.php */ |