blob: a72449820b228ca0e51d8f41e52e59cc7dfcb53e [file] [log] [blame]
Andrey Andreevbde70bd2012-03-20 14:15:12 +02001<?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
Andrey Andreevbde70bd2012-03-20 14:15:12 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevbde70bd2012-03-20 14:15:12 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Postgre Forge Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_postgre_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @param string the database name
41 * @return bool
42 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +020043 public function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
45 return "CREATE DATABASE ".$name;
46 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Drop database
52 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @param string the database name
54 * @return bool
55 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +020056 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
58 return "DROP DATABASE ".$name;
59 }
60
61 // --------------------------------------------------------------------
Andrey Andreevbde70bd2012-03-20 14:15:12 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 /**
Greg Aker0cbcc1a2011-12-27 17:11:38 -060064 * Process Fields
Greg Aker6924e402011-12-27 17:19:50 -060065 *
66 * @param mixed the fields
67 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000068 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +020069 protected function _process_fields($fields, $primary_keys=array())
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060071 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000072 $current_field_count = 0;
73
Andrey Andreevbde70bd2012-03-20 14:15:12 +020074 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 // Numeric field names aren't allowed in databases, so if the key is
77 // numeric, we know it was assigned by PHP and the developer manually
78 // entered the field information, so we'll simply add it to the list
79 if (is_numeric($field))
80 {
81 $sql .= "\n\t$attributes";
82 }
83 else
84 {
85 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020086
Andrey Andreev032e7ea2012-03-06 19:48:35 +020087 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +020088
Greg Aker678256c2010-12-21 12:05:12 -060089 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020090
Greg Aker678256c2010-12-21 12:05:12 -060091 // Convert datatypes to be PostgreSQL-compatible
92 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Greg Aker678256c2010-12-21 12:05:12 -060094 case 'TINYINT':
95 $attributes['TYPE'] = 'SMALLINT';
96 break;
97 case 'SMALLINT':
98 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
99 break;
100 case 'MEDIUMINT':
101 $attributes['TYPE'] = 'INTEGER';
102 break;
103 case 'INT':
104 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
105 break;
106 case 'BIGINT':
107 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
108 break;
109 case 'DOUBLE':
110 $attributes['TYPE'] = 'DOUBLE PRECISION';
111 break;
112 case 'DATETIME':
113 $attributes['TYPE'] = 'TIMESTAMP';
114 break;
115 case 'LONGTEXT':
116 $attributes['TYPE'] = 'TEXT';
117 break;
118 case 'BLOB':
119 $attributes['TYPE'] = 'BYTEA';
120 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Greg Aker678256c2010-12-21 12:05:12 -0600123 // If this is an auto-incrementing primary key, use the serial data type instead
Derek Jones37f4b9c2011-07-01 17:56:50 -0500124 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
Greg Aker678256c2010-12-21 12:05:12 -0600125 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Greg Aker678256c2010-12-21 12:05:12 -0600127 $sql .= ' SERIAL';
128 }
129 else
130 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500131 $sql .= ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600132 }
133
134 // Modified to prevent constraints with integer data types
135 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
136 {
137 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 if (array_key_exists('DEFAULT', $attributes))
141 {
142 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
146 {
147 $sql .= ' NULL';
148 }
149 else
150 {
Barry Mienydd671972010-10-04 16:33:58 +0200151 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Greg Aker678256c2010-12-21 12:05:12 -0600154 // Added new attribute to create unqite fields. Also works with MySQL
155 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Greg Aker678256c2010-12-21 12:05:12 -0600157 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // don't add a comma on the end of the last field
162 if (++$current_field_count < count($fields))
163 {
164 $sql .= ',';
165 }
166 }
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200167
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600168 return $sql;
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Create Table
175 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600176 * @param string the table name
177 * @param array the fields
178 * @param mixed primary key(s)
179 * @param mixed key(s)
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200180 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600181 * @return bool
182 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200183 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600184 {
185 $sql = 'CREATE TABLE ';
186
187 if ($if_not_exists === TRUE)
188 {
189 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
190 if ($this->db->table_exists($table))
191 {
192 return TRUE;
193 }
194 }
195
196 $sql .= $this->db->_escape_identifiers($table)." (";
197 $sql .= $this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
199 if (count($primary_keys) > 0)
200 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200201 // Something seems to break when passing an array to protect_identifiers()
Greg Aker678256c2010-12-21 12:05:12 -0600202 foreach ($primary_keys as $index => $key)
203 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200204 $primary_keys[$index] = $this->db->protect_identifiers($key);
Greg Aker678256c2010-12-21 12:05:12 -0600205 }
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Greg Aker678256c2010-12-21 12:05:12 -0600210 $sql .= "\n);";
211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 if (is_array($keys) && count($keys) > 0)
213 {
214 foreach ($keys as $key)
215 {
216 if (is_array($key))
217 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200218 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220 else
221 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200222 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Greg Aker678256c2010-12-21 12:05:12 -0600225 foreach ($key as $field)
226 {
227 $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
228 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230 }
231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 return $sql;
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Drop Table
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200239 *
240 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200242 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param string the ALTER type (ADD, DROP, CHANGE)
256 * @param string the column name
257 * @param string the table name
258 * @param string the column definition
259 * @param string the default value
260 * @param boolean should 'NOT NULL' be added
261 * @param string the field after which we should add the new field
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200262 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200264 public function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600265 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200266 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000267
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600268 // DROP has everything it needs now.
269 if ($alter_type == 'DROP')
270 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200271 return $sql.$this->db->protect_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600272 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000273
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600274 $sql .= $this->_process_fields($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000275
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600276 if ($after_field != '')
277 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200278 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600279 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000280
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600281 return $sql;
282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
284 // --------------------------------------------------------------------
285
286 /**
287 * Rename a table
288 *
289 * Generates a platform-specific query so that a table can be renamed
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @param string the old table name
292 * @param string the new table name
293 * @return string
294 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200295 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200297 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 +0000298 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000299}
300
301/* End of file postgre_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400302/* Location: ./system/database/drivers/postgre/postgre_forge.php */