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 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
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 | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 | /** |
| 41 | * MySQL Forge Class |
| 42 | * |
| 43 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 44 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 45 | * @link https://codeigniter.com/user_guide/database/ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | */ |
| 47 | class CI_DB_mysql_forge extends CI_DB_forge { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 48 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 49 | /** |
| 50 | * CREATE DATABASE statement |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 54 | protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 56 | /** |
Andrey Andreev | b0a97c1 | 2012-11-11 13:58:53 +0200 | [diff] [blame] | 57 | * CREATE TABLE keys flag |
| 58 | * |
| 59 | * Whether table keys are created from within the |
| 60 | * CREATE TABLE statement. |
| 61 | * |
| 62 | * @var bool |
| 63 | */ |
| 64 | protected $_create_table_keys = TRUE; |
| 65 | |
| 66 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 67 | * UNSIGNED support |
| 68 | * |
| 69 | * @var array |
| 70 | */ |
| 71 | protected $_unsigned = array( |
| 72 | 'TINYINT', |
| 73 | 'SMALLINT', |
| 74 | 'MEDIUMINT', |
| 75 | 'INT', |
| 76 | 'INTEGER', |
| 77 | 'BIGINT', |
| 78 | 'REAL', |
| 79 | 'DOUBLE', |
| 80 | 'DOUBLE PRECISION', |
| 81 | 'FLOAT', |
| 82 | 'DECIMAL', |
| 83 | 'NUMERIC' |
| 84 | ); |
| 85 | |
| 86 | /** |
| 87 | * NULL value representation in CREATE/ALTER TABLE statements |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
Andrey Andreev | 5289f27 | 2014-11-03 22:45:28 +0200 | [diff] [blame] | 91 | protected $_null = 'NULL'; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 92 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 93 | // -------------------------------------------------------------------- |
| 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | /** |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 96 | * CREATE TABLE attributes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | * |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 98 | * @param array $attributes Associative array of table attributes |
| 99 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | */ |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 101 | protected function _create_table_attr($attributes) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | { |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 103 | $sql = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 105 | foreach (array_keys($attributes) as $key) |
| 106 | { |
| 107 | if (is_string($key)) |
| 108 | { |
| 109 | $sql .= ' '.strtoupper($key).' = '.$attributes[$key]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( ! empty($this->db->char_set) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET')) |
| 114 | { |
| 115 | $sql .= ' DEFAULT CHARACTER SET = '.$this->db->char_set; |
| 116 | } |
| 117 | |
| 118 | if ( ! empty($this->db->dbcollat) && ! strpos($sql, 'COLLATE')) |
| 119 | { |
| 120 | $sql .= ' COLLATE = '.$this->db->dbcollat; |
| 121 | } |
| 122 | |
| 123 | return $sql; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // -------------------------------------------------------------------- |
| 127 | |
| 128 | /** |
| 129 | * ALTER TABLE |
| 130 | * |
| 131 | * @param string $alter_type ALTER type |
| 132 | * @param string $table Table name |
| 133 | * @param mixed $field Column definition |
| 134 | * @return string|string[] |
| 135 | */ |
| 136 | protected function _alter_table($alter_type, $table, $field) |
| 137 | { |
| 138 | if ($alter_type === 'DROP') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 140 | return parent::_alter_table($alter_type, $table, $field); |
| 141 | } |
| 142 | |
| 143 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); |
| 144 | for ($i = 0, $c = count($field); $i < $c; $i++) |
| 145 | { |
| 146 | if ($field[$i]['_literal'] !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 148 | $field[$i] = ($alter_type === 'ADD') |
| 149 | ? "\n\tADD ".$field[$i]['_literal'] |
| 150 | : "\n\tMODIFY ".$field[$i]['_literal']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | } |
| 152 | else |
| 153 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 154 | if ($alter_type === 'ADD') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 156 | $field[$i]['_literal'] = "\n\tADD "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
Andrey Andreev | e8b8963 | 2012-11-08 12:05:00 +0200 | [diff] [blame] | 160 | $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Andrey Andreev | e8b8963 | 2012-11-08 12:05:00 +0200 | [diff] [blame] | 163 | $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 167 | return array($sql.implode(',', $field)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 173 | * Process column |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 175 | * @param array $field |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 176 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 178 | protected function _process_column($field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | { |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 180 | $extra_clause = isset($field['after']) |
| 181 | ? ' AFTER '.$this->db->escape_identifiers($field['after']) : ''; |
| 182 | |
| 183 | if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE) |
| 184 | { |
| 185 | $extra_clause = ' FIRST'; |
| 186 | } |
| 187 | |
Zachary Flower | e4b10bf | 2014-11-03 10:42:57 -0700 | [diff] [blame] | 188 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 189 | return $this->db->escape_identifiers($field['name']) |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 190 | .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name'])) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 191 | .' '.$field['type'].$field['length'] |
| 192 | .$field['unsigned'] |
| 193 | .$field['null'] |
| 194 | .$field['default'] |
| 195 | .$field['auto_increment'] |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 196 | .$field['unique'] |
Andrey Andreev | 5289f27 | 2014-11-03 22:45:28 +0200 | [diff] [blame] | 197 | .(empty($field['comment']) ? '' : ' COMMENT '.$field['comment']) |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 198 | .$extra_clause; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 199 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 201 | // -------------------------------------------------------------------- |
| 202 | |
| 203 | /** |
| 204 | * Process indexes |
| 205 | * |
| 206 | * @param string $table (ignored) |
| 207 | * @return string |
| 208 | */ |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame] | 209 | protected function _process_indexes($table) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 210 | { |
| 211 | $sql = ''; |
| 212 | |
| 213 | for ($i = 0, $c = count($this->keys); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame] | 215 | if (is_array($this->keys[$i])) |
| 216 | { |
| 217 | for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++) |
| 218 | { |
| 219 | if ( ! isset($this->fields[$this->keys[$i][$i2]])) |
| 220 | { |
| 221 | unset($this->keys[$i][$i2]); |
| 222 | continue; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | elseif ( ! isset($this->fields[$this->keys[$i]])) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 227 | { |
| 228 | unset($this->keys[$i]); |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]); |
| 233 | |
| 234 | $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) |
| 235 | .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 238 | $this->keys = array(); |
| 239 | |
| 240 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | } |