blob: 3cab2640be860443d1ee878fae5f38d576bfd56b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Timothy Warren76e04352012-02-14 11:55:17 -05002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
45 * @link http://codeigniter.com/user_guide/database/
46 */
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
Zachary Flower9b512282014-11-02 21:51:19 -070088 /**
89 * COMMENT value representation in CREATE/ALTER TABLE statements
90 *
91 * @var string
92 */
93 protected $_comment = FALSE;
94
Andrey Andreevc98e93a2012-11-02 02:04:59 +020095 // --------------------------------------------------------------------
96
Timothy Warren76e04352012-02-14 11:55:17 -050097 /**
98 * Create database
99 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +0200100 * @param string $db_name
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500101 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500102 */
Andrey Andreevd947eba2012-04-09 14:58:28 +0300103 public function create_database($db_name)
Timothy Warren76e04352012-02-14 11:55:17 -0500104 {
Andrey Andreevd947eba2012-04-09 14:58:28 +0300105 // Firebird databases are flat files, so a path is required
106
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500107 // Hostname is needed for remote access
Andrey Andreevd947eba2012-04-09 14:58:28 +0300108 empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name;
109
110 return parent::create_database('"'.$db_name.'"');
Timothy Warren76e04352012-02-14 11:55:17 -0500111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Drop database
117 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200118 * @param string $db_name (ignored)
Timothy Warren76e04352012-02-14 11:55:17 -0500119 * @return bool
120 */
Andrey Andreevd947eba2012-04-09 14:58:28 +0300121 public function drop_database($db_name = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500122 {
Andrey Andreevd947eba2012-04-09 14:58:28 +0300123 if ( ! ibase_drop_db($this->conn_id))
124 {
125 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
126 }
Andrey Andreev5d281762012-06-11 22:05:40 +0300127 elseif ( ! empty($this->db->data_cache['db_names']))
128 {
129 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
130 if ($key !== FALSE)
131 {
132 unset($this->db->data_cache['db_names'][$key]);
133 }
134 }
Andrey Andreevd947eba2012-04-09 14:58:28 +0300135
136 return TRUE;
Timothy Warren76e04352012-02-14 11:55:17 -0500137 }
Andrey Andreevd947eba2012-04-09 14:58:28 +0300138
Timothy Warren76e04352012-02-14 11:55:17 -0500139 // --------------------------------------------------------------------
140
141 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200142 * ALTER TABLE
Timothy Warren76e04352012-02-14 11:55:17 -0500143 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200144 * @param string $alter_type ALTER type
145 * @param string $table Table name
146 * @param mixed $field Column definition
147 * @return string|string[]
148 */
149 protected function _alter_table($alter_type, $table, $field)
150 {
151 if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
152 {
153 return parent::_alter_table($alter_type, $table, $field);
154 }
155
156 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
157 $sqls = array();
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200158 for ($i = 0, $c = count($field); $i < $c; $i++)
Andrey Andreeva287a342012-11-05 23:19:59 +0200159 {
160 if ($field[$i]['_literal'] !== FALSE)
161 {
162 return FALSE;
163 }
164
165 if (isset($field[$i]['type']))
166 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200167 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identififers($field[$i]['name'])
168 .' TYPE '.$field[$i]['type'].$field[$i]['length'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200169 }
170
171 if ( ! empty($field[$i]['default']))
172 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200173 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
174 .' SET DEFAULT '.$field[$i]['default'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200175 }
176
177 if (isset($field[$i]['null']))
178 {
179 $sqls[] = 'UPDATE "RDB$RELATION_FIELDS" SET "RDB$NULL_FLAG" = '
180 .($field[$i]['null'] === TRUE ? 'NULL' : '1')
181 .' WHERE "RDB$FIELD_NAME" = '.$this->db->escape($field[$i]['name'])
182 .' AND "RDB$RELATION_NAME" = '.$this->db->escape($table);
183 }
184
185 if ( ! empty($field[$i]['new_name']))
186 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200187 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeva287a342012-11-05 23:19:59 +0200188 .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
189 }
190 }
191
192 return $sqls;
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Process column
199 *
200 * @param array $field
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500201 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500202 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200203 protected function _process_column($field)
Timothy Warren76e04352012-02-14 11:55:17 -0500204 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200205 return $this->db->escape_identifiers($field['name'])
206 .' '.$field['type'].$field['length']
207 .$field['null']
208 .$field['unique']
209 .$field['default'];
Timothy Warren76e04352012-02-14 11:55:17 -0500210 }
211
212 // --------------------------------------------------------------------
213
214 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200215 * Field attribute TYPE
Timothy Warren76e04352012-02-14 11:55:17 -0500216 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200217 * Performs a data type mapping between different databases.
Timothy Warren76e04352012-02-14 11:55:17 -0500218 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200219 * @param array &$attributes
220 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500221 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200222 protected function _attr_type(&$attributes)
Timothy Warren76e04352012-02-14 11:55:17 -0500223 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200224 switch (strtoupper($attributes['TYPE']))
225 {
226 case 'TINYINT':
227 $attributes['TYPE'] = 'SMALLINT';
228 $attributes['UNSIGNED'] = FALSE;
229 return;
230 case 'MEDIUMINT':
231 $attributes['TYPE'] = 'INTEGER';
232 $attributes['UNSIGNED'] = FALSE;
233 return;
234 case 'INT':
235 $attributes['TYPE'] = 'INTEGER';
236 return;
237 case 'BIGINT':
238 $attributes['TYPE'] = 'INT64';
239 return;
240 default: return;
241 }
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Field attribute AUTO_INCREMENT
248 *
249 * @param array &$attributes
250 * @param array &$field
251 * @return void
252 */
253 protected function _attr_auto_increment(&$attributes, &$field)
254 {
255 // Not supported
Timothy Warren76e04352012-02-14 11:55:17 -0500256 }
257
Timothy Warren76e04352012-02-14 11:55:17 -0500258}
259
Andrey Andreev26086872012-07-05 11:21:58 +0300260/* End of file ibase_forge.php */
261/* Location: ./system/database/drivers/ibase/ibase_forge.php */