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