blob: 4e66f81e3f53387d4295a62ca8b6c3f197a36e6f [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 Andreev394a3f12012-02-15 14:35:11 +020063 $sql .= "\n\t".$this->db->protect_identifiers($field)
Andrey Andreevc6953f42012-01-26 14:43:16 +020064 .( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : '');
Esen Sagynov2e087942011-08-09 23:35:01 -070065
Andrey Andreev69a60932012-01-26 14:04:06 +020066 if ( ! empty($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -070067 {
Esen Sagynov2ab2b1e2011-08-11 00:41:16 -070068 $sql .= ' '.$attributes['TYPE'];
Esen Sagynov2e087942011-08-09 23:35:01 -070069
Andrey Andreevc6953f42012-01-26 14:43:16 +020070 if ( ! empty($attributes['CONSTRAINT']))
Esen Sagynov2e087942011-08-09 23:35:01 -070071 {
72 switch ($attributes['TYPE'])
73 {
74 case 'decimal':
75 case 'float':
76 case 'numeric':
77 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
78 break;
Andrey Andreev394a3f12012-02-15 14:35:11 +020079 case 'enum':
80 // Will be supported in the future as part a part of
81 // MySQL compatibility features.
Andrey Andreev7f55d612012-01-26 13:44:28 +020082 break;
Esen Sagynov2e087942011-08-09 23:35:01 -070083 case 'set':
84 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
85 break;
86 default:
87 $sql .= '('.$attributes['CONSTRAINT'].')';
88 }
89 }
90 }
91
Andrey Andreev394a3f12012-02-15 14:35:11 +020092 /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type.
Andrey Andreev7f55d612012-01-26 13:44:28 +020093 * Will be supported in the next release as a part of MySQL Compatibility.
94 *
Andrey Andreev69a60932012-01-26 14:04:06 +020095 if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
Esen Sagynov2e087942011-08-09 23:35:01 -070096 {
Andrey Andreev7f55d612012-01-26 13:44:28 +020097 $sql .= ' UNSIGNED';
Esen Sagynov2e087942011-08-09 23:35:01 -070098 }
Andrey Andreev7f55d612012-01-26 13:44:28 +020099 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700100
Andrey Andreev69a60932012-01-26 14:04:06 +0200101 $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
Andrey Andreevc6953f42012-01-26 14:43:16 +0200102 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
103 .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '')
104 .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700105 }
106
107 // don't add a comma on the end of the last field
108 if (++$current_field_count < count($fields))
109 {
110 $sql .= ',';
111 }
112 }
113
114 return $sql;
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Create Table
121 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700122 * @param string the table name
123 * @param mixed the fields
124 * @param mixed primary key(s)
125 * @param mixed key(s)
Andrey Andreev582d6a82012-03-20 15:13:49 +0200126 * @param bool should 'IF NOT EXISTS' be added to the SQL
Esen Sagynov2e087942011-08-09 23:35:01 -0700127 * @return bool
128 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300129 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Esen Sagynov2e087942011-08-09 23:35:01 -0700130 {
131 $sql = 'CREATE TABLE ';
132
Andrey Andreev394a3f12012-02-15 14:35:11 +0200133 /* As of version 8.4.1 CUBRID does not support this SQL syntax.
Esen Sagynov2e087942011-08-09 23:35:01 -0700134 if ($if_not_exists === TRUE)
135 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200136 $sql .= 'IF NOT EXISTS ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700137 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200138 */
Esen Sagynov2e087942011-08-09 23:35:01 -0700139
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300140 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700141
142 // If there is a PK defined
143 if (count($primary_keys) > 0)
144 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200145 $key_name = $this->db->protect_identifiers('pk_'.$table.'_'.implode('_', $primary_keys));
Andrey Andreev7f55d612012-01-26 13:44:28 +0200146 $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->protect_identifiers($primary_keys)).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700147 }
148
149 if (is_array($keys) && count($keys) > 0)
150 {
151 foreach ($keys as $key)
152 {
153 if (is_array($key))
154 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200155 $key_name = $this->db->protect_identifiers('idx_'.$table.implode('_', $key));
Andrey Andreev7f55d612012-01-26 13:44:28 +0200156 $key = $this->db->protect_identifiers($key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700157 }
158 else
159 {
Andrey Andreev394a3f12012-02-15 14:35:11 +0200160 $key_name = $this->db->protect_identifiers('idx_'.$table.$key);
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 $key = array($key_name);
162 }
Andrey Andreev7f55d612012-01-26 13:44:28 +0200163
164 $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700165 }
166 }
167
Andrey Andreev7f55d612012-01-26 13:44:28 +0200168 return $sql."\n);";
Esen Sagynov2e087942011-08-09 23:35:01 -0700169 }
170
171 // --------------------------------------------------------------------
172
173 /**
Esen Sagynov2e087942011-08-09 23:35:01 -0700174 * Alter table query
175 *
176 * Generates a platform-specific query so that a table can be altered
177 * Called by add_column(), drop_column(), and column_alter(),
178 *
Esen Sagynov2e087942011-08-09 23:35:01 -0700179 * @param string the ALTER type (ADD, DROP, CHANGE)
180 * @param string the column name
181 * @param array fields
182 * @param string the field after which we should add the new field
Andrey Andreev7f55d612012-01-26 13:44:28 +0200183 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700184 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300185 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Esen Sagynov2e087942011-08-09 23:35:01 -0700186 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200187 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Esen Sagynov2e087942011-08-09 23:35:01 -0700188
189 // DROP has everything it needs now.
Andrey Andreev7f55d612012-01-26 13:44:28 +0200190 if ($alter_type === 'DROP')
Esen Sagynov2e087942011-08-09 23:35:01 -0700191 {
Andrey Andreev7f55d612012-01-26 13:44:28 +0200192 return $sql.$this->db->protect_identifiers($fields);
Esen Sagynov2e087942011-08-09 23:35:01 -0700193 }
194
Andrey Andreev7f55d612012-01-26 13:44:28 +0200195 return $sql.$this->_process_fields($fields)
196 .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
Esen Sagynov2e087942011-08-09 23:35:01 -0700197 }
198
Esen Sagynov2e087942011-08-09 23:35:01 -0700199}
200
201/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200202/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */