Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * MySQL Forge Class |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author ExpressionEngine Dev Team |
| 23 | * @link http://codeigniter.com/user_guide/database/ |
| 24 | */ |
| 25 | class CI_DB_mysql_forge extends CI_DB_forge { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 26 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Create database |
| 29 | * |
| 30 | * @access private |
| 31 | * @param string the database name |
| 32 | * @return bool |
| 33 | */ |
| 34 | function _create_database($name) |
| 35 | { |
| 36 | return "CREATE DATABASE ".$name; |
| 37 | } |
| 38 | |
| 39 | // -------------------------------------------------------------------- |
| 40 | |
| 41 | /** |
| 42 | * Drop database |
| 43 | * |
| 44 | * @access private |
| 45 | * @param string the database name |
| 46 | * @return bool |
| 47 | */ |
| 48 | function _drop_database($name) |
| 49 | { |
| 50 | return "DROP DATABASE ".$name; |
| 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * Process Fields |
| 57 | * |
| 58 | * @access private |
| 59 | * @param mixed the fields |
| 60 | * @return string |
| 61 | */ |
| 62 | function _process_fields($fields) |
| 63 | { |
| 64 | $current_field_count = 0; |
| 65 | $sql = ''; |
| 66 | |
| 67 | foreach ($fields as $field=>$attributes) |
| 68 | { |
| 69 | // Numeric field names aren't allowed in databases, so if the key is |
| 70 | // numeric, we know it was assigned by PHP and the developer manually |
| 71 | // entered the field information, so we'll simply add it to the list |
| 72 | if (is_numeric($field)) |
| 73 | { |
| 74 | $sql .= "\n\t$attributes"; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | $sql .= "\n\t".$this->db->_protect_identifiers($field); |
| 81 | |
| 82 | if (array_key_exists('NAME', $attributes)) |
| 83 | { |
| 84 | $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' '; |
| 85 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | if (array_key_exists('TYPE', $attributes)) |
| 88 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 89 | $sql .= ' '.$attributes['TYPE']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Phil Sturgeon | 27324cd | 2011-01-05 16:32:57 +0000 | [diff] [blame] | 91 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 92 | { |
| 93 | switch ($attributes['TYPE']) |
| 94 | { |
| 95 | case 'decimal': |
| 96 | case 'float': |
| 97 | case 'numeric': |
| 98 | $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; |
| 99 | break; |
| 100 | |
| 101 | case 'enum': |
| 102 | case 'set': |
| 103 | $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 108 | } |
| 109 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 113 | { |
| 114 | $sql .= ' UNSIGNED'; |
| 115 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | if (array_key_exists('DEFAULT', $attributes)) |
| 118 | { |
| 119 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 120 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Adam Jackett | c5961e7 | 2011-07-23 09:50:34 -0400 | [diff] [blame] | 122 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | { |
Adam Jackett | c5961e7 | 2011-07-23 09:50:34 -0400 | [diff] [blame] | 124 | $sql .= ' NULL'; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 132 | { |
| 133 | $sql .= ' AUTO_INCREMENT'; |
| 134 | } |
| 135 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | return $sql; |
| 145 | } |
| 146 | |
| 147 | // -------------------------------------------------------------------- |
| 148 | |
| 149 | /** |
| 150 | * Create Table |
| 151 | * |
| 152 | * @access private |
| 153 | * @param string the table name |
| 154 | * @param mixed the fields |
| 155 | * @param mixed primary key(s) |
| 156 | * @param mixed key(s) |
| 157 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 158 | * @return bool |
| 159 | */ |
| 160 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 161 | { |
| 162 | $sql = 'CREATE TABLE '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | if ($if_not_exists === TRUE) |
| 165 | { |
| 166 | $sql .= 'IF NOT EXISTS '; |
| 167 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | $sql .= $this->db->_escape_identifiers($table)." ("; |
| 170 | |
| 171 | $sql .= $this->_process_fields($fields); |
| 172 | |
| 173 | if (count($primary_keys) > 0) |
| 174 | { |
| 175 | $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys)); |
| 176 | $primary_keys = $this->db->_protect_identifiers($primary_keys); |
| 177 | $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")"; |
| 178 | } |
| 179 | |
| 180 | if (is_array($keys) && count($keys) > 0) |
| 181 | { |
| 182 | foreach ($keys as $key) |
| 183 | { |
| 184 | if (is_array($key)) |
| 185 | { |
| 186 | $key_name = $this->db->_protect_identifiers(implode('_', $key)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | $key = $this->db->_protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | } |
| 189 | else |
| 190 | { |
| 191 | $key_name = $this->db->_protect_identifiers($key); |
| 192 | $key = array($key_name); |
| 193 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")"; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};"; |
| 200 | |
| 201 | return $sql; |
| 202 | } |
| 203 | |
| 204 | // -------------------------------------------------------------------- |
| 205 | |
| 206 | /** |
| 207 | * Drop Table |
| 208 | * |
| 209 | * @access private |
| 210 | * @return string |
| 211 | */ |
| 212 | function _drop_table($table) |
| 213 | { |
| 214 | return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table); |
| 215 | } |
| 216 | |
| 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
| 220 | * Alter table query |
| 221 | * |
| 222 | * Generates a platform-specific query so that a table can be altered |
| 223 | * Called by add_column(), drop_column(), and column_alter(), |
| 224 | * |
| 225 | * @access private |
| 226 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 227 | * @param string the column name |
| 228 | * @param array fields |
| 229 | * @param string the field after which we should add the new field |
| 230 | * @return object |
| 231 | */ |
| 232 | function _alter_table($alter_type, $table, $fields, $after_field = '') |
| 233 | { |
| 234 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type "; |
| 235 | |
| 236 | // DROP has everything it needs now. |
| 237 | if ($alter_type == 'DROP') |
| 238 | { |
| 239 | return $sql.$this->db->_protect_identifiers($fields); |
| 240 | } |
| 241 | |
| 242 | $sql .= $this->_process_fields($fields); |
| 243 | |
| 244 | if ($after_field != '') |
| 245 | { |
| 246 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 247 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 248 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | return $sql; |
| 250 | } |
| 251 | |
| 252 | // -------------------------------------------------------------------- |
| 253 | |
| 254 | /** |
| 255 | * Rename a table |
| 256 | * |
| 257 | * Generates a platform-specific query so that a table can be renamed |
| 258 | * |
| 259 | * @access private |
| 260 | * @param string the old table name |
| 261 | * @param string the new table name |
| 262 | * @return string |
| 263 | */ |
| 264 | function _rename_table($table_name, $new_table_name) |
| 265 | { |
| 266 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); |
| 267 | return $sql; |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | /* End of file mysql_forge.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 273 | /* Location: ./system/database/drivers/mysql/mysql_forge.php */ |