blob: cdd5399f2091e58e93ccffe643adcc5109912d55 [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 * MySQLi 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_mysqli_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'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 if (array_key_exists('CONSTRAINT', $attributes))
105 {
106 $sql .= '('.$attributes['CONSTRAINT'].')';
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
110 {
111 $sql .= ' UNSIGNED';
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if (array_key_exists('DEFAULT', $attributes))
115 {
116 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Adam Jackett242c2582011-07-23 09:50:34 -0400119 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Adam Jackett242c2582011-07-23 09:50:34 -0400121 $sql .= ' NULL';
122 }
123 else
124 {
125 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
129 {
130 $sql .= ' AUTO_INCREMENT';
131 }
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // 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 Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 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 Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 if ($if_not_exists === TRUE)
162 {
163 $sql .= 'IF NOT EXISTS ';
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 $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 Mienydd671972010-10-04 16:33:58 +0200184 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186 else
187 {
188 $key_name = $this->db->_protect_identifiers($key);
189 $key = array($key_name);
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 $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 Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 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 Jonesa3ffbbb2008-05-11 18:18:29 +0000270/* Location: ./system/database/drivers/mysqli/mysqli_forge.php */