blob: 8b214ebe67daa0f3f22d71cc796546c0b273eefa [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
Andrey Andreevd947eba2012-04-09 14:58:28 +030037 protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE';
Andrey Andreevbde70bd2012-03-20 14:15:12 +020038
Derek Allard2067d1a2008-11-13 22:59:24 +000039 /**
Greg Aker0cbcc1a2011-12-27 17:11:38 -060040 * Process Fields
Greg Aker6924e402011-12-27 17:19:50 -060041 *
42 * @param mixed the fields
43 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000044 */
Andrey Andreevbde70bd2012-03-20 14:15:12 +020045 protected function _process_fields($fields, $primary_keys=array())
Derek Allard2067d1a2008-11-13 22:59:24 +000046 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060047 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000048 $current_field_count = 0;
49
Andrey Andreevbde70bd2012-03-20 14:15:12 +020050 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000051 {
52 // Numeric field names aren't allowed in databases, so if the key is
53 // numeric, we know it was assigned by PHP and the developer manually
54 // entered the field information, so we'll simply add it to the list
55 if (is_numeric($field))
56 {
57 $sql .= "\n\t$attributes";
58 }
59 else
60 {
61 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020062
Andrey Andreev032e7ea2012-03-06 19:48:35 +020063 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +020064
Greg Aker678256c2010-12-21 12:05:12 -060065 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020066
Greg Aker678256c2010-12-21 12:05:12 -060067 // Convert datatypes to be PostgreSQL-compatible
68 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Greg Aker678256c2010-12-21 12:05:12 -060070 case 'TINYINT':
71 $attributes['TYPE'] = 'SMALLINT';
72 break;
73 case 'SMALLINT':
74 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
75 break;
76 case 'MEDIUMINT':
77 $attributes['TYPE'] = 'INTEGER';
78 break;
79 case 'INT':
80 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
81 break;
82 case 'BIGINT':
83 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
84 break;
85 case 'DOUBLE':
86 $attributes['TYPE'] = 'DOUBLE PRECISION';
87 break;
88 case 'DATETIME':
89 $attributes['TYPE'] = 'TIMESTAMP';
90 break;
91 case 'LONGTEXT':
92 $attributes['TYPE'] = 'TEXT';
93 break;
94 case 'BLOB':
95 $attributes['TYPE'] = 'BYTEA';
96 break;
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Greg Aker678256c2010-12-21 12:05:12 -060099 // If this is an auto-incrementing primary key, use the serial data type instead
Derek Jones37f4b9c2011-07-01 17:56:50 -0500100 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
Greg Aker678256c2010-12-21 12:05:12 -0600101 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Greg Aker678256c2010-12-21 12:05:12 -0600103 $sql .= ' SERIAL';
104 }
105 else
106 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500107 $sql .= ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600108 }
109
110 // Modified to prevent constraints with integer data types
111 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
112 {
113 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if (array_key_exists('DEFAULT', $attributes))
117 {
118 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
122 {
123 $sql .= ' NULL';
124 }
125 else
126 {
Barry Mienydd671972010-10-04 16:33:58 +0200127 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Greg Aker678256c2010-12-21 12:05:12 -0600130 // Added new attribute to create unqite fields. Also works with MySQL
131 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Greg Aker678256c2010-12-21 12:05:12 -0600133 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // don't add a comma on the end of the last field
138 if (++$current_field_count < count($fields))
139 {
140 $sql .= ',';
141 }
142 }
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200143
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600144 return $sql;
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
150 * Create Table
151 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600152 * @param string the table name
153 * @param array the fields
154 * @param mixed primary key(s)
155 * @param mixed key(s)
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200156 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600157 * @return bool
158 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300159 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600160 {
161 $sql = 'CREATE TABLE ';
162
163 if ($if_not_exists === TRUE)
164 {
165 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
166 if ($this->db->table_exists($table))
167 {
168 return TRUE;
169 }
170 }
171
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300172 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173
174 if (count($primary_keys) > 0)
175 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200176 // Something seems to break when passing an array to protect_identifiers()
Greg Aker678256c2010-12-21 12:05:12 -0600177 foreach ($primary_keys as $index => $key)
178 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200179 $primary_keys[$index] = $this->db->protect_identifiers($key);
Greg Aker678256c2010-12-21 12:05:12 -0600180 }
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Greg Aker678256c2010-12-21 12:05:12 -0600185 $sql .= "\n);";
186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 if (is_array($keys) && count($keys) > 0)
188 {
189 foreach ($keys as $key)
190 {
191 if (is_array($key))
192 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200193 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
195 else
196 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200197 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Greg Aker678256c2010-12-21 12:05:12 -0600200 foreach ($key as $field)
201 {
202 $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
203 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
205 }
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 return $sql;
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * Alter table query
214 *
215 * Generates a platform-specific query so that a table can be altered
216 * Called by add_column(), drop_column(), and column_alter(),
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @param string the ALTER type (ADD, DROP, CHANGE)
219 * @param string the column name
220 * @param string the table name
221 * @param string the column definition
222 * @param string the default value
223 * @param boolean should 'NOT NULL' be added
224 * @param string the field after which we should add the new field
Andrey Andreevbde70bd2012-03-20 14:15:12 +0200225 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300227 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600228 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200229 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000230
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600231 // DROP has everything it needs now.
232 if ($alter_type == 'DROP')
233 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200234 return $sql.$this->db->protect_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600235 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000236
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600237 $sql .= $this->_process_fields($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600239 if ($after_field != '')
240 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200241 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600242 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000243
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600244 return $sql;
245 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247}
248
249/* End of file postgre_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400250/* Location: ./system/database/drivers/postgre/postgre_forge.php */