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