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