blob: 2aa6ee82a86e2b78cece5ad620b5fdf57ecd98f3 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
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)
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000042 * @param string the database name
43 * @return bool
44 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040045 protected function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000046 {
47 return "CREATE DATABASE ".$name;
48 }
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Drop database
54 *
Derek Allard2067d1a2008-11-13 22:59:24 +000055 * @param string the database name
56 * @return bool
57 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040058 protected function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
60 return "DROP DATABASE ".$name;
61 }
62
63 // --------------------------------------------------------------------
Greg Aker0cbcc1a2011-12-27 17:11:38 -060064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
Greg Aker0cbcc1a2011-12-27 17:11:38 -060066 * Process Fields
Greg Aker6924e402011-12-27 17:19:50 -060067 *
68 * @param mixed the fields
69 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000070 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040071 protected function _process_fields($fields, $primary_keys=array())
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060073 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000074 $current_field_count = 0;
75
76 foreach ($fields as $field=>$attributes)
77 {
78 // Numeric field names aren't allowed in databases, so if the key is
79 // numeric, we know it was assigned by PHP and the developer manually
80 // entered the field information, so we'll simply add it to the list
81 if (is_numeric($field))
82 {
83 $sql .= "\n\t$attributes";
84 }
85 else
86 {
87 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020088
Andrey Andreev032e7ea2012-03-06 19:48:35 +020089 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +020090
Greg Aker678256c2010-12-21 12:05:12 -060091 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020092
Greg Aker678256c2010-12-21 12:05:12 -060093 // Convert datatypes to be PostgreSQL-compatible
94 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
Greg Aker678256c2010-12-21 12:05:12 -060096 case 'TINYINT':
97 $attributes['TYPE'] = 'SMALLINT';
98 break;
99 case 'SMALLINT':
100 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
101 break;
102 case 'MEDIUMINT':
103 $attributes['TYPE'] = 'INTEGER';
104 break;
105 case 'INT':
106 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
107 break;
108 case 'BIGINT':
109 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
110 break;
111 case 'DOUBLE':
112 $attributes['TYPE'] = 'DOUBLE PRECISION';
113 break;
114 case 'DATETIME':
115 $attributes['TYPE'] = 'TIMESTAMP';
116 break;
117 case 'LONGTEXT':
118 $attributes['TYPE'] = 'TEXT';
119 break;
120 case 'BLOB':
121 $attributes['TYPE'] = 'BYTEA';
122 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Greg Aker678256c2010-12-21 12:05:12 -0600125 // If this is an auto-incrementing primary key, use the serial data type instead
Derek Jones37f4b9c2011-07-01 17:56:50 -0500126 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
Greg Aker678256c2010-12-21 12:05:12 -0600127 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Greg Aker678256c2010-12-21 12:05:12 -0600129 $sql .= ' SERIAL';
130 }
131 else
132 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500133 $sql .= ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600134 }
135
136 // Modified to prevent constraints with integer data types
137 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
138 {
139 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 if (array_key_exists('DEFAULT', $attributes))
143 {
144 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
148 {
149 $sql .= ' NULL';
150 }
151 else
152 {
Barry Mienydd671972010-10-04 16:33:58 +0200153 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Greg Aker678256c2010-12-21 12:05:12 -0600156 // Added new attribute to create unqite fields. Also works with MySQL
157 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Greg Aker678256c2010-12-21 12:05:12 -0600159 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // don't add a comma on the end of the last field
164 if (++$current_field_count < count($fields))
165 {
166 $sql .= ',';
167 }
168 }
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600169
170 return $sql;
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Create Table
177 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600178 * @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 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400185 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600186 {
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200203 // Something seems to break when passing an array to protect_identifiers()
Greg Aker678256c2010-12-21 12:05:12 -0600204 foreach ($primary_keys as $index => $key)
205 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200206 $primary_keys[$index] = $this->db->protect_identifiers($key);
Greg Aker678256c2010-12-21 12:05:12 -0600207 }
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200220 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222 else
223 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200224 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
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
Timothy Warren7a3f8972012-03-19 18:18:27 -0400241 *
242 * @param string the table name
243 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400245 protected function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @param string the ALTER type (ADD, DROP, CHANGE)
259 * @param string the column name
260 * @param string the table name
261 * @param string the column definition
262 * @param string the default value
263 * @param boolean should 'NOT NULL' be added
264 * @param string the field after which we should add the new field
265 * @return object
266 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400267 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600268 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200269 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600271 // DROP has everything it needs now.
272 if ($alter_type == 'DROP')
273 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200274 return $sql.$this->db->protect_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600275 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600277 $sql .= $this->_process_fields($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600279 if ($after_field != '')
280 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200281 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600284 return $sql;
285 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000286
287 // --------------------------------------------------------------------
288
289 /**
290 * Rename a table
291 *
292 * Generates a platform-specific query so that a table can be renamed
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string the old table name
295 * @param string the new table name
296 * @return string
297 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400298 protected function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200300 return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000302}
303
304/* End of file postgre_forge.php */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400305/* Location: ./system/database/drivers/postgre/postgre_forge.php */