Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/database/ |
| 36 | */ |
| 37 | class CI_DB_odbc_forge extends CI_DB_forge { |
| 38 | |
| 39 | /** |
| 40 | * Create database |
| 41 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * @param string the database name |
| 43 | * @return bool |
| 44 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame^] | 45 | protected function _create_database() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | * @param string the database name |
| 62 | * @return bool |
| 63 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame^] | 64 | protected function _drop_database($name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | { |
| 66 | // ODBC has no "drop database" command since it's |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | // designed to connect to an existing database |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | * @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 Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame^] | 87 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | { |
| 89 | $sql = 'CREATE TABLE '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | if ($if_not_exists === TRUE) |
| 92 | { |
| 93 | $sql .= 'IF NOT EXISTS '; |
| 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | $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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 112 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 114 | $sql .= ' '.$attributes['TYPE']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 117 | { |
| 118 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 122 | { |
| 123 | $sql .= ' UNSIGNED'; |
| 124 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | if (array_key_exists('DEFAULT', $attributes)) |
| 127 | { |
| 128 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 132 | { |
| 133 | $sql .= ' NULL'; |
| 134 | } |
| 135 | else |
| 136 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 141 | { |
| 142 | $sql .= ' AUTO_INCREMENT'; |
| 143 | } |
| 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // 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 Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 155 | $primary_keys = $this->db->protect_identifiers($primary_keys); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | if (is_array($keys) && count($keys) > 0) |
| 160 | { |
| 161 | foreach ($keys as $key) |
| 162 | { |
| 163 | if (is_array($key)) |
| 164 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 165 | $key = $this->db->protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | } |
| 167 | else |
| 168 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 169 | $key = array($this->db->protect_identifiers($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")"; |
| 173 | } |
| 174 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | $sql .= "\n)"; |
| 177 | |
| 178 | return $sql; |
| 179 | } |
| 180 | |
| 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
| 184 | * Drop Table |
| 185 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | * @return bool |
| 187 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame^] | 188 | protected function _drop_table($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | // Not a supported ODBC feature |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | * @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 Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame^] | 215 | protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 217 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | |
| 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 Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 243 | return $sql.' AFTER '.$this->db->protect_identifiers($after_field); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | return $sql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | * @param string the old table name |
| 259 | * @param string the new table name |
| 260 | * @return string |
| 261 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame^] | 262 | protected function _rename_table($table_name, $new_table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 264 | return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* End of file odbc_forge.php */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame] | 270 | /* Location: ./system/database/drivers/odbc/odbc_forge.php */ |