blob: 26054945789df4a2054dc115d8ac7a8687440771 [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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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 * MySQLi Forge Class
20 *
21 * @category Database
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
24 */
25class CI_DB_mysqli_forge extends CI_DB_forge {
Barry Mienydd671972010-10-04 16:33:58 +020026
Derek Allard2067d1a2008-11-13 22:59:24 +000027 /**
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 Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 $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 Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 if (array_key_exists('TYPE', $attributes))
88 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050089 $sql .= ' '.$attributes['TYPE'];
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 if (array_key_exists('CONSTRAINT', $attributes))
93 {
94 $sql .= '('.$attributes['CONSTRAINT'].')';
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
98 {
99 $sql .= ' UNSIGNED';
100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 if (array_key_exists('DEFAULT', $attributes))
103 {
104 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Adam Jackett242c2582011-07-23 09:50:34 -0400107 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Adam Jackett242c2582011-07-23 09:50:34 -0400109 $sql .= ' NULL';
110 }
111 else
112 {
113 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
117 {
118 $sql .= ' AUTO_INCREMENT';
119 }
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // don't add a comma on the end of the last field
123 if (++$current_field_count < count($fields))
124 {
125 $sql .= ',';
126 }
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 return $sql;
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Create Table
136 *
137 * @access private
138 * @param string the table name
139 * @param mixed the fields
140 * @param mixed primary key(s)
141 * @param mixed key(s)
142 * @param boolean should 'IF NOT EXISTS' be added to the SQL
143 * @return bool
144 */
145 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
146 {
147 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if ($if_not_exists === TRUE)
150 {
151 $sql .= 'IF NOT EXISTS ';
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 $sql .= $this->db->_escape_identifiers($table)." (";
155
156 $sql .= $this->_process_fields($fields);
157
158 if (count($primary_keys) > 0)
159 {
160 $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
161 $primary_keys = $this->db->_protect_identifiers($primary_keys);
162 $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
163 }
164
165 if (is_array($keys) && count($keys) > 0)
166 {
167 foreach ($keys as $key)
168 {
169 if (is_array($key))
170 {
171 $key_name = $this->db->_protect_identifiers(implode('_', $key));
Barry Mienydd671972010-10-04 16:33:58 +0200172 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174 else
175 {
176 $key_name = $this->db->_protect_identifiers($key);
177 $key = array($key_name);
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
181 }
182 }
183
184 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
185
186 return $sql;
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Drop Table
193 *
194 * @access private
195 * @return string
196 */
197 function _drop_table($table)
198 {
199 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Alter table query
206 *
207 * Generates a platform-specific query so that a table can be altered
208 * Called by add_column(), drop_column(), and column_alter(),
209 *
210 * @access private
211 * @param string the ALTER type (ADD, DROP, CHANGE)
212 * @param string the column name
213 * @param array fields
214 * @param string the field after which we should add the new field
215 * @return object
216 */
217 function _alter_table($alter_type, $table, $fields, $after_field = '')
218 {
219 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
220
221 // DROP has everything it needs now.
222 if ($alter_type == 'DROP')
223 {
224 return $sql.$this->db->_protect_identifiers($fields);
225 }
226
227 $sql .= $this->_process_fields($fields);
228
229 if ($after_field != '')
230 {
231 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 return $sql;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Rename a table
241 *
242 * Generates a platform-specific query so that a table can be renamed
243 *
244 * @access private
245 * @param string the old table name
246 * @param string the new table name
247 * @return string
248 */
249 function _rename_table($table_name, $new_table_name)
250 {
251 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
252 return $sql;
253 }
254
255}
256
257/* End of file mysqli_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000258/* Location: ./system/database/drivers/mysqli/mysqli_forge.php */