Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame^] | 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 ExpressionEngine Dev Team |
| 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)) |
| 129 | { |
| 130 | $sql .= ($attributes['NULL'] === TRUE) ? '' : ' NOT NULL'; |
| 131 | } |
| 132 | |
| 133 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 134 | { |
| 135 | $sql .= ' AUTO_INCREMENT'; |
| 136 | } |
| 137 | |
| 138 | if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) |
| 139 | { |
| 140 | $sql .= ' UNIQUE'; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // don't add a comma on the end of the last field |
| 145 | if (++$current_field_count < count($fields)) |
| 146 | { |
| 147 | $sql .= ','; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return $sql; |
| 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Create Table |
| 158 | * |
| 159 | * @access private |
| 160 | * @param string the table name |
| 161 | * @param mixed the fields |
| 162 | * @param mixed primary key(s) |
| 163 | * @param mixed key(s) |
| 164 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 165 | * @return bool |
| 166 | */ |
| 167 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 168 | { |
| 169 | $sql = 'CREATE TABLE '; |
| 170 | |
| 171 | if ($if_not_exists === TRUE) |
| 172 | { |
| 173 | //$sql .= 'IF NOT EXISTS '; |
| 174 | // As of version 8.4.0 CUBRID does not support this SQL syntax. |
| 175 | } |
| 176 | |
| 177 | $sql .= $this->db->_escape_identifiers($table)." ("; |
| 178 | |
| 179 | $sql .= $this->_process_fields($fields); |
| 180 | |
| 181 | // If there is a PK defined |
| 182 | if (count($primary_keys) > 0) |
| 183 | { |
| 184 | $key_name = "pk_" . $table . "_" . |
| 185 | $this->db->_protect_identifiers(implode('_', $primary_keys)); |
| 186 | |
| 187 | $primary_keys = $this->db->_protect_identifiers($primary_keys); |
| 188 | $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")"; |
| 189 | } |
| 190 | |
| 191 | if (is_array($keys) && count($keys) > 0) |
| 192 | { |
| 193 | foreach ($keys as $key) |
| 194 | { |
| 195 | if (is_array($key)) |
| 196 | { |
| 197 | $key_name = $this->db->_protect_identifiers(implode('_', $key)); |
| 198 | $key = $this->db->_protect_identifiers($key); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | $key_name = $this->db->_protect_identifiers($key); |
| 203 | $key = array($key_name); |
| 204 | } |
| 205 | |
| 206 | $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")"; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | $sql .= "\n);"; |
| 211 | |
| 212 | return $sql; |
| 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Drop Table |
| 219 | * |
| 220 | * @access private |
| 221 | * @return string |
| 222 | */ |
| 223 | function _drop_table($table) |
| 224 | { |
| 225 | return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table); |
| 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
| 229 | |
| 230 | /** |
| 231 | * Alter table query |
| 232 | * |
| 233 | * Generates a platform-specific query so that a table can be altered |
| 234 | * Called by add_column(), drop_column(), and column_alter(), |
| 235 | * |
| 236 | * @access private |
| 237 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 238 | * @param string the column name |
| 239 | * @param array fields |
| 240 | * @param string the field after which we should add the new field |
| 241 | * @return object |
| 242 | */ |
| 243 | function _alter_table($alter_type, $table, $fields, $after_field = '') |
| 244 | { |
| 245 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type "; |
| 246 | |
| 247 | // DROP has everything it needs now. |
| 248 | if ($alter_type == 'DROP') |
| 249 | { |
| 250 | return $sql.$this->db->_protect_identifiers($fields); |
| 251 | } |
| 252 | |
| 253 | $sql .= $this->_process_fields($fields); |
| 254 | |
| 255 | if ($after_field != '') |
| 256 | { |
| 257 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 258 | } |
| 259 | |
| 260 | return $sql; |
| 261 | } |
| 262 | |
| 263 | // -------------------------------------------------------------------- |
| 264 | |
| 265 | /** |
| 266 | * Rename a table |
| 267 | * |
| 268 | * Generates a platform-specific query so that a table can be renamed |
| 269 | * |
| 270 | * @access private |
| 271 | * @param string the old table name |
| 272 | * @param string the new table name |
| 273 | * @return string |
| 274 | */ |
| 275 | function _rename_table($table_name, $new_table_name) |
| 276 | { |
| 277 | $sql = 'RENAME TABLE '.$this->db->_protect_identifiers($table_name)." AS ".$this->db->_protect_identifiers($new_table_name); |
| 278 | return $sql; |
| 279 | } |
| 280 | |
| 281 | } |
| 282 | |
| 283 | /* End of file cubrid_forge.php */ |
| 284 | /* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ |