blob: a7b4efd9adfab59b7a909e2b8dfa0a0a7dc6fdcf [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 Andreevd947eba2012-04-09 14:58:28 +030038 protected $_create_database = FALSE;
39 protected $_drop_database = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070040
41 /**
42 * Process Fields
43 *
Esen Sagynov2e087942011-08-09 23:35:01 -070044 * @param mixed the fields
45 * @return string
46 */
Andrey Andreev7719a032012-01-27 21:05:55 +020047 protected function _process_fields($fields)
Esen Sagynov2e087942011-08-09 23:35:01 -070048 {
49 $current_field_count = 0;
50 $sql = '';
51
Andrey Andreev7f55d612012-01-26 13:44:28 +020052 foreach ($fields as $field => $attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -070053 {
54 // Numeric field names aren't allowed in databases, so if the key is
55 // numeric, we know it was assigned by PHP and the developer manually
56 // entered the field information, so we'll simply add it to the list
57 if (is_numeric($field))
58 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020059 $sql .= "\n\t".$attributes;
Esen Sagynov2e087942011-08-09 23:35:01 -070060 }
61 else
62 {
63 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev5ef194d2012-06-08 01:12:54 +030064
65 $sql .= "\n\t".$this->db->escape_identifiers($field);
66
Andrey Andreevcaa04f12012-06-08 01:39:20 +030067 empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' ';
Esen Sagynov2e087942011-08-09 23:35:01 -070068
Andrey Andreev69a60932012-01-26 14:04:06 +020069 if ( ! empty($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070070 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070071 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -070072
Andrey Andreevc6953f42012-01-26 14:43:16 +020073 if ( ! empty($attributes['CONSTRAINT']))
Esen Sagynov2e087942011-08-09 23:35:01 -070074 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030075 switch (strtolower($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070076 {
77 case 'decimal':
78 case 'float':
79 case 'numeric':
80 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
81 break;
Andrey Andreev394a3f12012-02-15 14:35:11 +020082 case 'enum':
83 // Will be supported in the future as part a part of
84 // MySQL compatibility features.
Andrey Andreev7f55d612012-01-26 13:44:28 +020085 break;
Esen Sagynov2e087942011-08-09 23:35:01 -070086 case 'set':
87 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
88 break;
89 default:
90 $sql .= '('.$attributes['CONSTRAINT'].')';
91 }
92 }
93 }
94
Andrey Andreev394a3f12012-02-15 14:35:11 +020095 /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type.
Andrey Andreev7f55d612012-01-26 13:44:28 +020096 * Will be supported in the next release as a part of MySQL Compatibility.
97 *
Andrey Andreev69a60932012-01-26 14:04:06 +020098 if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -070099 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200100 $sql .= ' UNSIGNED';
Esen Sagynov2e087942011-08-09 23:35:01 -0700101 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200102 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700103
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300104 if (isset($attributes['DEFAULT']))
105 {
106 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
107 }
108
109 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
110 ? ' NULL' : ' NOT NULL';
111
112 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
113 {
114 $sql .= ' AUTO_INCREMENT';
115 }
116
117 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
118 {
119 $sql .= ' UNIQUE';
120 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700121 }
122
123 // don't add a comma on the end of the last field
124 if (++$current_field_count < count($fields))
125 {
126 $sql .= ',';
127 }
128 }
129
130 return $sql;
131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Create Table
137 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700138 * @param string the table name
139 * @param mixed the fields
140 * @param mixed primary key(s)
141 * @param mixed key(s)
Andrey Andreev582d6a82012-03-20 15:13:49 +0200142 * @param bool should 'IF NOT EXISTS' be added to the SQL
Esen Sagynov2e087942011-08-09 23:35:01 -0700143 * @return bool
144 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300145 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700146 {
147 $sql = 'CREATE TABLE ';
148
Andrey Andreev394a3f12012-02-15 14:35:11 +0200149 /* As of version 8.4.1 CUBRID does not support this SQL syntax.
Esen Sagynov2e087942011-08-09 23:35:01 -0700150 if ($if_not_exists === TRUE)
151 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200152 $sql .= 'IF NOT EXISTS ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700153 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200154 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700155
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300156 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700157
158 // If there is a PK defined
159 if (count($primary_keys) > 0)
160 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300161 $key_name = $this->db->escape_identifiers('pk_'.$table.'_'.implode('_', $primary_keys));
Andrey Andreev9637b402012-06-08 15:39:24 +0300162 $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 }
164
165 if (is_array($keys) && count($keys) > 0)
166 {
167 foreach ($keys as $key)
168 {
169 if (is_array($key))
170 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300171 $key_name = $this->db->escape_identifiers('idx_'.$table.implode('_', $key));
Andrey Andreev9637b402012-06-08 15:39:24 +0300172 $key = $this->db->escape_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700173 }
174 else
175 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300176 $key_name = $this->db->escape_identifiers('idx_'.$table.$key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700177 $key = array($key_name);
178 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200179
180 $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700181 }
182 }
183
Andrey Andreev7f55d612012-01-26 13:44:28 +0200184 return $sql."\n);";
Esen Sagynov2e087942011-08-09 23:35:01 -0700185 }
186
187 // --------------------------------------------------------------------
188
189 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700190 * Alter table query
191 *
192 * Generates a platform-specific query so that a table can be altered
193 * Called by add_column(), drop_column(), and column_alter(),
194 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700195 * @param string the ALTER type (ADD, DROP, CHANGE)
196 * @param string the column name
197 * @param array fields
198 * @param string the field after which we should add the new field
Andrey Andreev7f55d612012-01-26 13:44:28 +0200199 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700200 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300201 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700202 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300203 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700204
205 // DROP has everything it needs now.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200206 if ($alter_type === 'DROP')
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300208 return $sql.$this->db->escape_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700209 }
210
Andrey Andreev7f55d612012-01-26 13:44:28 +0200211 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300212 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700213 }
214
Esen Sagynov2e087942011-08-09 23:35:01 -0700215}
216
217/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200218/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */