blob: 79d52218de81853a47dc957d92f09d7672fa55e0 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev214583f2012-01-26 16:39:09 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev214583f2012-01-26 16:39:09 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Postgre Forge Class
42 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020043 * @package CodeIgniter
44 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_DB_postgre_forge extends CI_DB_forge {
50
Andrey Andreevc98e93a2012-11-02 02:04:59 +020051 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020052 * UNSIGNED support
53 *
54 * @var array
55 */
56 protected $_unsigned = array(
57 'INT2' => 'INTEGER',
58 'SMALLINT' => 'INTEGER',
59 'INT' => 'BIGINT',
60 'INT4' => 'BIGINT',
61 'INTEGER' => 'BIGINT',
62 'INT8' => 'NUMERIC',
63 'BIGINT' => 'NUMERIC',
64 'REAL' => 'DOUBLE PRECISION',
65 'FLOAT' => 'DOUBLE PRECISION'
66 );
67
68 /**
69 * NULL value representation in CREATE/ALTER TABLE statements
Andrey Andreevc98e93a2012-11-02 02:04:59 +020070 *
71 * @var string
72 */
Andrey Andreev5289f272014-11-03 22:45:28 +020073 protected $_null = 'NULL';
Andrey Andreev214583f2012-01-26 16:39:09 +020074
Andrey Andreevc98e93a2012-11-02 02:04:59 +020075 // --------------------------------------------------------------------
76
Derek Allard2067d1a2008-11-13 22:59:24 +000077 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020078 * Class constructor
Greg Aker6924e402011-12-27 17:19:50 -060079 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020080 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020081 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000082 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020083 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020085 parent::__construct($db);
Derek Allard2067d1a2008-11-13 22:59:24 +000086
Andrey Andreeva287a342012-11-05 23:19:59 +020087 if (version_compare($this->db->version(), '9.0', '>'))
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreeva287a342012-11-05 23:19:59 +020089 $this->create_table_if = 'CREATE TABLE IF NOT EXISTS';
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
Greg Aker0cbcc1a2011-12-27 17:11:38 -060091 }
92
93 // --------------------------------------------------------------------
94
95 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020096 * ALTER TABLE
Greg Aker0cbcc1a2011-12-27 17:11:38 -060097 *
Andrey Andreeva287a342012-11-05 23:19:59 +020098 * @param string $alter_type ALTER type
99 * @param string $table Table name
100 * @param mixed $field Column definition
101 * @return string|string[]
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600102 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200103 protected function _alter_table($alter_type, $table, $field)
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600104 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200105 if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
106 {
107 return parent::_alter_table($alter_type, $table, $field);
108 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
Andrey Andreeva287a342012-11-05 23:19:59 +0200110 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
111 $sqls = array();
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200112 for ($i = 0, $c = count($field); $i < $c; $i++)
Andrey Andreeva287a342012-11-05 23:19:59 +0200113 {
114 if ($field[$i]['_literal'] !== FALSE)
115 {
116 return FALSE;
117 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000118
Andrey Andreeva287a342012-11-05 23:19:59 +0200119 if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
120 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200121 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
122 .' TYPE '.$field[$i]['type'].$field[$i]['length'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200123 }
124
125 if ( ! empty($field[$i]['default']))
126 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200127 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
128 .' SET DEFAULT '.$field[$i]['default'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200129 }
130
131 if (isset($field[$i]['null']))
132 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200133 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeve83ae5b2018-07-22 17:09:01 +0300134 .(trim($field[$i]['null']) === $this->_null ? ' DROP NOT NULL' : ' SET NOT NULL');
Andrey Andreeva287a342012-11-05 23:19:59 +0200135 }
136
137 if ( ! empty($field[$i]['new_name']))
138 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200139 $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeva287a342012-11-05 23:19:59 +0200140 .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
141 }
Andrey Andreev5289f272014-11-03 22:45:28 +0200142
143 if ( ! empty($field[$i]['comment']))
144 {
145 $sqls[] = 'COMMENT ON COLUMN '
146 .$this->db->escape_identifiers($table).'.'.$this->db->escape_identifiers($field[$i]['name'])
147 .' IS '.$field[$i]['comment'];
148 }
Andrey Andreeva287a342012-11-05 23:19:59 +0200149 }
150
151 return $sqls;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600152 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000153
Andrey Andreeva287a342012-11-05 23:19:59 +0200154 // --------------------------------------------------------------------
155
156 /**
157 * Field attribute TYPE
158 *
159 * Performs a data type mapping between different databases.
160 *
161 * @param array &$attributes
162 * @return void
163 */
164 protected function _attr_type(&$attributes)
165 {
Andrey Andreev71d8f722017-01-17 12:01:00 +0200166 // Reset field lengths for data types that don't support it
Andrey Andreeva287a342012-11-05 23:19:59 +0200167 if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE)
168 {
169 $attributes['CONSTRAINT'] = NULL;
170 }
171
172 switch (strtoupper($attributes['TYPE']))
173 {
174 case 'TINYINT':
175 $attributes['TYPE'] = 'SMALLINT';
176 $attributes['UNSIGNED'] = FALSE;
177 return;
178 case 'MEDIUMINT':
179 $attributes['TYPE'] = 'INTEGER';
180 $attributes['UNSIGNED'] = FALSE;
181 return;
182 default: return;
183 }
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Field attribute AUTO_INCREMENT
190 *
191 * @param array &$attributes
192 * @param array &$field
193 * @return void
194 */
195 protected function _attr_auto_increment(&$attributes, &$field)
196 {
197 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
198 {
199 $field['type'] = ($field['type'] === 'NUMERIC')
Andrey Andreev5289f272014-11-03 22:45:28 +0200200 ? 'BIGSERIAL'
201 : 'SERIAL';
Andrey Andreeva287a342012-11-05 23:19:59 +0200202 }
203 }
204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205}