1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * Postgre Forge Class
\r
21 * @category Database
\r
22 * @author ExpressionEngine Dev Team
\r
23 * @link http://codeigniter.com/user_guide/database/
\r
25 class CI_DB_postgre_forge extends CI_DB_forge {
\r
31 * @param string the database name
\r
34 function _create_database($name)
\r
36 return "CREATE DATABASE ".$name;
\r
39 // --------------------------------------------------------------------
\r
45 * @param string the database name
\r
48 function _drop_database($name)
\r
50 return "DROP DATABASE ".$name;
\r
53 // --------------------------------------------------------------------
\r
59 * @param string the table name
\r
60 * @param array the fields
\r
61 * @param mixed primary key(s)
\r
62 * @param mixed key(s)
\r
63 * @param boolean should 'IF NOT EXISTS' be added to the SQL
\r
66 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
\r
68 $sql = 'CREATE TABLE ';
\r
70 if ($if_not_exists === TRUE)
\r
72 $sql .= 'IF NOT EXISTS ';
\r
75 $sql .= $this->db->_escape_identifiers($table)." (";
\r
76 $current_field_count = 0;
\r
78 foreach ($fields as $field=>$attributes)
\r
80 // Numeric field names aren't allowed in databases, so if the key is
\r
81 // numeric, we know it was assigned by PHP and the developer manually
\r
82 // entered the field information, so we'll simply add it to the list
\r
83 if (is_numeric($field))
\r
85 $sql .= "\n\t$attributes";
\r
89 $attributes = array_change_key_case($attributes, CASE_UPPER);
\r
91 $sql .= "\n\t".$this->db->_protect_identifiers($field);
\r
93 $sql .= ' '.$attributes['TYPE'];
\r
95 if (array_key_exists('CONSTRAINT', $attributes))
\r
97 $sql .= '('.$attributes['CONSTRAINT'].')';
\r
100 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
\r
102 $sql .= ' UNSIGNED';
\r
105 if (array_key_exists('DEFAULT', $attributes))
\r
107 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
\r
110 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
\r
116 $sql .= ' NOT NULL';
\r
119 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
\r
121 $sql .= ' AUTO_INCREMENT';
\r
125 // don't add a comma on the end of the last field
\r
126 if (++$current_field_count < count($fields))
\r
132 if (count($primary_keys) > 0)
\r
134 $primary_keys = $this->db->_protect_identifiers($primary_keys);
\r
135 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
\r
138 if (is_array($keys) && count($keys) > 0)
\r
140 foreach ($keys as $key)
\r
142 if (is_array($key))
\r
144 $key = $this->db->_protect_identifiers($key);
\r
148 $key = array($this->db->_protect_identifiers($key));
\r
151 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
\r
160 // --------------------------------------------------------------------
\r
168 function _drop_table($table)
\r
170 return "DROP TABLE ".$this->db->_escape_identifiers($table)." CASCADE";
\r
173 // --------------------------------------------------------------------
\r
176 * Alter table query
\r
178 * Generates a platform-specific query so that a table can be altered
\r
179 * Called by add_column(), drop_column(), and column_alter(),
\r
182 * @param string the ALTER type (ADD, DROP, CHANGE)
\r
183 * @param string the column name
\r
184 * @param string the table name
\r
185 * @param string the column definition
\r
186 * @param string the default value
\r
187 * @param boolean should 'NOT NULL' be added
\r
188 * @param string the field after which we should add the new field
\r
191 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
\r
193 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
\r
195 // DROP has everything it needs now.
\r
196 if ($alter_type == 'DROP')
\r
201 $sql .= " $column_definition";
\r
203 if ($default_value != '')
\r
205 $sql .= " DEFAULT \"$default_value\"";
\r
208 if ($null === NULL)
\r
214 $sql .= ' NOT NULL';
\r
217 if ($after_field != '')
\r
219 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
\r
226 // --------------------------------------------------------------------
\r
231 * Generates a platform-specific query so that a table can be renamed
\r
234 * @param string the old table name
\r
235 * @param string the new table name
\r
238 function _rename_table($table_name, $new_table_name)
\r
240 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
\r
247 /* End of file postgre_forge.php */
\r
248 /* Location: ./system/database/drivers/postgre/postgre_forge.php */