blob: 4eb449ef2e54a7a91117903514414d0fba4db061 [file] [log] [blame]
Derek Allard39b622d2008-01-16 21:10:09 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Allard39b622d2008-01-16 21:10:09 +00009 * @copyright Copyright (c) 2006, 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 * MySQL 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_mysql_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 /**
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);
79
80 $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 }
86
87 if (array_key_exists('TYPE', $attributes))
88 {
89 $sql .= ' '.$attributes['TYPE'];
90 }
91
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
107 if (array_key_exists('NULL', $attributes))
108 {
109 $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
110 }
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 }
124
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);
153
154 if (count($primary_keys) > 0)
155 {
156 $primary_keys = $this->db->_protect_identifiers($primary_keys);
157 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
158 }
159
160 if (is_array($keys) && count($keys) > 0)
161 {
162 $keys = $this->db->_protect_identifiers($keys);
163 foreach ($keys as $key)
164 {
165 $sql .= ",\n\tKEY ($key)";
166 }
167 }
168
169 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
170
171 return $sql;
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Drop Table
178 *
179 * @access private
Derek Allard2385d302008-04-07 14:01:01 +0000180 * @return string
Derek Allard39b622d2008-01-16 21:10:09 +0000181 */
182 function _drop_table($table)
183 {
184 return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Alter table query
191 *
192 * Generates a platform-specific query so that a table can be altered
193 * Called by add_column(), drop_column(), and column_alter(),
194 *
195 * @access private
196 * @param string the ALTER type (ADD, DROP, CHANGE)
197 * @param string the column name
198 * @param array fields
199 * @param string the field after which we should add the new field
200 * @return object
201 */
202 function _alter_table($alter_type, $table, $fields, $after_field = '')
203 {
204 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
205
206 // DROP has everything it needs now.
207 if ($alter_type == 'DROP')
208 {
209 return $sql.$this->db->_protect_identifiers($fields);
210 }
211
212 $sql .= $this->_process_fields($fields);
213
214 if ($after_field != '')
215 {
216 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
217 }
218
219 return $sql;
220 }
221
Derek Allard2385d302008-04-07 14:01:01 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Rename a table
226 *
227 * Generates a platform-specific query so that a table can be renamed
228 *
229 * @access private
230 * @param string the old table name
231 * @param string the new table name
232 * @return string
233 */
234 function _rename_table($table_name, $new_table_name)
235 {
236 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
237 return $sql;
238 }
239
Derek Allard39b622d2008-01-16 21:10:09 +0000240}
Derek Jonesc7deac92008-05-11 16:27:41 +0000241?>