blob: 2a737c5ed054ebf0588651a4ab9a6068aa0ed954 [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
Andrey Andreevc98e93a2012-11-02 02:04:59 +020045 /**
Andrey Andreevb0a97c12012-11-11 13:58:53 +020046 * CREATE TABLE keys flag
47 *
48 * Whether table keys are created from within the
49 * CREATE TABLE statement.
50 *
51 * @var bool
52 */
53 protected $_create_table_keys = TRUE;
54
55 /**
Andrey Andreevc98e93a2012-11-02 02:04:59 +020056 * DROP DATABASE statement
57 *
58 * @var string
59 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030060 protected $_drop_database = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070061
62 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020063 * CREATE TABLE IF statement
Esen Sagynov2e087942011-08-09 23:35:01 -070064 *
Andrey Andreeva287a342012-11-05 23:19:59 +020065 * @var string
Esen Sagynov2e087942011-08-09 23:35:01 -070066 */
Andrey Andreeva287a342012-11-05 23:19:59 +020067 protected $_create_table_if = FALSE;
Esen Sagynov2e087942011-08-09 23:35:01 -070068
Andrey Andreeva287a342012-11-05 23:19:59 +020069 /**
70 * UNSIGNED support
71 *
72 * @var array
73 */
74 protected $_unsigned = array(
75 'SHORT' => 'INTEGER',
76 'SMALLINT' => 'INTEGER',
77 'INT' => 'BIGINT',
78 'INTEGER' => 'BIGINT',
79 'BIGINT' => 'NUMERIC',
80 'FLOAT' => 'DOUBLE',
81 'REAL' => 'DOUBLE'
82 );
83
84 // --------------------------------------------------------------------
85
86 /**
87 * ALTER TABLE
88 *
89 * @param string $alter_type ALTER type
90 * @param string $table Table name
91 * @param mixed $field Column definition
92 * @return string|string[]
93 */
94 protected function _alter_table($alter_type, $table, $field)
95 {
96 if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
Esen Sagynov2e087942011-08-09 23:35:01 -070097 {
Andrey Andreeva287a342012-11-05 23:19:59 +020098 return parent::_alter_table($alter_type, $table, $field);
99 }
100
101 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
102 $sqls = array();
103 for ($i = 0, $c = count($field); $i < $c; $i++)
104 {
105 if ($field[$i]['_literal'] !== FALSE)
Esen Sagynov2e087942011-08-09 23:35:01 -0700106 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200107 $sqls[] = $sql.' CHANGE '.$field[$i]['_literal'];
Esen Sagynov2e087942011-08-09 23:35:01 -0700108 }
109 else
110 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200111 $alter_type = empty($field[$i]['new_name']) ? ' MODIFY ' : ' CHANGE ';
112 $sqls[] = $sql.$alter_type$this->_process_column($field[$i]);
Esen Sagynov2e087942011-08-09 23:35:01 -0700113 }
114 }
115
Andrey Andreeva287a342012-11-05 23:19:59 +0200116 return $sqls;
Esen Sagynov2e087942011-08-09 23:35:01 -0700117 }
118
119 // --------------------------------------------------------------------
120
121 /**
Andrey Andreevb67277b2012-11-12 12:51:14 +0200122 * Process column
123 *
124 * @param array $field
125 * @return string
126 */
127 protected function _process_column($field)
128 {
129 $extra_clause = isset($field['after'])
130 ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
131
132 if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
133 {
134 $extra_clause = ' FIRST';
135 }
136
137 return $this->db->escape_identifiers($field['name'])
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200138 .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
Andrey Andreevb67277b2012-11-12 12:51:14 +0200139 .' '.$field['type'].$field['length']
140 .$field['unsigned']
141 .$field['null']
142 .$field['default']
143 .$field['auto_increment']
144 .$field['unique']
145 .$extra_clause;
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200151 * Field attribute TYPE
Esen Sagynov2e087942011-08-09 23:35:01 -0700152 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200153 * Performs a data type mapping between different databases.
154 *
155 * @param array &$attributes
156 * @return void
Esen Sagynov2e087942011-08-09 23:35:01 -0700157 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200158 protected function _attr_type(&$attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -0700159 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200160 switch (strtoupper($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200162 case 'TINYINT':
163 $attributes['TYPE'] = 'SMALLINT';
164 $attributes['UNSIGNED'] = FALSE;
165 return;
166 case 'MEDIUMINT':
167 $attributes['TYPE'] = 'INTEGER';
168 $attributes['UNSIGNED'] = FALSE;
169 return;
170 default: return;
Esen Sagynov2e087942011-08-09 23:35:01 -0700171 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700172 }
173
174 // --------------------------------------------------------------------
175
176 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200177 * Process indexes
Esen Sagynov2e087942011-08-09 23:35:01 -0700178 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200179 * @param string $table (ignored)
Andrey Andreev7f55d612012-01-26 13:44:28 +0200180 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700181 */
Andrey Andreev35451022012-11-25 17:20:04 +0200182 protected function _process_indexes($table)
Esen Sagynov2e087942011-08-09 23:35:01 -0700183 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200184 $sql = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700185
Andrey Andreeva287a342012-11-05 23:19:59 +0200186 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 {
Andrey Andreev35451022012-11-25 17:20:04 +0200188 if (is_array($this->keys[$i]))
189 {
190 for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
191 {
192 if ( ! isset($this->fields[$this->keys[$i][$i2]]))
193 {
194 unset($this->keys[$i][$i2]);
195 continue;
196 }
197 }
198 }
199 elseif ( ! isset($this->fields[$this->keys[$i]]))
Andrey Andreeva287a342012-11-05 23:19:59 +0200200 {
201 unset($this->keys[$i]);
202 continue;
203 }
204
205 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
206
207 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
208 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700209 }
210
Andrey Andreeva287a342012-11-05 23:19:59 +0200211 $this->keys = array();
212
213 return $sql;
Esen Sagynov2e087942011-08-09 23:35:01 -0700214 }
215
Esen Sagynov2e087942011-08-09 23:35:01 -0700216}
217
218/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200219/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */