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 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 37 | protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE'; |
Andrey Andreev | bde70bd | 2012-03-20 14:15:12 +0200 | [diff] [blame] | 38 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | /** |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 40 | * Process Fields |
Greg Aker | 6924e40 | 2011-12-27 17:19:50 -0600 | [diff] [blame] | 41 | * |
| 42 | * @param mixed the fields |
| 43 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | */ |
Andrey Andreev | bde70bd | 2012-03-20 14:15:12 +0200 | [diff] [blame] | 45 | protected function _process_fields($fields, $primary_keys=array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 47 | $sql = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | $current_field_count = 0; |
| 49 | |
Andrey Andreev | bde70bd | 2012-03-20 14:15:12 +0200 | [diff] [blame] | 50 | foreach ($fields as $field => $attributes) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | { |
| 52 | // Numeric field names aren't allowed in databases, so if the key is |
| 53 | // numeric, we know it was assigned by PHP and the developer manually |
| 54 | // entered the field information, so we'll simply add it to the list |
| 55 | if (is_numeric($field)) |
| 56 | { |
| 57 | $sql .= "\n\t$attributes"; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 63 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 65 | $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 67 | // Convert datatypes to be PostgreSQL-compatible |
| 68 | switch (strtoupper($attributes['TYPE'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 70 | case 'TINYINT': |
| 71 | $attributes['TYPE'] = 'SMALLINT'; |
| 72 | break; |
| 73 | case 'SMALLINT': |
| 74 | $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT'; |
| 75 | break; |
| 76 | case 'MEDIUMINT': |
| 77 | $attributes['TYPE'] = 'INTEGER'; |
| 78 | break; |
| 79 | case 'INT': |
| 80 | $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER'; |
| 81 | break; |
| 82 | case 'BIGINT': |
| 83 | $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT'; |
| 84 | break; |
| 85 | case 'DOUBLE': |
| 86 | $attributes['TYPE'] = 'DOUBLE PRECISION'; |
| 87 | break; |
| 88 | case 'DATETIME': |
| 89 | $attributes['TYPE'] = 'TIMESTAMP'; |
| 90 | break; |
| 91 | case 'LONGTEXT': |
| 92 | $attributes['TYPE'] = 'TEXT'; |
| 93 | break; |
| 94 | case 'BLOB': |
| 95 | $attributes['TYPE'] = 'BYTEA'; |
| 96 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 99 | // 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] | 100 | if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes) |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 101 | && $attributes['AUTO_INCREMENT'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 103 | $sql .= ' SERIAL'; |
| 104 | } |
| 105 | else |
| 106 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 107 | $sql .= ' '.$attributes['TYPE']; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Modified to prevent constraints with integer data types |
| 111 | if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false) |
| 112 | { |
| 113 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | if (array_key_exists('DEFAULT', $attributes)) |
| 117 | { |
| 118 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 122 | { |
| 123 | $sql .= ' NULL'; |
| 124 | } |
| 125 | else |
| 126 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | $sql .= ' NOT NULL'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 130 | // Added new attribute to create unqite fields. Also works with MySQL |
| 131 | if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 133 | $sql .= ' UNIQUE'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | // don't add a comma on the end of the last field |
| 138 | if (++$current_field_count < count($fields)) |
| 139 | { |
| 140 | $sql .= ','; |
| 141 | } |
| 142 | } |
Andrey Andreev | bde70bd | 2012-03-20 14:15:12 +0200 | [diff] [blame] | 143 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 144 | return $sql; |
| 145 | } |
| 146 | |
| 147 | // -------------------------------------------------------------------- |
| 148 | |
| 149 | /** |
| 150 | * Create Table |
| 151 | * |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 152 | * @param string the table name |
| 153 | * @param array the fields |
| 154 | * @param mixed primary key(s) |
| 155 | * @param mixed key(s) |
Andrey Andreev | bde70bd | 2012-03-20 14:15:12 +0200 | [diff] [blame] | 156 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 157 | * @return bool |
| 158 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 159 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 160 | { |
| 161 | $sql = 'CREATE TABLE '; |
| 162 | |
| 163 | if ($if_not_exists === TRUE) |
| 164 | { |
| 165 | // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually |
| 166 | if ($this->db->table_exists($table)) |
| 167 | { |
| 168 | return TRUE; |
| 169 | } |
| 170 | } |
| 171 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 172 | $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | |
| 174 | if (count($primary_keys) > 0) |
| 175 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 176 | // Something seems to break when passing an array to protect_identifiers() |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 177 | foreach ($primary_keys as $index => $key) |
| 178 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 179 | $primary_keys[$index] = $this->db->protect_identifiers($key); |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 180 | } |
| 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 183 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 185 | $sql .= "\n);"; |
| 186 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | if (is_array($keys) && count($keys) > 0) |
| 188 | { |
| 189 | foreach ($keys as $key) |
| 190 | { |
| 191 | if (is_array($key)) |
| 192 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 193 | $key = $this->db->protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | } |
| 195 | else |
| 196 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 197 | $key = array($this->db->protect_identifiers($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 200 | foreach ($key as $field) |
| 201 | { |
| 202 | $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); "; |
| 203 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | return $sql; |
| 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | * Alter table query |
| 214 | * |
| 215 | * Generates a platform-specific query so that a table can be altered |
| 216 | * Called by add_column(), drop_column(), and column_alter(), |
| 217 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 219 | * @param string the column name |
| 220 | * @param string the table name |
| 221 | * @param string the column definition |
| 222 | * @param string the default value |
| 223 | * @param boolean should 'NOT NULL' be added |
| 224 | * @param string the field after which we should add the new field |
Andrey Andreev | bde70bd | 2012-03-20 14:15:12 +0200 | [diff] [blame] | 225 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 227 | protected function _alter_table($alter_type, $table, $fields, $after_field = '') |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 228 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 229 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 231 | // DROP has everything it needs now. |
| 232 | if ($alter_type == 'DROP') |
| 233 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 234 | return $sql.$this->db->protect_identifiers($fields); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 235 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 237 | $sql .= $this->_process_fields($fields); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 239 | if ($after_field != '') |
| 240 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 241 | return $sql.' AFTER '.$this->db->protect_identifiers($after_field); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 242 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 244 | return $sql; |
| 245 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* End of file postgre_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 250 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |