blob: 5af834b8db2f303698b5673ad370567bc52a7c09 [file] [log] [blame]
Andrey Andreev214583f2012-01-26 16:39:09 +02001<?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
Andrey Andreev214583f2012-01-26 16:39:09 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev214583f2012-01-26 16:39:09 +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
Andrey Andreev214583f2012-01-26 16:39:09 +020041 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000042 */
Andrey Andreev214583f2012-01-26 16:39:09 +020043 public function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
Andrey Andreev214583f2012-01-26 16:39:09 +020045 return 'CREATE DATABASE '.$name;
Derek Allard2067d1a2008-11-13 22:59:24 +000046 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Drop database
52 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @param string the database name
Andrey Andreev214583f2012-01-26 16:39:09 +020054 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000055 */
Andrey Andreev214583f2012-01-26 16:39:09 +020056 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
Andrey Andreev214583f2012-01-26 16:39:09 +020058 return 'DROP DATABASE '.$name;
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60
61 // --------------------------------------------------------------------
Andrey Andreev214583f2012-01-26 16:39:09 +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 Andreev214583f2012-01-26 16:39:09 +020069 private 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 Andreev214583f2012-01-26 16:39:09 +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 {
Andrey Andreev214583f2012-01-26 16:39:09 +020081 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000082 }
83 else
84 {
Andrey Andreev214583f2012-01-26 16:39:09 +020085 $sql .= "\n\t".$this->db->protect_identifiers($field);
86
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev214583f2012-01-26 16:39:09 +020088 $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020089
Greg Aker678256c2010-12-21 12:05:12 -060090 // Convert datatypes to be PostgreSQL-compatible
91 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Greg Aker678256c2010-12-21 12:05:12 -060093 case 'TINYINT':
94 $attributes['TYPE'] = 'SMALLINT';
95 break;
96 case 'SMALLINT':
97 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
98 break;
99 case 'MEDIUMINT':
100 $attributes['TYPE'] = 'INTEGER';
101 break;
102 case 'INT':
103 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
104 break;
105 case 'BIGINT':
106 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
107 break;
108 case 'DOUBLE':
109 $attributes['TYPE'] = 'DOUBLE PRECISION';
110 break;
111 case 'DATETIME':
112 $attributes['TYPE'] = 'TIMESTAMP';
113 break;
114 case 'LONGTEXT':
115 $attributes['TYPE'] = 'TEXT';
116 break;
117 case 'BLOB':
118 $attributes['TYPE'] = 'BYTEA';
119 break;
Andrey Andreev214583f2012-01-26 16:39:09 +0200120 default:
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
Andrey Andreev214583f2012-01-26 16:39:09 +0200125 $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
126 ? ' SERIAL' : ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600127
128 // Modified to prevent constraints with integer data types
Andrey Andreev214583f2012-01-26 16:39:09 +0200129 if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE)
Greg Aker678256c2010-12-21 12:05:12 -0600130 {
131 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Andrey Andreev214583f2012-01-26 16:39:09 +0200134 $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
135 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
136 // Added new attribute to create unqite fields. Also works with MySQL
137 .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // don't add a comma on the end of the last field
141 if (++$current_field_count < count($fields))
142 {
143 $sql .= ',';
144 }
145 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200146
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600147 return $sql;
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Create Table
154 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600155 * @param string the table name
156 * @param array the fields
157 * @param mixed primary key(s)
158 * @param mixed key(s)
Andrey Andreev214583f2012-01-26 16:39:09 +0200159 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600160 * @return bool
161 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200162 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600163 {
164 $sql = 'CREATE TABLE ';
165
166 if ($if_not_exists === TRUE)
167 {
168 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
169 if ($this->db->table_exists($table))
170 {
171 return TRUE;
172 }
173 }
174
Andrey Andreev214583f2012-01-26 16:39:09 +0200175 $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176
177 if (count($primary_keys) > 0)
178 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200179 // Something seems to break when passing an array to protect_identifiers()
Greg Aker678256c2010-12-21 12:05:12 -0600180 foreach ($primary_keys as $index => $key)
181 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200182 $primary_keys[$index] = $this->db->protect_identifiers($key);
Greg Aker678256c2010-12-21 12:05:12 -0600183 }
184
Andrey Andreev214583f2012-01-26 16:39:09 +0200185 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Greg Aker678256c2010-12-21 12:05:12 -0600188 $sql .= "\n);";
189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 if (is_array($keys) && count($keys) > 0)
191 {
192 foreach ($keys as $key)
193 {
194 if (is_array($key))
195 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200196 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198 else
199 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200200 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Greg Aker678256c2010-12-21 12:05:12 -0600203 foreach ($key as $field)
204 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200205 $sql .= 'CREATE INDEX '.$table.'_'.str_replace(array('"', "'"), '', $field).'_index ON '.$table.' ('.$field.'); ';
Greg Aker678256c2010-12-21 12:05:12 -0600206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208 }
209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 return $sql;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Drop Table
Andrey Andreev214583f2012-01-26 16:39:09 +0200217 *
218 * @param string table name
219 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200221 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200223 return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table).' CASCADE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Alter table query
230 *
231 * Generates a platform-specific query so that a table can be altered
232 * Called by add_column(), drop_column(), and column_alter(),
233 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @param string the ALTER type (ADD, DROP, CHANGE)
235 * @param string the column name
236 * @param string the table name
237 * @param string the column definition
238 * @param string the default value
Andrey Andreev214583f2012-01-26 16:39:09 +0200239 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param string the field after which we should add the new field
Andrey Andreev214583f2012-01-26 16:39:09 +0200241 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200243 public function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600244 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200245 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600247 // DROP has everything it needs now.
Andrey Andreev214583f2012-01-26 16:39:09 +0200248 if ($alter_type === 'DROP')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600249 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200250 return $sql.$this->db->protect_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600251 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
Andrey Andreev214583f2012-01-26 16:39:09 +0200253 return $sql.$this->_process_fields($fields)
254 .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600255 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000256
257 // --------------------------------------------------------------------
258
259 /**
260 * Rename a table
261 *
262 * Generates a platform-specific query so that a table can be renamed
263 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @param string the old table name
265 * @param string the new table name
266 * @return string
267 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200268 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200270 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 +0000271 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273}
274
275/* End of file postgre_forge.php */
Andrey Andreev214583f2012-01-26 16:39:09 +0200276/* Location: ./system/database/drivers/postgre/postgre_forge.php */