blob: d9b55a87fb24caf3edf7d911f7dc06f5d6ef60aa [file] [log] [blame]
Timothy Warren817af192012-02-16 08:28:00 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -05002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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
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 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Interbase/Firebird Forge Class
32 *
33 * @category Database
34 * @author EllisLab Dev Team
35 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_interbase_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
Timothy Warren76e04352012-02-14 11:55:17 -050042 * @param string the database name
Timothy Warrenc2b712e2012-02-17 15:58:08 -050043 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050044 */
Timothy Warrenc2b712e2012-02-17 15:58:08 -050045 public function _create_database($filename='')
Timothy Warren76e04352012-02-14 11:55:17 -050046 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -050047 // Firebird databases are flat files, so a path is required
48 // Hostname is needed for remote access
49 return 'CREATE DATABASE "'.$this->hostname.':'.$filename.'"';
50
Timothy Warren76e04352012-02-14 11:55:17 -050051 }
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Drop database
57 *
Timothy Warren76e04352012-02-14 11:55:17 -050058 * @param string the database name - not used in this driver
59 * - the current db is dropped
60 * @return bool
61 */
Timothy Warren4be822b2012-02-14 12:07:34 -050062 public function _drop_database($name='')
Timothy Warren76e04352012-02-14 11:55:17 -050063 {
64 return ibase_drop_db($this->conn_id);
65 }
66 // --------------------------------------------------------------------
67
68 /**
69 * Create Table
70 *
Timothy Warren76e04352012-02-14 11:55:17 -050071 * @param string the table name
72 * @param array the fields
73 * @param mixed primary key(s)
74 * @param mixed key(s)
75 * @param boolean should 'IF NOT EXISTS' be added to the SQL
Timothy Warrenc2b712e2012-02-17 15:58:08 -050076 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050077 */
Timothy Warren4be822b2012-02-14 12:07:34 -050078 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren76e04352012-02-14 11:55:17 -050079 {
80 $sql = 'CREATE TABLE ';
81
82 $sql .= $this->db->_escape_identifiers($table)."(";
83 $current_field_count = 0;
84
85 foreach ($fields as $field=>$attributes)
86 {
87 // Numeric field names aren't allowed in databases, so if the key is
88 // numeric, we know it was assigned by PHP and the developer manually
89 // entered the field information, so we'll simply add it to the list
90 if (is_numeric($field))
91 {
92 $sql .= "\n\t$attributes";
93 }
94 else
95 {
96 $attributes = array_change_key_case($attributes, CASE_UPPER);
97
98 $sql .= "\n\t".$this->db->_protect_identifiers($field);
99
100 $sql .= ' '.$attributes['TYPE'];
101
102 if (array_key_exists('CONSTRAINT', $attributes))
103 {
104 $sql .= '('.$attributes['CONSTRAINT'].')';
105 }
106
107 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
108 {
109 $sql .= ' UNSIGNED';
110 }
111
112 if (array_key_exists('DEFAULT', $attributes))
113 {
114 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
115 }
116
117 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
118 {
119 $sql .= ' NULL';
120 }
121 else
122 {
123 $sql .= ' NOT NULL';
124 }
125
126 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
127 {
128 $sql .= ' AUTO_INCREMENT';
129 }
130 }
131
132 // don't add a comma on the end of the last field
133 if (++$current_field_count < count($fields))
134 {
135 $sql .= ',';
136 }
137 }
138
139 if (count($primary_keys) > 0)
140 {
141 $primary_keys = $this->db->_protect_identifiers($primary_keys);
142 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
143 }
144
145 if (is_array($keys) && count($keys) > 0)
146 {
147 foreach ($keys as $key)
148 {
149 if (is_array($key))
150 {
151 $key = $this->db->_protect_identifiers($key);
152 }
153 else
154 {
155 $key = array($this->db->_protect_identifiers($key));
156 }
157
158 $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
159 }
160 }
161
162 $sql .= "\n)";
163
164 return $sql;
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Drop Table
171 *
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500172 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500173 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500174 public function _drop_table($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500175 {
Timothy Warren817af192012-02-16 08:28:00 -0500176 return 'DROP TABLE '.$name;
Timothy Warren76e04352012-02-14 11:55:17 -0500177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Alter table query
183 *
184 * Generates a platform-specific query so that a table can be altered
185 * Called by add_column(), drop_column(), and column_alter(),
186 *
Timothy Warren76e04352012-02-14 11:55:17 -0500187 * @param string the ALTER type (ADD, DROP, CHANGE)
188 * @param string the column name
189 * @param string the table name
190 * @param string the column definition
191 * @param string the default value
192 * @param boolean should 'NOT NULL' be added
193 * @param string the field after which we should add the new field
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500194 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500195 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500196 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500197 {
198 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
199
Timothy Warren3d985a12012-02-15 11:38:00 -0500200 $sql .= " {$column_definition}";
Timothy Warren76e04352012-02-14 11:55:17 -0500201
202 if ($default_value != '')
203 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500204 $sql .= " DEFAULT \"{$default_value}\"";
Timothy Warren76e04352012-02-14 11:55:17 -0500205 }
206
207 if ($null === NULL)
208 {
209 $sql .= ' NULL';
210 }
211 else
212 {
213 $sql .= ' NOT NULL';
214 }
215
216 if ($after_field != '')
217 {
218 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
219 }
220
221 return $sql;
222
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Rename a table
229 *
230 * Generates a platform-specific query so that a table can be renamed
231 *
Timothy Warren76e04352012-02-14 11:55:17 -0500232 * @param string the old table name
233 * @param string the new table name
234 * @return string
235 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500236 public function _rename_table($table_name, $new_table_name)
Timothy Warren76e04352012-02-14 11:55:17 -0500237 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500238 return 'ALTER TABLE '.$this->db->_protect_identifiers($table_name).' RENAME TO '.$this->db->_protect_identifiers($new_table_name);
Timothy Warren76e04352012-02-14 11:55:17 -0500239 }
240}
241
242/* End of file interbase_forge.php */
243/* Location: ./system/database/drivers/interbase/interbase_forge.php */