blob: 425622fac0e734f44bfb7867f87ddfd725d7b41f [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 /**
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();
99 for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++)
100 {
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 {
108 $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length'];
109 }
110
111 if ( ! empty($field[$i]['default']))
112 {
113 $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
114 .' SET '.$field[$i]['default'];
115 }
116
117 if (isset($field[$i]['null']))
118 {
119 $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
120 .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL');
121 }
122
123 if ( ! empty($field[$i]['new_name']))
124 {
125 $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name'])
126 .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
127 }
128 }
129
130 return $sqls;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600131 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
Andrey Andreeva287a342012-11-05 23:19:59 +0200133 // --------------------------------------------------------------------
134
135 /**
136 * Field attribute TYPE
137 *
138 * Performs a data type mapping between different databases.
139 *
140 * @param array &$attributes
141 * @return void
142 */
143 protected function _attr_type(&$attributes)
144 {
145 // Reset field lenghts for data types that don't support it
146 if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE)
147 {
148 $attributes['CONSTRAINT'] = NULL;
149 }
150
151 switch (strtoupper($attributes['TYPE']))
152 {
153 case 'TINYINT':
154 $attributes['TYPE'] = 'SMALLINT';
155 $attributes['UNSIGNED'] = FALSE;
156 return;
157 case 'MEDIUMINT':
158 $attributes['TYPE'] = 'INTEGER';
159 $attributes['UNSIGNED'] = FALSE;
160 return;
161 default: return;
162 }
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Field attribute AUTO_INCREMENT
169 *
170 * @param array &$attributes
171 * @param array &$field
172 * @return void
173 */
174 protected function _attr_auto_increment(&$attributes, &$field)
175 {
176 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
177 {
178 $field['type'] = ($field['type'] === 'NUMERIC')
179 ? 'BIGSERIAL'
180 : 'SERIAL';
181 }
182 }
183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184}
185
186/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200187/* Location: ./system/database/drivers/postgre/postgre_forge.php */