blob: ce6ab45b28add52e0d1bece737622a5b53186118 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard39b622d2008-01-16 21:10:09 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellis9ba2bf22008-09-12 23:34:39 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard39b622d2008-01-16 21:10:09 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * MySQLi Forge Class
20 *
21 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000023 * @link http://codeigniter.com/user_guide/database/
Derek Allard39b622d2008-01-16 21:10:09 +000024 */
25class CI_DB_mysqli_forge extends CI_DB_forge {
26
27 /**
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 /**
Derek Allardd7f42112008-04-11 11:43:11 +000056 * Process Fields
Derek Allard39b622d2008-01-16 21:10:09 +000057 *
58 * @access private
Derek Allardd7f42112008-04-11 11:43:11 +000059 * @param mixed the fields
60 * @return string
Derek Allard39b622d2008-01-16 21:10:09 +000061 */
Derek Allardd7f42112008-04-11 11:43:11 +000062 function _process_fields($fields)
Derek Allard39b622d2008-01-16 21:10:09 +000063 {
Derek Allard39b622d2008-01-16 21:10:09 +000064 $current_field_count = 0;
Derek Allardd7f42112008-04-11 11:43:11 +000065 $sql = '';
Derek Allard39b622d2008-01-16 21:10:09 +000066
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);
79
80 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Derek Allardd7f42112008-04-11 11:43:11 +000081
82 if (array_key_exists('NAME', $attributes))
83 {
84 $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
85 }
Derek Allard39b622d2008-01-16 21:10:09 +000086
Derek Allardd7f42112008-04-11 11:43:11 +000087 if (array_key_exists('TYPE', $attributes))
88 {
89 $sql .= ' '.$attributes['TYPE'];
90 }
Derek Allard39b622d2008-01-16 21:10:09 +000091
92 if (array_key_exists('CONSTRAINT', $attributes))
93 {
94 $sql .= '('.$attributes['CONSTRAINT'].')';
95 }
96
97 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
98 {
99 $sql .= ' UNSIGNED';
100 }
101
102 if (array_key_exists('DEFAULT', $attributes))
103 {
104 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
105 }
106
Derek Allardd7f42112008-04-11 11:43:11 +0000107 if (array_key_exists('NULL', $attributes))
Derek Allard39b622d2008-01-16 21:10:09 +0000108 {
Derek Allardd7f42112008-04-11 11:43:11 +0000109 $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
Derek Allard39b622d2008-01-16 21:10:09 +0000110 }
111
112 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
113 {
114 $sql .= ' AUTO_INCREMENT';
115 }
116 }
117
118 // don't add a comma on the end of the last field
119 if (++$current_field_count < count($fields))
120 {
121 $sql .= ',';
122 }
123 }
Derek Allardd7f42112008-04-11 11:43:11 +0000124
125 return $sql;
126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * Create Table
132 *
133 * @access private
134 * @param string the table name
135 * @param mixed the fields
136 * @param mixed primary key(s)
137 * @param mixed key(s)
138 * @param boolean should 'IF NOT EXISTS' be added to the SQL
139 * @return bool
140 */
141 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
142 {
143 $sql = 'CREATE TABLE ';
144
145 if ($if_not_exists === TRUE)
146 {
147 $sql .= 'IF NOT EXISTS ';
148 }
149
150 $sql .= $this->db->_escape_table($table)." (";
151
152 $sql .= $this->_process_fields($fields);
Derek Allard39b622d2008-01-16 21:10:09 +0000153
154 if (count($primary_keys) > 0)
155 {
Derek Jonesbd440092008-05-29 17:52:11 +0000156 $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
Derek Allard39b622d2008-01-16 21:10:09 +0000157 $primary_keys = $this->db->_protect_identifiers($primary_keys);
Derek Jonesbd440092008-05-29 17:52:11 +0000158 $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
Derek Allard39b622d2008-01-16 21:10:09 +0000159 }
160
161 if (is_array($keys) && count($keys) > 0)
162 {
Derek Allard39b622d2008-01-16 21:10:09 +0000163 foreach ($keys as $key)
164 {
Derek Jonesbd440092008-05-29 17:52:11 +0000165 if (is_array($key))
166 {
167 $key_name = $this->db->_protect_identifiers(implode('_', $key));
168 $key = $this->db->_protect_identifiers($key);
169 }
170 else
171 {
172 $key_name = $this->db->_protect_identifiers($key);
173 $key = array($key_name);
174 }
175
176 $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
Derek Allard39b622d2008-01-16 21:10:09 +0000177 }
178 }
179
180 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
181
182 return $sql;
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
Derek Allardd7f42112008-04-11 11:43:11 +0000188 * Drop Table
189 *
190 * @access private
191 * @return string
192 */
193 function _drop_table($table)
194 {
195 return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000201 * Alter table query
202 *
203 * Generates a platform-specific query so that a table can be altered
204 * Called by add_column(), drop_column(), and column_alter(),
205 *
206 * @access private
207 * @param string the ALTER type (ADD, DROP, CHANGE)
208 * @param string the column name
Derek Allardd7f42112008-04-11 11:43:11 +0000209 * @param array fields
Derek Allard39b622d2008-01-16 21:10:09 +0000210 * @param string the field after which we should add the new field
211 * @return object
212 */
Derek Allardd7f42112008-04-11 11:43:11 +0000213 function _alter_table($alter_type, $table, $fields, $after_field = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000214 {
Derek Allardd7f42112008-04-11 11:43:11 +0000215 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
Derek Allard39b622d2008-01-16 21:10:09 +0000216
217 // DROP has everything it needs now.
218 if ($alter_type == 'DROP')
219 {
Derek Allardd7f42112008-04-11 11:43:11 +0000220 return $sql.$this->db->_protect_identifiers($fields);
Derek Allard39b622d2008-01-16 21:10:09 +0000221 }
222
Derek Allardd7f42112008-04-11 11:43:11 +0000223 $sql .= $this->_process_fields($fields);
Derek Allard39b622d2008-01-16 21:10:09 +0000224
225 if ($after_field != '')
226 {
227 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
228 }
229
Derek Allardd7f42112008-04-11 11:43:11 +0000230 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000231 }
Derek Allard2385d302008-04-07 14:01:01 +0000232
233 // --------------------------------------------------------------------
234
235 /**
236 * Rename a table
237 *
238 * Generates a platform-specific query so that a table can be renamed
239 *
240 * @access private
241 * @param string the old table name
242 * @param string the new table name
243 * @return string
244 */
245 function _rename_table($table_name, $new_table_name)
246 {
247 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
248 return $sql;
249 }
Derek Allardd7f42112008-04-11 11:43:11 +0000250
Derek Allard39b622d2008-01-16 21:10:09 +0000251}
Derek Jones0b59f272008-05-13 04:22:33 +0000252
253/* End of file mysqli_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000254/* Location: ./system/database/drivers/mysqli/mysqli_forge.php */