Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014, British Columbia Institute of Technology |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 41 | * Database Forge Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
| 43 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 44 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | * @link http://codeigniter.com/user_guide/database/ |
| 46 | */ |
Timothy Warren | 833d504 | 2012-03-19 16:12:03 -0400 | [diff] [blame] | 47 | abstract class CI_DB_forge { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 49 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 50 | * Database object |
| 51 | * |
| 52 | * @var object |
| 53 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 54 | protected $db; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 55 | |
| 56 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 57 | * Fields data |
| 58 | * |
| 59 | * @var array |
| 60 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 61 | public $fields = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Keys data |
| 65 | * |
| 66 | * @var array |
| 67 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 68 | public $keys = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Primary Keys data |
| 72 | * |
| 73 | * @var array |
| 74 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 75 | public $primary_keys = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Database character set |
| 79 | * |
| 80 | * @var string |
| 81 | */ |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 82 | public $db_char_set = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * CREATE DATABASE statement |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 91 | protected $_create_database = 'CREATE DATABASE %s'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * DROP DATABASE statement |
| 95 | * |
| 96 | * @var string |
| 97 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 98 | protected $_drop_database = 'DROP DATABASE %s'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 101 | * CREATE TABLE statement |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 102 | * |
| 103 | * @var string |
| 104 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 105 | protected $_create_table = "%s %s (%s\n)"; |
| 106 | |
| 107 | /** |
| 108 | * CREATE TABLE IF statement |
| 109 | * |
| 110 | * @var string |
| 111 | */ |
| 112 | protected $_create_table_if = 'CREATE TABLE IF NOT EXISTS'; |
| 113 | |
| 114 | /** |
| 115 | * CREATE TABLE keys flag |
| 116 | * |
| 117 | * Whether table keys are created from within the |
| 118 | * CREATE TABLE statement. |
| 119 | * |
| 120 | * @var bool |
| 121 | */ |
| 122 | protected $_create_table_keys = FALSE; |
| 123 | |
| 124 | /** |
| 125 | * DROP TABLE IF EXISTS statement |
| 126 | * |
| 127 | * @var string |
| 128 | */ |
| 129 | protected $_drop_table_if = 'DROP TABLE IF EXISTS'; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * RENAME TABLE statement |
| 133 | * |
| 134 | * @var string |
| 135 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 136 | protected $_rename_table = 'ALTER TABLE %s RENAME TO %s;'; |
| 137 | |
| 138 | /** |
| 139 | * UNSIGNED support |
| 140 | * |
| 141 | * @var bool|array |
| 142 | */ |
| 143 | protected $_unsigned = TRUE; |
| 144 | |
| 145 | /** |
| 146 | * NULL value representatin in CREATE/ALTER TABLE statements |
| 147 | * |
| 148 | * @var string |
| 149 | */ |
| 150 | protected $_null = ''; |
| 151 | |
| 152 | /** |
| 153 | * DEFAULT value representation in CREATE/ALTER TABLE statements |
| 154 | * |
| 155 | * @var string |
| 156 | */ |
| 157 | protected $_default = ' DEFAULT '; |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 158 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 159 | // -------------------------------------------------------------------- |
| 160 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 161 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 162 | * Class constructor |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 163 | * |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 164 | * @param object &$db Database object |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 165 | * @return void |
| 166 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 167 | public function __construct(&$db) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 169 | $this->db =& $db; |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 170 | log_message('debug', 'Database Forge Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Create database |
| 177 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 178 | * @param string $db_name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | * @return bool |
| 180 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 181 | public function create_database($db_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | { |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 183 | if ($this->_create_database === FALSE) |
| 184 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 185 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 186 | } |
| 187 | elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat))) |
| 188 | { |
| 189 | return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 190 | } |
| 191 | |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 192 | if ( ! empty($this->db->data_cache['db_names'])) |
| 193 | { |
| 194 | $this->db->data_cache['db_names'][] = $db_name; |
| 195 | } |
| 196 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 197 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // -------------------------------------------------------------------- |
| 201 | |
| 202 | /** |
| 203 | * Drop database |
| 204 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 205 | * @param string $db_name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | * @return bool |
| 207 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 208 | public function drop_database($db_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 210 | if ($db_name === '') |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 211 | { |
| 212 | show_error('A table name is required for that operation.'); |
| 213 | return FALSE; |
| 214 | } |
| 215 | elseif ($this->_drop_database === FALSE) |
| 216 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 217 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 218 | } |
| 219 | elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name))) |
| 220 | { |
| 221 | return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 222 | } |
| 223 | |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 224 | if ( ! empty($this->db->data_cache['db_names'])) |
| 225 | { |
| 226 | $key = array_search(strtolower($db_name), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); |
| 227 | if ($key !== FALSE) |
| 228 | { |
| 229 | unset($this->db->data_cache['db_names'][$key]); |
| 230 | } |
| 231 | } |
| 232 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 233 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Add Key |
| 240 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 241 | * @param string $key |
| 242 | * @param bool $primary |
Andrey Andreev | d8dba5d | 2012-12-17 15:42:01 +0200 | [diff] [blame] | 243 | * @return CI_DB_forge |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 245 | public function add_key($key = '', $primary = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 247 | if (empty($key)) |
| 248 | { |
| 249 | show_error('Key information is required for that operation.'); |
| 250 | } |
| 251 | |
Andrey Andreev | d743cdb | 2012-11-06 00:00:01 +0200 | [diff] [blame] | 252 | if ($primary === TRUE && is_array($key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 254 | foreach ($key as $one) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
| 256 | $this->add_key($one, $primary); |
| 257 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 258 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 259 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | if ($primary === TRUE) |
| 263 | { |
| 264 | $this->primary_keys[] = $key; |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | $this->keys[] = $key; |
| 269 | } |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 270 | |
| 271 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // -------------------------------------------------------------------- |
| 275 | |
| 276 | /** |
| 277 | * Add Field |
| 278 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 279 | * @param array $field |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 280 | * @return CI_DB_forge |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 282 | public function add_field($field = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 284 | if (empty($field)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | show_error('Field information is required.'); |
| 287 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | if (is_string($field)) |
| 290 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 291 | if ($field === 'id') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
| 293 | $this->add_field(array( |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 294 | 'id' => array( |
| 295 | 'type' => 'INT', |
| 296 | 'constraint' => 9, |
| 297 | 'auto_increment' => TRUE |
| 298 | ) |
| 299 | )); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | $this->add_key('id', TRUE); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | if (strpos($field, ' ') === FALSE) |
| 305 | { |
| 306 | show_error('Field information is required for that operation.'); |
| 307 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | $this->fields[] = $field; |
| 310 | } |
| 311 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | if (is_array($field)) |
| 314 | { |
| 315 | $this->fields = array_merge($this->fields, $field); |
| 316 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 317 | |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 318 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // -------------------------------------------------------------------- |
| 322 | |
| 323 | /** |
| 324 | * Create Table |
| 325 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 326 | * @param string $table Table name |
| 327 | * @param bool $if_not_exists Whether to add IF NOT EXISTS condition |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 328 | * @param array $attributes Associative array of table attributes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | * @return bool |
| 330 | */ |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 331 | public function create_table($table = '', $if_not_exists = FALSE, array $attributes = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 333 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | { |
| 335 | show_error('A table name is required for that operation.'); |
| 336 | } |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 337 | else |
| 338 | { |
| 339 | $table = $this->db->dbprefix.$table; |
| 340 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 342 | if (count($this->fields) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | show_error('Field information is required.'); |
| 345 | } |
| 346 | |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 347 | $sql = $this->_create_table($table, $if_not_exists, $attributes); |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 348 | |
| 349 | if (is_bool($sql)) |
| 350 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 351 | $this->_reset(); |
| 352 | if ($sql === FALSE) |
| 353 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 354 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 355 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 356 | } |
| 357 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 358 | if (($result = $this->db->query($sql)) !== FALSE) |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 359 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 360 | empty($this->db->data_cache['table_names']) OR $this->db->data_cache['table_names'][] = $table; |
| 361 | |
| 362 | // Most databases don't support creating indexes from within the CREATE TABLE statement |
| 363 | if ( ! empty($this->keys)) |
| 364 | { |
| 365 | for ($i = 0, $sqls = $this->_process_indexes($table), $c = count($sqls); $i < $c; $i++) |
| 366 | { |
| 367 | $this->db->query($sqls[$i]); |
| 368 | } |
| 369 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 370 | } |
| 371 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 372 | $this->_reset(); |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 373 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // -------------------------------------------------------------------- |
| 377 | |
| 378 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 379 | * Create Table |
| 380 | * |
| 381 | * @param string $table Table name |
| 382 | * @param bool $if_not_exists Whether to add 'IF NOT EXISTS' condition |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 383 | * @param array $attributes Associative array of table attributes |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 384 | * @return mixed |
| 385 | */ |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 386 | protected function _create_table($table, $if_not_exists, $attributes) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 387 | { |
| 388 | if ($if_not_exists === TRUE && $this->_create_table_if === FALSE) |
| 389 | { |
| 390 | if ($this->db->table_exists($table)) |
| 391 | { |
| 392 | return TRUE; |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | $if_not_exists = FALSE; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | $sql = ($if_not_exists) |
| 401 | ? sprintf($this->_create_table_if, $this->db->escape_identifiers($table)) |
| 402 | : 'CREATE TABLE'; |
| 403 | |
| 404 | $columns = $this->_process_fields(TRUE); |
| 405 | for ($i = 0, $c = count($columns); $i < $c; $i++) |
| 406 | { |
| 407 | $columns[$i] = ($columns[$i]['_literal'] !== FALSE) |
| 408 | ? "\n\t".$columns[$i]['_literal'] |
| 409 | : "\n\t".$this->_process_column($columns[$i]); |
| 410 | } |
| 411 | |
| 412 | $columns = implode(',', $columns) |
| 413 | .$this->_process_primary_keys($table); |
| 414 | |
| 415 | // Are indexes created from within the CREATE TABLE statement? (e.g. in MySQL) |
| 416 | if ($this->_create_table_keys === TRUE) |
| 417 | { |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame] | 418 | $columns .= $this->_process_indexes($table); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // _create_table will usually have the following format: "%s %s (%s\n)" |
Andrey Andreev | aaca5cb | 2014-03-31 17:20:55 +0300 | [diff] [blame] | 422 | $sql = sprintf($this->_create_table.'%s', |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 423 | $sql, |
| 424 | $this->db->escape_identifiers($table), |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 425 | $columns, |
| 426 | $this->_create_table_attr($attributes) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 427 | ); |
| 428 | |
| 429 | return $sql; |
| 430 | } |
| 431 | |
| 432 | // -------------------------------------------------------------------- |
| 433 | |
| 434 | /** |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 435 | * CREATE TABLE attributes |
| 436 | * |
| 437 | * @param array $attributes Associative array of table attributes |
| 438 | * @return string |
| 439 | */ |
| 440 | protected function _create_table_attr($attributes) |
| 441 | { |
| 442 | $sql = ''; |
| 443 | |
| 444 | foreach (array_keys($attributes) as $key) |
| 445 | { |
| 446 | if (is_string($key)) |
| 447 | { |
| 448 | $sql .= ' '.strtoupper($key).' '.$attributes[$key]; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return $sql; |
| 453 | } |
| 454 | |
| 455 | // -------------------------------------------------------------------- |
| 456 | |
| 457 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | * Drop Table |
| 459 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 460 | * @param string $table_name Table name |
| 461 | * @param bool $if_exists Whether to add an IF EXISTS condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | * @return bool |
| 463 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 464 | public function drop_table($table_name, $if_exists = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 466 | if ($table_name === '') |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 467 | { |
| 468 | return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE; |
| 469 | } |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 470 | |
| 471 | $query = $this->_drop_table($this->db->dbprefix.$table_name, $if_exists); |
| 472 | if ($query === FALSE) |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 473 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 474 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 475 | } |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 476 | elseif ($query === TRUE) |
| 477 | { |
| 478 | return TRUE; |
| 479 | } |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 480 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 481 | $query = $this->db->query($query); |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 482 | |
| 483 | // Update table list cache |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 484 | if ($query && ! empty($this->db->data_cache['table_names'])) |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 485 | { |
| 486 | $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE); |
| 487 | if ($key !== FALSE) |
| 488 | { |
| 489 | unset($this->db->data_cache['table_names'][$key]); |
| 490 | } |
| 491 | } |
| 492 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 493 | return $query; |
| 494 | } |
| 495 | |
| 496 | // -------------------------------------------------------------------- |
| 497 | |
| 498 | /** |
| 499 | * Drop Table |
| 500 | * |
| 501 | * Generates a platform-specific DROP TABLE string |
| 502 | * |
| 503 | * @param string $table Table name |
| 504 | * @param bool $if_exists Whether to add an IF EXISTS condition |
| 505 | * @return string |
| 506 | */ |
| 507 | protected function _drop_table($table, $if_exists) |
| 508 | { |
| 509 | $sql = 'DROP TABLE'; |
| 510 | |
| 511 | if ($if_exists) |
| 512 | { |
| 513 | if ($this->_drop_table_if === FALSE) |
| 514 | { |
| 515 | if ( ! $this->db->table_exists($table)) |
| 516 | { |
| 517 | return TRUE; |
| 518 | } |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | $sql = sprintf($this->_drop_table_if, $this->db->escape_identifiers($table)); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return $sql.' '.$this->db->escape_identifiers($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // -------------------------------------------------------------------- |
| 530 | |
| 531 | /** |
| 532 | * Rename Table |
| 533 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 534 | * @param string $table_name Old table name |
| 535 | * @param string $new_table_name New table name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | * @return bool |
| 537 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 538 | public function rename_table($table_name, $new_table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 540 | if ($table_name === '' OR $new_table_name === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | { |
| 542 | show_error('A table name is required for that operation.'); |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 543 | return FALSE; |
| 544 | } |
| 545 | elseif ($this->_rename_table === FALSE) |
| 546 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 547 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 549 | |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 550 | $result = $this->db->query(sprintf($this->_rename_table, |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 551 | $this->db->escape_identifiers($this->db->dbprefix.$table_name), |
| 552 | $this->db->escape_identifiers($this->db->dbprefix.$new_table_name)) |
| 553 | ); |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 554 | |
| 555 | if ($result && ! empty($this->db->data_cache['table_names'])) |
| 556 | { |
| 557 | $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE); |
| 558 | if ($key !== FALSE) |
| 559 | { |
| 560 | $this->db->data_cache['table_names'][$key] = $this->db->dbprefix.$new_table_name; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | // -------------------------------------------------------------------- |
| 568 | |
| 569 | /** |
| 570 | * Column Add |
| 571 | * |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 572 | * @todo Remove deprecated $_after option in 3.1+ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 573 | * @param string $table Table name |
| 574 | * @param array $field Column definition |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 575 | * @param string $_after Column for AFTER clause (deprecated) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | * @return bool |
| 577 | */ |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 578 | public function add_column($table = '', $field = array(), $_after = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 579 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 580 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | { |
| 582 | show_error('A table name is required for that operation.'); |
| 583 | } |
| 584 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 585 | // Work-around for literal column definitions |
| 586 | if ( ! is_array($field)) |
| 587 | { |
| 588 | $field = array($field); |
| 589 | } |
| 590 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 591 | foreach (array_keys($field) as $k) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 592 | { |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 593 | // Backwards-compatibility work-around for MySQL/CUBRID AFTER clause (remove in 3.1+) |
| 594 | if ($_after !== NULL && is_array($field[$k]) && ! isset($field[$k]['after'])) |
| 595 | { |
| 596 | $field[$k]['after'] = $_after; |
| 597 | } |
| 598 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 599 | $this->add_field(array($k => $field[$k])); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 600 | } |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 601 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 602 | $sqls = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->_process_fields()); |
| 603 | $this->_reset(); |
| 604 | if ($sqls === FALSE) |
| 605 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 606 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 607 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 608 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 609 | for ($i = 0, $c = count($sqls); $i < $c; $i++) |
| 610 | { |
Andrey Andreev | 137a742 | 2012-11-05 23:46:44 +0200 | [diff] [blame] | 611 | if ($this->db->query($sqls[$i]) === FALSE) |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 612 | { |
| 613 | return FALSE; |
| 614 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 617 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // -------------------------------------------------------------------- |
| 621 | |
| 622 | /** |
| 623 | * Column Drop |
| 624 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 625 | * @param string $table Table name |
| 626 | * @param string $column_name Column name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | * @return bool |
| 628 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 629 | public function drop_column($table = '', $column_name = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 631 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | { |
| 633 | show_error('A table name is required for that operation.'); |
| 634 | } |
| 635 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 636 | if ($column_name === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | { |
| 638 | show_error('A column name is required for that operation.'); |
| 639 | } |
| 640 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 641 | $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name); |
| 642 | if ($sql === FALSE) |
| 643 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 644 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | return $this->db->query($sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // -------------------------------------------------------------------- |
| 651 | |
| 652 | /** |
| 653 | * Column Modify |
| 654 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 655 | * @param string $table Table name |
| 656 | * @param string $field Column definition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | * @return bool |
| 658 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 659 | public function modify_column($table = '', $field = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 661 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | { |
| 663 | show_error('A table name is required for that operation.'); |
| 664 | } |
| 665 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 666 | // Work-around for literal column definitions |
| 667 | if ( ! is_array($field)) |
| 668 | { |
| 669 | $field = array($field); |
| 670 | } |
| 671 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 672 | foreach (array_keys($field) as $k) |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 673 | { |
| 674 | $this->add_field(array($k => $field[$k])); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 675 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 676 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 677 | if (count($this->fields) === 0) |
| 678 | { |
| 679 | show_error('Field information is required.'); |
| 680 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 681 | |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 682 | $sqls = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->_process_fields()); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 683 | $this->_reset(); |
| 684 | if ($sqls === FALSE) |
| 685 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 686 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | for ($i = 0, $c = count($sqls); $i < $c; $i++) |
| 690 | { |
Andrey Andreev | 137a742 | 2012-11-05 23:46:44 +0200 | [diff] [blame] | 691 | if ($this->db->query($sqls[$i]) === FALSE) |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 692 | { |
| 693 | return FALSE; |
| 694 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 695 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 696 | |
Robin Sowell | 8a54ef2 | 2009-03-04 14:49:53 +0000 | [diff] [blame] | 697 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | // -------------------------------------------------------------------- |
| 701 | |
| 702 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 703 | * ALTER TABLE |
| 704 | * |
| 705 | * @param string $alter_type ALTER type |
| 706 | * @param string $table Table name |
| 707 | * @param mixed $field Column definition |
| 708 | * @return string|string[] |
| 709 | */ |
| 710 | protected function _alter_table($alter_type, $table, $field) |
| 711 | { |
| 712 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '; |
| 713 | |
| 714 | // DROP has everything it needs now. |
| 715 | if ($alter_type === 'DROP') |
| 716 | { |
| 717 | return $sql.'DROP COLUMN '.$this->db->escape_identifiers($field); |
| 718 | } |
| 719 | |
Andrey Andreev | 13f6eab | 2013-03-15 10:56:55 +0200 | [diff] [blame] | 720 | $sql .= ($alter_type === 'ADD') |
| 721 | ? 'ADD ' |
| 722 | : $alter_type.' COLUMN '; |
| 723 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 724 | $sqls = array(); |
Andrey Andreev | 13f6eab | 2013-03-15 10:56:55 +0200 | [diff] [blame] | 725 | for ($i = 0, $c = count($field); $i < $c; $i++) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 726 | { |
| 727 | $sqls[] = $sql |
| 728 | .($field[$i]['_literal'] !== FALSE ? $field[$i]['_literal'] : $this->_process_column($field[$i])); |
| 729 | } |
| 730 | |
| 731 | return $sqls; |
| 732 | } |
| 733 | |
| 734 | // -------------------------------------------------------------------- |
| 735 | |
| 736 | /** |
| 737 | * Process fields |
| 738 | * |
| 739 | * @param bool $create_table |
| 740 | * @return array |
| 741 | */ |
| 742 | protected function _process_fields($create_table = FALSE) |
| 743 | { |
| 744 | $fields = array(); |
| 745 | |
| 746 | foreach ($this->fields as $key => $attributes) |
| 747 | { |
| 748 | if (is_int($key) && ! is_array($attributes)) |
| 749 | { |
| 750 | $fields[] = array('_literal' => $attributes); |
| 751 | continue; |
| 752 | } |
| 753 | |
| 754 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 755 | |
| 756 | if ($create_table === TRUE && empty($attributes['TYPE'])) |
| 757 | { |
| 758 | continue; |
| 759 | } |
| 760 | |
Andrey Andreev | 1394304 | 2014-03-17 11:50:45 +0200 | [diff] [blame] | 761 | isset($attributes['TYPE']) && $this->_attr_type($attributes); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 762 | |
| 763 | $field = array( |
Andrey Andreev | 1394304 | 2014-03-17 11:50:45 +0200 | [diff] [blame] | 764 | 'name' => $key, |
| 765 | 'new_name' => isset($attributes['NAME']) ? $attributes['NAME'] : NULL, |
| 766 | 'type' => isset($attributes['TYPE']) ? $attributes['TYPE'] : NULL, |
| 767 | 'length' => '', |
| 768 | 'unsigned' => '', |
| 769 | 'null' => '', |
| 770 | 'unique' => '', |
| 771 | 'default' => '', |
| 772 | 'auto_increment' => '', |
| 773 | '_literal' => FALSE |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 774 | ); |
| 775 | |
Andrey Andreev | 1394304 | 2014-03-17 11:50:45 +0200 | [diff] [blame] | 776 | isset($attributes['TYPE']) && $this->_attr_unsigned($attributes, $field); |
| 777 | |
Andrey Andreev | 5d69a6e | 2013-10-28 15:34:47 +0200 | [diff] [blame] | 778 | if ($create_table === FALSE) |
| 779 | { |
| 780 | if (isset($attributes['AFTER'])) |
| 781 | { |
Andrey Andreev | 96185a3 | 2013-10-28 16:04:02 +0200 | [diff] [blame] | 782 | $field['after'] = $attributes['AFTER']; |
Andrey Andreev | 5d69a6e | 2013-10-28 15:34:47 +0200 | [diff] [blame] | 783 | } |
| 784 | elseif (isset($attributes['FIRST'])) |
| 785 | { |
| 786 | $field['first'] = (bool) $attributes['FIRST']; |
| 787 | } |
| 788 | } |
| 789 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 790 | $this->_attr_default($attributes, $field); |
| 791 | |
| 792 | if (isset($attributes['NULL'])) |
| 793 | { |
| 794 | if ($attributes['NULL'] === TRUE) |
| 795 | { |
| 796 | $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; |
| 797 | } |
Andrey Andreev | 5d69a6e | 2013-10-28 15:34:47 +0200 | [diff] [blame] | 798 | else |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 799 | { |
| 800 | $field['null'] = ' NOT NULL'; |
| 801 | } |
| 802 | } |
Andrey Andreev | 5d69a6e | 2013-10-28 15:34:47 +0200 | [diff] [blame] | 803 | elseif ($create_table === TRUE) |
| 804 | { |
| 805 | $field['null'] = ' NOT NULL'; |
| 806 | } |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 807 | |
| 808 | $this->_attr_auto_increment($attributes, $field); |
| 809 | $this->_attr_unique($attributes, $field); |
Zachary Flower | e4b10bf | 2014-11-03 10:42:57 -0700 | [diff] [blame^] | 810 | |
| 811 | if (isset($attributes['COMMENT'])) |
| 812 | { |
| 813 | $field['comment'] = $this->db->escape($attributes['COMMENT']); |
| 814 | } |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 815 | |
| 816 | if (isset($attributes['TYPE']) && ! empty($attributes['CONSTRAINT'])) |
| 817 | { |
| 818 | switch (strtoupper($attributes['TYPE'])) |
| 819 | { |
| 820 | case 'ENUM': |
| 821 | case 'SET': |
| 822 | $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); |
Andrey Andreev | c5a0af2 | 2014-03-10 10:29:43 +0200 | [diff] [blame] | 823 | $field['length'] = is_array($attributes['CONSTRAINT']) |
| 824 | ? "('".implode("','", $attributes['CONSTRAINT'])."')" |
| 825 | : '('.$attributes['CONSTRAINT'].')'; |
Andrey Andreev | fde170c | 2014-03-10 19:55:11 +0200 | [diff] [blame] | 826 | break; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 827 | default: |
| 828 | $field['length'] = is_array($attributes['CONSTRAINT']) |
Andrey Andreev | c5a0af2 | 2014-03-10 10:29:43 +0200 | [diff] [blame] | 829 | ? '('.implode(',', $attributes['CONSTRAINT']).')' |
| 830 | : '('.$attributes['CONSTRAINT'].')'; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | $fields[] = $field; |
| 836 | } |
| 837 | |
| 838 | return $fields; |
| 839 | } |
| 840 | |
| 841 | // -------------------------------------------------------------------- |
| 842 | |
| 843 | /** |
| 844 | * Process column |
| 845 | * |
| 846 | * @param array $field |
| 847 | * @return string |
| 848 | */ |
| 849 | protected function _process_column($field) |
| 850 | { |
| 851 | return $this->db->escape_identifiers($field['name']) |
| 852 | .' '.$field['type'].$field['length'] |
| 853 | .$field['unsigned'] |
| 854 | .$field['default'] |
| 855 | .$field['null'] |
| 856 | .$field['auto_increment'] |
Zachary Flower | e4b10bf | 2014-11-03 10:42:57 -0700 | [diff] [blame^] | 857 | .$field['unique']; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | // -------------------------------------------------------------------- |
| 861 | |
| 862 | /** |
| 863 | * Field attribute TYPE |
| 864 | * |
| 865 | * Performs a data type mapping between different databases. |
| 866 | * |
| 867 | * @param array &$attributes |
| 868 | * @return void |
| 869 | */ |
| 870 | protected function _attr_type(&$attributes) |
| 871 | { |
| 872 | // Usually overriden by drivers |
| 873 | } |
| 874 | |
| 875 | // -------------------------------------------------------------------- |
| 876 | |
| 877 | /** |
| 878 | * Field attribute UNSIGNED |
| 879 | * |
| 880 | * Depending on the _unsigned property value: |
| 881 | * |
| 882 | * - TRUE will always set $field['unsigned'] to 'UNSIGNED' |
| 883 | * - FALSE will always set $field['unsigned'] to '' |
| 884 | * - array(TYPE) will set $field['unsigned'] to 'UNSIGNED', |
| 885 | * if $attributes['TYPE'] is found in the array |
| 886 | * - array(TYPE => UTYPE) will change $field['type'], |
| 887 | * from TYPE to UTYPE in case of a match |
| 888 | * |
| 889 | * @param array &$attributes |
| 890 | * @param array &$field |
| 891 | * @return void |
| 892 | */ |
| 893 | protected function _attr_unsigned(&$attributes, &$field) |
| 894 | { |
| 895 | if (empty($attributes['UNSIGNED']) OR $attributes['UNSIGNED'] !== TRUE) |
| 896 | { |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | // Reset the attribute in order to avoid issues if we do type conversion |
| 901 | $attributes['UNSIGNED'] = FALSE; |
| 902 | |
| 903 | if (is_array($this->_unsigned)) |
| 904 | { |
| 905 | foreach (array_keys($this->_unsigned) as $key) |
| 906 | { |
| 907 | if (is_int($key) && strcasecmp($attributes['TYPE'], $this->_unsigned[$key]) === 0) |
| 908 | { |
| 909 | $field['unsigned'] = ' UNSIGNED'; |
| 910 | return; |
| 911 | } |
| 912 | elseif (is_string($key) && strcasecmp($attributes['TYPE'], $key) === 0) |
| 913 | { |
| 914 | $field['type'] = $key; |
| 915 | return; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | $field['unsigned'] = ($this->_unsigned === TRUE) ? ' UNSIGNED' : ''; |
| 923 | } |
| 924 | |
| 925 | // -------------------------------------------------------------------- |
| 926 | |
| 927 | /** |
| 928 | * Field attribute DEFAULT |
| 929 | * |
| 930 | * @param array &$attributes |
| 931 | * @param array &$field |
| 932 | * @return void |
| 933 | */ |
| 934 | protected function _attr_default(&$attributes, &$field) |
| 935 | { |
| 936 | if ($this->_default === FALSE) |
| 937 | { |
| 938 | return; |
| 939 | } |
| 940 | |
| 941 | if (array_key_exists('DEFAULT', $attributes)) |
| 942 | { |
| 943 | if ($attributes['DEFAULT'] === NULL) |
| 944 | { |
| 945 | $field['default'] = empty($this->_null) ? '' : $this->_default.$this->_null; |
| 946 | |
| 947 | // Override the NULL attribute if that's our default |
Andrey Andreev | 22ce276 | 2014-08-15 11:59:16 +0300 | [diff] [blame] | 948 | $attributes['NULL'] = TRUE; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 949 | $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; |
| 950 | } |
| 951 | else |
| 952 | { |
| 953 | $field['default'] = $this->_default.$this->db->escape($attributes['DEFAULT']); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // -------------------------------------------------------------------- |
| 959 | |
| 960 | /** |
| 961 | * Field attribute UNIQUE |
| 962 | * |
| 963 | * @param array &$attributes |
| 964 | * @param array &$field |
| 965 | * @return void |
| 966 | */ |
| 967 | protected function _attr_unique(&$attributes, &$field) |
| 968 | { |
| 969 | if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) |
| 970 | { |
| 971 | $field['unique'] = ' UNIQUE'; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // -------------------------------------------------------------------- |
| 976 | |
| 977 | /** |
| 978 | * Field attribute AUTO_INCREMENT |
| 979 | * |
| 980 | * @param array &$attributes |
| 981 | * @param array &$field |
| 982 | * @return void |
| 983 | */ |
| 984 | protected function _attr_auto_increment(&$attributes, &$field) |
| 985 | { |
| 986 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) |
| 987 | { |
| 988 | $field['auto_increment'] = ' AUTO_INCREMENT'; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | // -------------------------------------------------------------------- |
| 993 | |
| 994 | /** |
| 995 | * Process primary keys |
| 996 | * |
| 997 | * @param string $table Table name |
| 998 | * @return string |
| 999 | */ |
| 1000 | protected function _process_primary_keys($table) |
| 1001 | { |
| 1002 | $sql = ''; |
| 1003 | |
| 1004 | for ($i = 0, $c = count($this->primary_keys); $i < $c; $i++) |
| 1005 | { |
| 1006 | if ( ! isset($this->fields[$this->primary_keys[$i]])) |
| 1007 | { |
| 1008 | unset($this->primary_keys[$i]); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | if (count($this->primary_keys) > 0) |
| 1013 | { |
| 1014 | $sql .= ",\n\tCONSTRAINT ".$this->db->escape_identifiers('pk_'.$table) |
| 1015 | .' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($this->primary_keys)).')'; |
| 1016 | } |
| 1017 | |
| 1018 | return $sql; |
| 1019 | } |
| 1020 | |
| 1021 | // -------------------------------------------------------------------- |
| 1022 | |
| 1023 | /** |
| 1024 | * Process indexes |
| 1025 | * |
| 1026 | * @param string $table |
| 1027 | * @return string |
| 1028 | */ |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame] | 1029 | protected function _process_indexes($table) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 1030 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 1031 | $sqls = array(); |
| 1032 | |
| 1033 | for ($i = 0, $c = count($this->keys); $i < $c; $i++) |
| 1034 | { |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame] | 1035 | if (is_array($this->keys[$i])) |
| 1036 | { |
| 1037 | for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++) |
| 1038 | { |
| 1039 | if ( ! isset($this->fields[$this->keys[$i][$i2]])) |
| 1040 | { |
| 1041 | unset($this->keys[$i][$i2]); |
| 1042 | continue; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | elseif ( ! isset($this->fields[$this->keys[$i]])) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 1047 | { |
| 1048 | unset($this->keys[$i]); |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
| 1052 | is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]); |
| 1053 | |
mjnaderi | d3a6ca2 | 2013-12-19 01:35:57 +0330 | [diff] [blame] | 1054 | $sqls[] = 'CREATE INDEX '.$this->db->escape_identifiers($table.'_'.implode('_', $this->keys[$i])) |
| 1055 | .' ON '.$this->db->escape_identifiers($table) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 1056 | .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).');'; |
| 1057 | } |
| 1058 | |
| 1059 | return $sqls; |
| 1060 | } |
| 1061 | |
| 1062 | // -------------------------------------------------------------------- |
| 1063 | |
| 1064 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1065 | * Reset |
| 1066 | * |
| 1067 | * Resets table creation vars |
| 1068 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1069 | * @return void |
| 1070 | */ |
Phil Sturgeon | a7de97e | 2011-12-31 18:41:08 +0000 | [diff] [blame] | 1071 | protected function _reset() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1072 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1073 | $this->fields = $this->keys = $this->primary_keys = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | } |
| 1077 | |
| 1078 | /* End of file DB_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 1079 | /* Location: ./system/database/DB_forge.php */ |