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 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 45 | /** |
Andrey Andreev | b0a97c1 | 2012-11-11 13:58:53 +0200 | [diff] [blame] | 46 | * CREATE TABLE keys flag |
| 47 | * |
| 48 | * Whether table keys are created from within the |
| 49 | * CREATE TABLE statement. |
| 50 | * |
| 51 | * @var bool |
| 52 | */ |
| 53 | protected $_create_table_keys = TRUE; |
| 54 | |
| 55 | /** |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 56 | * DROP DATABASE statement |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 60 | protected $_drop_database = FALSE; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 63 | * CREATE TABLE IF statement |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 64 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 65 | * @var string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 66 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 67 | protected $_create_table_if = FALSE; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 68 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 69 | /** |
| 70 | * UNSIGNED support |
| 71 | * |
| 72 | * @var array |
| 73 | */ |
| 74 | protected $_unsigned = array( |
| 75 | 'SHORT' => 'INTEGER', |
| 76 | 'SMALLINT' => 'INTEGER', |
| 77 | 'INT' => 'BIGINT', |
| 78 | 'INTEGER' => 'BIGINT', |
| 79 | 'BIGINT' => 'NUMERIC', |
| 80 | 'FLOAT' => 'DOUBLE', |
| 81 | 'REAL' => 'DOUBLE' |
| 82 | ); |
| 83 | |
| 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * ALTER TABLE |
| 88 | * |
| 89 | * @param string $alter_type ALTER type |
| 90 | * @param string $table Table name |
| 91 | * @param mixed $field Column definition |
| 92 | * @return string|string[] |
| 93 | */ |
| 94 | protected function _alter_table($alter_type, $table, $field) |
| 95 | { |
| 96 | if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 97 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 98 | return parent::_alter_table($alter_type, $table, $field); |
| 99 | } |
| 100 | |
| 101 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); |
| 102 | $sqls = array(); |
| 103 | for ($i = 0, $c = count($field); $i < $c; $i++) |
| 104 | { |
| 105 | if ($field[$i]['_literal'] !== FALSE) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 106 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 107 | $sqls[] = $sql.' CHANGE '.$field[$i]['_literal']; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 111 | $alter_type = empty($field[$i]['new_name']) ? ' MODIFY ' : ' CHANGE '; |
| 112 | $sqls[] = $sql.$alter_type$this->_process_column($field[$i]); |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 116 | return $sqls; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // -------------------------------------------------------------------- |
| 120 | |
| 121 | /** |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 122 | * Process column |
| 123 | * |
| 124 | * @param array $field |
| 125 | * @return string |
| 126 | */ |
| 127 | protected function _process_column($field) |
| 128 | { |
| 129 | $extra_clause = isset($field['after']) |
| 130 | ? ' AFTER '.$this->db->escape_identifiers($field['after']) : ''; |
| 131 | |
| 132 | if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE) |
| 133 | { |
| 134 | $extra_clause = ' FIRST'; |
| 135 | } |
| 136 | |
| 137 | return $this->db->escape_identifiers($field['name']) |
Andrey Andreev | 7ade8b7 | 2012-11-22 13:12:22 +0200 | [diff] [blame] | 138 | .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name'])) |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 139 | .' '.$field['type'].$field['length'] |
| 140 | .$field['unsigned'] |
| 141 | .$field['null'] |
| 142 | .$field['default'] |
| 143 | .$field['auto_increment'] |
| 144 | .$field['unique'] |
| 145 | .$extra_clause; |
| 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 151 | * Field attribute TYPE |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 152 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 153 | * Performs a data type mapping between different databases. |
| 154 | * |
| 155 | * @param array &$attributes |
| 156 | * @return void |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 157 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 158 | protected function _attr_type(&$attributes) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 159 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 160 | switch (strtoupper($attributes['TYPE'])) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 161 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 162 | case 'TINYINT': |
| 163 | $attributes['TYPE'] = 'SMALLINT'; |
| 164 | $attributes['UNSIGNED'] = FALSE; |
| 165 | return; |
| 166 | case 'MEDIUMINT': |
| 167 | $attributes['TYPE'] = 'INTEGER'; |
| 168 | $attributes['UNSIGNED'] = FALSE; |
| 169 | return; |
| 170 | default: return; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 171 | } |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 177 | * Process indexes |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 178 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 179 | * @param string $table (ignored) |
Andrey Andreev | 7f55d61 | 2012-01-26 13:44:28 +0200 | [diff] [blame] | 180 | * @return string |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 181 | */ |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame^] | 182 | protected function _process_indexes($table) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 183 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 184 | $sql = ''; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 185 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 186 | for ($i = 0, $c = count($this->keys); $i < $c; $i++) |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 187 | { |
Andrey Andreev | 3545102 | 2012-11-25 17:20:04 +0200 | [diff] [blame^] | 188 | if (is_array($this->keys[$i])) |
| 189 | { |
| 190 | for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++) |
| 191 | { |
| 192 | if ( ! isset($this->fields[$this->keys[$i][$i2]])) |
| 193 | { |
| 194 | unset($this->keys[$i][$i2]); |
| 195 | continue; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | elseif ( ! isset($this->fields[$this->keys[$i]])) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 200 | { |
| 201 | unset($this->keys[$i]); |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]); |
| 206 | |
| 207 | $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) |
| 208 | .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 211 | $this->keys = array(); |
| 212 | |
| 213 | return $sql; |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Esen Sagynov | 2e08794 | 2011-08-09 23:35:01 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* End of file cubrid_forge.php */ |
Andrey Andreev | fead055 | 2012-03-20 15:23:06 +0200 | [diff] [blame] | 219 | /* Location: ./system/database/drivers/cubrid/cubrid_forge.php */ |