blob: 3da5d2c494cbee5913676682f02c5778263ff09c [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
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @license http://www.codeigniter.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * MySQLi Forge Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
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 /**
56 * Drop Table
57 *
58 * @access private
59 * @return bool
60 */
61 function _drop_table($table)
62 {
63 return "DROP TABLE IF EXISTS ".$this->db->_escape_table($table);
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Create Table
70 *
71 * @access private
72 * @param string the table name
73 * @param array the fields
74 * @param mixed primary key(s)
75 * @param mixed key(s)
76 * @param boolean should 'IF NOT EXISTS' be added to the SQL
77 * @return bool
78 */
79 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
80 {
81 $sql = 'CREATE TABLE ';
82
83 if ($if_not_exists === TRUE)
84 {
85 $sql .= 'IF NOT EXISTS ';
86 }
87
88 $sql .= $this->db->_escape_table($table)." (";
89 $current_field_count = 0;
90
91 foreach ($fields as $field=>$attributes)
92 {
93 // Numeric field names aren't allowed in databases, so if the key is
94 // numeric, we know it was assigned by PHP and the developer manually
95 // entered the field information, so we'll simply add it to the list
96 if (is_numeric($field))
97 {
98 $sql .= "\n\t$attributes";
99 }
100 else
101 {
102 $attributes = array_change_key_case($attributes, CASE_UPPER);
103
104 $sql .= "\n\t".$this->db->_protect_identifiers($field);
105
106 $sql .= ' '.$attributes['TYPE'];
107
108 if (array_key_exists('CONSTRAINT', $attributes))
109 {
110 $sql .= '('.$attributes['CONSTRAINT'].')';
111 }
112
113 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
114 {
115 $sql .= ' UNSIGNED';
116 }
117
118 if (array_key_exists('DEFAULT', $attributes))
119 {
120 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
121 }
122
123 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
124 {
125 $sql .= ' NULL';
126 }
127 else
128 {
129 $sql .= ' NOT NULL';
130 }
131
132 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
133 {
134 $sql .= ' AUTO_INCREMENT';
135 }
136 }
137
138 // don't add a comma on the end of the last field
139 if (++$current_field_count < count($fields))
140 {
141 $sql .= ',';
142 }
143 }
144
145 if (count($primary_keys) > 0)
146 {
147 $primary_keys = $this->db->_protect_identifiers($primary_keys);
148 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
149 }
150
151 if (is_array($keys) && count($keys) > 0)
152 {
153 $keys = $this->db->_protect_identifiers($keys);
154 foreach ($keys as $key)
155 {
156 $sql .= ",\n\tKEY ($key)";
157 }
158 }
159
160 $sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
161
162 return $sql;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Alter table query
169 *
170 * Generates a platform-specific query so that a table can be altered
171 * Called by add_column(), drop_column(), and column_alter(),
172 *
173 * @access private
174 * @param string the ALTER type (ADD, DROP, CHANGE)
175 * @param string the column name
176 * @param string the table name
177 * @param string the column definition
178 * @param string the default value
179 * @param boolean should 'NOT NULL' be added
180 * @param string the field after which we should add the new field
181 * @return object
182 */
183 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
184 {
185 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
186
187 // DROP has everything it needs now.
188 if ($alter_type == 'DROP')
189 {
190 return $sql;
191 }
192
193 $sql .= " $column_definition";
194
195 if ($default_value != '')
196 {
197 $sql .= " DEFAULT \"$default_value\"";
198 }
199
200 if ($null === NULL)
201 {
202 $sql .= ' NULL';
203 }
204 else
205 {
206 $sql .= ' NOT NULL';
207 }
208
209 if ($after_field != '')
210 {
211 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
212 }
213
214 return $sql;
215 }
216}
217?>