blob: 7fca962b332f98dd4631318408237fc938a174f0 [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 Warren80ab8162011-08-22 18:26:12 -040042 * @param string the database name
43 * @return bool
44 */
Timothy Warren78a2de42012-03-19 18:57:56 -040045 public function _create_database()
Timothy Warren80ab8162011-08-22 18:26:12 -040046 {
47 // PDO has no "create database" command since it's
48 // designed to connect to an existing database
49 if ($this->db->db_debug)
50 {
51 return $this->db->display_error('db_unsuported_feature');
52 }
53 return FALSE;
54 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Drop database
60 *
Timothy Warren80ab8162011-08-22 18:26:12 -040061 * @param string the database name
62 * @return bool
63 */
Timothy Warren78a2de42012-03-19 18:57:56 -040064 public function _drop_database($name)
Timothy Warren80ab8162011-08-22 18:26:12 -040065 {
66 // PDO has no "drop database" command since it's
67 // designed to connect to an existing database
68 if ($this->db->db_debug)
69 {
70 return $this->db->display_error('db_unsuported_feature');
71 }
72 return FALSE;
73 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Create Table
79 *
Timothy Warren80ab8162011-08-22 18:26:12 -040080 * @param string the table name
81 * @param array the fields
82 * @param mixed primary key(s)
83 * @param mixed key(s)
84 * @param boolean should 'IF NOT EXISTS' be added to the SQL
85 * @return bool
86 */
Timothy Warren78a2de42012-03-19 18:57:56 -040087 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Timothy Warren80ab8162011-08-22 18:26:12 -040088 {
89 $sql = 'CREATE TABLE ';
90
91 if ($if_not_exists === TRUE)
92 {
93 $sql .= 'IF NOT EXISTS ';
94 }
95
Taufan Aditya18209332012-02-09 16:07:27 +070096 $sql .= '`'.$this->db->_escape_identifiers($table).'` (';
Timothy Warren80ab8162011-08-22 18:26:12 -040097 $current_field_count = 0;
98
99 foreach ($fields as $field=>$attributes)
100 {
101 // Numeric field names aren't allowed in databases, so if the key is
102 // numeric, we know it was assigned by PHP and the developer manually
103 // entered the field information, so we'll simply add it to the list
104 if (is_numeric($field))
105 {
106 $sql .= "\n\t$attributes";
107 }
108 else
109 {
110 $attributes = array_change_key_case($attributes, CASE_UPPER);
Taufan Aditya18209332012-02-09 16:07:27 +0700111 $numeric = array('SERIAL', 'INTEGER');
Timothy Warren80ab8162011-08-22 18:26:12 -0400112
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200113 $sql .= "\n\t".$this->db->protect_identifiers($field);
Timothy Warren80ab8162011-08-22 18:26:12 -0400114
115 $sql .= ' '.$attributes['TYPE'];
116
117 if (array_key_exists('CONSTRAINT', $attributes))
118 {
Taufan Aditya18209332012-02-09 16:07:27 +0700119 // Exception for Postgre numeric which not too happy with constraint within those type
120 if ( ! ($this->db->pdodriver == 'pgsql' && in_array($attributes['TYPE'], $numeric)))
121 {
122 $sql .= '('.$attributes['CONSTRAINT'].')';
123 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400124 }
125
126 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
127 {
128 $sql .= ' UNSIGNED';
129 }
130
131 if (array_key_exists('DEFAULT', $attributes))
132 {
133 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
134 }
135
136 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
137 {
138 $sql .= ' NULL';
139 }
140 else
141 {
142 $sql .= ' NOT NULL';
143 }
144
145 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
146 {
147 $sql .= ' AUTO_INCREMENT';
148 }
149 }
150
151 // don't add a comma on the end of the last field
152 if (++$current_field_count < count($fields))
153 {
154 $sql .= ',';
155 }
156 }
157
158 if (count($primary_keys) > 0)
159 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200160 $primary_keys = $this->db->protect_identifiers($primary_keys);
Timothy Warren80ab8162011-08-22 18:26:12 -0400161 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
162 }
163
164 if (is_array($keys) && count($keys) > 0)
165 {
166 foreach ($keys as $key)
167 {
168 if (is_array($key))
169 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200170 $key = $this->db->protect_identifiers($key);
Timothy Warren80ab8162011-08-22 18:26:12 -0400171 }
172 else
173 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200174 $key = array($this->db->protect_identifiers($key));
Timothy Warren80ab8162011-08-22 18:26:12 -0400175 }
176
177 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
178 }
179 }
180
181 $sql .= "\n)";
182
183 return $sql;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Drop Table
190 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 * @return bool
192 */
Timothy Warren78a2de42012-03-19 18:57:56 -0400193 public function _drop_table($table)
Timothy Warren80ab8162011-08-22 18:26:12 -0400194 {
195 // Not a supported PDO feature
196 if ($this->db->db_debug)
197 {
198 return $this->db->display_error('db_unsuported_feature');
199 }
200 return FALSE;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Alter table query
207 *
208 * Generates a platform-specific query so that a table can be altered
209 * Called by add_column(), drop_column(), and column_alter(),
210 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400211 * @param string the ALTER type (ADD, DROP, CHANGE)
212 * @param string the column name
213 * @param string the table name
214 * @param string the column definition
215 * @param string the default value
216 * @param boolean should 'NOT NULL' be added
217 * @param string the field after which we should add the new field
218 * @return object
219 */
Timothy Warren78a2de42012-03-19 18:57:56 -0400220 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Timothy Warren80ab8162011-08-22 18:26:12 -0400221 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200222 $sql = 'ALTER TABLE `'.$this->db->protect_identifiers($table).'` '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400223
224 // DROP has everything it needs now.
225 if ($alter_type == 'DROP')
226 {
227 return $sql;
228 }
229
230 $sql .= " $column_definition";
231
232 if ($default_value != '')
233 {
234 $sql .= " DEFAULT \"$default_value\"";
235 }
236
237 if ($null === NULL)
238 {
239 $sql .= ' NULL';
240 }
241 else
242 {
243 $sql .= ' NOT NULL';
244 }
245
246 if ($after_field != '')
247 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200248 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Timothy Warren80ab8162011-08-22 18:26:12 -0400249 }
250
251 return $sql;
252
253 }
254
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Rename a table
260 *
261 * Generates a platform-specific query so that a table can be renamed
262 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400263 * @param string the old table name
264 * @param string the new table name
265 * @return string
266 */
Timothy Warren78a2de42012-03-19 18:57:56 -0400267 public function _rename_table($table_name, $new_table_name)
Timothy Warren80ab8162011-08-22 18:26:12 -0400268 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200269 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 -0400270 }
271
Timothy Warren80ab8162011-08-22 18:26:12 -0400272}
273
274/* End of file pdo_forge.php */
Timothy Warren78a2de42012-03-19 18:57:56 -0400275/* Location: ./system/database/drivers/pdo/pdo_forge.php */