CodeIgniter installed
[living-lab-site.git] / system / database / drivers / mysql / mysql_forge.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @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  * MySQL Forge Class
20  *
21  * @category    Database
22  * @author              ExpressionEngine Dev Team
23  * @link                http://codeigniter.com/user_guide/database/
24  */
25 class 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                                         if (array_key_exists('CONSTRAINT', $attributes))
92                                         {
93                                                 switch ($attributes['TYPE'])
94                                                 {
95                                                         case 'decimal':
96                                                         case 'float':
97                                                         case 'numeric':
98                                                                 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
99                                                         break;
100
101                                                         case 'enum':
102                                                         case 'set':
103                                                                 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
104                                                         break;
105
106                                                         default:
107                                                                 $sql .= '('.$attributes['CONSTRAINT'].')';
108                                                 }
109                                         }
110                                 }
111
112                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
113                                 {
114                                         $sql .= ' UNSIGNED';
115                                 }
116
117                                 if (array_key_exists('DEFAULT', $attributes))
118                                 {
119                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
120                                 }
121
122                                 if (array_key_exists('NULL', $attributes))
123                                 {
124                                         $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL';
125                                 }
126
127                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
128                                 {
129                                         $sql .= ' AUTO_INCREMENT';
130                                 }
131                         }
132
133                         // don't add a comma on the end of the last field
134                         if (++$current_field_count < count($fields))
135                         {
136                                 $sql .= ',';
137                         }
138                 }
139
140                 return $sql;
141         }
142
143         // --------------------------------------------------------------------
144
145         /**
146          * Create Table
147          *
148          * @access      private
149          * @param       string  the table name
150          * @param       mixed   the fields
151          * @param       mixed   primary key(s)
152          * @param       mixed   key(s)
153          * @param       boolean should 'IF NOT EXISTS' be added to the SQL
154          * @return      bool
155          */
156         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
157         {
158                 $sql = 'CREATE TABLE ';
159
160                 if ($if_not_exists === TRUE)
161                 {
162                         $sql .= 'IF NOT EXISTS ';
163                 }
164
165                 $sql .= $this->db->_escape_identifiers($table)." (";
166
167                 $sql .= $this->_process_fields($fields);
168
169                 if (count($primary_keys) > 0)
170                 {
171                         $key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
172                         $primary_keys = $this->db->_protect_identifiers($primary_keys);
173                         $sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
174                 }
175
176                 if (is_array($keys) && count($keys) > 0)
177                 {
178                         foreach ($keys as $key)
179                         {
180                                 if (is_array($key))
181                                 {
182                                         $key_name = $this->db->_protect_identifiers(implode('_', $key));
183                                         $key = $this->db->_protect_identifiers($key);
184                                 }
185                                 else
186                                 {
187                                         $key_name = $this->db->_protect_identifiers($key);
188                                         $key = array($key_name);
189                                 }
190
191                                 $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
192                         }
193                 }
194
195                 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
196
197                 return $sql;
198         }
199
200         // --------------------------------------------------------------------
201
202         /**
203          * Drop Table
204          *
205          * @access      private
206          * @return      string
207          */
208         function _drop_table($table)
209         {
210                 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
211         }
212
213         // --------------------------------------------------------------------
214
215         /**
216          * Alter table query
217          *
218          * Generates a platform-specific query so that a table can be altered
219          * Called by add_column(), drop_column(), and column_alter(),
220          *
221          * @access      private
222          * @param       string  the ALTER type (ADD, DROP, CHANGE)
223          * @param       string  the column name
224          * @param       array   fields
225          * @param       string  the field after which we should add the new field
226          * @return      object
227          */
228         function _alter_table($alter_type, $table, $fields, $after_field = '')
229         {
230                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
231
232                 // DROP has everything it needs now.
233                 if ($alter_type == 'DROP')
234                 {
235                         return $sql.$this->db->_protect_identifiers($fields);
236                 }
237
238                 $sql .= $this->_process_fields($fields);
239
240                 if ($after_field != '')
241                 {
242                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
243                 }
244
245                 return $sql;
246         }
247
248         // --------------------------------------------------------------------
249
250         /**
251          * Rename a table
252          *
253          * Generates a platform-specific query so that a table can be renamed
254          *
255          * @access      private
256          * @param       string  the old table name
257          * @param       string  the new table name
258          * @return      string
259          */
260         function _rename_table($table_name, $new_table_name)
261         {
262                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
263                 return $sql;
264         }
265
266 }
267
268 /* End of file mysql_forge.php */
269 /* Location: ./system/database/drivers/mysql/mysql_forge.php */