blob: d54b99b2355e8bbbcaaf153e08532ad6328cf0b3 [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 // --------------------------------------------------------------------
Greg Aker0cbcc1a2011-12-27 17:11:38 -060066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 /**
Greg Aker0cbcc1a2011-12-27 17:11:38 -060068 * Process Fields
Greg Aker6924e402011-12-27 17:19:50 -060069 *
70 * @param mixed the fields
71 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000072 */
Greg Aker0cbcc1a2011-12-27 17:11:38 -060073 function _process_fields($fields, $primary_keys=array())
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060075 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000076 $current_field_count = 0;
77
78 foreach ($fields as $field=>$attributes)
79 {
80 // Numeric field names aren't allowed in databases, so if the key is
81 // numeric, we know it was assigned by PHP and the developer manually
82 // entered the field information, so we'll simply add it to the list
83 if (is_numeric($field))
84 {
85 $sql .= "\n\t$attributes";
86 }
87 else
88 {
89 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +020092
Greg Aker678256c2010-12-21 12:05:12 -060093 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020094
Greg Aker678256c2010-12-21 12:05:12 -060095 // Convert datatypes to be PostgreSQL-compatible
96 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Greg Aker678256c2010-12-21 12:05:12 -060098 case 'TINYINT':
99 $attributes['TYPE'] = 'SMALLINT';
100 break;
101 case 'SMALLINT':
102 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
103 break;
104 case 'MEDIUMINT':
105 $attributes['TYPE'] = 'INTEGER';
106 break;
107 case 'INT':
108 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
109 break;
110 case 'BIGINT':
111 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
112 break;
113 case 'DOUBLE':
114 $attributes['TYPE'] = 'DOUBLE PRECISION';
115 break;
116 case 'DATETIME':
117 $attributes['TYPE'] = 'TIMESTAMP';
118 break;
119 case 'LONGTEXT':
120 $attributes['TYPE'] = 'TEXT';
121 break;
122 case 'BLOB':
123 $attributes['TYPE'] = 'BYTEA';
124 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Greg Aker678256c2010-12-21 12:05:12 -0600127 // If this is an auto-incrementing primary key, use the serial data type instead
Derek Jones37f4b9c2011-07-01 17:56:50 -0500128 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
Greg Aker678256c2010-12-21 12:05:12 -0600129 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Greg Aker678256c2010-12-21 12:05:12 -0600131 $sql .= ' SERIAL';
132 }
133 else
134 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500135 $sql .= ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600136 }
137
138 // Modified to prevent constraints with integer data types
139 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
140 {
141 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 if (array_key_exists('DEFAULT', $attributes))
145 {
146 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
150 {
151 $sql .= ' NULL';
152 }
153 else
154 {
Barry Mienydd671972010-10-04 16:33:58 +0200155 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Greg Aker678256c2010-12-21 12:05:12 -0600158 // Added new attribute to create unqite fields. Also works with MySQL
159 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Greg Aker678256c2010-12-21 12:05:12 -0600161 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // don't add a comma on the end of the last field
166 if (++$current_field_count < count($fields))
167 {
168 $sql .= ',';
169 }
170 }
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600171
172 return $sql;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Create Table
179 *
180 * @access private
181 * @param string the table name
182 * @param array the fields
183 * @param mixed primary key(s)
184 * @param mixed key(s)
185 * @param boolean should 'IF NOT EXISTS' be added to the SQL
186 * @return bool
187 */
188 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
189 {
190 $sql = 'CREATE TABLE ';
191
192 if ($if_not_exists === TRUE)
193 {
194 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
195 if ($this->db->table_exists($table))
196 {
197 return TRUE;
198 }
199 }
200
201 $sql .= $this->db->_escape_identifiers($table)." (";
202 $sql .= $this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
204 if (count($primary_keys) > 0)
205 {
Greg Aker678256c2010-12-21 12:05:12 -0600206 // Something seems to break when passing an array to _protect_identifiers()
207 foreach ($primary_keys as $index => $key)
208 {
209 $primary_keys[$index] = $this->db->_protect_identifiers($key);
210 }
211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Greg Aker678256c2010-12-21 12:05:12 -0600215 $sql .= "\n);";
216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 if (is_array($keys) && count($keys) > 0)
218 {
219 foreach ($keys as $key)
220 {
221 if (is_array($key))
222 {
Barry Mienydd671972010-10-04 16:33:58 +0200223 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225 else
226 {
227 $key = array($this->db->_protect_identifiers($key));
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Greg Aker678256c2010-12-21 12:05:12 -0600230 foreach ($key as $field)
231 {
232 $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
233 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235 }
236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 return $sql;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Drop Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 */
245 function _drop_table($table)
246 {
Greg Aker678256c2010-12-21 12:05:12 -0600247 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * Alter table query
254 *
255 * Generates a platform-specific query so that a table can be altered
256 * Called by add_column(), drop_column(), and column_alter(),
257 *
258 * @access private
259 * @param string the ALTER type (ADD, DROP, CHANGE)
260 * @param string the column name
261 * @param string the table name
262 * @param string the column definition
263 * @param string the default value
264 * @param boolean should 'NOT NULL' be added
265 * @param string the field after which we should add the new field
266 * @return object
267 */
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600268 function _alter_table($alter_type, $table, $fields, $after_field = '')
269 {
270 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600272 // DROP has everything it needs now.
273 if ($alter_type == 'DROP')
274 {
275 return $sql.$this->db->_protect_identifiers($fields);
276 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000277
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600278 $sql .= $this->_process_fields($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600280 if ($after_field != '')
281 {
282 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
283 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600285 return $sql;
286 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000287
288 // --------------------------------------------------------------------
289
290 /**
291 * Rename a table
292 *
293 * Generates a platform-specific query so that a table can be renamed
294 *
295 * @access private
296 * @param string the old table name
297 * @param string the new table name
298 * @return string
299 */
300 function _rename_table($table_name, $new_table_name)
301 {
302 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
303 return $sql;
304 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000305}
306
307/* End of file postgre_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000308/* Location: ./system/database/drivers/postgre/postgre_forge.php */