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