blob: 486a8dd7f357c1e2c21072c6ad661f54c2eeece5 [file] [log] [blame]
Andrey Andreev9a8a1112012-03-20 14:37:23 +02001<?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
Andrey Andreev9a8a1112012-03-20 14:37:23 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev9a8a1112012-03-20 14:37:23 +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 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * ODBC Forge Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/database/
34 */
35class CI_DB_odbc_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @param string the database name
41 * @return bool
42 */
Andrey Andreev9a8a1112012-03-20 14:37:23 +020043 public function _create_database()
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
45 // ODBC has no "create database" command since it's
46 // designed to connect to an existing database
47 if ($this->db->db_debug)
48 {
49 return $this->db->display_error('db_unsuported_feature');
50 }
51 return FALSE;
52 }
53
54 // --------------------------------------------------------------------
55
56 /**
57 * Drop database
58 *
Derek Allard2067d1a2008-11-13 22:59:24 +000059 * @param string the database name
60 * @return bool
61 */
Andrey Andreev9a8a1112012-03-20 14:37:23 +020062 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
64 // ODBC has no "drop database" command since it's
Barry Mienydd671972010-10-04 16:33:58 +020065 // designed to connect to an existing database
Derek Allard2067d1a2008-11-13 22:59:24 +000066 if ($this->db->db_debug)
67 {
68 return $this->db->display_error('db_unsuported_feature');
69 }
70 return FALSE;
71 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Create Table
77 *
Derek Allard2067d1a2008-11-13 22:59:24 +000078 * @param string the table name
79 * @param array the fields
80 * @param mixed primary key(s)
81 * @param mixed key(s)
Andrey Andreev9a8a1112012-03-20 14:37:23 +020082 * @param bool should 'IF NOT EXISTS' be added to the SQL
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @return bool
84 */
Andrey Andreev9a8a1112012-03-20 14:37:23 +020085 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 if ($if_not_exists === TRUE)
90 {
91 $sql .= 'IF NOT EXISTS ';
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 $sql .= $this->db->_escape_identifiers($table)." (";
95 $current_field_count = 0;
96
Andrey Andreev9a8a1112012-03-20 14:37:23 +020097 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
99 // Numeric field names aren't allowed in databases, so if the key is
100 // numeric, we know it was assigned by PHP and the developer manually
101 // entered the field information, so we'll simply add it to the list
102 if (is_numeric($field))
103 {
104 $sql .= "\n\t$attributes";
105 }
106 else
107 {
108 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200109
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200110 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Jones37f4b9c2011-07-01 17:56:50 -0500112 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if (array_key_exists('CONSTRAINT', $attributes))
115 {
116 $sql .= '('.$attributes['CONSTRAINT'].')';
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
120 {
121 $sql .= ' UNSIGNED';
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 if (array_key_exists('DEFAULT', $attributes))
125 {
126 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
130 {
131 $sql .= ' NULL';
132 }
133 else
134 {
Barry Mienydd671972010-10-04 16:33:58 +0200135 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
139 {
140 $sql .= ' AUTO_INCREMENT';
141 }
142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // don't add a comma on the end of the last field
145 if (++$current_field_count < count($fields))
146 {
147 $sql .= ',';
148 }
149 }
150
151 if (count($primary_keys) > 0)
152 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200153 $primary_keys = $this->db->protect_identifiers($primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if (is_array($keys) && count($keys) > 0)
158 {
159 foreach ($keys as $key)
160 {
161 if (is_array($key))
162 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200163 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165 else
166 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200167 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
171 }
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 $sql .= "\n)";
175
176 return $sql;
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Drop Table
183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @return bool
185 */
Andrey Andreev9a8a1112012-03-20 14:37:23 +0200186 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Barry Mienydd671972010-10-04 16:33:58 +0200188 // Not a supported ODBC feature
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 if ($this->db->db_debug)
190 {
191 return $this->db->display_error('db_unsuported_feature');
192 }
193 return FALSE;
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Alter table query
200 *
201 * Generates a platform-specific query so that a table can be altered
202 * Called by add_column(), drop_column(), and column_alter(),
203 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param string the ALTER type (ADD, DROP, CHANGE)
205 * @param string the column name
206 * @param string the table name
207 * @param string the column definition
208 * @param string the default value
Andrey Andreev9a8a1112012-03-20 14:37:23 +0200209 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 * @param string the field after which we should add the new field
Andrey Andreev9a8a1112012-03-20 14:37:23 +0200211 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 */
Andrey Andreev9a8a1112012-03-20 14:37:23 +0200213 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200215 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
217 // DROP has everything it needs now.
218 if ($alter_type == 'DROP')
219 {
220 return $sql;
221 }
222
223 $sql .= " $column_definition";
224
225 if ($default_value != '')
226 {
227 $sql .= " DEFAULT \"$default_value\"";
228 }
229
230 if ($null === NULL)
231 {
232 $sql .= ' NULL';
233 }
234 else
235 {
236 $sql .= ' NOT NULL';
237 }
238
239 if ($after_field != '')
240 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200241 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Rename a table
253 *
254 * Generates a platform-specific query so that a table can be renamed
255 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @param string the old table name
257 * @param string the new table name
258 * @return string
259 */
Andrey Andreev9a8a1112012-03-20 14:37:23 +0200260 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200262 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 +0000263 }
264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265}
266
267/* End of file odbc_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400268/* Location: ./system/database/drivers/odbc/odbc_forge.php */