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 | |
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 | 7719a03 | 2012-01-27 21:05:55 +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 | 7f55d61 | 2012-01-26 13:44:28 +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 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 58 | $sql .= "\n\t".$attributes; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 59 | } |
| 60 | else |
| 61 | { |
| 62 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 63 | $sql .= "\n\t".$this->db->protect_identifiers($field) |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 64 | .( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 65 | |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 66 | if ( ! empty($attributes['TYPE'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 67 | { |
Esen Sagynov | 2ab2b1e | 2011-08-11 00:41:16 -0700 | [diff] [blame] | 68 | $sql .= ' '.$attributes['TYPE']; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 69 | |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 70 | if ( ! empty($attributes['CONSTRAINT'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 71 | { |
| 72 | switch ($attributes['TYPE']) |
| 73 | { |
| 74 | case 'decimal': |
| 75 | case 'float': |
| 76 | case 'numeric': |
| 77 | $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; |
| 78 | break; |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 79 | case 'enum': |
| 80 | // Will be supported in the future as part a part of |
| 81 | // MySQL compatibility features. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 82 | break; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 83 | case 'set': |
| 84 | $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; |
| 85 | break; |
| 86 | default: |
| 87 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 92 | /* 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] | 93 | * Will be supported in the next release as a part of MySQL Compatibility. |
| 94 | * |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 95 | if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 96 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 97 | $sql .= ' UNSIGNED'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 98 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 99 | */ |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 100 | |
Andrey Andreev | 69a6093 | 2012-01-26 14:04:06 +0200 | [diff] [blame] | 101 | $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') |
Andrey Andreev | c6953f4 | 2012-01-26 14:43:16 +0200 | [diff] [blame] | 102 | .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') |
| 103 | .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '') |
| 104 | .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // don't add a comma on the end of the last field |
| 108 | if (++$current_field_count < count($fields)) |
| 109 | { |
| 110 | $sql .= ','; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return $sql; |
| 115 | } |
| 116 | |
| 117 | // -------------------------------------------------------------------- |
| 118 | |
| 119 | /** |
| 120 | * Create Table |
| 121 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 122 | * @param string the table name |
| 123 | * @param mixed the fields |
| 124 | * @param mixed primary key(s) |
| 125 | * @param mixed key(s) |
Andrey Andreev | 582d6a8 | 2012-03-20 15:13:49 +0200 | [diff] [blame] | 126 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 127 | * @return bool |
| 128 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 129 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 130 | { |
| 131 | $sql = 'CREATE TABLE '; |
| 132 | |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 133 | /* 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] | 134 | if ($if_not_exists === TRUE) |
| 135 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 136 | $sql .= 'IF NOT EXISTS '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 137 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 138 | */ |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 139 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 140 | $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 141 | |
| 142 | // If there is a PK defined |
| 143 | if (count($primary_keys) > 0) |
| 144 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 145 | $key_name = $this->db->protect_identifiers('pk_'.$table.'_'.implode('_', $primary_keys)); |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 146 | $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] | 147 | } |
| 148 | |
| 149 | if (is_array($keys) && count($keys) > 0) |
| 150 | { |
| 151 | foreach ($keys as $key) |
| 152 | { |
| 153 | if (is_array($key)) |
| 154 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 155 | $key_name = $this->db->protect_identifiers('idx_'.$table.implode('_', $key)); |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 156 | $key = $this->db->protect_identifiers($key); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
Andrey Andreev | 394a3f1 | 2012-02-15 14:35:11 +0200 | [diff] [blame] | 160 | $key_name = $this->db->protect_identifiers('idx_'.$table.$key); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 161 | $key = array($key_name); |
| 162 | } |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 163 | |
| 164 | $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 168 | return $sql."\n);"; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 174 | * Alter table query |
| 175 | * |
| 176 | * Generates a platform-specific query so that a table can be altered |
| 177 | * Called by add_column(), drop_column(), and column_alter(), |
| 178 | * |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 179 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 180 | * @param string the column name |
| 181 | * @param array fields |
| 182 | * @param string the field after which we should add the new field |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 183 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 184 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 185 | protected function _alter_table($alter_type, $table, $fields, $after_field = '') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 186 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 187 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 188 | |
| 189 | // DROP has everything it needs now. |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 190 | if ($alter_type === 'DROP') |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 191 | { |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 192 | return $sql.$this->db->protect_identifiers($fields); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 195 | return $sql.$this->_process_fields($fields) |
| 196 | .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : ''); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* End of file cubrid_forge.php */ |
Andrey Andreev | fead055 | 2012-03-20 15:23:06 +0200 | [diff] [blame] | 202 | /* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ |