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 | // -------------------------------------------------------------------- |
| 66 | |
| 67 | /** |
| 68 | * Create Table |
| 69 | * |
| 70 | * @access private |
| 71 | * @param string the table name |
| 72 | * @param array the fields |
| 73 | * @param mixed primary key(s) |
| 74 | * @param mixed key(s) |
| 75 | * @param boolean should 'IF NOT EXISTS' be added to the SQL |
| 76 | * @return bool |
| 77 | */ |
| 78 | function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
| 79 | { |
| 80 | $sql = 'CREATE TABLE '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | if ($if_not_exists === TRUE) |
| 83 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 84 | if ($this->db->table_exists($table)) |
| 85 | { |
| 86 | return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement |
| 87 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | $sql .= $this->db->_escape_identifiers($table)." ("; |
| 91 | $current_field_count = 0; |
| 92 | |
| 93 | foreach ($fields as $field=>$attributes) |
| 94 | { |
| 95 | // Numeric field names aren't allowed in databases, so if the key is |
| 96 | // numeric, we know it was assigned by PHP and the developer manually |
| 97 | // entered the field information, so we'll simply add it to the list |
| 98 | if (is_numeric($field)) |
| 99 | { |
| 100 | $sql .= "\n\t$attributes"; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | $sql .= "\n\t".$this->db->_protect_identifiers($field); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 108 | $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 110 | // Convert datatypes to be PostgreSQL-compatible |
| 111 | switch (strtoupper($attributes['TYPE'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 113 | case 'TINYINT': |
| 114 | $attributes['TYPE'] = 'SMALLINT'; |
| 115 | break; |
| 116 | case 'SMALLINT': |
| 117 | $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT'; |
| 118 | break; |
| 119 | case 'MEDIUMINT': |
| 120 | $attributes['TYPE'] = 'INTEGER'; |
| 121 | break; |
| 122 | case 'INT': |
| 123 | $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER'; |
| 124 | break; |
| 125 | case 'BIGINT': |
| 126 | $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT'; |
| 127 | break; |
| 128 | case 'DOUBLE': |
| 129 | $attributes['TYPE'] = 'DOUBLE PRECISION'; |
| 130 | break; |
| 131 | case 'DATETIME': |
| 132 | $attributes['TYPE'] = 'TIMESTAMP'; |
| 133 | break; |
| 134 | case 'LONGTEXT': |
| 135 | $attributes['TYPE'] = 'TEXT'; |
| 136 | break; |
| 137 | case 'BLOB': |
| 138 | $attributes['TYPE'] = 'BYTEA'; |
| 139 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 142 | // 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] | 143 | if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes) |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 144 | && $attributes['AUTO_INCREMENT'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 146 | $sql .= ' SERIAL'; |
| 147 | } |
| 148 | else |
| 149 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 150 | $sql .= ' '.$attributes['TYPE']; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Modified to prevent constraints with integer data types |
| 154 | if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false) |
| 155 | { |
| 156 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | if (array_key_exists('DEFAULT', $attributes)) |
| 160 | { |
| 161 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 165 | { |
| 166 | $sql .= ' NULL'; |
| 167 | } |
| 168 | else |
| 169 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 173 | // Added new attribute to create unqite fields. Also works with MySQL |
| 174 | if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 176 | $sql .= ' UNIQUE'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | // don't add a comma on the end of the last field |
| 181 | if (++$current_field_count < count($fields)) |
| 182 | { |
| 183 | $sql .= ','; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (count($primary_keys) > 0) |
| 188 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 189 | // Something seems to break when passing an array to _protect_identifiers() |
| 190 | foreach ($primary_keys as $index => $key) |
| 191 | { |
| 192 | $primary_keys[$index] = $this->db->_protect_identifiers($key); |
| 193 | } |
| 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 196 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 198 | $sql .= "\n);"; |
| 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | if (is_array($keys) && count($keys) > 0) |
| 201 | { |
| 202 | foreach ($keys as $key) |
| 203 | { |
| 204 | if (is_array($key)) |
| 205 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | $key = $this->db->_protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
| 210 | $key = array($this->db->_protect_identifiers($key)); |
| 211 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 213 | foreach ($key as $field) |
| 214 | { |
| 215 | $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); "; |
| 216 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | return $sql; |
| 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
| 226 | * Drop Table |
| 227 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 228 | * @access private |
| 229 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | */ |
| 231 | function _drop_table($table) |
| 232 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 233 | return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Alter table query |
| 240 | * |
| 241 | * Generates a platform-specific query so that a table can be altered |
| 242 | * Called by add_column(), drop_column(), and column_alter(), |
| 243 | * |
| 244 | * @access private |
| 245 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 246 | * @param string the column name |
| 247 | * @param string the table name |
| 248 | * @param string the column definition |
| 249 | * @param string the default value |
| 250 | * @param boolean should 'NOT NULL' be added |
| 251 | * @param string the field after which we should add the new field |
| 252 | * @return object |
| 253 | */ |
| 254 | function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
| 255 | { |
| 256 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name); |
| 257 | |
| 258 | // DROP has everything it needs now. |
| 259 | if ($alter_type == 'DROP') |
| 260 | { |
| 261 | return $sql; |
| 262 | } |
| 263 | |
| 264 | $sql .= " $column_definition"; |
| 265 | |
| 266 | if ($default_value != '') |
| 267 | { |
| 268 | $sql .= " DEFAULT \"$default_value\""; |
| 269 | } |
| 270 | |
| 271 | if ($null === NULL) |
| 272 | { |
| 273 | $sql .= ' NULL'; |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | $sql .= ' NOT NULL'; |
| 278 | } |
| 279 | |
| 280 | if ($after_field != '') |
| 281 | { |
| 282 | $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); |
| 283 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | return $sql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // -------------------------------------------------------------------- |
| 290 | |
| 291 | /** |
| 292 | * Rename a table |
| 293 | * |
| 294 | * Generates a platform-specific query so that a table can be renamed |
| 295 | * |
| 296 | * @access private |
| 297 | * @param string the old table name |
| 298 | * @param string the new table name |
| 299 | * @return string |
| 300 | */ |
| 301 | function _rename_table($table_name, $new_table_name) |
| 302 | { |
| 303 | $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name); |
| 304 | return $sql; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | } |
| 309 | |
| 310 | /* End of file postgre_forge.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 311 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |