Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 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 | */ |
| 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 | */ |
| 37 | class CI_DB_interbase_forge extends CI_DB_forge { |
| 38 | |
| 39 | /** |
| 40 | * Create database |
| 41 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 42 | * @param string the database name |
| 43 | * @return bool |
| 44 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 45 | public function _create_database() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 46 | { |
| 47 | // In Interbase/Firebird, a database is created when you connect to the database. |
| 48 | // We'll return TRUE so that an error isn't generated |
| 49 | return TRUE; |
| 50 | } |
| 51 | |
| 52 | // -------------------------------------------------------------------- |
| 53 | |
| 54 | /** |
| 55 | * Drop database |
| 56 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 57 | * @param string the database name - not used in this driver |
| 58 | * - the current db is dropped |
| 59 | * @return bool |
| 60 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 61 | public function _drop_database($name='') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 62 | { |
| 63 | return ibase_drop_db($this->conn_id); |
| 64 | } |
| 65 | // -------------------------------------------------------------------- |
| 66 | |
| 67 | /** |
| 68 | * Create Table |
| 69 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 70 | * @param string the table name |
| 71 | * @param array the fields |
| 72 | * @param mixed primary key(s) |
| 73 | * @param mixed key(s) |
| 74 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 75 | * @return bool |
| 76 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 77 | public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 78 | { |
| 79 | $sql = 'CREATE TABLE '; |
| 80 | |
| 81 | $sql .= $this->db->_escape_identifiers($table)."("; |
| 82 | $current_field_count = 0; |
| 83 | |
| 84 | foreach ($fields as $field=>$attributes) |
| 85 | { |
| 86 | // Numeric field names aren't allowed in databases, so if the key is |
| 87 | // numeric, we know it was assigned by PHP and the developer manually |
| 88 | // entered the field information, so we'll simply add it to the list |
| 89 | if (is_numeric($field)) |
| 90 | { |
| 91 | $sql .= "\n\t$attributes"; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 96 | |
| 97 | $sql .= "\n\t".$this->db->_protect_identifiers($field); |
| 98 | |
| 99 | $sql .= ' '.$attributes['TYPE']; |
| 100 | |
| 101 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 102 | { |
| 103 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 104 | } |
| 105 | |
| 106 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 107 | { |
| 108 | $sql .= ' UNSIGNED'; |
| 109 | } |
| 110 | |
| 111 | if (array_key_exists('DEFAULT', $attributes)) |
| 112 | { |
| 113 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 114 | } |
| 115 | |
| 116 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 117 | { |
| 118 | $sql .= ' NULL'; |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | $sql .= ' NOT NULL'; |
| 123 | } |
| 124 | |
| 125 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 126 | { |
| 127 | $sql .= ' AUTO_INCREMENT'; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // don't add a comma on the end of the last field |
| 132 | if (++$current_field_count < count($fields)) |
| 133 | { |
| 134 | $sql .= ','; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (count($primary_keys) > 0) |
| 139 | { |
| 140 | $primary_keys = $this->db->_protect_identifiers($primary_keys); |
| 141 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 142 | } |
| 143 | |
| 144 | if (is_array($keys) && count($keys) > 0) |
| 145 | { |
| 146 | foreach ($keys as $key) |
| 147 | { |
| 148 | if (is_array($key)) |
| 149 | { |
| 150 | $key = $this->db->_protect_identifiers($key); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | $key = array($this->db->_protect_identifiers($key)); |
| 155 | } |
| 156 | |
| 157 | $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")"; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | $sql .= "\n)"; |
| 162 | |
| 163 | return $sql; |
| 164 | } |
| 165 | |
| 166 | // -------------------------------------------------------------------- |
| 167 | |
| 168 | /** |
| 169 | * Drop Table |
| 170 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 171 | * @return bool |
| 172 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 173 | public function _drop_table($table) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 174 | { |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame^] | 175 | return 'DROP TABLE '.$name; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Alter table query |
| 182 | * |
| 183 | * Generates a platform-specific query so that a table can be altered |
| 184 | * Called by add_column(), drop_column(), and column_alter(), |
| 185 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 186 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 187 | * @param string the column name |
| 188 | * @param string the table name |
| 189 | * @param string the column definition |
| 190 | * @param string the default value |
| 191 | * @param boolean should 'NOT NULL' be added |
| 192 | * @param string the field after which we should add the new field |
| 193 | * @return object |
| 194 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 195 | public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 196 | { |
| 197 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name); |
| 198 | |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 199 | $sql .= " {$column_definition}"; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 200 | |
| 201 | if ($default_value != '') |
| 202 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 203 | $sql .= " DEFAULT \"{$default_value}\""; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if ($null === NULL) |
| 207 | { |
| 208 | $sql .= ' NULL'; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | $sql .= ' NOT NULL'; |
| 213 | } |
| 214 | |
| 215 | if ($after_field != '') |
| 216 | { |
| 217 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 218 | } |
| 219 | |
| 220 | return $sql; |
| 221 | |
| 222 | } |
| 223 | |
| 224 | // -------------------------------------------------------------------- |
| 225 | |
| 226 | /** |
| 227 | * Rename a table |
| 228 | * |
| 229 | * Generates a platform-specific query so that a table can be renamed |
| 230 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 231 | * @param string the old table name |
| 232 | * @param string the new table name |
| 233 | * @return string |
| 234 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 235 | public function _rename_table($table_name, $new_table_name) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 236 | { |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame^] | 237 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name).' RENAME TO '.$this->db->_protect_identifiers($new_table_name); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 238 | return $sql; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* End of file interbase_forge.php */ |
| 243 | /* Location: ./system/database/drivers/interbase/interbase_forge.php */ |