blob: 478b2dbfb2ac1a2cf95c6cce803492bd12295df6 [file] [log] [blame]
Timothy Warren80ab8162011-08-22 18:26:12 -04001<?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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * PDO Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040035 * @link http://codeigniter.com/database/
36 */
37class CI_DB_pdo_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
42 * @access private
43 * @param string the database name
44 * @return bool
45 */
46 function _create_database()
47 {
48 // PDO has no "create database" command since it's
49 // designed to connect to an existing database
50 if ($this->db->db_debug)
51 {
52 return $this->db->display_error('db_unsuported_feature');
53 }
54 return FALSE;
55 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Drop database
61 *
62 * @access private
63 * @param string the database name
64 * @return bool
65 */
66 function _drop_database($name)
67 {
68 // PDO has no "drop database" command since it's
69 // designed to connect to an existing database
70 if ($this->db->db_debug)
71 {
72 return $this->db->display_error('db_unsuported_feature');
73 }
74 return FALSE;
75 }
76
77 // --------------------------------------------------------------------
78
79 /**
80 * Create Table
81 *
82 * @access private
83 * @param string the table name
84 * @param array the fields
85 * @param mixed primary key(s)
86 * @param mixed key(s)
87 * @param boolean should 'IF NOT EXISTS' be added to the SQL
88 * @return bool
89 */
90 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
91 {
92 $sql = 'CREATE TABLE ';
93
94 if ($if_not_exists === TRUE)
95 {
96 $sql .= 'IF NOT EXISTS ';
97 }
98
Taufan Aditya18209332012-02-09 16:07:27 +070099 $sql .= '`'.$this->db->_escape_identifiers($table).'` (';
Timothy Warren80ab8162011-08-22 18:26:12 -0400100 $current_field_count = 0;
101
102 foreach ($fields as $field=>$attributes)
103 {
104 // Numeric field names aren't allowed in databases, so if the key is
105 // numeric, we know it was assigned by PHP and the developer manually
106 // entered the field information, so we'll simply add it to the list
107 if (is_numeric($field))
108 {
109 $sql .= "\n\t$attributes";
110 }
111 else
112 {
113 $attributes = array_change_key_case($attributes, CASE_UPPER);
Taufan Aditya18209332012-02-09 16:07:27 +0700114 $numeric = array('SERIAL', 'INTEGER');
Timothy Warren80ab8162011-08-22 18:26:12 -0400115
116 $sql .= "\n\t".$this->db->_protect_identifiers($field);
117
118 $sql .= ' '.$attributes['TYPE'];
119
120 if (array_key_exists('CONSTRAINT', $attributes))
121 {
Taufan Aditya18209332012-02-09 16:07:27 +0700122 // Exception for Postgre numeric which not too happy with constraint within those type
123 if ( ! ($this->db->pdodriver == 'pgsql' && in_array($attributes['TYPE'], $numeric)))
124 {
125 $sql .= '('.$attributes['CONSTRAINT'].')';
126 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400127 }
128
129 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
130 {
131 $sql .= ' UNSIGNED';
132 }
133
134 if (array_key_exists('DEFAULT', $attributes))
135 {
136 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
137 }
138
139 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
140 {
141 $sql .= ' NULL';
142 }
143 else
144 {
145 $sql .= ' NOT NULL';
146 }
147
148 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
149 {
150 $sql .= ' AUTO_INCREMENT';
151 }
152 }
153
154 // don't add a comma on the end of the last field
155 if (++$current_field_count < count($fields))
156 {
157 $sql .= ',';
158 }
159 }
160
161 if (count($primary_keys) > 0)
162 {
163 $primary_keys = $this->db->_protect_identifiers($primary_keys);
164 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
165 }
166
167 if (is_array($keys) && count($keys) > 0)
168 {
169 foreach ($keys as $key)
170 {
171 if (is_array($key))
172 {
173 $key = $this->db->_protect_identifiers($key);
174 }
175 else
176 {
177 $key = array($this->db->_protect_identifiers($key));
178 }
179
180 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
181 }
182 }
183
184 $sql .= "\n)";
185
186 return $sql;
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Drop Table
193 *
194 * @access private
195 * @return bool
196 */
197 function _drop_table($table)
198 {
199 // Not a supported PDO feature
200 if ($this->db->db_debug)
201 {
202 return $this->db->display_error('db_unsuported_feature');
203 }
204 return FALSE;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Alter table query
211 *
212 * Generates a platform-specific query so that a table can be altered
213 * Called by add_column(), drop_column(), and column_alter(),
214 *
215 * @access private
216 * @param string the ALTER type (ADD, DROP, CHANGE)
217 * @param string the column name
218 * @param string the table name
219 * @param string the column definition
220 * @param string the default value
221 * @param boolean should 'NOT NULL' be added
222 * @param string the field after which we should add the new field
223 * @return object
224 */
225 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
226 {
Taufan Aditya18209332012-02-09 16:07:27 +0700227 $sql = 'ALTER TABLE `'.$this->db->_protect_identifiers($table)."` $alter_type ".$this->db->_protect_identifiers($column_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400228
229 // DROP has everything it needs now.
230 if ($alter_type == 'DROP')
231 {
232 return $sql;
233 }
234
235 $sql .= " $column_definition";
236
237 if ($default_value != '')
238 {
239 $sql .= " DEFAULT \"$default_value\"";
240 }
241
242 if ($null === NULL)
243 {
244 $sql .= ' NULL';
245 }
246 else
247 {
248 $sql .= ' NOT NULL';
249 }
250
251 if ($after_field != '')
252 {
253 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
254 }
255
256 return $sql;
257
258 }
259
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Rename a table
265 *
266 * Generates a platform-specific query so that a table can be renamed
267 *
268 * @access private
269 * @param string the old table name
270 * @param string the new table name
271 * @return string
272 */
273 function _rename_table($table_name, $new_table_name)
274 {
275 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
276 return $sql;
277 }
278
Timothy Warren80ab8162011-08-22 18:26:12 -0400279}
280
281/* End of file pdo_forge.php */
282/* Location: ./system/database/drivers/pdo/pdo_forge.php */