Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [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 |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -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 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Andrey Andreev | cf63120 | 2012-02-16 11:36:28 +0200 | [diff] [blame] | 24 | * @since Version 2.1 |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 28 | /** |
| 29 | * CUBRID Forge Class |
| 30 | * |
| 31 | * @category Database |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 32 | * @author Esen Sagynov |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
| 35 | class CI_DB_cubrid_forge extends CI_DB_forge { |
| 36 | |
| 37 | /** |
| 38 | * Create database |
| 39 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 40 | * @param string the database name |
| 41 | * @return bool |
| 42 | */ |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 43 | public function _create_database($name) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 44 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 45 | // CUBRID does not allow to create a database in SQL. GUI or |
| 46 | // command line tools have to be used for this purpose. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 47 | return FALSE; |
| 48 | } |
| 49 | |
| 50 | // -------------------------------------------------------------------- |
| 51 | |
| 52 | /** |
| 53 | * Drop database |
| 54 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 55 | * @param string the database name |
| 56 | * @return bool |
| 57 | */ |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 58 | public function _drop_database($name) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 59 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 60 | // CUBRID does not allow to drop a database in SQL. GUI or |
| 61 | // command line tools have to be used for this purpose. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 62 | return FALSE; |
| 63 | } |
| 64 | |
| 65 | // -------------------------------------------------------------------- |
| 66 | |
| 67 | /** |
| 68 | * Process Fields |
| 69 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 70 | * @param mixed the fields |
| 71 | * @return string |
| 72 | */ |
Andrey Andreev | 7719a03 | 2012-01-27 21:05:55 +0200 | [diff] [blame] | 73 | protected function _process_fields($fields) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 74 | { |
| 75 | $current_field_count = 0; |
| 76 | $sql = ''; |
| 77 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 78 | foreach ($fields as $field => $attributes) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 79 | { |
| 80 | // Numeric field names aren't allowed in databases, so if the key is |
| 81 | // numeric, we know it was assigned by PHP and the developer manually |
| 82 | // entered the field information, so we'll simply add it to the list |
| 83 | if (is_numeric($field)) |
| 84 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 85 | $sql .= "\n\t".$attributes; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 86 | } |
| 87 | else |
| 88 | { |
| 89 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 90 | $sql .= "\n\t".$this->db->protect_identifiers($field) |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 91 | .( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 92 | |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 93 | if ( ! empty($attributes['TYPE'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 94 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 95 | $sql .= ' '.$attributes['TYPE']; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 96 | |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 97 | if ( ! empty($attributes['CONSTRAINT'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 98 | { |
| 99 | switch ($attributes['TYPE']) |
| 100 | { |
| 101 | case 'decimal': |
| 102 | case 'float': |
| 103 | case 'numeric': |
| 104 | $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; |
| 105 | break; |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 106 | case 'enum': |
| 107 | // Will be supported in the future as part a part of |
| 108 | // MySQL compatibility features. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 109 | break; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 110 | case 'set': |
| 111 | $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; |
| 112 | break; |
| 113 | default: |
| 114 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 119 | /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 120 | * Will be supported in the next release as a part of MySQL Compatibility. |
| 121 | * |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 122 | if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 123 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 124 | $sql .= ' UNSIGNED'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 125 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 126 | */ |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 127 | |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 128 | $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 129 | .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') |
| 130 | .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') |
| 131 | .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // don't add a comma on the end of the last field |
| 135 | if (++$current_field_count < count($fields)) |
| 136 | { |
| 137 | $sql .= ','; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return $sql; |
| 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
| 145 | |
| 146 | /** |
| 147 | * Create Table |
| 148 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 149 | * @param string the table name |
| 150 | * @param mixed the fields |
| 151 | * @param mixed primary key(s) |
| 152 | * @param mixed key(s) |
Andrey Andreev | 582d6a8 | 2012-03-20 15:13:49 +0200 | [diff] [blame] | 153 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 154 | * @return bool |
| 155 | */ |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 156 | public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 157 | { |
| 158 | $sql = 'CREATE TABLE '; |
| 159 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 160 | /* As of version 8.4.1 CUBRID does not support this SQL syntax. |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 161 | if ($if_not_exists === TRUE) |
| 162 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 163 | $sql .= 'IF NOT EXISTS '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 164 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 165 | */ |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 166 | |
Andrey Andreev | 2e430a3 | 2012-02-12 23:37:58 +0200 | [diff] [blame] | 167 | $sql .= $this->db->protect_identifiers($table).' ('.$this->_process_fields($fields); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 168 | |
| 169 | // If there is a PK defined |
| 170 | if (count($primary_keys) > 0) |
| 171 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 172 | $key_name = $this->db->protect_identifiers('pk_'.$table.'_'.implode('_', $primary_keys)); |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 173 | $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->protect_identifiers($primary_keys)).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (is_array($keys) && count($keys) > 0) |
| 177 | { |
| 178 | foreach ($keys as $key) |
| 179 | { |
| 180 | if (is_array($key)) |
| 181 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 182 | $key_name = $this->db->protect_identifiers('idx_'.$table.implode('_', $key)); |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 183 | $key = $this->db->protect_identifiers($key); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 187 | $key_name = $this->db->protect_identifiers('idx_'.$table.$key); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 188 | $key = array($key_name); |
| 189 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 190 | |
| 191 | $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 195 | return $sql."\n);"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
| 199 | |
| 200 | /** |
| 201 | * Drop Table |
| 202 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 203 | * @return string |
| 204 | */ |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 205 | public function _drop_table($table) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 206 | { |
Andrey Andreev | 2e430a3 | 2012-02-12 23:37:58 +0200 | [diff] [blame] | 207 | return 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * Alter table query |
| 214 | * |
| 215 | * Generates a platform-specific query so that a table can be altered |
| 216 | * Called by add_column(), drop_column(), and column_alter(), |
| 217 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 218 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 219 | * @param string the column name |
| 220 | * @param array fields |
| 221 | * @param string the field after which we should add the new field |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 222 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 223 | */ |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 224 | public function _alter_table($alter_type, $table, $fields, $after_field = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 225 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 226 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 227 | |
| 228 | // DROP has everything it needs now. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 229 | if ($alter_type === 'DROP') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 230 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 231 | return $sql.$this->db->protect_identifiers($fields); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 234 | return $sql.$this->_process_fields($fields) |
| 235 | .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
| 239 | |
| 240 | /** |
| 241 | * Rename a table |
| 242 | * |
| 243 | * Generates a platform-specific query so that a table can be renamed |
| 244 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 245 | * @param string the old table name |
| 246 | * @param string the new table name |
| 247 | * @return string |
| 248 | */ |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 249 | public function _rename_table($table_name, $new_table_name) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 250 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 251 | return 'RENAME TABLE '.$this->db->protect_identifiers($table_name).' AS '.$this->db->protect_identifiers($new_table_name); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | /* End of file cubrid_forge.php */ |
Andrey Andreev | fead055 | 2012-03-20 15:23:06 +0200 | [diff] [blame^] | 257 | /* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ |