blob: e3f7146880fa2a389541d9f5892ad1182214a189 [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 *
7 * NOTICE OF LICENSE
Timothy Warren817af192012-02-16 08:28:00 -05008 *
Timothy Warren76e04352012-02-14 11:55:17 -05009 * Licensed under the Open Software License version 3.0
Timothy Warren817af192012-02-16 08:28:00 -050010 *
Timothy Warren76e04352012-02-14 11:55:17 -050011 * 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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Timothy Warren76e04352012-02-14 11:55:17 -050022 * @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 Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -050028
Timothy Warren76e04352012-02-14 11:55:17 -050029/**
30 * Interbase/Firebird Forge Class
31 *
32 * @category Database
33 * @author EllisLab Dev Team
34 * @link http://codeigniter.com/user_guide/database/
35 */
Andrey Andreev26086872012-07-05 11:21:58 +030036class CI_DB_ibase_forge extends CI_DB_forge {
Timothy Warren76e04352012-02-14 11:55:17 -050037
Andrey Andreevc98e93a2012-11-02 02:04:59 +020038 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020039 * CREATE TABLE IF statement
Andrey Andreevc98e93a2012-11-02 02:04:59 +020040 *
41 * @var string
42 */
Andrey Andreeva287a342012-11-05 23:19:59 +020043 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 Andreevd947eba2012-04-09 14:58:28 +030076
Andrey Andreevc98e93a2012-11-02 02:04:59 +020077 // --------------------------------------------------------------------
78
Timothy Warren76e04352012-02-14 11:55:17 -050079 /**
80 * Create database
81 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020082 * @param string $db_name
Timothy Warrenc2b712e2012-02-17 15:58:08 -050083 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050084 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030085 public function create_database($db_name)
Timothy Warren76e04352012-02-14 11:55:17 -050086 {
Andrey Andreevd947eba2012-04-09 14:58:28 +030087 // Firebird databases are flat files, so a path is required
88
Timothy Warrenc2b712e2012-02-17 15:58:08 -050089 // Hostname is needed for remote access
Andrey Andreevd947eba2012-04-09 14:58:28 +030090 empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name;
91
92 return parent::create_database('"'.$db_name.'"');
Timothy Warren76e04352012-02-14 11:55:17 -050093 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Drop database
99 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200100 * @param string $db_name (ignored)
Timothy Warren76e04352012-02-14 11:55:17 -0500101 * @return bool
102 */
Andrey Andreevd947eba2012-04-09 14:58:28 +0300103 public function drop_database($db_name = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500104 {
Andrey Andreevd947eba2012-04-09 14:58:28 +0300105 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 Andreev5d281762012-06-11 22:05:40 +0300109 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 Andreevd947eba2012-04-09 14:58:28 +0300117
118 return TRUE;
Timothy Warren76e04352012-02-14 11:55:17 -0500119 }
Andrey Andreevd947eba2012-04-09 14:58:28 +0300120
Timothy Warren76e04352012-02-14 11:55:17 -0500121 // --------------------------------------------------------------------
122
123 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200124 * ALTER TABLE
Timothy Warren76e04352012-02-14 11:55:17 -0500125 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200126 * @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();
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200140 for ($i = 0, $c = count($field); $i < $c; $i++)
Andrey Andreeva287a342012-11-05 23:19:59 +0200141 {
142 if ($field[$i]['_literal'] !== FALSE)
143 {
144 return FALSE;
145 }
146
147 if (isset($field[$i]['type']))
148 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200149 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identififers($field[$i]['name'])
150 .' TYPE '.$field[$i]['type'].$field[$i]['length'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200151 }
152
153 if ( ! empty($field[$i]['default']))
154 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200155 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
156 .' SET DEFAULT '.$field[$i]['default'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200157 }
158
159 if (isset($field[$i]['null']))
160 {
161 $sqls[] = 'UPDATE "RDB$RELATION_FIELDS" SET "RDB$NULL_FLAG" = '
162 .($field[$i]['null'] === TRUE ? 'NULL' : '1')
163 .' WHERE "RDB$FIELD_NAME" = '.$this->db->escape($field[$i]['name'])
164 .' AND "RDB$RELATION_NAME" = '.$this->db->escape($table);
165 }
166
167 if ( ! empty($field[$i]['new_name']))
168 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200169 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeva287a342012-11-05 23:19:59 +0200170 .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
171 }
172 }
173
174 return $sqls;
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Process column
181 *
182 * @param array $field
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500183 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500184 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200185 protected function _process_column($field)
Timothy Warren76e04352012-02-14 11:55:17 -0500186 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200187 return $this->db->escape_identifiers($field['name'])
188 .' '.$field['type'].$field['length']
189 .$field['null']
190 .$field['unique']
191 .$field['default'];
Timothy Warren76e04352012-02-14 11:55:17 -0500192 }
193
194 // --------------------------------------------------------------------
195
196 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200197 * Field attribute TYPE
Timothy Warren76e04352012-02-14 11:55:17 -0500198 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200199 * Performs a data type mapping between different databases.
Timothy Warren76e04352012-02-14 11:55:17 -0500200 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200201 * @param array &$attributes
202 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500203 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200204 protected function _attr_type(&$attributes)
Timothy Warren76e04352012-02-14 11:55:17 -0500205 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200206 switch (strtoupper($attributes['TYPE']))
207 {
208 case 'TINYINT':
209 $attributes['TYPE'] = 'SMALLINT';
210 $attributes['UNSIGNED'] = FALSE;
211 return;
212 case 'MEDIUMINT':
213 $attributes['TYPE'] = 'INTEGER';
214 $attributes['UNSIGNED'] = FALSE;
215 return;
216 case 'INT':
217 $attributes['TYPE'] = 'INTEGER';
218 return;
219 case 'BIGINT':
220 $attributes['TYPE'] = 'INT64';
221 return;
222 default: return;
223 }
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Field attribute AUTO_INCREMENT
230 *
231 * @param array &$attributes
232 * @param array &$field
233 * @return void
234 */
235 protected function _attr_auto_increment(&$attributes, &$field)
236 {
237 // Not supported
Timothy Warren76e04352012-02-14 11:55:17 -0500238 }
239
Timothy Warren76e04352012-02-14 11:55:17 -0500240}
241
Andrey Andreev26086872012-07-05 11:21:58 +0300242/* End of file ibase_forge.php */
243/* Location: ./system/database/drivers/ibase/ibase_forge.php */