blob: b6c6a3b52089314f23128e428c603159986b115e [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Esen Sagynov2e087942011-08-09 23:35:01 -07002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Esen Sagynov2e087942011-08-09 23:35:01 -07006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7f55d612012-01-26 13:44:28 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev7f55d612012-01-26 13:44:28 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 2.1.0
Esen Sagynov2e087942011-08-09 23:35:01 -070036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Esen Sagynov2e087942011-08-09 23:35:01 -070039
Esen Sagynov2e087942011-08-09 23:35:01 -070040/**
41 * CUBRID Forge Class
42 *
43 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070044 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070045 * @link http://codeigniter.com/user_guide/database/
46 */
47class CI_DB_cubrid_forge extends CI_DB_forge {
48
Andrey Andreevc98e93a2012-11-02 02:04:59 +020049 /**
50 * CREATE DATABASE statement
51 *
52 * @var string
53 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030054 protected $_create_database = FALSE;
Andrey Andreevc98e93a2012-11-02 02:04:59 +020055
Andrey Andreevc98e93a2012-11-02 02:04:59 +020056 /**
Andrey Andreevb0a97c12012-11-11 13:58:53 +020057 * CREATE TABLE keys flag
58 *
59 * Whether table keys are created from within the
60 * CREATE TABLE statement.
61 *
62 * @var bool
63 */
64 protected $_create_table_keys = TRUE;
65
66 /**
Andrey Andreevc98e93a2012-11-02 02:04:59 +020067 * DROP DATABASE statement
68 *
69 * @var string
70 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030071 protected $_drop_database = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070072
73 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020074 * CREATE TABLE IF statement
Esen Sagynov2e087942011-08-09 23:35:01 -070075 *
Andrey Andreeva287a342012-11-05 23:19:59 +020076 * @var string
Esen Sagynov2e087942011-08-09 23:35:01 -070077 */
Andrey Andreeva287a342012-11-05 23:19:59 +020078 protected $_create_table_if = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070079
Andrey Andreeva287a342012-11-05 23:19:59 +020080 /**
81 * UNSIGNED support
82 *
83 * @var array
84 */
85 protected $_unsigned = array(
86 'SHORT' => 'INTEGER',
87 'SMALLINT' => 'INTEGER',
88 'INT' => 'BIGINT',
89 'INTEGER' => 'BIGINT',
90 'BIGINT' => 'NUMERIC',
91 'FLOAT' => 'DOUBLE',
92 'REAL' => 'DOUBLE'
93 );
94
Zachary Flower9b512282014-11-02 21:51:19 -070095 /**
96 * COMMENT value representation in CREATE/ALTER TABLE statements
97 *
98 * @var string
99 */
100 protected $_comment = FALSE;
101
Andrey Andreeva287a342012-11-05 23:19:59 +0200102 // --------------------------------------------------------------------
103
104 /**
105 * ALTER TABLE
106 *
107 * @param string $alter_type ALTER type
108 * @param string $table Table name
109 * @param mixed $field Column definition
110 * @return string|string[]
111 */
112 protected function _alter_table($alter_type, $table, $field)
113 {
114 if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
Esen Sagynov2e087942011-08-09 23:35:01 -0700115 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200116 return parent::_alter_table($alter_type, $table, $field);
117 }
118
119 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
120 $sqls = array();
121 for ($i = 0, $c = count($field); $i < $c; $i++)
122 {
123 if ($field[$i]['_literal'] !== FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700124 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200125 $sqls[] = $sql.' CHANGE '.$field[$i]['_literal'];
Esen Sagynov2e087942011-08-09 23:35:01 -0700126 }
127 else
128 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200129 $alter_type = empty($field[$i]['new_name']) ? ' MODIFY ' : ' CHANGE ';
Andrey Andreev5e872502012-12-15 20:59:06 +0200130 $sqls[] = $sql.$alter_type.$this->_process_column($field[$i]);
Esen Sagynov2e087942011-08-09 23:35:01 -0700131 }
132 }
133
Andrey Andreeva287a342012-11-05 23:19:59 +0200134 return $sqls;
Esen Sagynov2e087942011-08-09 23:35:01 -0700135 }
136
137 // --------------------------------------------------------------------
138
139 /**
Andrey Andreevb67277b2012-11-12 12:51:14 +0200140 * Process column
141 *
142 * @param array $field
143 * @return string
144 */
145 protected function _process_column($field)
146 {
147 $extra_clause = isset($field['after'])
148 ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
149
150 if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
151 {
152 $extra_clause = ' FIRST';
153 }
154
155 return $this->db->escape_identifiers($field['name'])
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200156 .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
Andrey Andreevb67277b2012-11-12 12:51:14 +0200157 .' '.$field['type'].$field['length']
158 .$field['unsigned']
159 .$field['null']
160 .$field['default']
161 .$field['auto_increment']
162 .$field['unique']
163 .$extra_clause;
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200169 * Field attribute TYPE
Esen Sagynov2e087942011-08-09 23:35:01 -0700170 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200171 * Performs a data type mapping between different databases.
172 *
173 * @param array &$attributes
174 * @return void
Esen Sagynov2e087942011-08-09 23:35:01 -0700175 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200176 protected function _attr_type(&$attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -0700177 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200178 switch (strtoupper($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -0700179 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200180 case 'TINYINT':
181 $attributes['TYPE'] = 'SMALLINT';
182 $attributes['UNSIGNED'] = FALSE;
183 return;
184 case 'MEDIUMINT':
185 $attributes['TYPE'] = 'INTEGER';
186 $attributes['UNSIGNED'] = FALSE;
187 return;
188 default: return;
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 }
191
192 // --------------------------------------------------------------------
193
194 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200195 * Process indexes
Esen Sagynov2e087942011-08-09 23:35:01 -0700196 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200197 * @param string $table (ignored)
Andrey Andreev7f55d612012-01-26 13:44:28 +0200198 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 */
Andrey Andreev35451022012-11-25 17:20:04 +0200200 protected function _process_indexes($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700201 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200202 $sql = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700203
Andrey Andreeva287a342012-11-05 23:19:59 +0200204 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Esen Sagynov2e087942011-08-09 23:35:01 -0700205 {
Andrey Andreev35451022012-11-25 17:20:04 +0200206 if (is_array($this->keys[$i]))
207 {
208 for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
209 {
210 if ( ! isset($this->fields[$this->keys[$i][$i2]]))
211 {
212 unset($this->keys[$i][$i2]);
213 continue;
214 }
215 }
216 }
217 elseif ( ! isset($this->fields[$this->keys[$i]]))
Andrey Andreeva287a342012-11-05 23:19:59 +0200218 {
219 unset($this->keys[$i]);
220 continue;
221 }
222
223 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
224
225 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
226 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700227 }
228
Andrey Andreeva287a342012-11-05 23:19:59 +0200229 $this->keys = array();
230
231 return $sql;
Esen Sagynov2e087942011-08-09 23:35:01 -0700232 }
233
Esen Sagynov2e087942011-08-09 23:35:01 -0700234}
235
236/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200237/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */