blob: bf3066529e9e1227e0f56b82162503889ceef4b0 [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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
99 $sql .= $this->db->_escape_identifiers($table)." (";
100 $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);
114
115 $sql .= "\n\t".$this->db->_protect_identifiers($field);
116
117 $sql .= ' '.$attributes['TYPE'];
118
119 if (array_key_exists('CONSTRAINT', $attributes))
120 {
121 $sql .= '('.$attributes['CONSTRAINT'].')';
122 }
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 {
158 $primary_keys = $this->db->_protect_identifiers($primary_keys);
159 $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 {
168 $key = $this->db->_protect_identifiers($key);
169 }
170 else
171 {
172 $key = array($this->db->_protect_identifiers($key));
173 }
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 *
189 * @access private
190 * @return bool
191 */
192 function _drop_table($table)
193 {
194 // Not a supported PDO feature
195 if ($this->db->db_debug)
196 {
197 return $this->db->display_error('db_unsuported_feature');
198 }
199 return FALSE;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Alter table query
206 *
207 * Generates a platform-specific query so that a table can be altered
208 * Called by add_column(), drop_column(), and column_alter(),
209 *
210 * @access private
211 * @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 */
220 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
221 {
222 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
223
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 {
248 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
249 }
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 *
263 * @access private
264 * @param string the old table name
265 * @param string the new table name
266 * @return string
267 */
268 function _rename_table($table_name, $new_table_name)
269 {
270 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
271 return $sql;
272 }
273
274
275}
276
277/* End of file pdo_forge.php */
278/* Location: ./system/database/drivers/pdo/pdo_forge.php */