blob: 2e5c81de3480997de5c10a334eda3269b7f6fb80 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren80ab8162011-08-22 18:26:12 -04006 *
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 *
Timothy Warren5137ebc2012-03-19 19:05:25 -040042 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -040043 * @param string the database name
44 * @return bool
45 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040046 function _create_database()
Timothy Warren80ab8162011-08-22 18:26:12 -040047 {
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 *
Timothy Warren5137ebc2012-03-19 19:05:25 -040062 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -040063 * @param string the database name
64 * @return bool
65 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040066 function _drop_database($name)
Timothy Warren80ab8162011-08-22 18:26:12 -040067 {
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 *
Timothy Warren5137ebc2012-03-19 19:05:25 -040082 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -040083 * @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 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040090 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren80ab8162011-08-22 18:26:12 -040091 {
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
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200116 $sql .= "\n\t".$this->db->protect_identifiers($field);
Timothy Warren80ab8162011-08-22 18:26:12 -0400117
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200163 $primary_keys = $this->db->protect_identifiers($primary_keys);
Timothy Warren80ab8162011-08-22 18:26:12 -0400164 $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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200173 $key = $this->db->protect_identifiers($key);
Timothy Warren80ab8162011-08-22 18:26:12 -0400174 }
175 else
176 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200177 $key = array($this->db->protect_identifiers($key));
Timothy Warren80ab8162011-08-22 18:26:12 -0400178 }
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 *
Timothy Warren5137ebc2012-03-19 19:05:25 -0400194 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 * @return bool
196 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400197 function _drop_table($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400198 {
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 *
Timothy Warren5137ebc2012-03-19 19:05:25 -0400215 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -0400216 * @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 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400225 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400226 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200227 $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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200253 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 }
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 *
Timothy Warren5137ebc2012-03-19 19:05:25 -0400268 * @access private
Timothy Warren80ab8162011-08-22 18:26:12 -0400269 * @param string the old table name
270 * @param string the new table name
271 * @return string
272 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400273 function _rename_table($table_name, $new_table_name)
Timothy Warren80ab8162011-08-22 18:26:12 -0400274 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200275 return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 }
277
Timothy Warren80ab8162011-08-22 18:26:12 -0400278}
279
280/* End of file pdo_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400281/* Location: ./system/database/drivers/pdo/pdo_forge.php */