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