blob: 410d0652cdb3a40d3abfb994b5d2a6390660294a [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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 *
42 * @access private
43 * @param string the database name
44 * @return bool
45 */
46 function _create_database($name)
47 {
48 // CUBRID does not allow to create a database in SQL. The GUI tools
49 // have to be used for this purpose.
50 return FALSE;
51 }
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Drop database
57 *
58 * @access private
59 * @param string the database name
60 * @return bool
61 */
62 function _drop_database($name)
63 {
64 // CUBRID does not allow to drop a database in SQL. The GUI tools
65 // have to be used for this purpose.
66 return FALSE;
67 }
68
69 // --------------------------------------------------------------------
70
71 /**
72 * Process Fields
73 *
74 * @access private
75 * @param mixed the fields
76 * @return string
77 */
78 function _process_fields($fields)
79 {
80 $current_field_count = 0;
81 $sql = '';
82
83 foreach ($fields as $field=>$attributes)
84 {
85 // Numeric field names aren't allowed in databases, so if the key is
86 // numeric, we know it was assigned by PHP and the developer manually
87 // entered the field information, so we'll simply add it to the list
88 if (is_numeric($field))
89 {
90 $sql .= "\n\t$attributes";
91 }
92 else
93 {
94 $attributes = array_change_key_case($attributes, CASE_UPPER);
95
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070096 $sql .= "\n\t\"" . $this->db->_protect_identifiers($field) . "\"";
Esen Sagynov2e087942011-08-09 23:35:01 -070097
98 if (array_key_exists('NAME', $attributes))
99 {
100 $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
101 }
102
103 if (array_key_exists('TYPE', $attributes))
104 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700105 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -0700106
107 if (array_key_exists('CONSTRAINT', $attributes))
108 {
109 switch ($attributes['TYPE'])
110 {
111 case 'decimal':
112 case 'float':
113 case 'numeric':
114 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
115 break;
116 case 'enum': // As of version 8.4.0 CUBRID does not support
117 // enum data type.
118 break;
119 case 'set':
120 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
121 break;
122 default:
123 $sql .= '('.$attributes['CONSTRAINT'].')';
124 }
125 }
126 }
127
128 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
129 {
130 //$sql .= ' UNSIGNED';
131 // As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type.
132 // Will be supported in the next release as a part of MySQL Compatibility.
133 }
134
135 if (array_key_exists('DEFAULT', $attributes))
136 {
137 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
138 }
139
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700140 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700141 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700142 $sql .= ' NULL';
143 }
144 else
145 {
146 $sql .= ' NOT NULL';
Esen Sagynov2e087942011-08-09 23:35:01 -0700147 }
148
149 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
150 {
151 $sql .= ' AUTO_INCREMENT';
152 }
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700153
154 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700155 {
156 $sql .= ' UNIQUE';
157 }
158 }
159
160 // don't add a comma on the end of the last field
161 if (++$current_field_count < count($fields))
162 {
163 $sql .= ',';
164 }
165 }
166
167 return $sql;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Create Table
174 *
175 * @access private
176 * @param string the table name
177 * @param mixed the fields
178 * @param mixed primary key(s)
179 * @param mixed key(s)
180 * @param boolean should 'IF NOT EXISTS' be added to the SQL
181 * @return bool
182 */
183 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
184 {
185 $sql = 'CREATE TABLE ';
186
187 if ($if_not_exists === TRUE)
188 {
189 //$sql .= 'IF NOT EXISTS ';
190 // As of version 8.4.0 CUBRID does not support this SQL syntax.
191 }
192
193 $sql .= $this->db->_escape_identifiers($table)." (";
194
195 $sql .= $this->_process_fields($fields);
196
197 // If there is a PK defined
198 if (count($primary_keys) > 0)
199 {
200 $key_name = "pk_" . $table . "_" .
201 $this->db->_protect_identifiers(implode('_', $primary_keys));
202
203 $primary_keys = $this->db->_protect_identifiers($primary_keys);
204 $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")";
205 }
206
207 if (is_array($keys) && count($keys) > 0)
208 {
209 foreach ($keys as $key)
210 {
211 if (is_array($key))
212 {
213 $key_name = $this->db->_protect_identifiers(implode('_', $key));
214 $key = $this->db->_protect_identifiers($key);
215 }
216 else
217 {
218 $key_name = $this->db->_protect_identifiers($key);
219 $key = array($key_name);
220 }
221
Esen Sagynovee3e5942011-08-10 03:22:58 -0700222 $sql .= ",\n\tKEY \"{$key_name}\" (" . implode(', ', $key) . ")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700223 }
224 }
225
226 $sql .= "\n);";
227
228 return $sql;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Drop Table
235 *
236 * @access private
237 * @return string
238 */
239 function _drop_table($table)
240 {
241 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Alter table query
248 *
249 * Generates a platform-specific query so that a table can be altered
250 * Called by add_column(), drop_column(), and column_alter(),
251 *
252 * @access private
253 * @param string the ALTER type (ADD, DROP, CHANGE)
254 * @param string the column name
255 * @param array fields
256 * @param string the field after which we should add the new field
257 * @return object
258 */
259 function _alter_table($alter_type, $table, $fields, $after_field = '')
260 {
261 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
262
263 // DROP has everything it needs now.
264 if ($alter_type == 'DROP')
265 {
266 return $sql.$this->db->_protect_identifiers($fields);
267 }
268
269 $sql .= $this->_process_fields($fields);
270
271 if ($after_field != '')
272 {
273 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
274 }
275
276 return $sql;
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Rename a table
283 *
284 * Generates a platform-specific query so that a table can be renamed
285 *
286 * @access private
287 * @param string the old table name
288 * @param string the new table name
289 * @return string
290 */
291 function _rename_table($table_name, $new_table_name)
292 {
293 $sql = 'RENAME TABLE '.$this->db->_protect_identifiers($table_name)." AS ".$this->db->_protect_identifiers($new_table_name);
294 return $sql;
295 }
296
297}
298
299/* End of file cubrid_forge.php */
300/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */