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 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
| 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
| 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * PDO Forge Class |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author ExpressionEngine Dev Team |
| 23 | * @link http://codeigniter.com/database/ |
| 24 | */ |
| 25 | class CI_DB_pdo_forge extends CI_DB_forge { |
| 26 | |
| 27 | /** |
| 28 | * Create database |
| 29 | * |
| 30 | * @access private |
| 31 | * @param string the database name |
| 32 | * @return bool |
| 33 | */ |
| 34 | function _create_database() |
| 35 | { |
| 36 | // PDO has no "create database" command since it's |
| 37 | // designed to connect to an existing database |
| 38 | if ($this->db->db_debug) |
| 39 | { |
| 40 | return $this->db->display_error('db_unsuported_feature'); |
| 41 | } |
| 42 | return FALSE; |
| 43 | } |
| 44 | |
| 45 | // -------------------------------------------------------------------- |
| 46 | |
| 47 | /** |
| 48 | * Drop database |
| 49 | * |
| 50 | * @access private |
| 51 | * @param string the database name |
| 52 | * @return bool |
| 53 | */ |
| 54 | function _drop_database($name) |
| 55 | { |
| 56 | // PDO has no "drop database" command since it's |
| 57 | // designed to connect to an existing database |
| 58 | if ($this->db->db_debug) |
| 59 | { |
| 60 | return $this->db->display_error('db_unsuported_feature'); |
| 61 | } |
| 62 | return FALSE; |
| 63 | } |
| 64 | |
| 65 | // -------------------------------------------------------------------- |
| 66 | |
| 67 | /** |
| 68 | * Create Table |
| 69 | * |
| 70 | * @access private |
| 71 | * @param string the table name |
| 72 | * @param array the fields |
| 73 | * @param mixed primary key(s) |
| 74 | * @param mixed key(s) |
| 75 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 76 | * @return bool |
| 77 | */ |
| 78 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 79 | { |
| 80 | $sql = 'CREATE TABLE '; |
| 81 | |
| 82 | if ($if_not_exists === TRUE) |
| 83 | { |
| 84 | $sql .= 'IF NOT EXISTS '; |
| 85 | } |
| 86 | |
| 87 | $sql .= $this->db->_escape_identifiers($table)." ("; |
| 88 | $current_field_count = 0; |
| 89 | |
| 90 | foreach ($fields as $field=>$attributes) |
| 91 | { |
| 92 | // Numeric field names aren't allowed in databases, so if the key is |
| 93 | // numeric, we know it was assigned by PHP and the developer manually |
| 94 | // entered the field information, so we'll simply add it to the list |
| 95 | if (is_numeric($field)) |
| 96 | { |
| 97 | $sql .= "\n\t$attributes"; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 102 | |
| 103 | $sql .= "\n\t".$this->db->_protect_identifiers($field); |
| 104 | |
| 105 | $sql .= ' '.$attributes['TYPE']; |
| 106 | |
| 107 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 108 | { |
| 109 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 110 | } |
| 111 | |
| 112 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 113 | { |
| 114 | $sql .= ' UNSIGNED'; |
| 115 | } |
| 116 | |
| 117 | if (array_key_exists('DEFAULT', $attributes)) |
| 118 | { |
| 119 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 120 | } |
| 121 | |
| 122 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 123 | { |
| 124 | $sql .= ' NULL'; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | $sql .= ' NOT NULL'; |
| 129 | } |
| 130 | |
| 131 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 132 | { |
| 133 | $sql .= ' AUTO_INCREMENT'; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // don't add a comma on the end of the last field |
| 138 | if (++$current_field_count < count($fields)) |
| 139 | { |
| 140 | $sql .= ','; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (count($primary_keys) > 0) |
| 145 | { |
| 146 | $primary_keys = $this->db->_protect_identifiers($primary_keys); |
| 147 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 148 | } |
| 149 | |
| 150 | if (is_array($keys) && count($keys) > 0) |
| 151 | { |
| 152 | foreach ($keys as $key) |
| 153 | { |
| 154 | if (is_array($key)) |
| 155 | { |
| 156 | $key = $this->db->_protect_identifiers($key); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | $key = array($this->db->_protect_identifiers($key)); |
| 161 | } |
| 162 | |
| 163 | $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")"; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | $sql .= "\n)"; |
| 168 | |
| 169 | return $sql; |
| 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Drop Table |
| 176 | * |
| 177 | * @access private |
| 178 | * @return bool |
| 179 | */ |
| 180 | function _drop_table($table) |
| 181 | { |
| 182 | // Not a supported PDO feature |
| 183 | if ($this->db->db_debug) |
| 184 | { |
| 185 | return $this->db->display_error('db_unsuported_feature'); |
| 186 | } |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Alter table query |
| 194 | * |
| 195 | * Generates a platform-specific query so that a table can be altered |
| 196 | * Called by add_column(), drop_column(), and column_alter(), |
| 197 | * |
| 198 | * @access private |
| 199 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 200 | * @param string the column name |
| 201 | * @param string the table name |
| 202 | * @param string the column definition |
| 203 | * @param string the default value |
| 204 | * @param boolean should 'NOT NULL' be added |
| 205 | * @param string the field after which we should add the new field |
| 206 | * @return object |
| 207 | */ |
| 208 | function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
| 209 | { |
| 210 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name); |
| 211 | |
| 212 | // DROP has everything it needs now. |
| 213 | if ($alter_type == 'DROP') |
| 214 | { |
| 215 | return $sql; |
| 216 | } |
| 217 | |
| 218 | $sql .= " $column_definition"; |
| 219 | |
| 220 | if ($default_value != '') |
| 221 | { |
| 222 | $sql .= " DEFAULT \"$default_value\""; |
| 223 | } |
| 224 | |
| 225 | if ($null === NULL) |
| 226 | { |
| 227 | $sql .= ' NULL'; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | $sql .= ' NOT NULL'; |
| 232 | } |
| 233 | |
| 234 | if ($after_field != '') |
| 235 | { |
| 236 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 237 | } |
| 238 | |
| 239 | return $sql; |
| 240 | |
| 241 | } |
| 242 | |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Rename a table |
| 248 | * |
| 249 | * Generates a platform-specific query so that a table can be renamed |
| 250 | * |
| 251 | * @access private |
| 252 | * @param string the old table name |
| 253 | * @param string the new table name |
| 254 | * @return string |
| 255 | */ |
| 256 | function _rename_table($table_name, $new_table_name) |
| 257 | { |
| 258 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); |
| 259 | return $sql; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | } |
| 264 | |
| 265 | /* End of file pdo_forge.php */ |
| 266 | /* Location: ./system/database/drivers/pdo/pdo_forge.php */ |