Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 28 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 29 | /** |
| 30 | * CUBRID Forge Class |
| 31 | * |
| 32 | * @category Database |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 33 | * @author Esen Sagynov |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 34 | * @link http://codeigniter.com/user_guide/database/ |
| 35 | */ |
| 36 | class CI_DB_cubrid_forge extends CI_DB_forge { |
| 37 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame^] | 38 | /** |
| 39 | * CREATE DATABASE statement |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 43 | protected $_create_database = FALSE; |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame^] | 44 | |
| 45 | // -------------------------------------------------------------------- |
| 46 | |
| 47 | /** |
| 48 | * DROP DATABASE statement |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 52 | protected $_drop_database = FALSE; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Process Fields |
| 56 | * |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame^] | 57 | * @param mixed $fields |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 58 | * @return string |
| 59 | */ |
Andrey Andreev | 7719a03 | 2012-01-27 21:05:55 +0200 | [diff] [blame] | 60 | protected function _process_fields($fields) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 61 | { |
| 62 | $current_field_count = 0; |
| 63 | $sql = ''; |
| 64 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 65 | foreach ($fields as $field => $attributes) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 66 | { |
| 67 | // Numeric field names aren't allowed in databases, so if the key is |
| 68 | // numeric, we know it was assigned by PHP and the developer manually |
| 69 | // entered the field information, so we'll simply add it to the list |
| 70 | if (is_numeric($field)) |
| 71 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 72 | $sql .= "\n\t".$attributes; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 73 | } |
| 74 | else |
| 75 | { |
| 76 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 77 | |
| 78 | $sql .= "\n\t".$this->db->escape_identifiers($field); |
| 79 | |
Andrey Andreev | caa04f1 | 2012-06-08 01:39:20 +0300 | [diff] [blame] | 80 | empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 81 | |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 82 | if ( ! empty($attributes['TYPE'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 83 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 84 | $sql .= ' '.$attributes['TYPE']; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 85 | |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 86 | if ( ! empty($attributes['CONSTRAINT'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 87 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 88 | switch (strtolower($attributes['TYPE'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 89 | { |
| 90 | case 'decimal': |
| 91 | case 'float': |
| 92 | case 'numeric': |
| 93 | $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; |
| 94 | break; |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 95 | case 'enum': |
| 96 | // Will be supported in the future as part a part of |
| 97 | // MySQL compatibility features. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 98 | break; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 99 | case 'set': |
| 100 | $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; |
| 101 | break; |
| 102 | default: |
| 103 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 108 | /* 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] | 109 | * Will be supported in the next release as a part of MySQL Compatibility. |
| 110 | * |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 111 | if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 112 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 113 | $sql .= ' UNSIGNED'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 114 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 115 | */ |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 116 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 117 | if (isset($attributes['DEFAULT'])) |
| 118 | { |
| 119 | $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; |
| 120 | } |
| 121 | |
| 122 | $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) |
| 123 | ? ' NULL' : ' NOT NULL'; |
| 124 | |
| 125 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 126 | { |
| 127 | $sql .= ' AUTO_INCREMENT'; |
| 128 | } |
| 129 | |
| 130 | if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) |
| 131 | { |
| 132 | $sql .= ' UNIQUE'; |
| 133 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // don't add a comma on the end of the last field |
| 137 | if (++$current_field_count < count($fields)) |
| 138 | { |
| 139 | $sql .= ','; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return $sql; |
| 144 | } |
| 145 | |
| 146 | // -------------------------------------------------------------------- |
| 147 | |
| 148 | /** |
| 149 | * Create Table |
| 150 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 151 | * @param string the table name |
| 152 | * @param mixed the fields |
| 153 | * @param mixed primary key(s) |
| 154 | * @param mixed key(s) |
Andrey Andreev | 582d6a8 | 2012-03-20 15:13:49 +0200 | [diff] [blame] | 155 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 156 | * @return bool |
| 157 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 158 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 159 | { |
| 160 | $sql = 'CREATE TABLE '; |
| 161 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 162 | /* 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] | 163 | if ($if_not_exists === TRUE) |
| 164 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 165 | $sql .= 'IF NOT EXISTS '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 166 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 167 | */ |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 168 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 169 | $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 170 | |
| 171 | // If there is a PK defined |
| 172 | if (count($primary_keys) > 0) |
| 173 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 174 | $key_name = $this->db->escape_identifiers('pk_'.$table.'_'.implode('_', $primary_keys)); |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 175 | $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($primary_keys)).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | if (is_array($keys) && count($keys) > 0) |
| 179 | { |
| 180 | foreach ($keys as $key) |
| 181 | { |
| 182 | if (is_array($key)) |
| 183 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 184 | $key_name = $this->db->escape_identifiers('idx_'.$table.implode('_', $key)); |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 185 | $key = $this->db->escape_identifiers($key); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 186 | } |
| 187 | else |
| 188 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 189 | $key_name = $this->db->escape_identifiers('idx_'.$table.$key); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 190 | $key = array($key_name); |
| 191 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 192 | |
| 193 | $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 197 | return $sql."\n);"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // -------------------------------------------------------------------- |
| 201 | |
| 202 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 203 | * Alter table query |
| 204 | * |
| 205 | * Generates a platform-specific query so that a table can be altered |
| 206 | * Called by add_column(), drop_column(), and column_alter(), |
| 207 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 208 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 209 | * @param string the column name |
| 210 | * @param array fields |
| 211 | * @param string the field after which we should add the new field |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 212 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 213 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 214 | protected function _alter_table($alter_type, $table, $fields, $after_field = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 215 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 216 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 217 | |
| 218 | // DROP has everything it needs now. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 219 | if ($alter_type === 'DROP') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 220 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 221 | return $sql.$this->db->escape_identifiers($fields); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 224 | return $sql.$this->_process_fields($fields) |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 225 | .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* End of file cubrid_forge.php */ |
Andrey Andreev | fead055 | 2012-03-20 15:23:06 +0200 | [diff] [blame] | 231 | /* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ |