blob: 166cc4e6a6423a218e16c332389a3163bb70850d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @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)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Postgre Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_postgre_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($name)
47 {
48 return "CREATE DATABASE ".$name;
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Drop database
55 *
56 * @access private
57 * @param string the database name
58 * @return bool
59 */
60 function _drop_database($name)
61 {
62 return "DROP DATABASE ".$name;
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Create Table
69 *
70 * @access private
71 * @param string the table name
72 * @param array the fields
73 * @param mixed primary key(s)
74 * @param mixed key(s)
75 * @param boolean should 'IF NOT EXISTS' be added to the SQL
76 * @return bool
77 */
78 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
79 {
80 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 if ($if_not_exists === TRUE)
83 {
Greg Aker678256c2010-12-21 12:05:12 -060084 if ($this->db->table_exists($table))
85 {
86 return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement
87 }
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 $sql .= $this->db->_escape_identifiers($table)." (";
91 $current_field_count = 0;
92
93 foreach ($fields as $field=>$attributes)
94 {
95 // Numeric field names aren't allowed in databases, so if the key is
96 // numeric, we know it was assigned by PHP and the developer manually
97 // entered the field information, so we'll simply add it to the list
98 if (is_numeric($field))
99 {
100 $sql .= "\n\t$attributes";
101 }
102 else
103 {
104 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200107
Greg Aker678256c2010-12-21 12:05:12 -0600108 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200109
Greg Aker678256c2010-12-21 12:05:12 -0600110 // Convert datatypes to be PostgreSQL-compatible
111 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Greg Aker678256c2010-12-21 12:05:12 -0600113 case 'TINYINT':
114 $attributes['TYPE'] = 'SMALLINT';
115 break;
116 case 'SMALLINT':
117 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
118 break;
119 case 'MEDIUMINT':
120 $attributes['TYPE'] = 'INTEGER';
121 break;
122 case 'INT':
123 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
124 break;
125 case 'BIGINT':
126 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
127 break;
128 case 'DOUBLE':
129 $attributes['TYPE'] = 'DOUBLE PRECISION';
130 break;
131 case 'DATETIME':
132 $attributes['TYPE'] = 'TIMESTAMP';
133 break;
134 case 'LONGTEXT':
135 $attributes['TYPE'] = 'TEXT';
136 break;
137 case 'BLOB':
138 $attributes['TYPE'] = 'BYTEA';
139 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Greg Aker678256c2010-12-21 12:05:12 -0600142 // If this is an auto-incrementing primary key, use the serial data type instead
Derek Jones37f4b9c2011-07-01 17:56:50 -0500143 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
Greg Aker678256c2010-12-21 12:05:12 -0600144 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Greg Aker678256c2010-12-21 12:05:12 -0600146 $sql .= ' SERIAL';
147 }
148 else
149 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500150 $sql .= ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600151 }
152
153 // Modified to prevent constraints with integer data types
154 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
155 {
156 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if (array_key_exists('DEFAULT', $attributes))
160 {
161 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
165 {
166 $sql .= ' NULL';
167 }
168 else
169 {
Barry Mienydd671972010-10-04 16:33:58 +0200170 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Greg Aker678256c2010-12-21 12:05:12 -0600173 // Added new attribute to create unqite fields. Also works with MySQL
174 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Greg Aker678256c2010-12-21 12:05:12 -0600176 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 // don't add a comma on the end of the last field
181 if (++$current_field_count < count($fields))
182 {
183 $sql .= ',';
184 }
185 }
186
187 if (count($primary_keys) > 0)
188 {
Greg Aker678256c2010-12-21 12:05:12 -0600189 // Something seems to break when passing an array to _protect_identifiers()
190 foreach ($primary_keys as $index => $key)
191 {
192 $primary_keys[$index] = $this->db->_protect_identifiers($key);
193 }
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Greg Aker678256c2010-12-21 12:05:12 -0600198 $sql .= "\n);";
199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 if (is_array($keys) && count($keys) > 0)
201 {
202 foreach ($keys as $key)
203 {
204 if (is_array($key))
205 {
Barry Mienydd671972010-10-04 16:33:58 +0200206 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208 else
209 {
210 $key = array($this->db->_protect_identifiers($key));
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Greg Aker678256c2010-12-21 12:05:12 -0600213 foreach ($key as $field)
214 {
215 $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
216 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218 }
219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 return $sql;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Drop Table
227 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500228 * @access private
229 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 */
231 function _drop_table($table)
232 {
Greg Aker678256c2010-12-21 12:05:12 -0600233 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Alter table query
240 *
241 * Generates a platform-specific query so that a table can be altered
242 * Called by add_column(), drop_column(), and column_alter(),
243 *
244 * @access private
245 * @param string the ALTER type (ADD, DROP, CHANGE)
246 * @param string the column name
247 * @param string the table name
248 * @param string the column definition
249 * @param string the default value
250 * @param boolean should 'NOT NULL' be added
251 * @param string the field after which we should add the new field
252 * @return object
253 */
254 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
255 {
256 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
257
258 // DROP has everything it needs now.
259 if ($alter_type == 'DROP')
260 {
261 return $sql;
262 }
263
264 $sql .= " $column_definition";
265
266 if ($default_value != '')
267 {
268 $sql .= " DEFAULT \"$default_value\"";
269 }
270
271 if ($null === NULL)
272 {
273 $sql .= ' NULL';
274 }
275 else
276 {
277 $sql .= ' NOT NULL';
278 }
279
280 if ($after_field != '')
281 {
282 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Rename a table
293 *
294 * Generates a platform-specific query so that a table can be renamed
295 *
296 * @access private
297 * @param string the old table name
298 * @param string the new table name
299 * @return string
300 */
301 function _rename_table($table_name, $new_table_name)
302 {
303 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
304 return $sql;
305 }
306
307
308}
309
310/* End of file postgre_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000311/* Location: ./system/database/drivers/postgre/postgre_forge.php */