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