blob: c434e95109a7a7658f20304f3f286d9153f773f3 [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 Andreev5ef194d2012-06-08 01:12:54 +030061 $sql .= "\n\t".$this->db->escape_identifiers($field);
Andrey Andreev214583f2012-01-26 16:39:09 +020062
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 Andreev5ef194d2012-06-08 01:12:54 +0300110 if (isset($attributes['DEFAULT']))
111 {
112 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
113 }
114
115 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
116 ? ' NULL' : ' NOT NULL';
117
118 // Added new attribute to create unique fields. Also works with MySQL
119 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
120 {
121 $sql .= ' UNIQUE';
122 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // don't add a comma on the end of the last field
126 if (++$current_field_count < count($fields))
127 {
128 $sql .= ',';
129 }
130 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200131
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600132 return $sql;
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Create Table
139 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600140 * @param string the table name
141 * @param array the fields
142 * @param mixed primary key(s)
143 * @param mixed key(s)
Andrey Andreev214583f2012-01-26 16:39:09 +0200144 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600145 * @return bool
146 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300147 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600148 {
149 $sql = 'CREATE TABLE ';
150
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300151 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
152 if ($if_not_exists === TRUE && $this->db->table_exists($table))
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600153 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300154 return TRUE;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600155 }
156
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300157 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000158
159 if (count($primary_keys) > 0)
160 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300161 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($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 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300170 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300171 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300172 : array($this->db->escape_identifiers($key));
Barry Mienydd671972010-10-04 16:33:58 +0200173
Greg Aker678256c2010-12-21 12:05:12 -0600174 foreach ($key as $field)
175 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300176 $sql .= "\nCREATE INDEX ".$this->db->escape_identifiers($table.'_'.str_replace(array('"', "'"), '', $field).'_index')
177 .' ON '.$this->db->escape_identifiers($table).' ('.$this->db->escape_identifiers($field).');';
Greg Aker678256c2010-12-21 12:05:12 -0600178 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180 }
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 return $sql;
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * Alter table query
189 *
190 * Generates a platform-specific query so that a table can be altered
191 * Called by add_column(), drop_column(), and column_alter(),
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string the ALTER type (ADD, DROP, CHANGE)
194 * @param string the column name
195 * @param string the table name
196 * @param string the column definition
197 * @param string the default value
Andrey Andreev214583f2012-01-26 16:39:09 +0200198 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 * @param string the field after which we should add the new field
Andrey Andreev214583f2012-01-26 16:39:09 +0200200 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300202 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600203 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300204 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600206 // DROP has everything it needs now.
Andrey Andreev214583f2012-01-26 16:39:09 +0200207 if ($alter_type === 'DROP')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600208 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300209 return $sql.$this->db->escape_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600210 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Andrey Andreev214583f2012-01-26 16:39:09 +0200212 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300213 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600214 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216}
217
218/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200219/* Location: ./system/database/drivers/postgre/postgre_forge.php */