blob: d328aa241df49183dcb95b8dd2e38f790a0c0baa [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 *
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 */
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
Andrey Andreevd947eba2012-04-09 14:58:28 +030037 protected $_create_database = FALSE;
38 protected $_drop_database = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070039
40 /**
41 * Process Fields
42 *
Esen Sagynov2e087942011-08-09 23:35:01 -070043 * @param mixed the fields
44 * @return string
45 */
Andrey Andreev7719a032012-01-27 21:05:55 +020046 protected function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070047 {
48 $current_field_count = 0;
49 $sql = '';
50
Andrey Andreev7f55d612012-01-26 13:44:28 +020051 foreach ($fields as $field => $attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -070052 {
53 // Numeric field names aren't allowed in databases, so if the key is
54 // numeric, we know it was assigned by PHP and the developer manually
55 // entered the field information, so we'll simply add it to the list
56 if (is_numeric($field))
57 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020058 $sql .= "\n\t".$attributes;
Esen Sagynov2e087942011-08-09 23:35:01 -070059 }
60 else
61 {
62 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev5ef194d2012-06-08 01:12:54 +030063
64 $sql .= "\n\t".$this->db->escape_identifiers($field);
65
Andrey Andreevcaa04f12012-06-08 01:39:20 +030066 empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' ';
Esen Sagynov2e087942011-08-09 23:35:01 -070067
Andrey Andreev69a60932012-01-26 14:04:06 +020068 if ( ! empty($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070069 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070070 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -070071
Andrey Andreevc6953f42012-01-26 14:43:16 +020072 if ( ! empty($attributes['CONSTRAINT']))
Esen Sagynov2e087942011-08-09 23:35:01 -070073 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030074 switch (strtolower($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070075 {
76 case 'decimal':
77 case 'float':
78 case 'numeric':
79 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
80 break;
Andrey Andreev394a3f12012-02-15 14:35:11 +020081 case 'enum':
82 // Will be supported in the future as part a part of
83 // MySQL compatibility features.
Andrey Andreev7f55d612012-01-26 13:44:28 +020084 break;
Esen Sagynov2e087942011-08-09 23:35:01 -070085 case 'set':
86 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
87 break;
88 default:
89 $sql .= '('.$attributes['CONSTRAINT'].')';
90 }
91 }
92 }
93
Andrey Andreev394a3f12012-02-15 14:35:11 +020094 /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type.
Andrey Andreev7f55d612012-01-26 13:44:28 +020095 * Will be supported in the next release as a part of MySQL Compatibility.
96 *
Andrey Andreev69a60932012-01-26 14:04:06 +020097 if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -070098 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020099 $sql .= ' UNSIGNED';
Esen Sagynov2e087942011-08-09 23:35:01 -0700100 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200101 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700102
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300103 if (isset($attributes['DEFAULT']))
104 {
105 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
106 }
107
108 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
109 ? ' NULL' : ' NOT NULL';
110
111 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
112 {
113 $sql .= ' AUTO_INCREMENT';
114 }
115
116 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
117 {
118 $sql .= ' UNIQUE';
119 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700120 }
121
122 // don't add a comma on the end of the last field
123 if (++$current_field_count < count($fields))
124 {
125 $sql .= ',';
126 }
127 }
128
129 return $sql;
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Create Table
136 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700137 * @param string the table name
138 * @param mixed the fields
139 * @param mixed primary key(s)
140 * @param mixed key(s)
Andrey Andreev582d6a82012-03-20 15:13:49 +0200141 * @param bool should 'IF NOT EXISTS' be added to the SQL
Esen Sagynov2e087942011-08-09 23:35:01 -0700142 * @return bool
143 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300144 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700145 {
146 $sql = 'CREATE TABLE ';
147
Andrey Andreev394a3f12012-02-15 14:35:11 +0200148 /* As of version 8.4.1 CUBRID does not support this SQL syntax.
Esen Sagynov2e087942011-08-09 23:35:01 -0700149 if ($if_not_exists === TRUE)
150 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200151 $sql .= 'IF NOT EXISTS ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700152 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200153 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700154
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300155 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700156
157 // If there is a PK defined
158 if (count($primary_keys) > 0)
159 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300160 $key_name = $this->db->escape_identifiers('pk_'.$table.'_'.implode('_', $primary_keys));
Andrey Andreev9637b402012-06-08 15:39:24 +0300161 $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700162 }
163
164 if (is_array($keys) && count($keys) > 0)
165 {
166 foreach ($keys as $key)
167 {
168 if (is_array($key))
169 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300170 $key_name = $this->db->escape_identifiers('idx_'.$table.implode('_', $key));
Andrey Andreev9637b402012-06-08 15:39:24 +0300171 $key = $this->db->escape_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 }
173 else
174 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300175 $key_name = $this->db->escape_identifiers('idx_'.$table.$key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 $key = array($key_name);
177 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200178
179 $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700180 }
181 }
182
Andrey Andreev7f55d612012-01-26 13:44:28 +0200183 return $sql."\n);";
Esen Sagynov2e087942011-08-09 23:35:01 -0700184 }
185
186 // --------------------------------------------------------------------
187
188 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700189 * Alter table query
190 *
191 * Generates a platform-specific query so that a table can be altered
192 * Called by add_column(), drop_column(), and column_alter(),
193 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700194 * @param string the ALTER type (ADD, DROP, CHANGE)
195 * @param string the column name
196 * @param array fields
197 * @param string the field after which we should add the new field
Andrey Andreev7f55d612012-01-26 13:44:28 +0200198 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700199 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300200 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700201 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300202 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700203
204 // DROP has everything it needs now.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200205 if ($alter_type === 'DROP')
Esen Sagynov2e087942011-08-09 23:35:01 -0700206 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300207 return $sql.$this->db->escape_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700208 }
209
Andrey Andreev7f55d612012-01-26 13:44:28 +0200210 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300211 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700212 }
213
Esen Sagynov2e087942011-08-09 23:35:01 -0700214}
215
216/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200217/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */