blob: 7368b9447026242a152accdaf842833488c6d8b9 [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
Derek Allard2067d1a2008-11-13 22:59:24 +000069 */
Greg Aker0cbcc1a2011-12-27 17:11:38 -060070 function _process_fields($fields, $primary_keys=array())
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060072 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000073 $current_field_count = 0;
74
75 foreach ($fields as $field=>$attributes)
76 {
77 // Numeric field names aren't allowed in databases, so if the key is
78 // numeric, we know it was assigned by PHP and the developer manually
79 // entered the field information, so we'll simply add it to the list
80 if (is_numeric($field))
81 {
82 $sql .= "\n\t$attributes";
83 }
84 else
85 {
86 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +020089
Greg Aker678256c2010-12-21 12:05:12 -060090 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020091
Greg Aker678256c2010-12-21 12:05:12 -060092 // Convert datatypes to be PostgreSQL-compatible
93 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Greg Aker678256c2010-12-21 12:05:12 -060095 case 'TINYINT':
96 $attributes['TYPE'] = 'SMALLINT';
97 break;
98 case 'SMALLINT':
99 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
100 break;
101 case 'MEDIUMINT':
102 $attributes['TYPE'] = 'INTEGER';
103 break;
104 case 'INT':
105 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
106 break;
107 case 'BIGINT':
108 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
109 break;
110 case 'DOUBLE':
111 $attributes['TYPE'] = 'DOUBLE PRECISION';
112 break;
113 case 'DATETIME':
114 $attributes['TYPE'] = 'TIMESTAMP';
115 break;
116 case 'LONGTEXT':
117 $attributes['TYPE'] = 'TEXT';
118 break;
119 case 'BLOB':
120 $attributes['TYPE'] = 'BYTEA';
121 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Greg Aker678256c2010-12-21 12:05:12 -0600124 // If this is an auto-incrementing primary key, use the serial data type instead
Derek Jones37f4b9c2011-07-01 17:56:50 -0500125 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
Greg Aker678256c2010-12-21 12:05:12 -0600126 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Greg Aker678256c2010-12-21 12:05:12 -0600128 $sql .= ' SERIAL';
129 }
130 else
131 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500132 $sql .= ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600133 }
134
135 // Modified to prevent constraints with integer data types
136 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
137 {
138 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 if (array_key_exists('DEFAULT', $attributes))
142 {
143 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
147 {
148 $sql .= ' NULL';
149 }
150 else
151 {
Barry Mienydd671972010-10-04 16:33:58 +0200152 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Greg Aker678256c2010-12-21 12:05:12 -0600155 // Added new attribute to create unqite fields. Also works with MySQL
156 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Greg Aker678256c2010-12-21 12:05:12 -0600158 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // don't add a comma on the end of the last field
163 if (++$current_field_count < count($fields))
164 {
165 $sql .= ',';
166 }
167 }
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600168
169 return $sql;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Create Table
176 *
177 * @access private
178 * @param string the table name
179 * @param array the fields
180 * @param mixed primary key(s)
181 * @param mixed key(s)
182 * @param boolean should 'IF NOT EXISTS' be added to the SQL
183 * @return bool
184 */
185 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
186 {
187 $sql = 'CREATE TABLE ';
188
189 if ($if_not_exists === TRUE)
190 {
191 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
192 if ($this->db->table_exists($table))
193 {
194 return TRUE;
195 }
196 }
197
198 $sql .= $this->db->_escape_identifiers($table)." (";
199 $sql .= $this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
201 if (count($primary_keys) > 0)
202 {
Greg Aker678256c2010-12-21 12:05:12 -0600203 // Something seems to break when passing an array to _protect_identifiers()
204 foreach ($primary_keys as $index => $key)
205 {
206 $primary_keys[$index] = $this->db->_protect_identifiers($key);
207 }
208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Greg Aker678256c2010-12-21 12:05:12 -0600212 $sql .= "\n);";
213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if (is_array($keys) && count($keys) > 0)
215 {
216 foreach ($keys as $key)
217 {
218 if (is_array($key))
219 {
Barry Mienydd671972010-10-04 16:33:58 +0200220 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222 else
223 {
224 $key = array($this->db->_protect_identifiers($key));
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Greg Aker678256c2010-12-21 12:05:12 -0600227 foreach ($key as $field)
228 {
229 $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
230 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232 }
233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 return $sql;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Drop Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 */
242 function _drop_table($table)
243 {
Greg Aker678256c2010-12-21 12:05:12 -0600244 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Alter table query
251 *
252 * Generates a platform-specific query so that a table can be altered
253 * Called by add_column(), drop_column(), and column_alter(),
254 *
255 * @access private
256 * @param string the ALTER type (ADD, DROP, CHANGE)
257 * @param string the column name
258 * @param string the table name
259 * @param string the column definition
260 * @param string the default value
261 * @param boolean should 'NOT NULL' be added
262 * @param string the field after which we should add the new field
263 * @return object
264 */
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600265 function _alter_table($alter_type, $table, $fields, $after_field = '')
266 {
267 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600269 // DROP has everything it needs now.
270 if ($alter_type == 'DROP')
271 {
272 return $sql.$this->db->_protect_identifiers($fields);
273 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600275 $sql .= $this->_process_fields($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600277 if ($after_field != '')
278 {
279 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
280 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000281
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600282 return $sql;
283 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
285 // --------------------------------------------------------------------
286
287 /**
288 * Rename a table
289 *
290 * Generates a platform-specific query so that a table can be renamed
291 *
292 * @access private
293 * @param string the old table name
294 * @param string the new table name
295 * @return string
296 */
297 function _rename_table($table_name, $new_table_name)
298 {
299 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
300 return $sql;
301 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000302}
303
304/* End of file postgre_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000305/* Location: ./system/database/drivers/postgre/postgre_forge.php */