Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 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 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [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 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [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) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Timothy Warren | 018af7a | 2011-09-07 12:07:35 -0400 | [diff] [blame] | 24 | * @since Version 2.1.0 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * PDO Forge Class |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 35 | * @link http://codeigniter.com/database/ |
| 36 | */ |
| 37 | class CI_DB_pdo_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 | // PDO 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 | // PDO has no "drop database" command since it's |
| 69 | // designed to connect to an existing database |
| 70 | 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 '; |
| 93 | |
| 94 | if ($if_not_exists === TRUE) |
| 95 | { |
| 96 | $sql .= 'IF NOT EXISTS '; |
| 97 | } |
| 98 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 99 | $sql .= '`'.$this->db->_escape_identifiers($table).'` ('; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 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); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 114 | $numeric = array('SERIAL', 'INTEGER'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 115 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 116 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 117 | |
| 118 | $sql .= ' '.$attributes['TYPE']; |
| 119 | |
| 120 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 121 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 122 | // Exception for Postgre numeric which not too happy with constraint within those type |
| 123 | if ( ! ($this->db->pdodriver == 'pgsql' && in_array($attributes['TYPE'], $numeric))) |
| 124 | { |
| 125 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 126 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 130 | { |
| 131 | $sql .= ' UNSIGNED'; |
| 132 | } |
| 133 | |
| 134 | if (array_key_exists('DEFAULT', $attributes)) |
| 135 | { |
| 136 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 137 | } |
| 138 | |
| 139 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 140 | { |
| 141 | $sql .= ' NULL'; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | $sql .= ' NOT NULL'; |
| 146 | } |
| 147 | |
| 148 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 149 | { |
| 150 | $sql .= ' AUTO_INCREMENT'; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // don't add a comma on the end of the last field |
| 155 | if (++$current_field_count < count($fields)) |
| 156 | { |
| 157 | $sql .= ','; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (count($primary_keys) > 0) |
| 162 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 163 | $primary_keys = $this->db->protect_identifiers($primary_keys); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 164 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 165 | } |
| 166 | |
| 167 | if (is_array($keys) && count($keys) > 0) |
| 168 | { |
| 169 | foreach ($keys as $key) |
| 170 | { |
| 171 | if (is_array($key)) |
| 172 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 173 | $key = $this->db->protect_identifiers($key); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 177 | $key = array($this->db->protect_identifiers($key)); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")"; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | $sql .= "\n)"; |
| 185 | |
| 186 | return $sql; |
| 187 | } |
| 188 | |
| 189 | // -------------------------------------------------------------------- |
| 190 | |
| 191 | /** |
| 192 | * Drop Table |
| 193 | * |
| 194 | * @access private |
| 195 | * @return bool |
| 196 | */ |
| 197 | function _drop_table($table) |
| 198 | { |
| 199 | // Not a supported PDO feature |
| 200 | if ($this->db->db_debug) |
| 201 | { |
| 202 | return $this->db->display_error('db_unsuported_feature'); |
| 203 | } |
| 204 | return FALSE; |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | /** |
| 210 | * Alter table query |
| 211 | * |
| 212 | * Generates a platform-specific query so that a table can be altered |
| 213 | * Called by add_column(), drop_column(), and column_alter(), |
| 214 | * |
| 215 | * @access private |
| 216 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 217 | * @param string the column name |
| 218 | * @param string the table name |
| 219 | * @param string the column definition |
| 220 | * @param string the default value |
| 221 | * @param boolean should 'NOT NULL' be added |
| 222 | * @param string the field after which we should add the new field |
| 223 | * @return object |
| 224 | */ |
| 225 | function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
| 226 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 227 | $sql = 'ALTER TABLE `'.$this->db->protect_identifiers($table).'` '.$alter_type.' '.$this->db->protect_identifiers($column_name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 228 | |
| 229 | // DROP has everything it needs now. |
| 230 | if ($alter_type == 'DROP') |
| 231 | { |
| 232 | return $sql; |
| 233 | } |
| 234 | |
| 235 | $sql .= " $column_definition"; |
| 236 | |
| 237 | if ($default_value != '') |
| 238 | { |
| 239 | $sql .= " DEFAULT \"$default_value\""; |
| 240 | } |
| 241 | |
| 242 | if ($null === NULL) |
| 243 | { |
| 244 | $sql .= ' NULL'; |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | $sql .= ' NOT NULL'; |
| 249 | } |
| 250 | |
| 251 | if ($after_field != '') |
| 252 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 253 | return $sql.' AFTER '.$this->db->protect_identifiers($after_field); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return $sql; |
| 257 | |
| 258 | } |
| 259 | |
| 260 | |
| 261 | // -------------------------------------------------------------------- |
| 262 | |
| 263 | /** |
| 264 | * Rename a table |
| 265 | * |
| 266 | * Generates a platform-specific query so that a table can be renamed |
| 267 | * |
| 268 | * @access private |
| 269 | * @param string the old table name |
| 270 | * @param string the new table name |
| 271 | * @return string |
| 272 | */ |
| 273 | function _rename_table($table_name, $new_table_name) |
| 274 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 275 | return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* End of file pdo_forge.php */ |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 281 | /* Location: ./system/database/drivers/pdo/pdo_forge.php */ |