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