blob: b08fa5177ec04b07b7db7707a353ce79facf34e7 [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 Andreevc98e93a2012-11-02 02:04:59 +020038 /**
39 * DROP TABLE statement
40 *
41 * @var string
42 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030043 protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE';
Andrey Andreev214583f2012-01-26 16:39:09 +020044
Andrey Andreevc98e93a2012-11-02 02:04:59 +020045 // --------------------------------------------------------------------
46
Derek Allard2067d1a2008-11-13 22:59:24 +000047 /**
Greg Aker0cbcc1a2011-12-27 17:11:38 -060048 * Process Fields
Greg Aker6924e402011-12-27 17:19:50 -060049 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030050 * @param mixed $fields
Andrey Andreevc98e93a2012-11-02 02:04:59 +020051 * @param array $primary_keys
Greg Aker6924e402011-12-27 17:19:50 -060052 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000053 */
Andrey Andreev2b3edbd2012-01-27 21:02:53 +020054 protected function _process_fields($fields, $primary_keys = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Greg Aker0cbcc1a2011-12-27 17:11:38 -060056 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000057 $current_field_count = 0;
58
Andrey Andreev214583f2012-01-26 16:39:09 +020059 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
61 // Numeric field names aren't allowed in databases, so if the key is
62 // numeric, we know it was assigned by PHP and the developer manually
63 // entered the field information, so we'll simply add it to the list
64 if (is_numeric($field))
65 {
Andrey Andreev214583f2012-01-26 16:39:09 +020066 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
68 else
69 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +030070 $sql .= "\n\t".$this->db->escape_identifiers($field);
Andrey Andreev214583f2012-01-26 16:39:09 +020071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 $attributes = array_change_key_case($attributes, CASE_UPPER);
Andrey Andreev214583f2012-01-26 16:39:09 +020073 $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020074
Greg Aker678256c2010-12-21 12:05:12 -060075 // Convert datatypes to be PostgreSQL-compatible
76 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000077 {
Greg Aker678256c2010-12-21 12:05:12 -060078 case 'TINYINT':
79 $attributes['TYPE'] = 'SMALLINT';
80 break;
81 case 'SMALLINT':
82 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
83 break;
84 case 'MEDIUMINT':
85 $attributes['TYPE'] = 'INTEGER';
86 break;
87 case 'INT':
88 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
89 break;
90 case 'BIGINT':
91 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
92 break;
93 case 'DOUBLE':
94 $attributes['TYPE'] = 'DOUBLE PRECISION';
95 break;
96 case 'DATETIME':
97 $attributes['TYPE'] = 'TIMESTAMP';
98 break;
99 case 'LONGTEXT':
100 $attributes['TYPE'] = 'TEXT';
101 break;
102 case 'BLOB':
103 $attributes['TYPE'] = 'BYTEA';
104 break;
Andrey Andreev214583f2012-01-26 16:39:09 +0200105 default:
106 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Greg Aker678256c2010-12-21 12:05:12 -0600109 // If this is an auto-incrementing primary key, use the serial data type instead
Andrey Andreev214583f2012-01-26 16:39:09 +0200110 $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
111 ? ' SERIAL' : ' '.$attributes['TYPE'];
Greg Aker678256c2010-12-21 12:05:12 -0600112
113 // Modified to prevent constraints with integer data types
Andrey Andreev214583f2012-01-26 16:39:09 +0200114 if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE)
Greg Aker678256c2010-12-21 12:05:12 -0600115 {
116 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300119 if (isset($attributes['DEFAULT']))
120 {
121 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
122 }
123
124 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
125 ? ' NULL' : ' NOT NULL';
126
127 // Added new attribute to create unique fields. Also works with MySQL
128 if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE)
129 {
130 $sql .= ' UNIQUE';
131 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // don't add a comma on the end of the last field
135 if (++$current_field_count < count($fields))
136 {
137 $sql .= ',';
138 }
139 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200140
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600141 return $sql;
142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Create Table
148 *
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600149 * @param string the table name
150 * @param array the fields
151 * @param mixed primary key(s)
152 * @param mixed key(s)
Andrey Andreev214583f2012-01-26 16:39:09 +0200153 * @param bool should 'IF NOT EXISTS' be added to the SQL
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600154 * @return bool
155 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300156 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600157 {
158 $sql = 'CREATE TABLE ';
159
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300160 // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually
161 if ($if_not_exists === TRUE && $this->db->table_exists($table))
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600162 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300163 return TRUE;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600164 }
165
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300166 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167
168 if (count($primary_keys) > 0)
169 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300170 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Greg Aker678256c2010-12-21 12:05:12 -0600173 $sql .= "\n);";
174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if (is_array($keys) && count($keys) > 0)
176 {
177 foreach ($keys as $key)
178 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300179 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300180 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300181 : array($this->db->escape_identifiers($key));
Barry Mienydd671972010-10-04 16:33:58 +0200182
Greg Aker678256c2010-12-21 12:05:12 -0600183 foreach ($key as $field)
184 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300185 $sql .= "\nCREATE INDEX ".$this->db->escape_identifiers($table.'_'.str_replace(array('"', "'"), '', $field).'_index')
186 .' ON '.$this->db->escape_identifiers($table).' ('.$this->db->escape_identifiers($field).');';
Greg Aker678256c2010-12-21 12:05:12 -0600187 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189 }
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return $sql;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * Alter table query
198 *
199 * Generates a platform-specific query so that a table can be altered
200 * Called by add_column(), drop_column(), and column_alter(),
201 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300202 * @param string $alter_type the ALTER type (ADD, DROP, CHANGE)
203 * @param string $table the table name
204 * @param string $fields the column definition
Andrey Andreevc98e93a2012-11-02 02:04:59 +0200205 * @param string $after_field
Andrey Andreev214583f2012-01-26 16:39:09 +0200206 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300208 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600209 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300210 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600212 // DROP has everything it needs now.
Andrey Andreev214583f2012-01-26 16:39:09 +0200213 if ($alter_type === 'DROP')
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600214 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300215 return $sql.$this->db->escape_identifiers($fields);
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600216 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000217
Andrey Andreev214583f2012-01-26 16:39:09 +0200218 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300219 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600220 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222}
223
224/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200225/* Location: ./system/database/drivers/postgre/postgre_forge.php */