blob: bbda484c4215c1ee70a9ca05f6a391bb2c975ac6 [file] [log] [blame]
Andrey Andreev582d6a82012-03-20 15:13:49 +02001<?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
Andrey Andreev582d6a82012-03-20 15:13:49 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev582d6a82012-03-20 15:13:49 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Esen Sagynov2e087942011-08-09 23:35:01 -070028/**
29 * CUBRID Forge Class
30 *
31 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070032 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070033 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_cubrid_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Esen Sagynov2e087942011-08-09 23:35:01 -070040 * @param string the database name
41 * @return bool
42 */
Andrey Andreev582d6a82012-03-20 15:13:49 +020043 public function _create_database($name)
Esen Sagynov2e087942011-08-09 23:35:01 -070044 {
45 // CUBRID does not allow to create a database in SQL. The GUI tools
46 // have to be used for this purpose.
47 return FALSE;
48 }
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Drop database
54 *
Esen Sagynov2e087942011-08-09 23:35:01 -070055 * @param string the database name
56 * @return bool
57 */
Andrey Andreev582d6a82012-03-20 15:13:49 +020058 public function _drop_database($name)
Esen Sagynov2e087942011-08-09 23:35:01 -070059 {
60 // CUBRID does not allow to drop a database in SQL. The GUI tools
61 // have to be used for this purpose.
62 return FALSE;
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Process Fields
69 *
Esen Sagynov2e087942011-08-09 23:35:01 -070070 * @param mixed the fields
71 * @return string
72 */
Andrey Andreev582d6a82012-03-20 15:13:49 +020073 protected function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070074 {
75 $current_field_count = 0;
76 $sql = '';
77
Andrey Andreev582d6a82012-03-20 15:13:49 +020078 foreach ($fields as $field => $attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -070079 {
80 // Numeric field names aren't allowed in databases, so if the key is
81 // numeric, we know it was assigned by PHP and the developer manually
82 // entered the field information, so we'll simply add it to the list
83 if (is_numeric($field))
84 {
85 $sql .= "\n\t$attributes";
86 }
87 else
88 {
89 $attributes = array_change_key_case($attributes, CASE_UPPER);
90
Andrey Andreev032e7ea2012-03-06 19:48:35 +020091 $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"';
Esen Sagynov2e087942011-08-09 23:35:01 -070092
93 if (array_key_exists('NAME', $attributes))
94 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +020095 $sql .= ' '.$this->db->protect_identifiers($attributes['NAME']).' ';
Esen Sagynov2e087942011-08-09 23:35:01 -070096 }
97
98 if (array_key_exists('TYPE', $attributes))
99 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700100 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -0700101
102 if (array_key_exists('CONSTRAINT', $attributes))
103 {
104 switch ($attributes['TYPE'])
105 {
106 case 'decimal':
107 case 'float':
108 case 'numeric':
109 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
110 break;
111 case 'enum': // As of version 8.4.0 CUBRID does not support
112 // enum data type.
113 break;
114 case 'set':
115 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
116 break;
117 default:
118 $sql .= '('.$attributes['CONSTRAINT'].')';
119 }
120 }
121 }
122
123 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
124 {
125 //$sql .= ' UNSIGNED';
126 // As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type.
127 // Will be supported in the next release as a part of MySQL Compatibility.
128 }
129
130 if (array_key_exists('DEFAULT', $attributes))
131 {
132 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
133 }
134
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700135 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700136 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700137 $sql .= ' NULL';
138 }
139 else
140 {
141 $sql .= ' NOT NULL';
Esen Sagynov2e087942011-08-09 23:35:01 -0700142 }
143
144 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
145 {
146 $sql .= ' AUTO_INCREMENT';
147 }
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700148
149 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700150 {
151 $sql .= ' UNIQUE';
152 }
153 }
154
155 // don't add a comma on the end of the last field
156 if (++$current_field_count < count($fields))
157 {
158 $sql .= ',';
159 }
160 }
161
162 return $sql;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Create Table
169 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700170 * @param string the table name
171 * @param mixed the fields
172 * @param mixed primary key(s)
173 * @param mixed key(s)
Andrey Andreev582d6a82012-03-20 15:13:49 +0200174 * @param bool should 'IF NOT EXISTS' be added to the SQL
Esen Sagynov2e087942011-08-09 23:35:01 -0700175 * @return bool
176 */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200177 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700178 {
179 $sql = 'CREATE TABLE ';
180
181 if ($if_not_exists === TRUE)
182 {
183 //$sql .= 'IF NOT EXISTS ';
184 // As of version 8.4.0 CUBRID does not support this SQL syntax.
185 }
186
187 $sql .= $this->db->_escape_identifiers($table)." (";
188
189 $sql .= $this->_process_fields($fields);
190
191 // If there is a PK defined
192 if (count($primary_keys) > 0)
193 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200194 $key_name = 'pk_'.$table.'_'.$this->db->protect_identifiers(implode('_', $primary_keys));
195
196 $primary_keys = $this->db->protect_identifiers($primary_keys);
Esen Sagynov2e087942011-08-09 23:35:01 -0700197 $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")";
198 }
199
200 if (is_array($keys) && count($keys) > 0)
201 {
202 foreach ($keys as $key)
203 {
204 if (is_array($key))
205 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200206 $key_name = $this->db->protect_identifiers(implode('_', $key));
207 $key = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700208 }
209 else
210 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200211 $key_name = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700212 $key = array($key_name);
213 }
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200214
Esen Sagynovee3e5942011-08-10 03:22:58 -0700215 $sql .= ",\n\tKEY \"{$key_name}\" (" . implode(', ', $key) . ")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700216 }
217 }
218
219 $sql .= "\n);";
220
221 return $sql;
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Drop Table
228 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700229 * @return string
230 */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200231 public function _drop_table($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700232 {
233 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Alter table query
240 *
241 * Generates a platform-specific query so that a table can be altered
242 * Called by add_column(), drop_column(), and column_alter(),
243 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700244 * @param string the ALTER type (ADD, DROP, CHANGE)
245 * @param string the column name
246 * @param array fields
247 * @param string the field after which we should add the new field
Andrey Andreev582d6a82012-03-20 15:13:49 +0200248 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700249 */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200250 public function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700251 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200252 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700253
254 // DROP has everything it needs now.
255 if ($alter_type == 'DROP')
256 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200257 return $sql.$this->db->protect_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700258 }
259
260 $sql .= $this->_process_fields($fields);
261
262 if ($after_field != '')
263 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200264 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Esen Sagynov2e087942011-08-09 23:35:01 -0700265 }
266
267 return $sql;
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Rename a table
274 *
275 * Generates a platform-specific query so that a table can be renamed
276 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700277 * @param string the old table name
278 * @param string the new table name
279 * @return string
280 */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200281 public function _rename_table($table_name, $new_table_name)
Esen Sagynov2e087942011-08-09 23:35:01 -0700282 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200283 return 'RENAME TABLE '.$this->db->protect_identifiers($table_name).' AS '.$this->db->protect_identifiers($new_table_name);
Esen Sagynov2e087942011-08-09 23:35:01 -0700284 }
285
286}
287
288/* End of file cubrid_forge.php */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200289/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */