blob: 05762ba5ab9a59e689d2790b97ca3be683cd314a [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 Andreeva287a342012-11-05 23:19:59 +0200111 $sqls[] = $sql.' CHANGE '.$this->_process_column($field[$i]);
112 if ( ! empty($field[$i]['new_name']))
Esen Sagynov2e087942011-08-09 23:35:01 -0700113 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200114 $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
115 .' AS '.$this->db->escape_identifiers($field[$i]['name']);
Esen Sagynov2e087942011-08-09 23:35:01 -0700116 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700117 }
118 }
119
Andrey Andreeva287a342012-11-05 23:19:59 +0200120 return $sqls;
Esen Sagynov2e087942011-08-09 23:35:01 -0700121 }
122
123 // --------------------------------------------------------------------
124
125 /**
Andrey Andreevb67277b2012-11-12 12:51:14 +0200126 * Process column
127 *
128 * @param array $field
129 * @return string
130 */
131 protected function _process_column($field)
132 {
133 $extra_clause = isset($field['after'])
134 ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
135
136 if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
137 {
138 $extra_clause = ' FIRST';
139 }
140
141 return $this->db->escape_identifiers($field['name'])
142 .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
143 .' '.$field['type'].$field['length']
144 .$field['unsigned']
145 .$field['null']
146 .$field['default']
147 .$field['auto_increment']
148 .$field['unique']
149 .$extra_clause;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200155 * Field attribute TYPE
Esen Sagynov2e087942011-08-09 23:35:01 -0700156 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200157 * Performs a data type mapping between different databases.
158 *
159 * @param array &$attributes
160 * @return void
Esen Sagynov2e087942011-08-09 23:35:01 -0700161 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200162 protected function _attr_type(&$attributes)
Esen Sagynov2e087942011-08-09 23:35:01 -0700163 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200164 switch (strtoupper($attributes['TYPE']))
Esen Sagynov2e087942011-08-09 23:35:01 -0700165 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200166 case 'TINYINT':
167 $attributes['TYPE'] = 'SMALLINT';
168 $attributes['UNSIGNED'] = FALSE;
169 return;
170 case 'MEDIUMINT':
171 $attributes['TYPE'] = 'INTEGER';
172 $attributes['UNSIGNED'] = FALSE;
173 return;
174 default: return;
Esen Sagynov2e087942011-08-09 23:35:01 -0700175 }
Esen Sagynov2e087942011-08-09 23:35:01 -0700176 }
177
178 // --------------------------------------------------------------------
179
180 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200181 * Process indexes
Esen Sagynov2e087942011-08-09 23:35:01 -0700182 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200183 * @param string $table (ignored)
Andrey Andreev7f55d612012-01-26 13:44:28 +0200184 * @return string
Esen Sagynov2e087942011-08-09 23:35:01 -0700185 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200186 protected function _process_indexes($table = NULL)
Esen Sagynov2e087942011-08-09 23:35:01 -0700187 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200188 $sql = '';
Esen Sagynov2e087942011-08-09 23:35:01 -0700189
Andrey Andreeva287a342012-11-05 23:19:59 +0200190 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Esen Sagynov2e087942011-08-09 23:35:01 -0700191 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200192 if ( ! isset($this->fields[$this->keys[$i]]))
193 {
194 unset($this->keys[$i]);
195 continue;
196 }
197
198 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
199
200 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
201 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Esen Sagynov2e087942011-08-09 23:35:01 -0700202 }
203
Andrey Andreeva287a342012-11-05 23:19:59 +0200204 $this->keys = array();
205
206 return $sql;
Esen Sagynov2e087942011-08-09 23:35:01 -0700207 }
208
Esen Sagynov2e087942011-08-09 23:35:01 -0700209}
210
211/* End of file cubrid_forge.php */
Andrey Andreevfead0552012-03-20 15:23:06 +0200212/* Location: ./system/database/drivers/cubrid/cubrid_forge.php */