Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [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 |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * MySQL Forge Class |
| 30 | * |
| 31 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 32 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
| 35 | class CI_DB_mysql_forge extends CI_DB_forge { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 36 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 37 | protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Process Fields |
| 41 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * @param mixed the fields |
| 43 | * @return string |
| 44 | */ |
Andrey Andreev | 820999c | 2012-03-20 15:15:57 +0200 | [diff] [blame] | 45 | protected function _process_fields($fields) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
| 47 | $current_field_count = 0; |
| 48 | $sql = ''; |
| 49 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 50 | foreach ($fields as $field => $attributes) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | { |
| 52 | // Numeric field names aren't allowed in databases, so if the key is |
| 53 | // numeric, we know it was assigned by PHP and the developer manually |
| 54 | // entered the field information, so we'll simply add it to the list |
| 55 | if (is_numeric($field)) |
| 56 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 57 | $sql .= "\n\t".$attributes; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | } |
| 59 | else |
| 60 | { |
| 61 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 63 | $sql .= "\n\t".$this->db->escape_identifiers($field); |
| 64 | |
| 65 | empty($attributes['NAME']) OR ' '.$this->db->protect_identifiers($attributes['NAME']).' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 67 | if ( ! empty($attributes['TYPE'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 69 | $sql .= ' '.$attributes['TYPE']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 71 | if ( ! empty($attributes['CONSTRAINT'])) |
Phil Sturgeon | 27324cd | 2011-01-05 16:32:57 +0000 | [diff] [blame] | 72 | { |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 73 | switch (strtolower($attributes['TYPE'])) |
Phil Sturgeon | 27324cd | 2011-01-05 16:32:57 +0000 | [diff] [blame] | 74 | { |
| 75 | case 'decimal': |
| 76 | case 'float': |
| 77 | case 'numeric': |
| 78 | $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 79 | break; |
Phil Sturgeon | 27324cd | 2011-01-05 16:32:57 +0000 | [diff] [blame] | 80 | case 'enum': |
| 81 | case 'set': |
| 82 | $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 83 | break; |
Phil Sturgeon | 27324cd | 2011-01-05 16:32:57 +0000 | [diff] [blame] | 84 | default: |
| 85 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 86 | } |
| 87 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 90 | if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) |
| 91 | { |
| 92 | $sql .= ' UNSIGNED'; |
| 93 | } |
| 94 | |
| 95 | if (isset($attributes['DEFAULT'])) |
| 96 | { |
| 97 | $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; |
| 98 | } |
| 99 | |
| 100 | $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) |
| 101 | ? ' NULL' : ' NOT NULL'; |
| 102 | |
| 103 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 104 | { |
| 105 | $sql .= ' AUTO_INCREMENT'; |
| 106 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | // don't add a comma on the end of the last field |
| 110 | if (++$current_field_count < count($fields)) |
| 111 | { |
| 112 | $sql .= ','; |
| 113 | } |
| 114 | } |
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 | return $sql; |
| 117 | } |
| 118 | |
| 119 | // -------------------------------------------------------------------- |
| 120 | |
| 121 | /** |
| 122 | * Create Table |
| 123 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | * @param string the table name |
| 125 | * @param mixed the fields |
| 126 | * @param mixed primary key(s) |
| 127 | * @param mixed key(s) |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 128 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | * @return bool |
| 130 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 131 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
| 133 | $sql = 'CREATE TABLE '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | if ($if_not_exists === TRUE) |
| 136 | { |
| 137 | $sql .= 'IF NOT EXISTS '; |
| 138 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 140 | $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | |
| 142 | if (count($primary_keys) > 0) |
| 143 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 144 | $key_name = $this->db->escape_identifiers(implode('_', $primary_keys)); |
| 145 | $sql .= ",\n\tPRIMARY KEY ".$key_name.' ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (is_array($keys) && count($keys) > 0) |
| 149 | { |
| 150 | foreach ($keys as $key) |
| 151 | { |
| 152 | if (is_array($key)) |
| 153 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 154 | $key_name = $this->db->escape_identifiers(implode('_', $key)); |
| 155 | $key = $this->db->escape_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 159 | $key_name = $this->db->escape_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | $key = array($key_name); |
| 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 163 | $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 167 | return $sql."\n) DEFAULT CHARACTER SET ".$this->db->char_set.' COLLATE '.$this->db->dbcollat.';'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | * Alter table query |
| 174 | * |
| 175 | * Generates a platform-specific query so that a table can be altered |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 176 | * Called by add_column(), drop_column() and column_alter() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 179 | * @param string the column name |
| 180 | * @param array fields |
| 181 | * @param string the field after which we should add the new field |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 182 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 184 | protected function _alter_table($alter_type, $table, $fields, $after_field = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 186 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | |
| 188 | // DROP has everything it needs now. |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 189 | if ($alter_type === 'DROP') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 191 | return $sql.$this->db->escape_identifiers($fields); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Andrey Andreev | 2caf289 | 2012-01-27 00:12:03 +0200 | [diff] [blame] | 194 | return $sql.$this->_process_fields($fields) |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame^] | 195 | .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* End of file mysql_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 201 | /* Location: ./system/database/drivers/mysql/mysql_forge.php */ |