Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Postgre Forge Class |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/database/ |
| 36 | */ |
| 37 | class CI_DB_postgre_forge extends CI_DB_forge { |
| 38 | |
| 39 | /** |
| 40 | * Create database |
| 41 | * |
| 42 | * @access private |
| 43 | * @param string the database name |
| 44 | * @return bool |
| 45 | */ |
| 46 | function _create_database($name) |
| 47 | { |
| 48 | return "CREATE DATABASE ".$name; |
| 49 | } |
| 50 | |
| 51 | // -------------------------------------------------------------------- |
| 52 | |
| 53 | /** |
| 54 | * Drop database |
| 55 | * |
| 56 | * @access private |
| 57 | * @param string the database name |
| 58 | * @return bool |
| 59 | */ |
| 60 | function _drop_database($name) |
| 61 | { |
| 62 | return "DROP DATABASE ".$name; |
| 63 | } |
| 64 | |
| 65 | // -------------------------------------------------------------------- |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | /** |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 68 | * Process Fields |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | */ |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 70 | function _process_fields($fields, $primary_keys=array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | { |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 72 | $sql = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | $current_field_count = 0; |
| 74 | |
| 75 | foreach ($fields as $field=>$attributes) |
| 76 | { |
| 77 | // Numeric field names aren't allowed in databases, so if the key is |
| 78 | // numeric, we know it was assigned by PHP and the developer manually |
| 79 | // entered the field information, so we'll simply add it to the list |
| 80 | if (is_numeric($field)) |
| 81 | { |
| 82 | $sql .= "\n\t$attributes"; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | $sql .= "\n\t".$this->db->_protect_identifiers($field); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 90 | $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 92 | // Convert datatypes to be PostgreSQL-compatible |
| 93 | switch (strtoupper($attributes['TYPE'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 95 | case 'TINYINT': |
| 96 | $attributes['TYPE'] = 'SMALLINT'; |
| 97 | break; |
| 98 | case 'SMALLINT': |
| 99 | $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT'; |
| 100 | break; |
| 101 | case 'MEDIUMINT': |
| 102 | $attributes['TYPE'] = 'INTEGER'; |
| 103 | break; |
| 104 | case 'INT': |
| 105 | $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER'; |
| 106 | break; |
| 107 | case 'BIGINT': |
| 108 | $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT'; |
| 109 | break; |
| 110 | case 'DOUBLE': |
| 111 | $attributes['TYPE'] = 'DOUBLE PRECISION'; |
| 112 | break; |
| 113 | case 'DATETIME': |
| 114 | $attributes['TYPE'] = 'TIMESTAMP'; |
| 115 | break; |
| 116 | case 'LONGTEXT': |
| 117 | $attributes['TYPE'] = 'TEXT'; |
| 118 | break; |
| 119 | case 'BLOB': |
| 120 | $attributes['TYPE'] = 'BYTEA'; |
| 121 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 124 | // If this is an auto-incrementing primary key, use the serial data type instead |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 125 | if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes) |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 126 | && $attributes['AUTO_INCREMENT'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 128 | $sql .= ' SERIAL'; |
| 129 | } |
| 130 | else |
| 131 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 132 | $sql .= ' '.$attributes['TYPE']; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Modified to prevent constraints with integer data types |
| 136 | if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false) |
| 137 | { |
| 138 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | if (array_key_exists('DEFAULT', $attributes)) |
| 142 | { |
| 143 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 147 | { |
| 148 | $sql .= ' NULL'; |
| 149 | } |
| 150 | else |
| 151 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 155 | // Added new attribute to create unqite fields. Also works with MySQL |
| 156 | if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 158 | $sql .= ' UNIQUE'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | // don't add a comma on the end of the last field |
| 163 | if (++$current_field_count < count($fields)) |
| 164 | { |
| 165 | $sql .= ','; |
| 166 | } |
| 167 | } |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 168 | |
| 169 | return $sql; |
| 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Create Table |
| 176 | * |
| 177 | * @access private |
| 178 | * @param string the table name |
| 179 | * @param array the fields |
| 180 | * @param mixed primary key(s) |
| 181 | * @param mixed key(s) |
| 182 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 183 | * @return bool |
| 184 | */ |
| 185 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 186 | { |
| 187 | $sql = 'CREATE TABLE '; |
| 188 | |
| 189 | if ($if_not_exists === TRUE) |
| 190 | { |
| 191 | // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually |
| 192 | if ($this->db->table_exists($table)) |
| 193 | { |
| 194 | return TRUE; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | $sql .= $this->db->_escape_identifiers($table)." ("; |
| 199 | $sql .= $this->_process_fields($fields, $primary_keys); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | |
| 201 | if (count($primary_keys) > 0) |
| 202 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 203 | // Something seems to break when passing an array to _protect_identifiers() |
| 204 | foreach ($primary_keys as $index => $key) |
| 205 | { |
| 206 | $primary_keys[$index] = $this->db->_protect_identifiers($key); |
| 207 | } |
| 208 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 212 | $sql .= "\n);"; |
| 213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | if (is_array($keys) && count($keys) > 0) |
| 215 | { |
| 216 | foreach ($keys as $key) |
| 217 | { |
| 218 | if (is_array($key)) |
| 219 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | $key = $this->db->_protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | } |
| 222 | else |
| 223 | { |
| 224 | $key = array($this->db->_protect_identifiers($key)); |
| 225 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 227 | foreach ($key as $field) |
| 228 | { |
| 229 | $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); "; |
| 230 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | return $sql; |
| 235 | } |
| 236 | |
| 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
| 240 | * Drop Table |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | */ |
| 242 | function _drop_table($table) |
| 243 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 244 | return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // -------------------------------------------------------------------- |
| 248 | |
| 249 | /** |
| 250 | * Alter table query |
| 251 | * |
| 252 | * Generates a platform-specific query so that a table can be altered |
| 253 | * Called by add_column(), drop_column(), and column_alter(), |
| 254 | * |
| 255 | * @access private |
| 256 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 257 | * @param string the column name |
| 258 | * @param string the table name |
| 259 | * @param string the column definition |
| 260 | * @param string the default value |
| 261 | * @param boolean should 'NOT NULL' be added |
| 262 | * @param string the field after which we should add the new field |
| 263 | * @return object |
| 264 | */ |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 265 | function _alter_table($alter_type, $table, $fields, $after_field = '') |
| 266 | { |
| 267 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 269 | // DROP has everything it needs now. |
| 270 | if ($alter_type == 'DROP') |
| 271 | { |
| 272 | return $sql.$this->db->_protect_identifiers($fields); |
| 273 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 275 | $sql .= $this->_process_fields($fields); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 277 | if ($after_field != '') |
| 278 | { |
| 279 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 280 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame^] | 282 | return $sql; |
| 283 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | |
| 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
| 288 | * Rename a table |
| 289 | * |
| 290 | * Generates a platform-specific query so that a table can be renamed |
| 291 | * |
| 292 | * @access private |
| 293 | * @param string the old table name |
| 294 | * @param string the new table name |
| 295 | * @return string |
| 296 | */ |
| 297 | function _rename_table($table_name, $new_table_name) |
| 298 | { |
| 299 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); |
| 300 | return $sql; |
| 301 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* End of file postgre_forge.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 305 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |