blob: aac82c1e5a0283f2ad03af737795981b28337a02 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Postgre Forge Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_postgre_forge extends CI_DB_forge {
37
Andrey Andreevd947eba2012-04-09 14:58:28 +030038 protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE';
Andrey Andreev214583f2012-01-26 16:39:09 +020039
Derek Allard2067d1a2008-11-13 22:59:24 +000040 /**
Greg Aker0cbcc1a2011-12-27 17:11:38 -060041 * Process Fields
Greg Aker6924e402011-12-27 17:19:50 -060042 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030043 * @param mixed $fields
44 * @param array $primary_keys = array()
Greg Aker6924e402011-12-27 17:19:50 -060045 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000046 */
Andrey Andreev2b3edbd2012-01-27 21:02:53 +020047 protected function _process_fields($fields, $primary_keys = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000048 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060049 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000050 $current_field_count = 0;
51
Andrey Andreev214583f2012-01-26 16:39:09 +020052 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
54 // Numeric field names aren't allowed in databases, so if the key is
55 // numeric, we know it was assigned by PHP and the developer manually
56 // entered the field information, so we'll simply add it to the list
57 if (is_numeric($field))
58 {
Andrey Andreev214583f2012-01-26 16:39:09 +020059 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
61 else
62 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030063 $sql .= "\n\t".$this->db->escape_identifiers($field);
Andrey Andreev214583f2012-01-26 16:39:09 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev214583f2012-01-26 16:39:09 +020066 $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020067
Greg Aker678256c2010-12-21 12:05:12 -060068 // Convert datatypes to be PostgreSQL-compatible
69 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Greg Aker678256c2010-12-21 12:05:12 -060071 case 'TINYINT':
72 $attributes['TYPE'] = 'SMALLINT';
73 break;
74 case 'SMALLINT':
75 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
76 break;
77 case 'MEDIUMINT':
78 $attributes['TYPE'] = 'INTEGER';
79 break;
80 case 'INT':
81 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
82 break;
83 case 'BIGINT':
84 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
85 break;
86 case 'DOUBLE':
87 $attributes['TYPE'] = 'DOUBLE PRECISION';
88 break;
89 case 'DATETIME':
90 $attributes['TYPE'] = 'TIMESTAMP';
91 break;
92 case 'LONGTEXT':
93 $attributes['TYPE'] = 'TEXT';
94 break;
95 case 'BLOB':
96 $attributes['TYPE'] = 'BYTEA';
97 break;
Andrey Andreev214583f2012-01-26 16:39:09 +020098 default:
99 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Greg Aker678256c2010-12-21 12:05:12 -0600102 // If this is an auto-incrementing primary key, use the serial data type instead
Andrey Andreev214583f2012-01-26 16:39:09 +0200103 $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
104 ? ' SERIAL' : ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600105
106 // Modified to prevent constraints with integer data types
Andrey Andreev214583f2012-01-26 16:39:09 +0200107 if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE)
Greg Aker678256c2010-12-21 12:05:12 -0600108 {
109 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300112 if (isset($attributes['DEFAULT']))
113 {
114 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
115 }
116
117 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
118 ? ' NULL' : ' NOT NULL';
119
120 // Added new attribute to create unique fields. Also works with MySQL
121 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
122 {
123 $sql .= ' UNIQUE';
124 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // don't add a comma on the end of the last field
128 if (++$current_field_count < count($fields))
129 {
130 $sql .= ',';
131 }
132 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200133
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600134 return $sql;
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Create Table
141 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600142 * @param string the table name
143 * @param array the fields
144 * @param mixed primary key(s)
145 * @param mixed key(s)
Andrey Andreev214583f2012-01-26 16:39:09 +0200146 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600147 * @return bool
148 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300149 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600150 {
151 $sql = 'CREATE TABLE ';
152
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300153 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
154 if ($if_not_exists === TRUE && $this->db->table_exists($table))
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600155 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300156 return TRUE;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600157 }
158
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300159 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
161 if (count($primary_keys) > 0)
162 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300163 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Greg Aker678256c2010-12-21 12:05:12 -0600166 $sql .= "\n);";
167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 if (is_array($keys) && count($keys) > 0)
169 {
170 foreach ($keys as $key)
171 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300172 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300173 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300174 : array($this->db->escape_identifiers($key));
Barry Mienydd671972010-10-04 16:33:58 +0200175
Greg Aker678256c2010-12-21 12:05:12 -0600176 foreach ($key as $field)
177 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300178 $sql .= "\nCREATE INDEX ".$this->db->escape_identifiers($table.'_'.str_replace(array('"', "'"), '', $field).'_index')
179 .' ON '.$this->db->escape_identifiers($table).' ('.$this->db->escape_identifiers($field).');';
Greg Aker678256c2010-12-21 12:05:12 -0600180 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182 }
183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 return $sql;
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * Alter table query
191 *
192 * Generates a platform-specific query so that a table can be altered
193 * Called by add_column(), drop_column(), and column_alter(),
194 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300195 * @param string $alter_type the ALTER type (ADD, DROP, CHANGE)
196 * @param string $table the table name
197 * @param string $fields the column definition
198 * @param string $after_field = ''
Andrey Andreev214583f2012-01-26 16:39:09 +0200199 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300201 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600202 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300203 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600205 // DROP has everything it needs now.
Andrey Andreev214583f2012-01-26 16:39:09 +0200206 if ($alter_type === 'DROP')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600207 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300208 return $sql.$this->db->escape_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Andrey Andreev214583f2012-01-26 16:39:09 +0200211 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300212 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600213 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215}
216
217/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200218/* Location: ./system/database/drivers/postgre/postgre_forge.php */