blob: 97b82776da5e50586f64df9c808e6fa88539e48f [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020039 * UNSIGNED support
40 *
41 * @var array
42 */
43 protected $_unsigned = array(
44 'INT2' => 'INTEGER',
45 'SMALLINT' => 'INTEGER',
46 'INT' => 'BIGINT',
47 'INT4' => 'BIGINT',
48 'INTEGER' => 'BIGINT',
49 'INT8' => 'NUMERIC',
50 'BIGINT' => 'NUMERIC',
51 'REAL' => 'DOUBLE PRECISION',
52 'FLOAT' => 'DOUBLE PRECISION'
53 );
54
55 /**
56 * NULL value representation in CREATE/ALTER TABLE statements
Andrey Andreevc98e93a2012-11-02 02:04:59 +020057 *
58 * @var string
59 */
Andrey Andreeva287a342012-11-05 23:19:59 +020060 protected $_null = 'NULL';
Andrey Andreev214583f2012-01-26 16:39:09 +020061
Andrey Andreevc98e93a2012-11-02 02:04:59 +020062 // --------------------------------------------------------------------
63
Derek Allard2067d1a2008-11-13 22:59:24 +000064 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020065 * Class constructor
Greg Aker6924e402011-12-27 17:19:50 -060066 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020067 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020068 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000069 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020070 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020072 parent::__construct($db);
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreeva287a342012-11-05 23:19:59 +020074 if (version_compare($this->db->version(), '9.0', '>'))
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
Andrey Andreeva287a342012-11-05 23:19:59 +020076 $this->create_table_if = 'CREATE TABLE IF NOT EXISTS';
Derek Allard2067d1a2008-11-13 22:59:24 +000077 }
Greg Aker0cbcc1a2011-12-27 17:11:38 -060078 }
79
80 // --------------------------------------------------------------------
81
82 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020083 * ALTER TABLE
Greg Aker0cbcc1a2011-12-27 17:11:38 -060084 *
Andrey Andreeva287a342012-11-05 23:19:59 +020085 * @param string $alter_type ALTER type
86 * @param string $table Table name
87 * @param mixed $field Column definition
88 * @return string|string[]
Greg Aker0cbcc1a2011-12-27 17:11:38 -060089 */
Andrey Andreeva287a342012-11-05 23:19:59 +020090 protected function _alter_table($alter_type, $table, $field)
Greg Aker0cbcc1a2011-12-27 17:11:38 -060091 {
Andrey Andreeva287a342012-11-05 23:19:59 +020092 if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
93 {
94 return parent::_alter_table($alter_type, $table, $field);
95 }
Derek Allard2067d1a2008-11-13 22:59:24 +000096
Andrey Andreeva287a342012-11-05 23:19:59 +020097 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
98 $sqls = array();
Andrey Andreev7ade8b72012-11-22 13:12:22 +020099 for ($i = 0, $c = count($field); $i < $c; $i++)
Andrey Andreeva287a342012-11-05 23:19:59 +0200100 {
101 if ($field[$i]['_literal'] !== FALSE)
102 {
103 return FALSE;
104 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
Andrey Andreeva287a342012-11-05 23:19:59 +0200106 if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
107 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200108 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
109 .' TYPE '.$field[$i]['type'].$field[$i]['length'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200110 }
111
112 if ( ! empty($field[$i]['default']))
113 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200114 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
115 .' SET DEFAULT '.$field[$i]['default'];
Andrey Andreeva287a342012-11-05 23:19:59 +0200116 }
117
118 if (isset($field[$i]['null']))
119 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200120 $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeva287a342012-11-05 23:19:59 +0200121 .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL');
122 }
123
124 if ( ! empty($field[$i]['new_name']))
125 {
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200126 $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
Andrey Andreeva287a342012-11-05 23:19:59 +0200127 .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
128 }
129 }
130
131 return $sqls;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600132 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000133
Andrey Andreeva287a342012-11-05 23:19:59 +0200134 // --------------------------------------------------------------------
135
136 /**
137 * Field attribute TYPE
138 *
139 * Performs a data type mapping between different databases.
140 *
141 * @param array &$attributes
142 * @return void
143 */
144 protected function _attr_type(&$attributes)
145 {
146 // Reset field lenghts for data types that don't support it
147 if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE)
148 {
149 $attributes['CONSTRAINT'] = NULL;
150 }
151
152 switch (strtoupper($attributes['TYPE']))
153 {
154 case 'TINYINT':
155 $attributes['TYPE'] = 'SMALLINT';
156 $attributes['UNSIGNED'] = FALSE;
157 return;
158 case 'MEDIUMINT':
159 $attributes['TYPE'] = 'INTEGER';
160 $attributes['UNSIGNED'] = FALSE;
161 return;
162 default: return;
163 }
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Field attribute AUTO_INCREMENT
170 *
171 * @param array &$attributes
172 * @param array &$field
173 * @return void
174 */
175 protected function _attr_auto_increment(&$attributes, &$field)
176 {
177 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
178 {
179 $field['type'] = ($field['type'] === 'NUMERIC')
180 ? 'BIGSERIAL'
181 : 'SERIAL';
182 }
183 }
184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185}
186
187/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200188/* Location: ./system/database/drivers/postgre/postgre_forge.php */