Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [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 |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [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) |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [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 |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [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: |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -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 3.0.0 |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [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'); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 39 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 40 | /** |
| 41 | * Interbase/Firebird Forge Class |
| 42 | * |
| 43 | * @category Database |
| 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/ |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 46 | */ |
Andrey Andreev | 2608687 | 2012-07-05 11:21:58 +0300 | [diff] [blame] | 47 | class CI_DB_ibase_forge extends CI_DB_forge { |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 48 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 49 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 50 | * CREATE TABLE IF statement |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 51 | * |
| 52 | * @var string |
| 53 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 54 | protected $_create_table_if = FALSE; |
| 55 | |
| 56 | /** |
| 57 | * RENAME TABLE statement |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
| 61 | protected $_rename_table = FALSE; |
| 62 | |
| 63 | /** |
| 64 | * DROP TABLE IF statement |
| 65 | * |
| 66 | * @var string |
| 67 | */ |
| 68 | protected $_drop_table_if = FALSE; |
| 69 | |
| 70 | /** |
| 71 | * UNSIGNED support |
| 72 | * |
| 73 | * @var array |
| 74 | */ |
| 75 | protected $_unsigned = array( |
| 76 | 'SMALLINT' => 'INTEGER', |
| 77 | 'INTEGER' => 'INT64', |
| 78 | 'FLOAT' => 'DOUBLE PRECISION' |
| 79 | ); |
| 80 | |
| 81 | /** |
| 82 | * NULL value representation in CREATE/ALTER TABLE statements |
| 83 | * |
| 84 | * @var string |
| 85 | */ |
| 86 | protected $_null = 'NULL'; |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 87 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 88 | // -------------------------------------------------------------------- |
| 89 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 90 | /** |
| 91 | * Create database |
| 92 | * |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 93 | * @param string $db_name |
Andrey Andreev | ee9d428 | 2017-06-05 10:44:37 +0300 | [diff] [blame] | 94 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 95 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 96 | public function create_database($db_name) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 97 | { |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 98 | // Firebird databases are flat files, so a path is required |
| 99 | |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 100 | // Hostname is needed for remote access |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 101 | empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name; |
| 102 | |
| 103 | return parent::create_database('"'.$db_name.'"'); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // -------------------------------------------------------------------- |
| 107 | |
| 108 | /** |
| 109 | * Drop database |
| 110 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 111 | * @param string $db_name (ignored) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 112 | * @return bool |
| 113 | */ |
Andrey Andreev | da270b2 | 2016-10-17 18:22:43 +0300 | [diff] [blame] | 114 | public function drop_database($db_name) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 115 | { |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 116 | if ( ! ibase_drop_db($this->conn_id)) |
| 117 | { |
| 118 | return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 119 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame] | 120 | elseif ( ! empty($this->db->data_cache['db_names'])) |
| 121 | { |
| 122 | $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); |
| 123 | if ($key !== FALSE) |
| 124 | { |
| 125 | unset($this->db->data_cache['db_names'][$key]); |
| 126 | } |
| 127 | } |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 128 | |
| 129 | return TRUE; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 130 | } |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 131 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 132 | // -------------------------------------------------------------------- |
| 133 | |
| 134 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 135 | * ALTER TABLE |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 136 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 137 | * @param string $alter_type ALTER type |
| 138 | * @param string $table Table name |
| 139 | * @param mixed $field Column definition |
| 140 | * @return string|string[] |
| 141 | */ |
| 142 | protected function _alter_table($alter_type, $table, $field) |
| 143 | { |
| 144 | if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) |
| 145 | { |
| 146 | return parent::_alter_table($alter_type, $table, $field); |
| 147 | } |
| 148 | |
| 149 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); |
| 150 | $sqls = array(); |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 151 | for ($i = 0, $c = count($field); $i < $c; $i++) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 152 | { |
| 153 | if ($field[$i]['_literal'] !== FALSE) |
| 154 | { |
| 155 | return FALSE; |
| 156 | } |
| 157 | |
| 158 | if (isset($field[$i]['type'])) |
| 159 | { |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 160 | $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identififers($field[$i]['name']) |
| 161 | .' TYPE '.$field[$i]['type'].$field[$i]['length']; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if ( ! empty($field[$i]['default'])) |
| 165 | { |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 166 | $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name']) |
| 167 | .' SET DEFAULT '.$field[$i]['default']; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (isset($field[$i]['null'])) |
| 171 | { |
| 172 | $sqls[] = 'UPDATE "RDB$RELATION_FIELDS" SET "RDB$NULL_FLAG" = ' |
| 173 | .($field[$i]['null'] === TRUE ? 'NULL' : '1') |
| 174 | .' WHERE "RDB$FIELD_NAME" = '.$this->db->escape($field[$i]['name']) |
| 175 | .' AND "RDB$RELATION_NAME" = '.$this->db->escape($table); |
| 176 | } |
| 177 | |
| 178 | if ( ! empty($field[$i]['new_name'])) |
| 179 | { |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 180 | $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name']) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 181 | .' TO '.$this->db->escape_identifiers($field[$i]['new_name']); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return $sqls; |
| 186 | } |
| 187 | |
| 188 | // -------------------------------------------------------------------- |
| 189 | |
| 190 | /** |
| 191 | * Process column |
| 192 | * |
| 193 | * @param array $field |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 194 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 195 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 196 | protected function _process_column($field) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 197 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 198 | return $this->db->escape_identifiers($field['name']) |
| 199 | .' '.$field['type'].$field['length'] |
| 200 | .$field['null'] |
| 201 | .$field['unique'] |
| 202 | .$field['default']; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
| 207 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 208 | * Field attribute TYPE |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 209 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 210 | * Performs a data type mapping between different databases. |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 211 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 212 | * @param array &$attributes |
| 213 | * @return void |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 214 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 215 | protected function _attr_type(&$attributes) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 216 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 217 | switch (strtoupper($attributes['TYPE'])) |
| 218 | { |
| 219 | case 'TINYINT': |
| 220 | $attributes['TYPE'] = 'SMALLINT'; |
| 221 | $attributes['UNSIGNED'] = FALSE; |
| 222 | return; |
| 223 | case 'MEDIUMINT': |
| 224 | $attributes['TYPE'] = 'INTEGER'; |
| 225 | $attributes['UNSIGNED'] = FALSE; |
| 226 | return; |
| 227 | case 'INT': |
| 228 | $attributes['TYPE'] = 'INTEGER'; |
| 229 | return; |
| 230 | case 'BIGINT': |
| 231 | $attributes['TYPE'] = 'INT64'; |
| 232 | return; |
| 233 | default: return; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
| 240 | * Field attribute AUTO_INCREMENT |
| 241 | * |
| 242 | * @param array &$attributes |
| 243 | * @param array &$field |
| 244 | * @return void |
| 245 | */ |
| 246 | protected function _attr_auto_increment(&$attributes, &$field) |
| 247 | { |
| 248 | // Not supported |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 251 | } |