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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 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 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 28 | /** |
| 29 | * Interbase/Firebird Forge Class |
| 30 | * |
| 31 | * @category Database |
| 32 | * @author EllisLab Dev Team |
| 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
| 35 | class CI_DB_interbase_forge extends CI_DB_forge { |
| 36 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 37 | protected $_drop_table = 'DROP TABLE %s'; |
| 38 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 39 | /** |
| 40 | * Create database |
| 41 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 42 | * @param string the database name |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 43 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 44 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 45 | public function create_database($db_name) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 46 | { |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 47 | // Firebird databases are flat files, so a path is required |
| 48 | |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 49 | // Hostname is needed for remote access |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 50 | empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name; |
| 51 | |
| 52 | return parent::create_database('"'.$db_name.'"'); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // -------------------------------------------------------------------- |
| 56 | |
| 57 | /** |
| 58 | * Drop database |
| 59 | * |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 60 | * @param string the database name |
| 61 | * - not used in this driver, the current db is dropped |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 62 | * @return bool |
| 63 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 64 | public function drop_database($db_name = '') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 65 | { |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 66 | if ( ! ibase_drop_db($this->conn_id)) |
| 67 | { |
| 68 | return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 69 | } |
| 70 | |
| 71 | return TRUE; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 72 | } |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 73 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 74 | // -------------------------------------------------------------------- |
| 75 | |
| 76 | /** |
| 77 | * Create Table |
| 78 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 79 | * @param string the table name |
| 80 | * @param array the fields |
| 81 | * @param mixed primary key(s) |
| 82 | * @param mixed key(s) |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 83 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 84 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 85 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 86 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 87 | { |
| 88 | $sql = 'CREATE TABLE '; |
| 89 | |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 90 | $sql .= $this->db->protect_identifiers($table)."("; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 91 | $current_field_count = 0; |
| 92 | |
Timothy Warren | 125fe73 | 2012-02-21 07:58:25 -0500 | [diff] [blame] | 93 | foreach ($fields as $field => $attributes) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 94 | { |
| 95 | // Numeric field names aren't allowed in databases, so if the key is |
| 96 | // numeric, we know it was assigned by PHP and the developer manually |
| 97 | // entered the field information, so we'll simply add it to the list |
| 98 | if (is_numeric($field)) |
| 99 | { |
| 100 | $sql .= "\n\t$attributes"; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 105 | |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 106 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 107 | |
| 108 | $sql .= ' '.$attributes['TYPE']; |
| 109 | |
| 110 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 111 | { |
| 112 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 113 | } |
| 114 | |
| 115 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 116 | { |
| 117 | $sql .= ' UNSIGNED'; |
| 118 | } |
| 119 | |
| 120 | if (array_key_exists('DEFAULT', $attributes)) |
| 121 | { |
| 122 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 123 | } |
| 124 | |
| 125 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 126 | { |
| 127 | $sql .= ' NULL'; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | $sql .= ' NOT NULL'; |
| 132 | } |
| 133 | |
| 134 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 135 | { |
| 136 | $sql .= ' AUTO_INCREMENT'; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // don't add a comma on the end of the last field |
| 141 | if (++$current_field_count < count($fields)) |
| 142 | { |
| 143 | $sql .= ','; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (count($primary_keys) > 0) |
| 148 | { |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 149 | $primary_keys = $this->db->protect_identifiers($primary_keys); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 150 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 151 | } |
| 152 | |
| 153 | if (is_array($keys) && count($keys) > 0) |
| 154 | { |
| 155 | foreach ($keys as $key) |
| 156 | { |
| 157 | if (is_array($key)) |
| 158 | { |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 159 | $key = $this->db->protect_identifiers($key); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 163 | $key = array($this->db->protect_identifiers($key)); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")"; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $sql .= "\n)"; |
| 171 | |
| 172 | return $sql; |
| 173 | } |
| 174 | |
| 175 | // -------------------------------------------------------------------- |
| 176 | |
| 177 | /** |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 178 | * Alter table query |
| 179 | * |
| 180 | * Generates a platform-specific query so that a table can be altered |
| 181 | * Called by add_column(), drop_column(), and column_alter(), |
| 182 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 183 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 184 | * @param string the column name |
| 185 | * @param string the table name |
| 186 | * @param string the column definition |
| 187 | * @param string the default value |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame^] | 188 | * @param bool should 'NOT NULL' be added |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 189 | * @param string the field after which we should add the new field |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 190 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 191 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 192 | protected 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] | 193 | { |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 194 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table)." $alter_type ".$this->db->protect_identifiers($column_name); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 195 | |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 196 | $sql .= " {$column_definition}"; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 197 | |
| 198 | if ($default_value != '') |
| 199 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 200 | $sql .= " DEFAULT \"{$default_value}\""; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if ($null === NULL) |
| 204 | { |
| 205 | $sql .= ' NULL'; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | $sql .= ' NOT NULL'; |
| 210 | } |
| 211 | |
| 212 | if ($after_field != '') |
| 213 | { |
Timothy Warren | dd044b3 | 2012-02-20 18:58:24 -0500 | [diff] [blame] | 214 | $sql .= ' AFTER ' . $this->db->protect_identifiers($after_field); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return $sql; |
| 218 | |
| 219 | } |
| 220 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* End of file interbase_forge.php */ |
| 224 | /* Location: ./system/database/drivers/interbase/interbase_forge.php */ |