blob: 4b1c2839ae0e3eae5696370e626ee8212c80283e [file] [log] [blame]
Andrey Andreev7f55d612012-01-26 13:44:28 +02001<?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
Andrey Andreev7f55d612012-01-26 13:44:28 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7f55d612012-01-26 13:44:28 +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 Andreev7f55d612012-01-26 13:44:28 +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 Andreev7f55d612012-01-26 13:44:28 +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 Andreev7f55d612012-01-26 13:44:28 +020073 private function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070074 {
75 $current_field_count = 0;
76 $sql = '';
77
Andrey Andreev7f55d612012-01-26 13:44:28 +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 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020085 $sql .= "\n\t".$attributes;
Esen Sagynov2e087942011-08-09 23:35:01 -070086 }
87 else
88 {
89 $attributes = array_change_key_case($attributes, CASE_UPPER);
90
Andrey Andreev7f55d612012-01-26 13:44:28 +020091 $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"'
Andrey Andreevc6953f42012-01-26 14:43:16 +020092 .( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : '');
Esen Sagynov2e087942011-08-09 23:35:01 -070093
Andrey Andreev69a60932012-01-26 14:04:06 +020094 if ( ! empty($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070095 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070096 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -070097
Andrey Andreevc6953f42012-01-26 14:43:16 +020098 if ( ! empty($attributes['CONSTRAINT']))
Esen Sagynov2e087942011-08-09 23:35:01 -070099 {
100 switch ($attributes['TYPE'])
101 {
102 case 'decimal':
103 case 'float':
104 case 'numeric':
105 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
106 break;
Andrey Andreev7f55d612012-01-26 13:44:28 +0200107 case 'enum': // As of version 8.4.0 CUBRID does not support enum data type
108 break;
Esen Sagynov2e087942011-08-09 23:35:01 -0700109 case 'set':
110 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
111 break;
112 default:
113 $sql .= '('.$attributes['CONSTRAINT'].')';
114 }
115 }
116 }
117
Andrey Andreev7f55d612012-01-26 13:44:28 +0200118 /* As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type.
119 * Will be supported in the next release as a part of MySQL Compatibility.
120 *
Andrey Andreev69a60932012-01-26 14:04:06 +0200121 if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700122 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200123 $sql .= ' UNSIGNED';
Esen Sagynov2e087942011-08-09 23:35:01 -0700124 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200125 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700126
Andrey Andreev69a60932012-01-26 14:04:06 +0200127 $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
Andrey Andreevc6953f42012-01-26 14:43:16 +0200128 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
129 .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '')
130 .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700131 }
132
133 // don't add a comma on the end of the last field
134 if (++$current_field_count < count($fields))
135 {
136 $sql .= ',';
137 }
138 }
139
140 return $sql;
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Create Table
147 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700148 * @param string the table name
149 * @param mixed the fields
150 * @param mixed primary key(s)
151 * @param mixed key(s)
152 * @param boolean should 'IF NOT EXISTS' be added to the SQL
153 * @return bool
154 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200155 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 {
157 $sql = 'CREATE TABLE ';
158
Andrey Andreev7f55d612012-01-26 13:44:28 +0200159 /* As of version 8.4.0 CUBRID does not support this SQL syntax.
Esen Sagynov2e087942011-08-09 23:35:01 -0700160 if ($if_not_exists === TRUE)
161 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200162 $sql .= 'IF NOT EXISTS ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200164 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700165
Andrey Andreev7f55d612012-01-26 13:44:28 +0200166 $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700167
168 // If there is a PK defined
169 if (count($primary_keys) > 0)
170 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200171 $key_name = 'pk_'.$table.'_'.$this->db->protect_identifiers(implode('_', $primary_keys));
172 $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->protect_identifiers($primary_keys)).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700173 }
174
175 if (is_array($keys) && count($keys) > 0)
176 {
177 foreach ($keys as $key)
178 {
179 if (is_array($key))
180 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200181 $key_name = $this->db->protect_identifiers(implode('_', $key));
182 $key = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 }
184 else
185 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200186 $key_name = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 $key = array($key_name);
188 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200189
190 $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700191 }
192 }
193
Andrey Andreev7f55d612012-01-26 13:44:28 +0200194 return $sql."\n);";
Esen Sagynov2e087942011-08-09 23:35:01 -0700195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Drop Table
201 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700202 * @return string
203 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200204 public function _drop_table($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700205 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200206 return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table);
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Alter table query
213 *
214 * Generates a platform-specific query so that a table can be altered
215 * Called by add_column(), drop_column(), and column_alter(),
216 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700217 * @param string the ALTER type (ADD, DROP, CHANGE)
218 * @param string the column name
219 * @param array fields
220 * @param string the field after which we should add the new field
Andrey Andreev7f55d612012-01-26 13:44:28 +0200221 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700222 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200223 public function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700224 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200225 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700226
227 // DROP has everything it needs now.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200228 if ($alter_type === 'DROP')
Esen Sagynov2e087942011-08-09 23:35:01 -0700229 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200230 return $sql.$this->db->protect_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700231 }
232
Andrey Andreev7f55d612012-01-26 13:44:28 +0200233 return $sql.$this->_process_fields($fields)
234 .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Rename a table
241 *
242 * Generates a platform-specific query so that a table can be renamed
243 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700244 * @param string the old table name
245 * @param string the new table name
246 * @return string
247 */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200248 public function _rename_table($table_name, $new_table_name)
Esen Sagynov2e087942011-08-09 23:35:01 -0700249 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200250 return 'RENAME TABLE '.$this->db->protect_identifiers($table_name).' AS '.$this->db->protect_identifiers($new_table_name);
Esen Sagynov2e087942011-08-09 23:35:01 -0700251 }
252
253}
254
255/* End of file cubrid_forge.php */
Andrey Andreev7f55d612012-01-26 13:44:28 +0200256/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */