1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
22
* @author ExpressionEngine Dev Team
23
* @link http://codeigniter.com/user_guide/database/
25
class CI_DB_postgre_forge extends CI_DB_forge {
31
* @param string the database name
34
function _create_database($name)
36
return "CREATE DATABASE ".$name;
39
// --------------------------------------------------------------------
45
* @param string the database name
48
function _drop_database($name)
50
return "DROP DATABASE ".$name;
53
// --------------------------------------------------------------------
59
* @param string the table name
60
* @param array the fields
61
* @param mixed primary key(s)
63
* @param boolean should 'IF NOT EXISTS' be added to the SQL
66
function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
68
$sql = 'CREATE TABLE ';
70
if ($if_not_exists === TRUE)
72
if ($this->db->table_exists($table))
74
return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement
78
$sql .= $this->db->_escape_identifiers($table)." (";
79
$current_field_count = 0;
81
foreach ($fields as $field=>$attributes)
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))
88
$sql .= "\n\t$attributes";
92
$attributes = array_change_key_case($attributes, CASE_UPPER);
94
$sql .= "\n\t".$this->db->_protect_identifiers($field);
96
$is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
98
// Convert datatypes to be PostgreSQL-compatible
99
switch (strtoupper($attributes['TYPE']))
102
$attributes['TYPE'] = 'SMALLINT';
105
$attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
108
$attributes['TYPE'] = 'INTEGER';
111
$attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
114
$attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
117
$attributes['TYPE'] = 'DOUBLE PRECISION';
120
$attributes['TYPE'] = 'TIMESTAMP';
123
$attributes['TYPE'] = 'TEXT';
126
$attributes['TYPE'] = 'BYTEA';
130
// 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)
138
$sql .= ' '.$attributes['TYPE'];
141
// Modified to prevent constraints with integer data types
142
if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
144
$sql .= '('.$attributes['CONSTRAINT'].')';
147
if (array_key_exists('DEFAULT', $attributes))
149
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
152
if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
161
// Added new attribute to create unqite fields. Also works with MySQL
162
if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
168
// don't add a comma on the end of the last field
169
if (++$current_field_count < count($fields))
175
if (count($primary_keys) > 0)
177
// Something seems to break when passing an array to _protect_identifiers()
178
foreach ($primary_keys as $index => $key)
180
$primary_keys[$index] = $this->db->_protect_identifiers($key);
183
$sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
188
if (is_array($keys) && count($keys) > 0)
190
foreach ($keys as $key)
194
$key = $this->db->_protect_identifiers($key);
198
$key = array($this->db->_protect_identifiers($key));
201
foreach ($key as $field)
203
$sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
211
// --------------------------------------------------------------------
219
function _drop_table($table)
221
return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
224
// --------------------------------------------------------------------
229
* Generates a platform-specific query so that a table can be altered
230
* Called by add_column(), drop_column(), and column_alter(),
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
242
function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
244
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
246
// DROP has everything it needs now.
247
if ($alter_type == 'DROP')
252
$sql .= " $column_definition";
254
if ($default_value != '')
256
$sql .= " DEFAULT \"$default_value\"";
268
if ($after_field != '')
270
$sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
277
// --------------------------------------------------------------------
282
* Generates a platform-specific query so that a table can be renamed
285
* @param string the old table name
286
* @param string the new table name
289
function _rename_table($table_name, $new_table_name)
291
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
298
/* End of file postgre_forge.php */
299
/* Location: ./system/database/drivers/postgre/postgre_forge.php */
b'\\ No newline at end of file'