blob: 94c97af50034af453664c8c538665a796148c56b [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 *
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 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
Andrey Andreevd947eba2012-04-09 14:58:28 +030037 protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE';
Andrey Andreev214583f2012-01-26 16:39:09 +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 Andreev2b3edbd2012-01-27 21:02:53 +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 Andreev214583f2012-01-26 16:39:09 +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 {
Andrey Andreev214583f2012-01-26 16:39:09 +020057 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000058 }
59 else
60 {
Andrey Andreev214583f2012-01-26 16:39:09 +020061 $sql .= "\n\t".$this->db->protect_identifiers($field);
62
Derek Allard2067d1a2008-11-13 22:59:24 +000063 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev214583f2012-01-26 16:39:09 +020064 $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020065
Greg Aker678256c2010-12-21 12:05:12 -060066 // Convert datatypes to be PostgreSQL-compatible
67 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
Greg Aker678256c2010-12-21 12:05:12 -060069 case 'TINYINT':
70 $attributes['TYPE'] = 'SMALLINT';
71 break;
72 case 'SMALLINT':
73 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
74 break;
75 case 'MEDIUMINT':
76 $attributes['TYPE'] = 'INTEGER';
77 break;
78 case 'INT':
79 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
80 break;
81 case 'BIGINT':
82 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
83 break;
84 case 'DOUBLE':
85 $attributes['TYPE'] = 'DOUBLE PRECISION';
86 break;
87 case 'DATETIME':
88 $attributes['TYPE'] = 'TIMESTAMP';
89 break;
90 case 'LONGTEXT':
91 $attributes['TYPE'] = 'TEXT';
92 break;
93 case 'BLOB':
94 $attributes['TYPE'] = 'BYTEA';
95 break;
Andrey Andreev214583f2012-01-26 16:39:09 +020096 default:
97 break;
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Greg Aker678256c2010-12-21 12:05:12 -0600100 // If this is an auto-incrementing primary key, use the serial data type instead
Andrey Andreev214583f2012-01-26 16:39:09 +0200101 $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
102 ? ' SERIAL' : ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600103
104 // Modified to prevent constraints with integer data types
Andrey Andreev214583f2012-01-26 16:39:09 +0200105 if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE)
Greg Aker678256c2010-12-21 12:05:12 -0600106 {
107 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Andrey Andreev214583f2012-01-26 16:39:09 +0200110 $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
111 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
112 // Added new attribute to create unqite fields. Also works with MySQL
113 .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 // don't add a comma on the end of the last field
117 if (++$current_field_count < count($fields))
118 {
119 $sql .= ',';
120 }
121 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200122
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600123 return $sql;
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Create Table
130 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600131 * @param string the table name
132 * @param array the fields
133 * @param mixed primary key(s)
134 * @param mixed key(s)
Andrey Andreev214583f2012-01-26 16:39:09 +0200135 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600136 * @return bool
137 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300138 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600139 {
140 $sql = 'CREATE TABLE ';
141
142 if ($if_not_exists === TRUE)
143 {
144 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
145 if ($this->db->table_exists($table))
146 {
147 return TRUE;
148 }
149 }
150
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300151 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152
153 if (count($primary_keys) > 0)
154 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200155 // Something seems to break when passing an array to protect_identifiers()
Greg Aker678256c2010-12-21 12:05:12 -0600156 foreach ($primary_keys as $index => $key)
157 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200158 $primary_keys[$index] = $this->db->protect_identifiers($key);
Greg Aker678256c2010-12-21 12:05:12 -0600159 }
160
Andrey Andreev214583f2012-01-26 16:39:09 +0200161 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Greg Aker678256c2010-12-21 12:05:12 -0600164 $sql .= "\n);";
165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 if (is_array($keys) && count($keys) > 0)
167 {
168 foreach ($keys as $key)
169 {
170 if (is_array($key))
171 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200172 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174 else
175 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200176 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Greg Aker678256c2010-12-21 12:05:12 -0600179 foreach ($key as $field)
180 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200181 $sql .= 'CREATE INDEX '.$table.'_'.str_replace(array('"', "'"), '', $field).'_index ON '.$table.' ('.$field.'); ';
Greg Aker678256c2010-12-21 12:05:12 -0600182 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184 }
185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 return $sql;
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * Alter table query
193 *
194 * Generates a platform-specific query so that a table can be altered
195 * Called by add_column(), drop_column(), and column_alter(),
196 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @param string the ALTER type (ADD, DROP, CHANGE)
198 * @param string the column name
199 * @param string the table name
200 * @param string the column definition
201 * @param string the default value
Andrey Andreev214583f2012-01-26 16:39:09 +0200202 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param string the field after which we should add the new field
Andrey Andreev214583f2012-01-26 16:39:09 +0200204 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300206 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600207 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200208 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600210 // DROP has everything it needs now.
Andrey Andreev214583f2012-01-26 16:39:09 +0200211 if ($alter_type === 'DROP')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600212 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200213 return $sql.$this->db->protect_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600214 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Andrey Andreev214583f2012-01-26 16:39:09 +0200216 return $sql.$this->_process_fields($fields)
217 .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600218 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220}
221
222/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200223/* Location: ./system/database/drivers/postgre/postgre_forge.php */