blob: fbd449aca616764f2c6fedb041b53bf071148a1c [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
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 Andreev582d6a82012-03-20 15:13:49 +020046 protected function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070047 {
48 $current_field_count = 0;
49 $sql = '';
50
Andrey Andreev582d6a82012-03-20 15:13:49 +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 {
58 $sql .= "\n\t$attributes";
59 }
60 else
61 {
62 $attributes = array_change_key_case($attributes, CASE_UPPER);
63
Andrey Andreev032e7ea2012-03-06 19:48:35 +020064 $sql .= "\n\t\"".$this->db->protect_identifiers($field).'"';
Esen Sagynov2e087942011-08-09 23:35:01 -070065
66 if (array_key_exists('NAME', $attributes))
67 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +020068 $sql .= ' '.$this->db->protect_identifiers($attributes['NAME']).' ';
Esen Sagynov2e087942011-08-09 23:35:01 -070069 }
70
71 if (array_key_exists('TYPE', $attributes))
72 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070073 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -070074
75 if (array_key_exists('CONSTRAINT', $attributes))
76 {
77 switch ($attributes['TYPE'])
78 {
79 case 'decimal':
80 case 'float':
81 case 'numeric':
82 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
83 break;
84 case 'enum': // As of version 8.4.0 CUBRID does not support
85 // enum data type.
86 break;
87 case 'set':
88 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
89 break;
90 default:
91 $sql .= '('.$attributes['CONSTRAINT'].')';
92 }
93 }
94 }
95
96 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
97 {
98 //$sql .= ' UNSIGNED';
99 // As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type.
100 // Will be supported in the next release as a part of MySQL Compatibility.
101 }
102
103 if (array_key_exists('DEFAULT', $attributes))
104 {
105 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
106 }
107
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700108 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700109 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700110 $sql .= ' NULL';
111 }
112 else
113 {
114 $sql .= ' NOT NULL';
Esen Sagynov2e087942011-08-09 23:35:01 -0700115 }
116
117 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
118 {
119 $sql .= ' AUTO_INCREMENT';
120 }
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -0700121
122 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700123 {
124 $sql .= ' UNIQUE';
125 }
126 }
127
128 // don't add a comma on the end of the last field
129 if (++$current_field_count < count($fields))
130 {
131 $sql .= ',';
132 }
133 }
134
135 return $sql;
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Create Table
142 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700143 * @param string the table name
144 * @param mixed the fields
145 * @param mixed primary key(s)
146 * @param mixed key(s)
Andrey Andreev582d6a82012-03-20 15:13:49 +0200147 * @param bool should 'IF NOT EXISTS' be added to the SQL
Esen Sagynov2e087942011-08-09 23:35:01 -0700148 * @return bool
149 */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200150 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700151 {
152 $sql = 'CREATE TABLE ';
153
154 if ($if_not_exists === TRUE)
155 {
156 //$sql .= 'IF NOT EXISTS ';
157 // As of version 8.4.0 CUBRID does not support this SQL syntax.
158 }
159
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300160 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700161
162 // If there is a PK defined
163 if (count($primary_keys) > 0)
164 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200165 $key_name = 'pk_'.$table.'_'.$this->db->protect_identifiers(implode('_', $primary_keys));
166
167 $primary_keys = $this->db->protect_identifiers($primary_keys);
Esen Sagynov2e087942011-08-09 23:35:01 -0700168 $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")";
169 }
170
171 if (is_array($keys) && count($keys) > 0)
172 {
173 foreach ($keys as $key)
174 {
175 if (is_array($key))
176 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200177 $key_name = $this->db->protect_identifiers(implode('_', $key));
178 $key = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700179 }
180 else
181 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200182 $key_name = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 $key = array($key_name);
184 }
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200185
Esen Sagynovee3e5942011-08-10 03:22:58 -0700186 $sql .= ",\n\tKEY \"{$key_name}\" (" . implode(', ', $key) . ")";
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 }
188 }
189
190 $sql .= "\n);";
191
192 return $sql;
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700198 * Alter table query
199 *
200 * Generates a platform-specific query so that a table can be altered
201 * Called by add_column(), drop_column(), and column_alter(),
202 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700203 * @param string the ALTER type (ADD, DROP, CHANGE)
204 * @param string the column name
205 * @param array fields
206 * @param string the field after which we should add the new field
Andrey Andreev582d6a82012-03-20 15:13:49 +0200207 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700208 */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200209 public function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700210 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200211 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700212
213 // DROP has everything it needs now.
214 if ($alter_type == 'DROP')
215 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200216 return $sql.$this->db->protect_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700217 }
218
219 $sql .= $this->_process_fields($fields);
220
221 if ($after_field != '')
222 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200223 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Esen Sagynov2e087942011-08-09 23:35:01 -0700224 }
225
226 return $sql;
227 }
228
Esen Sagynov2e087942011-08-09 23:35:01 -0700229}
230
231/* End of file cubrid_forge.php */
Andrey Andreev582d6a82012-03-20 15:13:49 +0200232/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */