blob: 1164d9bb334b7cb36966160d214424a0dee91067 [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 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030042 * @param mixed $fields
43 * @param array $primary_keys = array()
Greg Aker6924e402011-12-27 17:19:50 -060044 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000045 */
Andrey Andreev2b3edbd2012-01-27 21:02:53 +020046 protected function _process_fields($fields, $primary_keys = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060048 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000049 $current_field_count = 0;
50
Andrey Andreev214583f2012-01-26 16:39:09 +020051 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000052 {
53 // Numeric field names aren't allowed in databases, so if the key is
54 // numeric, we know it was assigned by PHP and the developer manually
55 // entered the field information, so we'll simply add it to the list
56 if (is_numeric($field))
57 {
Andrey Andreev214583f2012-01-26 16:39:09 +020058 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60 else
61 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030062 $sql .= "\n\t".$this->db->escape_identifiers($field);
Andrey Andreev214583f2012-01-26 16:39:09 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev214583f2012-01-26 16:39:09 +020065 $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $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;
Andrey Andreev214583f2012-01-26 16:39:09 +020097 default:
98 break;
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Greg Aker678256c2010-12-21 12:05:12 -0600101 // If this is an auto-incrementing primary key, use the serial data type instead
Andrey Andreev214583f2012-01-26 16:39:09 +0200102 $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
103 ? ' SERIAL' : ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600104
105 // Modified to prevent constraints with integer data types
Andrey Andreev214583f2012-01-26 16:39:09 +0200106 if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE)
Greg Aker678256c2010-12-21 12:05:12 -0600107 {
108 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300111 if (isset($attributes['DEFAULT']))
112 {
113 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
114 }
115
116 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
117 ? ' NULL' : ' NOT NULL';
118
119 // Added new attribute to create unique fields. Also works with MySQL
120 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
121 {
122 $sql .= ' UNIQUE';
123 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // don't add a comma on the end of the last field
127 if (++$current_field_count < count($fields))
128 {
129 $sql .= ',';
130 }
131 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200132
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600133 return $sql;
134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Create Table
140 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600141 * @param string the table name
142 * @param array the fields
143 * @param mixed primary key(s)
144 * @param mixed key(s)
Andrey Andreev214583f2012-01-26 16:39:09 +0200145 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600146 * @return bool
147 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300148 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600149 {
150 $sql = 'CREATE TABLE ';
151
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300152 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
153 if ($if_not_exists === TRUE && $this->db->table_exists($table))
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600154 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300155 return TRUE;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600156 }
157
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300158 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159
160 if (count($primary_keys) > 0)
161 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300162 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Greg Aker678256c2010-12-21 12:05:12 -0600165 $sql .= "\n);";
166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 if (is_array($keys) && count($keys) > 0)
168 {
169 foreach ($keys as $key)
170 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300171 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300172 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300173 : array($this->db->escape_identifiers($key));
Barry Mienydd671972010-10-04 16:33:58 +0200174
Greg Aker678256c2010-12-21 12:05:12 -0600175 foreach ($key as $field)
176 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300177 $sql .= "\nCREATE INDEX ".$this->db->escape_identifiers($table.'_'.str_replace(array('"', "'"), '', $field).'_index')
178 .' ON '.$this->db->escape_identifiers($table).' ('.$this->db->escape_identifiers($field).');';
Greg Aker678256c2010-12-21 12:05:12 -0600179 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181 }
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return $sql;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * Alter table query
190 *
191 * Generates a platform-specific query so that a table can be altered
192 * Called by add_column(), drop_column(), and column_alter(),
193 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300194 * @param string $alter_type the ALTER type (ADD, DROP, CHANGE)
195 * @param string $table the table name
196 * @param string $fields the column definition
197 * @param string $after_field = ''
Andrey Andreev214583f2012-01-26 16:39:09 +0200198 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300200 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600201 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300202 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600204 // DROP has everything it needs now.
Andrey Andreev214583f2012-01-26 16:39:09 +0200205 if ($alter_type === 'DROP')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600206 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300207 return $sql.$this->db->escape_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600208 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
Andrey Andreev214583f2012-01-26 16:39:09 +0200210 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300211 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600212 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214}
215
216/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200217/* Location: ./system/database/drivers/postgre/postgre_forge.php */