blob: 91a1c6861f2b49d5490faecc47fe8c21551fa801 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Postgre Forge Class
20 *
21 * @category Database
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
24 */
25class CI_DB_postgre_forge extends CI_DB_forge {
26
27 /**
28 * Create database
29 *
30 * @access private
31 * @param string the database name
32 * @return bool
33 */
34 function _create_database($name)
35 {
36 return "CREATE DATABASE ".$name;
37 }
38
39 // --------------------------------------------------------------------
40
41 /**
42 * Drop database
43 *
44 * @access private
45 * @param string the database name
46 * @return bool
47 */
48 function _drop_database($name)
49 {
50 return "DROP DATABASE ".$name;
51 }
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Create Table
57 *
58 * @access private
59 * @param string the table name
60 * @param array the fields
61 * @param mixed primary key(s)
62 * @param mixed key(s)
63 * @param boolean should 'IF NOT EXISTS' be added to the SQL
64 * @return bool
65 */
66 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
67 {
68 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 if ($if_not_exists === TRUE)
71 {
Greg Aker678256c2010-12-21 12:05:12 -060072 if ($this->db->table_exists($table))
73 {
74 return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement
75 }
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 $sql .= $this->db->_escape_identifiers($table)." (";
79 $current_field_count = 0;
80
81 foreach ($fields as $field=>$attributes)
82 {
83 // Numeric field names aren't allowed in databases, so if the key is
84 // numeric, we know it was assigned by PHP and the developer manually
85 // entered the field information, so we'll simply add it to the list
86 if (is_numeric($field))
87 {
88 $sql .= "\n\t$attributes";
89 }
90 else
91 {
92 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +020095
Greg Aker678256c2010-12-21 12:05:12 -060096 $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020097
Greg Aker678256c2010-12-21 12:05:12 -060098 // Convert datatypes to be PostgreSQL-compatible
99 switch (strtoupper($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Greg Aker678256c2010-12-21 12:05:12 -0600101 case 'TINYINT':
102 $attributes['TYPE'] = 'SMALLINT';
103 break;
104 case 'SMALLINT':
105 $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
106 break;
107 case 'MEDIUMINT':
108 $attributes['TYPE'] = 'INTEGER';
109 break;
110 case 'INT':
111 $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
112 break;
113 case 'BIGINT':
114 $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
115 break;
116 case 'DOUBLE':
117 $attributes['TYPE'] = 'DOUBLE PRECISION';
118 break;
119 case 'DATETIME':
120 $attributes['TYPE'] = 'TIMESTAMP';
121 break;
122 case 'LONGTEXT':
123 $attributes['TYPE'] = 'TEXT';
124 break;
125 case 'BLOB':
126 $attributes['TYPE'] = 'BYTEA';
127 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Greg Aker678256c2010-12-21 12:05:12 -0600130 // If this is an auto-incrementing primary key, use the serial data type instead
131 if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
132 && $attributes['AUTO_INCREMENT'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Greg Aker678256c2010-12-21 12:05:12 -0600134 $sql .= ' SERIAL';
135 }
136 else
137 {
138 $sql .= ' '.$attributes['TYPE'];
139 }
140
141 // Modified to prevent constraints with integer data types
142 if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
143 {
144 $sql .= '('.$attributes['CONSTRAINT'].')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 if (array_key_exists('DEFAULT', $attributes))
148 {
149 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
153 {
154 $sql .= ' NULL';
155 }
156 else
157 {
Barry Mienydd671972010-10-04 16:33:58 +0200158 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Greg Aker678256c2010-12-21 12:05:12 -0600161 // Added new attribute to create unqite fields. Also works with MySQL
162 if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Greg Aker678256c2010-12-21 12:05:12 -0600164 $sql .= ' UNIQUE';
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // don't add a comma on the end of the last field
169 if (++$current_field_count < count($fields))
170 {
171 $sql .= ',';
172 }
173 }
174
175 if (count($primary_keys) > 0)
176 {
Greg Aker678256c2010-12-21 12:05:12 -0600177 // Something seems to break when passing an array to _protect_identifiers()
178 foreach ($primary_keys as $index => $key)
179 {
180 $primary_keys[$index] = $this->db->_protect_identifiers($key);
181 }
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Greg Aker678256c2010-12-21 12:05:12 -0600186 $sql .= "\n);";
187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 if (is_array($keys) && count($keys) > 0)
189 {
190 foreach ($keys as $key)
191 {
192 if (is_array($key))
193 {
Barry Mienydd671972010-10-04 16:33:58 +0200194 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196 else
197 {
198 $key = array($this->db->_protect_identifiers($key));
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Greg Aker678256c2010-12-21 12:05:12 -0600201 foreach ($key as $field)
202 {
203 $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
204 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206 }
207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 return $sql;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Drop Table
215 *
Greg Aker678256c2010-12-21 12:05:12 -0600216 * @access private
217 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 */
219 function _drop_table($table)
220 {
Greg Aker678256c2010-12-21 12:05:12 -0600221 return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Alter table query
228 *
229 * Generates a platform-specific query so that a table can be altered
230 * Called by add_column(), drop_column(), and column_alter(),
231 *
232 * @access private
233 * @param string the ALTER type (ADD, DROP, CHANGE)
234 * @param string the column name
235 * @param string the table name
236 * @param string the column definition
237 * @param string the default value
238 * @param boolean should 'NOT NULL' be added
239 * @param string the field after which we should add the new field
240 * @return object
241 */
242 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
243 {
244 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
245
246 // DROP has everything it needs now.
247 if ($alter_type == 'DROP')
248 {
249 return $sql;
250 }
251
252 $sql .= " $column_definition";
253
254 if ($default_value != '')
255 {
256 $sql .= " DEFAULT \"$default_value\"";
257 }
258
259 if ($null === NULL)
260 {
261 $sql .= ' NULL';
262 }
263 else
264 {
265 $sql .= ' NOT NULL';
266 }
267
268 if ($after_field != '')
269 {
270 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Rename a table
281 *
282 * Generates a platform-specific query so that a table can be renamed
283 *
284 * @access private
285 * @param string the old table name
286 * @param string the new table name
287 * @return string
288 */
289 function _rename_table($table_name, $new_table_name)
290 {
291 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
292 return $sql;
293 }
294
295
296}
297
298/* End of file postgre_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000299/* Location: ./system/database/drivers/postgre/postgre_forge.php */