blob: 77aa4ae0130fcbb6c658587218e1b136b30893cb [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Timothy Warren76e04352012-02-14 11:55:17 -05002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Timothy Warren76e04352012-02-14 11:55:17 -05006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Timothy Warren817af192012-02-16 08:28:00 -05008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Timothy Warren817af192012-02-16 08:28:00 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Warren76e04352012-02-14 11:55:17 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 3.0.0
Timothy Warren76e04352012-02-14 11:55:17 -050036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -050039
Timothy Warren76e04352012-02-14 11:55:17 -050040/**
41 * Interbase/Firebird Forge Class
42 *
43 * @category Database
44 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020045 * @link https://codeigniter.com/user_guide/database/
Timothy Warren76e04352012-02-14 11:55:17 -050046 */
Andrey Andreev26086872012-07-05 11:21:58 +030047class CI_DB_ibase_forge extends CI_DB_forge {
Timothy Warren76e04352012-02-14 11:55:17 -050048
Andrey Andreevc98e93a2012-11-02 02:04:59 +020049 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020050 * CREATE TABLE IF statement
Andrey Andreevc98e93a2012-11-02 02:04:59 +020051 *
52 * @var string
53 */
Andrey Andreeva287a342012-11-05 23:19:59 +020054 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 Andreevd947eba2012-04-09 14:58:28 +030087
Andrey Andreevc98e93a2012-11-02 02:04:59 +020088 // --------------------------------------------------------------------
89
Timothy Warren76e04352012-02-14 11:55:17 -050090 /**
91 * Create database
92 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020093 * @param string $db_name
Andrey Andreevee9d4282017-06-05 10:44:37 +030094 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -050095 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030096 public function create_database($db_name)
Timothy Warren76e04352012-02-14 11:55:17 -050097 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030098 // Firebird databases are flat files, so a path is required
99
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500100 // Hostname is needed for remote access
Andrey Andreevd947eba2012-04-09 14:58:28 +0300101 empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name;
102
103 return parent::create_database('"'.$db_name.'"');
Timothy Warren76e04352012-02-14 11:55:17 -0500104 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * Drop database
110 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200111 * @param string $db_name (ignored)
Timothy Warren76e04352012-02-14 11:55:17 -0500112 * @return bool
113 */
Andrey Andreevda270b22016-10-17 18:22:43 +0300114 public function drop_database($db_name)
Timothy Warren76e04352012-02-14 11:55:17 -0500115 {
Andrey Andreevd947eba2012-04-09 14:58:28 +0300116 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 Andreev5d281762012-06-11 22:05:40 +0300120 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 Andreevd947eba2012-04-09 14:58:28 +0300128
129 return TRUE;
Timothy Warren76e04352012-02-14 11:55:17 -0500130 }
Andrey Andreevd947eba2012-04-09 14:58:28 +0300131
Timothy Warren76e04352012-02-14 11:55:17 -0500132 // --------------------------------------------------------------------
133
134 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200135 * ALTER TABLE
Timothy Warren76e04352012-02-14 11:55:17 -0500136 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200137 * @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 Andreev7ade8b72012-11-22 13:12:22 +0200151 for ($i = 0, $c = count($field); $i < $c; $i++)
Andrey Andreeva287a342012-11-05 23:19:59 +0200152 {
153 if ($field[$i]['_literal'] !== FALSE)
154 {
155 return FALSE;
156 }
157
158 if (isset($field[$i]['type']))
159 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200160 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identififers($field[$i]['name'])
161 .' TYPE '.$field[$i]['type'].$field[$i]['length'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200162 }
163
164 if ( ! empty($field[$i]['default']))
165 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200166 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
167 .' SET DEFAULT '.$field[$i]['default'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200168 }
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 Andreev7ade8b72012-11-22 13:12:22 +0200180 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeva287a342012-11-05 23:19:59 +0200181 .' 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 Warrenc2b712e2012-02-17 15:58:08 -0500194 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500195 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200196 protected function _process_column($field)
Timothy Warren76e04352012-02-14 11:55:17 -0500197 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200198 return $this->db->escape_identifiers($field['name'])
199 .' '.$field['type'].$field['length']
200 .$field['null']
201 .$field['unique']
202 .$field['default'];
Timothy Warren76e04352012-02-14 11:55:17 -0500203 }
204
205 // --------------------------------------------------------------------
206
207 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200208 * Field attribute TYPE
Timothy Warren76e04352012-02-14 11:55:17 -0500209 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200210 * Performs a data type mapping between different databases.
Timothy Warren76e04352012-02-14 11:55:17 -0500211 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200212 * @param array &$attributes
213 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500214 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200215 protected function _attr_type(&$attributes)
Timothy Warren76e04352012-02-14 11:55:17 -0500216 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200217 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 Warren76e04352012-02-14 11:55:17 -0500249 }
250
Timothy Warren76e04352012-02-14 11:55:17 -0500251}