blob: 6bff3542f604ce4ac71a354a09a883357d1a7725 [file] [log] [blame]
Andrey Andreev9fd79f52012-03-20 23:04:13 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
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
Andrey Andreev9fd79f52012-03-20 23:04:13 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev9fd79f52012-03-20 23:04:13 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Timothy Warren80ab8162011-08-22 18:26:12 -040028/**
29 * PDO Forge Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040033 * @link http://codeigniter.com/database/
34 */
35class CI_DB_pdo_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Timothy Warren80ab8162011-08-22 18:26:12 -040040 * @param string the database name
41 * @return bool
42 */
Andrey Andreev9fd79f52012-03-20 23:04:13 +020043 public function _create_database()
Timothy Warren80ab8162011-08-22 18:26:12 -040044 {
45 // PDO has no "create database" command since it's
46 // designed to connect to an existing database
47 if ($this->db->db_debug)
48 {
49 return $this->db->display_error('db_unsuported_feature');
50 }
51 return FALSE;
52 }
53
54 // --------------------------------------------------------------------
55
56 /**
57 * Drop database
58 *
Timothy Warren80ab8162011-08-22 18:26:12 -040059 * @param string the database name
60 * @return bool
61 */
Andrey Andreev9fd79f52012-03-20 23:04:13 +020062 public function _drop_database($name)
Timothy Warren80ab8162011-08-22 18:26:12 -040063 {
64 // PDO has no "drop database" command since it's
65 // designed to connect to an existing database
66 if ($this->db->db_debug)
67 {
68 return $this->db->display_error('db_unsuported_feature');
69 }
70 return FALSE;
71 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Create Table
77 *
Timothy Warren80ab8162011-08-22 18:26:12 -040078 * @param string the table name
79 * @param array the fields
80 * @param mixed primary key(s)
81 * @param mixed key(s)
Andrey Andreev9fd79f52012-03-20 23:04:13 +020082 * @param bool should 'IF NOT EXISTS' be added to the SQL
Timothy Warren80ab8162011-08-22 18:26:12 -040083 * @return bool
84 */
Andrey Andreev9fd79f52012-03-20 23:04:13 +020085 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren80ab8162011-08-22 18:26:12 -040086 {
87 $sql = 'CREATE TABLE ';
88
89 if ($if_not_exists === TRUE)
90 {
91 $sql .= 'IF NOT EXISTS ';
92 }
93
Taufan Aditya18209332012-02-09 16:07:27 +070094 $sql .= '`'.$this->db->_escape_identifiers($table).'` (';
Timothy Warren80ab8162011-08-22 18:26:12 -040095 $current_field_count = 0;
96
97 foreach ($fields as $field=>$attributes)
98 {
99 // Numeric field names aren't allowed in databases, so if the key is
100 // numeric, we know it was assigned by PHP and the developer manually
101 // entered the field information, so we'll simply add it to the list
102 if (is_numeric($field))
103 {
104 $sql .= "\n\t$attributes";
105 }
106 else
107 {
108 $attributes = array_change_key_case($attributes, CASE_UPPER);
Taufan Aditya18209332012-02-09 16:07:27 +0700109 $numeric = array('SERIAL', 'INTEGER');
Timothy Warren80ab8162011-08-22 18:26:12 -0400110
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200111 $sql .= "\n\t".$this->db->protect_identifiers($field);
Timothy Warren80ab8162011-08-22 18:26:12 -0400112
113 $sql .= ' '.$attributes['TYPE'];
114
115 if (array_key_exists('CONSTRAINT', $attributes))
116 {
Taufan Aditya18209332012-02-09 16:07:27 +0700117 // Exception for Postgre numeric which not too happy with constraint within those type
118 if ( ! ($this->db->pdodriver == 'pgsql' && in_array($attributes['TYPE'], $numeric)))
119 {
120 $sql .= '('.$attributes['CONSTRAINT'].')';
121 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400122 }
123
124 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
125 {
126 $sql .= ' UNSIGNED';
127 }
128
129 if (array_key_exists('DEFAULT', $attributes))
130 {
131 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
132 }
133
134 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
135 {
136 $sql .= ' NULL';
137 }
138 else
139 {
140 $sql .= ' NOT NULL';
141 }
142
143 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
144 {
145 $sql .= ' AUTO_INCREMENT';
146 }
147 }
148
149 // don't add a comma on the end of the last field
150 if (++$current_field_count < count($fields))
151 {
152 $sql .= ',';
153 }
154 }
155
156 if (count($primary_keys) > 0)
157 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200158 $primary_keys = $this->db->protect_identifiers($primary_keys);
Timothy Warren80ab8162011-08-22 18:26:12 -0400159 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
160 }
161
162 if (is_array($keys) && count($keys) > 0)
163 {
164 foreach ($keys as $key)
165 {
166 if (is_array($key))
167 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200168 $key = $this->db->protect_identifiers($key);
Timothy Warren80ab8162011-08-22 18:26:12 -0400169 }
170 else
171 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200172 $key = array($this->db->protect_identifiers($key));
Timothy Warren80ab8162011-08-22 18:26:12 -0400173 }
174
175 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
176 }
177 }
178
179 $sql .= "\n)";
180
181 return $sql;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Drop Table
188 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400189 * @return bool
190 */
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200191 public function _drop_table($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 {
193 // Not a supported PDO feature
194 if ($this->db->db_debug)
195 {
196 return $this->db->display_error('db_unsuported_feature');
197 }
198 return FALSE;
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Alter table query
205 *
206 * Generates a platform-specific query so that a table can be altered
207 * Called by add_column(), drop_column(), and column_alter(),
208 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400209 * @param string the ALTER type (ADD, DROP, CHANGE)
210 * @param string the column name
211 * @param string the table name
212 * @param string the column definition
213 * @param string the default value
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200214 * @param bool should 'NOT NULL' be added
Timothy Warren80ab8162011-08-22 18:26:12 -0400215 * @param string the field after which we should add the new field
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200216 * @return string
Timothy Warren80ab8162011-08-22 18:26:12 -0400217 */
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200218 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400219 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200220 $sql = 'ALTER TABLE `'.$this->db->protect_identifiers($table).'` '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400221
222 // DROP has everything it needs now.
223 if ($alter_type == 'DROP')
224 {
225 return $sql;
226 }
227
228 $sql .= " $column_definition";
229
230 if ($default_value != '')
231 {
232 $sql .= " DEFAULT \"$default_value\"";
233 }
234
235 if ($null === NULL)
236 {
237 $sql .= ' NULL';
238 }
239 else
240 {
241 $sql .= ' NOT NULL';
242 }
243
244 if ($after_field != '')
245 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200246 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Timothy Warren80ab8162011-08-22 18:26:12 -0400247 }
248
249 return $sql;
250
251 }
252
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Rename a table
258 *
259 * Generates a platform-specific query so that a table can be renamed
260 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400261 * @param string the old table name
262 * @param string the new table name
263 * @return string
264 */
Andrey Andreev9fd79f52012-03-20 23:04:13 +0200265 public function _rename_table($table_name, $new_table_name)
Timothy Warren80ab8162011-08-22 18:26:12 -0400266 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200267 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 -0400268 }
269
Timothy Warren80ab8162011-08-22 18:26:12 -0400270}
271
272/* End of file pdo_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400273/* Location: ./system/database/drivers/pdo/pdo_forge.php */