Andrey Andreev | 214583f | 2012-01-26 16:39:09 +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 | * |
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 |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +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 | 214583f | 2012-01-26 16:39:09 +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 |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 41 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 43 | public function _create_database($name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 45 | return 'CREATE DATABASE '.$name; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 54 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 56 | public function _drop_database($name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 58 | return 'DROP DATABASE '.$name; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // -------------------------------------------------------------------- |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +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 | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 69 | private 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 | 214583f | 2012-01-26 16:39:09 +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 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 81 | $sql .= "\n\t".$attributes; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 85 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
| 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 88 | $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE); |
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 | // Convert datatypes to be PostgreSQL-compatible |
| 91 | switch (strtoupper($attributes['TYPE'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | { |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 93 | case 'TINYINT': |
| 94 | $attributes['TYPE'] = 'SMALLINT'; |
| 95 | break; |
| 96 | case 'SMALLINT': |
| 97 | $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT'; |
| 98 | break; |
| 99 | case 'MEDIUMINT': |
| 100 | $attributes['TYPE'] = 'INTEGER'; |
| 101 | break; |
| 102 | case 'INT': |
| 103 | $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER'; |
| 104 | break; |
| 105 | case 'BIGINT': |
| 106 | $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT'; |
| 107 | break; |
| 108 | case 'DOUBLE': |
| 109 | $attributes['TYPE'] = 'DOUBLE PRECISION'; |
| 110 | break; |
| 111 | case 'DATETIME': |
| 112 | $attributes['TYPE'] = 'TIMESTAMP'; |
| 113 | break; |
| 114 | case 'LONGTEXT': |
| 115 | $attributes['TYPE'] = 'TEXT'; |
| 116 | break; |
| 117 | case 'BLOB': |
| 118 | $attributes['TYPE'] = 'BYTEA'; |
| 119 | break; |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 120 | default: |
| 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 |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 125 | $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 126 | ? ' SERIAL' : ' '.$attributes['TYPE']; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 127 | |
| 128 | // Modified to prevent constraints with integer data types |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 129 | if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE) |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 130 | { |
| 131 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 134 | $sql .= (isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '') |
| 135 | .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') |
| 136 | // Added new attribute to create unqite fields. Also works with MySQL |
| 137 | .(( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) ? ' UNIQUE' : ''); |
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 | // don't add a comma on the end of the last field |
| 141 | if (++$current_field_count < count($fields)) |
| 142 | { |
| 143 | $sql .= ','; |
| 144 | } |
| 145 | } |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 146 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 147 | return $sql; |
| 148 | } |
| 149 | |
| 150 | // -------------------------------------------------------------------- |
| 151 | |
| 152 | /** |
| 153 | * Create Table |
| 154 | * |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 155 | * @param string the table name |
| 156 | * @param array the fields |
| 157 | * @param mixed primary key(s) |
| 158 | * @param mixed key(s) |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 159 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 160 | * @return bool |
| 161 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 162 | public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 163 | { |
| 164 | $sql = 'CREATE TABLE '; |
| 165 | |
| 166 | if ($if_not_exists === TRUE) |
| 167 | { |
| 168 | // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually |
| 169 | if ($this->db->table_exists($table)) |
| 170 | { |
| 171 | return TRUE; |
| 172 | } |
| 173 | } |
| 174 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 175 | $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | |
| 177 | if (count($primary_keys) > 0) |
| 178 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 179 | // Something seems to break when passing an array to protect_identifiers() |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 180 | foreach ($primary_keys as $index => $key) |
| 181 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 182 | $primary_keys[$index] = $this->db->protect_identifiers($key); |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 185 | $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 188 | $sql .= "\n);"; |
| 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | if (is_array($keys) && count($keys) > 0) |
| 191 | { |
| 192 | foreach ($keys as $key) |
| 193 | { |
| 194 | if (is_array($key)) |
| 195 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 196 | $key = $this->db->protect_identifiers($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | } |
| 198 | else |
| 199 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 200 | $key = array($this->db->protect_identifiers($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 203 | foreach ($key as $field) |
| 204 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 205 | $sql .= 'CREATE INDEX '.$table.'_'.str_replace(array('"', "'"), '', $field).'_index ON '.$table.' ('.$field.'); '; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 206 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | return $sql; |
| 211 | } |
| 212 | |
| 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Drop Table |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 217 | * |
| 218 | * @param string table name |
| 219 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 221 | public function _drop_table($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 223 | return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table).' CASCADE'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // -------------------------------------------------------------------- |
| 227 | |
| 228 | /** |
| 229 | * Alter table query |
| 230 | * |
| 231 | * Generates a platform-specific query so that a table can be altered |
| 232 | * Called by add_column(), drop_column(), and column_alter(), |
| 233 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 235 | * @param string the column name |
| 236 | * @param string the table name |
| 237 | * @param string the column definition |
| 238 | * @param string the default value |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 239 | * @param bool should 'NOT NULL' be added |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | * @param string the field after which we should add the new field |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 241 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 243 | public function _alter_table($alter_type, $table, $fields, $after_field = '') |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 244 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 245 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 247 | // DROP has everything it needs now. |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 248 | if ($alter_type === 'DROP') |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 249 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 250 | return $sql.$this->db->protect_identifiers($fields); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 251 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 253 | return $sql.$this->_process_fields($fields) |
| 254 | .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : ''); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 255 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | |
| 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
| 260 | * Rename a table |
| 261 | * |
| 262 | * Generates a platform-specific query so that a table can be renamed |
| 263 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | * @param string the old table name |
| 265 | * @param string the new table name |
| 266 | * @return string |
| 267 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 268 | public function _rename_table($table_name, $new_table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 270 | 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] | 271 | } |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 272 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* End of file postgre_forge.php */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 276 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |