blob: ad26763a42a67b5fd75f829c69af0b4607d12a96 [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 Andreeva287a342012-11-05 23:19:59 +020067 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000068 */
Andrey Andreeva287a342012-11-05 23:19:59 +020069 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Andrey Andreeva287a342012-11-05 23:19:59 +020071 parent::__construct();
Derek Allard2067d1a2008-11-13 22:59:24 +000072
Andrey Andreeva287a342012-11-05 23:19:59 +020073 if (version_compare($this->db->version(), '9.0', '>'))
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
Andrey Andreeva287a342012-11-05 23:19:59 +020075 $this->create_table_if = 'CREATE TABLE IF NOT EXISTS';
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
Greg Aker0cbcc1a2011-12-27 17:11:38 -060077 }
78
79 // --------------------------------------------------------------------
80
81 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020082 * ALTER TABLE
Greg Aker0cbcc1a2011-12-27 17:11:38 -060083 *
Andrey Andreeva287a342012-11-05 23:19:59 +020084 * @param string $alter_type ALTER type
85 * @param string $table Table name
86 * @param mixed $field Column definition
87 * @return string|string[]
Greg Aker0cbcc1a2011-12-27 17:11:38 -060088 */
Andrey Andreeva287a342012-11-05 23:19:59 +020089 protected function _alter_table($alter_type, $table, $field)
Greg Aker0cbcc1a2011-12-27 17:11:38 -060090 {
Andrey Andreeva287a342012-11-05 23:19:59 +020091 if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
92 {
93 return parent::_alter_table($alter_type, $table, $field);
94 }
Derek Allard2067d1a2008-11-13 22:59:24 +000095
Andrey Andreeva287a342012-11-05 23:19:59 +020096 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
97 $sqls = array();
98 for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++)
99 {
100 if ($field[$i]['_literal'] !== FALSE)
101 {
102 return FALSE;
103 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
Andrey Andreeva287a342012-11-05 23:19:59 +0200105 if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
106 {
107 $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length'];
108 }
109
110 if ( ! empty($field[$i]['default']))
111 {
112 $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
113 .' SET '.$field[$i]['default'];
114 }
115
116 if (isset($field[$i]['null']))
117 {
118 $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name'])
119 .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL');
120 }
121
122 if ( ! empty($field[$i]['new_name']))
123 {
124 $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name'])
125 .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
126 }
127 }
128
129 return $sqls;
Greg Aker0cbcc1a2011-12-27 17:11:38 -0600130 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000131
Andrey Andreeva287a342012-11-05 23:19:59 +0200132 // --------------------------------------------------------------------
133
134 /**
135 * Field attribute TYPE
136 *
137 * Performs a data type mapping between different databases.
138 *
139 * @param array &$attributes
140 * @return void
141 */
142 protected function _attr_type(&$attributes)
143 {
144 // Reset field lenghts for data types that don't support it
145 if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE)
146 {
147 $attributes['CONSTRAINT'] = NULL;
148 }
149
150 switch (strtoupper($attributes['TYPE']))
151 {
152 case 'TINYINT':
153 $attributes['TYPE'] = 'SMALLINT';
154 $attributes['UNSIGNED'] = FALSE;
155 return;
156 case 'MEDIUMINT':
157 $attributes['TYPE'] = 'INTEGER';
158 $attributes['UNSIGNED'] = FALSE;
159 return;
160 default: return;
161 }
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Field attribute AUTO_INCREMENT
168 *
169 * @param array &$attributes
170 * @param array &$field
171 * @return void
172 */
173 protected function _attr_auto_increment(&$attributes, &$field)
174 {
175 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
176 {
177 $field['type'] = ($field['type'] === 'NUMERIC')
178 ? 'BIGSERIAL'
179 : 'SERIAL';
180 }
181 }
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183}
184
185/* End of file postgre_forge.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200186/* Location: ./system/database/drivers/postgre/postgre_forge.php */