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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * @param string the database name |
| 43 | * @return bool |
| 44 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 45 | protected function _create_database($name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
| 47 | return "CREATE DATABASE ".$name; |
| 48 | } |
| 49 | |
| 50 | // -------------------------------------------------------------------- |
| 51 | |
| 52 | /** |
| 53 | * Drop database |
| 54 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | * @param string the database name |
| 56 | * @return bool |
| 57 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 58 | protected function _drop_database($name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | { |
| 60 | return "DROP DATABASE ".$name; |
| 61 | } |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | /** |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 66 | * Process Fields |
Greg Aker | 6924e40 | 2011-12-27 17:19:50 -0600 | [diff] [blame] | 67 | * |
| 68 | * @param mixed the fields |
| 69 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 71 | protected function _process_fields($fields, $primary_keys=array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | { |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 73 | $sql = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | $current_field_count = 0; |
| 75 | |
| 76 | foreach ($fields as $field=>$attributes) |
| 77 | { |
| 78 | // Numeric field names aren't allowed in databases, so if the key is |
| 79 | // numeric, we know it was assigned by PHP and the developer manually |
| 80 | // entered the field information, so we'll simply add it to the list |
| 81 | if (is_numeric($field)) |
| 82 | { |
| 83 | $sql .= "\n\t$attributes"; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 88 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 89 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 91 | $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 93 | // Convert datatypes to be PostgreSQL-compatible |
| 94 | switch (strtoupper($attributes['TYPE'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 96 | case 'TINYINT': |
| 97 | $attributes['TYPE'] = 'SMALLINT'; |
| 98 | break; |
| 99 | case 'SMALLINT': |
| 100 | $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT'; |
| 101 | break; |
| 102 | case 'MEDIUMINT': |
| 103 | $attributes['TYPE'] = 'INTEGER'; |
| 104 | break; |
| 105 | case 'INT': |
| 106 | $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER'; |
| 107 | break; |
| 108 | case 'BIGINT': |
| 109 | $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT'; |
| 110 | break; |
| 111 | case 'DOUBLE': |
| 112 | $attributes['TYPE'] = 'DOUBLE PRECISION'; |
| 113 | break; |
| 114 | case 'DATETIME': |
| 115 | $attributes['TYPE'] = 'TIMESTAMP'; |
| 116 | break; |
| 117 | case 'LONGTEXT': |
| 118 | $attributes['TYPE'] = 'TEXT'; |
| 119 | break; |
| 120 | case 'BLOB': |
| 121 | $attributes['TYPE'] = 'BYTEA'; |
| 122 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 125 | // 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] | 126 | if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes) |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 127 | && $attributes['AUTO_INCREMENT'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 129 | $sql .= ' SERIAL'; |
| 130 | } |
| 131 | else |
| 132 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 133 | $sql .= ' '.$attributes['TYPE']; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Modified to prevent constraints with integer data types |
| 137 | if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false) |
| 138 | { |
| 139 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | if (array_key_exists('DEFAULT', $attributes)) |
| 143 | { |
| 144 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 145 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 148 | { |
| 149 | $sql .= ' NULL'; |
| 150 | } |
| 151 | else |
| 152 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 156 | // Added new attribute to create unqite fields. Also works with MySQL |
| 157 | if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 159 | $sql .= ' UNIQUE'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | // don't add a comma on the end of the last field |
| 164 | if (++$current_field_count < count($fields)) |
| 165 | { |
| 166 | $sql .= ','; |
| 167 | } |
| 168 | } |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 169 | |
| 170 | return $sql; |
| 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Create Table |
| 177 | * |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 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 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 185 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 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 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 203 | // Something seems to break when passing an array to protect_identifiers() |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 204 | foreach ($primary_keys as $index => $key) |
| 205 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 206 | $primary_keys[$index] = $this->db->protect_identifiers($key); |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 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 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +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 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 224 | $key = array($this->db->protect_identifiers($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 241 | * |
| 242 | * @param string the table name |
| 243 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 245 | protected function _drop_table($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 247 | return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // -------------------------------------------------------------------- |
| 251 | |
| 252 | /** |
| 253 | * Alter table query |
| 254 | * |
| 255 | * Generates a platform-specific query so that a table can be altered |
| 256 | * Called by add_column(), drop_column(), and column_alter(), |
| 257 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 259 | * @param string the column name |
| 260 | * @param string the table name |
| 261 | * @param string the column definition |
| 262 | * @param string the default value |
| 263 | * @param boolean should 'NOT NULL' be added |
| 264 | * @param string the field after which we should add the new field |
| 265 | * @return object |
| 266 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 267 | protected function _alter_table($alter_type, $table, $fields, $after_field = '') |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 268 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 269 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 271 | // DROP has everything it needs now. |
| 272 | if ($alter_type == 'DROP') |
| 273 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 274 | return $sql.$this->db->protect_identifiers($fields); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 275 | } |
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 | $sql .= $this->_process_fields($fields); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 279 | if ($after_field != '') |
| 280 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 281 | return $sql.' AFTER '.$this->db->protect_identifiers($after_field); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 282 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 284 | return $sql; |
| 285 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | |
| 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * Rename a table |
| 291 | * |
| 292 | * Generates a platform-specific query so that a table can be renamed |
| 293 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | * @param string the old table name |
| 295 | * @param string the new table name |
| 296 | * @return string |
| 297 | */ |
Timothy Warren | 5137ebc | 2012-03-19 19:05:25 -0400 | [diff] [blame] | 298 | protected function _rename_table($table_name, $new_table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 300 | return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* End of file postgre_forge.php */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 305 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |