blob: e326a2801bbb1bc90c69ea725fceb46fd7cd804a [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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 *
42 * @access private
43 * @param string the database name
44 * @return bool
45 */
46 function _create_database()
47 {
48 // ODBC has no "create database" command since it's
49 // designed to connect to an existing database
50 if ($this->db->db_debug)
51 {
52 return $this->db->display_error('db_unsuported_feature');
53 }
54 return FALSE;
55 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Drop database
61 *
62 * @access private
63 * @param string the database name
64 * @return bool
65 */
66 function _drop_database($name)
67 {
68 // ODBC has no "drop database" command since it's
Barry Mienydd671972010-10-04 16:33:58 +020069 // designed to connect to an existing database
Derek Allard2067d1a2008-11-13 22:59:24 +000070 if ($this->db->db_debug)
71 {
72 return $this->db->display_error('db_unsuported_feature');
73 }
74 return FALSE;
75 }
76
77 // --------------------------------------------------------------------
78
79 /**
80 * Create Table
81 *
82 * @access private
83 * @param string the table name
84 * @param array the fields
85 * @param mixed primary key(s)
86 * @param mixed key(s)
87 * @param boolean should 'IF NOT EXISTS' be added to the SQL
88 * @return bool
89 */
90 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
91 {
92 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 if ($if_not_exists === TRUE)
95 {
96 $sql .= 'IF NOT EXISTS ';
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 $sql .= $this->db->_escape_identifiers($table)." (";
100 $current_field_count = 0;
101
102 foreach ($fields as $field=>$attributes)
103 {
104 // Numeric field names aren't allowed in databases, so if the key is
105 // numeric, we know it was assigned by PHP and the developer manually
106 // entered the field information, so we'll simply add it to the list
107 if (is_numeric($field))
108 {
109 $sql .= "\n\t$attributes";
110 }
111 else
112 {
113 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Jones37f4b9c2011-07-01 17:56:50 -0500117 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if (array_key_exists('CONSTRAINT', $attributes))
120 {
121 $sql .= '('.$attributes['CONSTRAINT'].')';
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
125 {
126 $sql .= ' UNSIGNED';
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if (array_key_exists('DEFAULT', $attributes))
130 {
131 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
135 {
136 $sql .= ' NULL';
137 }
138 else
139 {
Barry Mienydd671972010-10-04 16:33:58 +0200140 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
144 {
145 $sql .= ' AUTO_INCREMENT';
146 }
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // don't add a comma on the end of the last field
150 if (++$current_field_count < count($fields))
151 {
152 $sql .= ',';
153 }
154 }
155
156 if (count($primary_keys) > 0)
157 {
158 $primary_keys = $this->db->_protect_identifiers($primary_keys);
159 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if (is_array($keys) && count($keys) > 0)
163 {
164 foreach ($keys as $key)
165 {
166 if (is_array($key))
167 {
Barry Mienydd671972010-10-04 16:33:58 +0200168 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170 else
171 {
172 $key = array($this->db->_protect_identifiers($key));
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
176 }
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 $sql .= "\n)";
180
181 return $sql;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Drop Table
188 *
189 * @access private
190 * @return bool
191 */
192 function _drop_table($table)
193 {
Barry Mienydd671972010-10-04 16:33:58 +0200194 // Not a supported ODBC feature
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 if ($this->db->db_debug)
196 {
197 return $this->db->display_error('db_unsuported_feature');
198 }
199 return FALSE;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Alter table query
206 *
207 * Generates a platform-specific query so that a table can be altered
208 * Called by add_column(), drop_column(), and column_alter(),
209 *
210 * @access private
211 * @param string the ALTER type (ADD, DROP, CHANGE)
212 * @param string the column name
213 * @param string the table name
214 * @param string the column definition
215 * @param string the default value
216 * @param boolean should 'NOT NULL' be added
217 * @param string the field after which we should add the new field
218 * @return object
219 */
220 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
221 {
222 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
223
224 // DROP has everything it needs now.
225 if ($alter_type == 'DROP')
226 {
227 return $sql;
228 }
229
230 $sql .= " $column_definition";
231
232 if ($default_value != '')
233 {
234 $sql .= " DEFAULT \"$default_value\"";
235 }
236
237 if ($null === NULL)
238 {
239 $sql .= ' NULL';
240 }
241 else
242 {
243 $sql .= ' NOT NULL';
244 }
245
246 if ($after_field != '')
247 {
248 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Rename a table
260 *
261 * Generates a platform-specific query so that a table can be renamed
262 *
263 * @access private
264 * @param string the old table name
265 * @param string the new table name
266 * @return string
267 */
268 function _rename_table($table_name, $new_table_name)
269 {
270 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
271 return $sql;
272 }
273
274
275}
276
277/* End of file odbc_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000278/* Location: ./system/database/drivers/odbc/odbc_forge.php */