blob: 6bfc7c28f7df49ca3bfdcdaad84f4ed2bcd466b5 [file] [log] [blame]
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
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 Sagynov2e087942011-08-09 23:35:01 -070019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Esen Sagynov2e087942011-08-09 23:35:01 -070023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CUBRID Forge Class
32 *
33 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070034 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_cubrid_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
Esen Sagynov2e087942011-08-09 23:35:01 -070042 * @param string the database name
43 * @return bool
44 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -040045 protected function _create_database($name)
Esen Sagynov2e087942011-08-09 23:35:01 -070046 {
47 // CUBRID does not allow to create a database in SQL. The GUI tools
48 // have to be used for this purpose.
49 return FALSE;
50 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Drop database
56 *
Esen Sagynov2e087942011-08-09 23:35:01 -070057 * @param string the database name
58 * @return bool
59 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -040060 protected function _drop_database($name)
Esen Sagynov2e087942011-08-09 23:35:01 -070061 {
62 // CUBRID does not allow to drop a database in SQL. The GUI tools
63 // have to be used for this purpose.
64 return FALSE;
65 }
66
67 // --------------------------------------------------------------------
68
69 /**
70 * Process Fields
71 *
Esen Sagynov2e087942011-08-09 23:35:01 -070072 * @param mixed the fields
73 * @return string
74 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -040075 protected function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070076 {
77 $current_field_count = 0;
78 $sql = '';
79
80 foreach ($fields as $field=>$attributes)
81 {
82 // Numeric field names aren't allowed in databases, so if the key is
83 // numeric, we know it was assigned by PHP and the developer manually
84 // entered the field information, so we'll simply add it to the list
85 if (is_numeric($field))
86 {
87 $sql .= "\n\t$attributes";
88 }
89 else
90 {
91 $attributes = array_change_key_case($attributes, CASE_UPPER);
92
Andrey Andreev032e7ea2012-03-06 19:48:35 +020093 $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"';
Esen Sagynov2e087942011-08-09 23:35:01 -070094
95 if (array_key_exists('NAME', $attributes))
96 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +020097 $sql .= ' '.$this->db->protect_identifiers($attributes['NAME']).' ';
Esen Sagynov2e087942011-08-09 23:35:01 -070098 }
99
100 if (array_key_exists('TYPE', $attributes))
101 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700102 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -0700103
104 if (array_key_exists('CONSTRAINT', $attributes))
105 {
106 switch ($attributes['TYPE'])
107 {
108 case 'decimal':
109 case 'float':
110 case 'numeric':
111 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
112 break;
113 case 'enum': // As of version 8.4.0 CUBRID does not support
114 // enum data type.
115 break;
116 case 'set':
117 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
118 break;
119 default:
120 $sql .= '('.$attributes['CONSTRAINT'].')';
121 }
122 }
123 }
124
125 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
126 {
127 //$sql .= ' UNSIGNED';
128 // As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type.
129 // Will be supported in the next release as a part of MySQL Compatibility.
130 }
131
132 if (array_key_exists('DEFAULT', $attributes))
133 {
134 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
135 }
136
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700137 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700138 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700139 $sql .= ' NULL';
140 }
141 else
142 {
143 $sql .= ' NOT NULL';
Esen Sagynov2e087942011-08-09 23:35:01 -0700144 }
145
146 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
147 {
148 $sql .= ' AUTO_INCREMENT';
149 }
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700150
151 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700152 {
153 $sql .= ' UNIQUE';
154 }
155 }
156
157 // don't add a comma on the end of the last field
158 if (++$current_field_count < count($fields))
159 {
160 $sql .= ',';
161 }
162 }
163
164 return $sql;
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Create Table
171 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 * @param string the table name
173 * @param mixed the fields
174 * @param mixed primary key(s)
175 * @param mixed key(s)
176 * @param boolean should 'IF NOT EXISTS' be added to the SQL
177 * @return bool
178 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -0400179 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 {
181 $sql = 'CREATE TABLE ';
182
183 if ($if_not_exists === TRUE)
184 {
185 //$sql .= 'IF NOT EXISTS ';
186 // As of version 8.4.0 CUBRID does not support this SQL syntax.
187 }
188
189 $sql .= $this->db->_escape_identifiers($table)." (";
190
191 $sql .= $this->_process_fields($fields);
192
193 // If there is a PK defined
194 if (count($primary_keys) > 0)
195 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200196 $key_name = 'pk_'.$table.'_'.$this->db->protect_identifiers(implode('_', $primary_keys));
197
198 $primary_keys = $this->db->protect_identifiers($primary_keys);
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")";
200 }
201
202 if (is_array($keys) && count($keys) > 0)
203 {
204 foreach ($keys as $key)
205 {
206 if (is_array($key))
207 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200208 $key_name = $this->db->protect_identifiers(implode('_', $key));
209 $key = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700210 }
211 else
212 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200213 $key_name = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700214 $key = array($key_name);
215 }
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200216
Esen Sagynovee3e5942011-08-10 03:22:58 -0700217 $sql .= ",\n\tKEY \"{$key_name}\" (" . implode(', ', $key) . ")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700218 }
219 }
220
221 $sql .= "\n);";
222
223 return $sql;
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Drop Table
230 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700231 * @return string
232 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -0400233 protected function _drop_table($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700234 {
235 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Alter table query
242 *
243 * Generates a platform-specific query so that a table can be altered
244 * Called by add_column(), drop_column(), and column_alter(),
245 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700246 * @param string the ALTER type (ADD, DROP, CHANGE)
247 * @param string the column name
248 * @param array fields
249 * @param string the field after which we should add the new field
250 * @return object
251 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -0400252 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700253 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200254 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700255
256 // DROP has everything it needs now.
257 if ($alter_type == 'DROP')
258 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200259 return $sql.$this->db->protect_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700260 }
261
262 $sql .= $this->_process_fields($fields);
263
264 if ($after_field != '')
265 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200266 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Esen Sagynov2e087942011-08-09 23:35:01 -0700267 }
268
269 return $sql;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Rename a table
276 *
277 * Generates a platform-specific query so that a table can be renamed
278 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700279 * @param string the old table name
280 * @param string the new table name
281 * @return string
282 */
Timothy Warrenf83f0d52012-03-19 17:38:30 -0400283 protected function _rename_table($table_name, $new_table_name)
Esen Sagynov2e087942011-08-09 23:35:01 -0700284 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200285 return 'RENAME TABLE '.$this->db->protect_identifiers($table_name).' AS '.$this->db->protect_identifiers($new_table_name);
Esen Sagynov2e087942011-08-09 23:35:01 -0700286 }
287
288}
289
290/* End of file cubrid_forge.php */
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200291/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */