blob: 33d502137724a2bed3eb746208beeb771b4ba756 [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 *
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
Andrey Andreevcf631202012-02-16 11:36:28 +020024 * @since Version 2.1
Esen Sagynov2e087942011-08-09 23:35:01 -070025 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Esen Sagynov2e087942011-08-09 23:35:01 -070028
Esen Sagynov2e087942011-08-09 23:35:01 -070029/**
30 * CUBRID Forge Class
31 *
32 * @category Database
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070033 * @author Esen Sagynov
Esen Sagynov2e087942011-08-09 23:35:01 -070034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_cubrid_forge extends CI_DB_forge {
37
Andrey Andreevc98e93a2012-11-02 02:04:59 +020038 /**
39 * CREATE DATABASE statement
40 *
41 * @var string
42 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030043 protected $_create_database = FALSE;
Andrey Andreevc98e93a2012-11-02 02:04:59 +020044
45 // --------------------------------------------------------------------
46
47 /**
48 * DROP DATABASE statement
49 *
50 * @var string
51 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030052 protected $_drop_database = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070053
54 /**
55 * Process Fields
56 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020057 * @param mixed $fields
Esen Sagynov2e087942011-08-09 23:35:01 -070058 * @return string
59 */
Andrey Andreev7719a032012-01-27 21:05:55 +020060 protected function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070061 {
62 $current_field_count = 0;
63 $sql = '';
64
Andrey Andreev7f55d612012-01-26 13:44:28 +020065 foreach ($fields as $field => $attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -070066 {
67 // Numeric field names aren't allowed in databases, so if the key is
68 // numeric, we know it was assigned by PHP and the developer manually
69 // entered the field information, so we'll simply add it to the list
70 if (is_numeric($field))
71 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020072 $sql .= "\n\t".$attributes;
Esen Sagynov2e087942011-08-09 23:35:01 -070073 }
74 else
75 {
76 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev5ef194d2012-06-08 01:12:54 +030077
78 $sql .= "\n\t".$this->db->escape_identifiers($field);
79
Andrey Andreevcaa04f12012-06-08 01:39:20 +030080 empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' ';
Esen Sagynov2e087942011-08-09 23:35:01 -070081
Andrey Andreev69a60932012-01-26 14:04:06 +020082 if ( ! empty($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070083 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070084 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -070085
Andrey Andreevc6953f42012-01-26 14:43:16 +020086 if ( ! empty($attributes['CONSTRAINT']))
Esen Sagynov2e087942011-08-09 23:35:01 -070087 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030088 switch (strtolower($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070089 {
90 case 'decimal':
91 case 'float':
92 case 'numeric':
93 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
94 break;
Andrey Andreev394a3f12012-02-15 14:35:11 +020095 case 'enum':
96 // Will be supported in the future as part a part of
97 // MySQL compatibility features.
Andrey Andreev7f55d612012-01-26 13:44:28 +020098 break;
Esen Sagynov2e087942011-08-09 23:35:01 -070099 case 'set':
100 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
101 break;
102 default:
103 $sql .= '('.$attributes['CONSTRAINT'].')';
104 }
105 }
106 }
107
Andrey Andreev394a3f12012-02-15 14:35:11 +0200108 /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200109 * Will be supported in the next release as a part of MySQL Compatibility.
110 *
Andrey Andreev69a60932012-01-26 14:04:06 +0200111 if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700112 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200113 $sql .= ' UNSIGNED';
Esen Sagynov2e087942011-08-09 23:35:01 -0700114 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200115 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700116
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300117 if (isset($attributes['DEFAULT']))
118 {
119 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
120 }
121
122 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
123 ? ' NULL' : ' NOT NULL';
124
125 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
126 {
127 $sql .= ' AUTO_INCREMENT';
128 }
129
130 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
131 {
132 $sql .= ' UNIQUE';
133 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700134 }
135
136 // don't add a comma on the end of the last field
137 if (++$current_field_count < count($fields))
138 {
139 $sql .= ',';
140 }
141 }
142
143 return $sql;
144 }
145
146 // --------------------------------------------------------------------
147
148 /**
149 * Create Table
150 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700151 * @param string the table name
152 * @param mixed the fields
153 * @param mixed primary key(s)
154 * @param mixed key(s)
Andrey Andreev582d6a82012-03-20 15:13:49 +0200155 * @param bool should 'IF NOT EXISTS' be added to the SQL
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 * @return bool
157 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300158 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700159 {
160 $sql = 'CREATE TABLE ';
161
Andrey Andreev394a3f12012-02-15 14:35:11 +0200162 /* As of version 8.4.1 CUBRID does not support this SQL syntax.
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 if ($if_not_exists === TRUE)
164 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200165 $sql .= 'IF NOT EXISTS ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700166 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200167 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700168
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300169 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700170
171 // If there is a PK defined
172 if (count($primary_keys) > 0)
173 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300174 $key_name = $this->db->escape_identifiers('pk_'.$table.'_'.implode('_', $primary_keys));
Andrey Andreev9637b402012-06-08 15:39:24 +0300175 $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 }
177
178 if (is_array($keys) && count($keys) > 0)
179 {
180 foreach ($keys as $key)
181 {
182 if (is_array($key))
183 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300184 $key_name = $this->db->escape_identifiers('idx_'.$table.implode('_', $key));
Andrey Andreev9637b402012-06-08 15:39:24 +0300185 $key = $this->db->escape_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700186 }
187 else
188 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300189 $key_name = $this->db->escape_identifiers('idx_'.$table.$key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 $key = array($key_name);
191 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200192
193 $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 }
195 }
196
Andrey Andreev7f55d612012-01-26 13:44:28 +0200197 return $sql."\n);";
Esen Sagynov2e087942011-08-09 23:35:01 -0700198 }
199
200 // --------------------------------------------------------------------
201
202 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700203 * Alter table query
204 *
205 * Generates a platform-specific query so that a table can be altered
206 * Called by add_column(), drop_column(), and column_alter(),
207 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700208 * @param string the ALTER type (ADD, DROP, CHANGE)
209 * @param string the column name
210 * @param array fields
211 * @param string the field after which we should add the new field
Andrey Andreev7f55d612012-01-26 13:44:28 +0200212 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300214 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700215 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300216 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700217
218 // DROP has everything it needs now.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200219 if ($alter_type === 'DROP')
Esen Sagynov2e087942011-08-09 23:35:01 -0700220 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300221 return $sql.$this->db->escape_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700222 }
223
Andrey Andreev7f55d612012-01-26 13:44:28 +0200224 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300225 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700226 }
227
Esen Sagynov2e087942011-08-09 23:35:01 -0700228}
229
230/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200231/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */