blob: 89aaf5de3ac01285a14a45a4971b953c4c315aa0 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @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 Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * MySQL Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_mysql_forge extends CI_DB_forge {
Barry Mienydd671972010-10-04 16:33:58 +020038
Derek Allard2067d1a2008-11-13 22:59:24 +000039 /**
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 Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 $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 Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 if (array_key_exists('TYPE', $attributes))
100 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500101 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200102
Phil Sturgeon27324cd2011-01-05 16:32:57 +0000103 if (array_key_exists('CONSTRAINT', $attributes))
104 {
105 switch ($attributes['TYPE'])
106 {
107 case 'decimal':
108 case 'float':
109 case 'numeric':
110 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
111 break;
112
113 case 'enum':
114 case 'set':
115 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
116 break;
117
118 default:
119 $sql .= '('.$attributes['CONSTRAINT'].')';
120 }
121 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
125 {
126 $sql .= ' UNSIGNED';
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if (array_key_exists('DEFAULT', $attributes))
130 {
131 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Adam Jackett242c2582011-07-23 09:50:34 -0400134 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Adam Jackett242c2582011-07-23 09:50:34 -0400136 $sql .= ' NULL';
137 }
138 else
139 {
140 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
144 {
145 $sql .= ' AUTO_INCREMENT';
146 }
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // don't add a comma on the end of the last field
150 if (++$current_field_count < count($fields))
151 {
152 $sql .= ',';
153 }
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 return $sql;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Create Table
163 *
164 * @access private
165 * @param string the table name
166 * @param mixed the fields
167 * @param mixed primary key(s)
168 * @param mixed key(s)
169 * @param boolean should 'IF NOT EXISTS' be added to the SQL
170 * @return bool
171 */
172 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
173 {
174 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if ($if_not_exists === TRUE)
177 {
178 $sql .= 'IF NOT EXISTS ';
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 $sql .= $this->db->_escape_identifiers($table)." (";
182
183 $sql .= $this->_process_fields($fields);
184
185 if (count($primary_keys) > 0)
186 {
187 $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
188 $primary_keys = $this->db->_protect_identifiers($primary_keys);
189 $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
190 }
191
192 if (is_array($keys) && count($keys) > 0)
193 {
194 foreach ($keys as $key)
195 {
196 if (is_array($key))
197 {
198 $key_name = $this->db->_protect_identifiers(implode('_', $key));
Barry Mienydd671972010-10-04 16:33:58 +0200199 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201 else
202 {
203 $key_name = $this->db->_protect_identifiers($key);
204 $key = array($key_name);
205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
208 }
209 }
210
211 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
212
213 return $sql;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Drop Table
220 *
221 * @access private
222 * @return string
223 */
224 function _drop_table($table)
225 {
226 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Alter table query
233 *
234 * Generates a platform-specific query so that a table can be altered
235 * Called by add_column(), drop_column(), and column_alter(),
236 *
237 * @access private
238 * @param string the ALTER type (ADD, DROP, CHANGE)
239 * @param string the column name
240 * @param array fields
241 * @param string the field after which we should add the new field
242 * @return object
243 */
244 function _alter_table($alter_type, $table, $fields, $after_field = '')
245 {
246 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
247
248 // DROP has everything it needs now.
249 if ($alter_type == 'DROP')
250 {
251 return $sql.$this->db->_protect_identifiers($fields);
252 }
253
254 $sql .= $this->_process_fields($fields);
255
256 if ($after_field != '')
257 {
258 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 return $sql;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Rename a table
268 *
269 * Generates a platform-specific query so that a table can be renamed
270 *
271 * @access private
272 * @param string the old table name
273 * @param string the new table name
274 * @return string
275 */
276 function _rename_table($table_name, $new_table_name)
277 {
278 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
279 return $sql;
280 }
281
282}
283
284/* End of file mysql_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000285/* Location: ./system/database/drivers/mysql/mysql_forge.php */