blob: 121c096063dbcbb8949e4b8244add288c79c128d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @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)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * ODBC Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/database/
36 */
37class CI_DB_odbc_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
Derek Allard2067d1a2008-11-13 22:59:24 +000042 * @param string the database name
43 * @return bool
44 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040045 protected function _create_database()
Derek Allard2067d1a2008-11-13 22:59:24 +000046 {
47 // ODBC has no "create database" command since it's
48 // designed to connect to an existing database
49 if ($this->db->db_debug)
50 {
51 return $this->db->display_error('db_unsuported_feature');
52 }
53 return FALSE;
54 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Drop database
60 *
Derek Allard2067d1a2008-11-13 22:59:24 +000061 * @param string the database name
62 * @return bool
63 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040064 protected function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
66 // ODBC has no "drop database" command since it's
Barry Mienydd671972010-10-04 16:33:58 +020067 // designed to connect to an existing database
Derek Allard2067d1a2008-11-13 22:59:24 +000068 if ($this->db->db_debug)
69 {
70 return $this->db->display_error('db_unsuported_feature');
71 }
72 return FALSE;
73 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Create Table
79 *
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @param string the table name
81 * @param array the fields
82 * @param mixed primary key(s)
83 * @param mixed key(s)
84 * @param boolean should 'IF NOT EXISTS' be added to the SQL
85 * @return bool
86 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040087 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
89 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if ($if_not_exists === TRUE)
92 {
93 $sql .= 'IF NOT EXISTS ';
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 $sql .= $this->db->_escape_identifiers($table)." (";
97 $current_field_count = 0;
98
99 foreach ($fields as $field=>$attributes)
100 {
101 // Numeric field names aren't allowed in databases, so if the key is
102 // numeric, we know it was assigned by PHP and the developer manually
103 // entered the field information, so we'll simply add it to the list
104 if (is_numeric($field))
105 {
106 $sql .= "\n\t$attributes";
107 }
108 else
109 {
110 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200112 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Jones37f4b9c2011-07-01 17:56:50 -0500114 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if (array_key_exists('CONSTRAINT', $attributes))
117 {
118 $sql .= '('.$attributes['CONSTRAINT'].')';
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
122 {
123 $sql .= ' UNSIGNED';
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if (array_key_exists('DEFAULT', $attributes))
127 {
128 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
132 {
133 $sql .= ' NULL';
134 }
135 else
136 {
Barry Mienydd671972010-10-04 16:33:58 +0200137 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
141 {
142 $sql .= ' AUTO_INCREMENT';
143 }
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // don't add a comma on the end of the last field
147 if (++$current_field_count < count($fields))
148 {
149 $sql .= ',';
150 }
151 }
152
153 if (count($primary_keys) > 0)
154 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200155 $primary_keys = $this->db->protect_identifiers($primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if (is_array($keys) && count($keys) > 0)
160 {
161 foreach ($keys as $key)
162 {
163 if (is_array($key))
164 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200165 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167 else
168 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200169 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
173 }
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 $sql .= "\n)";
177
178 return $sql;
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Drop Table
185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @return bool
187 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400188 protected function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Barry Mienydd671972010-10-04 16:33:58 +0200190 // Not a supported ODBC feature
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 if ($this->db->db_debug)
192 {
193 return $this->db->display_error('db_unsuported_feature');
194 }
195 return FALSE;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Alter table query
202 *
203 * Generates a platform-specific query so that a table can be altered
204 * Called by add_column(), drop_column(), and column_alter(),
205 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @param string the ALTER type (ADD, DROP, CHANGE)
207 * @param string the column name
208 * @param string the table name
209 * @param string the column definition
210 * @param string the default value
211 * @param boolean should 'NOT NULL' be added
212 * @param string the field after which we should add the new field
213 * @return object
214 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400215 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200217 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
219 // DROP has everything it needs now.
220 if ($alter_type == 'DROP')
221 {
222 return $sql;
223 }
224
225 $sql .= " $column_definition";
226
227 if ($default_value != '')
228 {
229 $sql .= " DEFAULT \"$default_value\"";
230 }
231
232 if ($null === NULL)
233 {
234 $sql .= ' NULL';
235 }
236 else
237 {
238 $sql .= ' NOT NULL';
239 }
240
241 if ($after_field != '')
242 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200243 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250
251 // --------------------------------------------------------------------
252
253 /**
254 * Rename a table
255 *
256 * Generates a platform-specific query so that a table can be renamed
257 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @param string the old table name
259 * @param string the new table name
260 * @return string
261 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400262 protected function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200264 return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267}
268
269/* End of file odbc_forge.php */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400270/* Location: ./system/database/drivers/odbc/odbc_forge.php */