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 | * |
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 | 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 | |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 37 | protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE'; |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +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 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame^] | 42 | * @param mixed $fields |
| 43 | * @param array $primary_keys = array() |
Greg Aker | 6924e40 | 2011-12-27 17:19:50 -0600 | [diff] [blame] | 44 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | */ |
Andrey Andreev | 2b3edbd | 2012-01-27 21:02:53 +0200 | [diff] [blame] | 46 | protected function _process_fields($fields, $primary_keys = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | { |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 48 | $sql = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | $current_field_count = 0; |
| 50 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 51 | foreach ($fields as $field => $attributes) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | { |
| 53 | // Numeric field names aren't allowed in databases, so if the key is |
| 54 | // numeric, we know it was assigned by PHP and the developer manually |
| 55 | // entered the field information, so we'll simply add it to the list |
| 56 | if (is_numeric($field)) |
| 57 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 58 | $sql .= "\n\t".$attributes; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | } |
| 60 | else |
| 61 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 62 | $sql .= "\n\t".$this->db->escape_identifiers($field); |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 65 | $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $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; |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 97 | default: |
| 98 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 101 | // 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] | 102 | $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 103 | ? ' SERIAL' : ' '.$attributes['TYPE']; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 104 | |
| 105 | // Modified to prevent constraints with integer data types |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 106 | if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE) |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 107 | { |
| 108 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 111 | if (isset($attributes['DEFAULT'])) |
| 112 | { |
| 113 | $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; |
| 114 | } |
| 115 | |
| 116 | $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) |
| 117 | ? ' NULL' : ' NOT NULL'; |
| 118 | |
| 119 | // Added new attribute to create unique fields. Also works with MySQL |
| 120 | if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) |
| 121 | { |
| 122 | $sql .= ' UNIQUE'; |
| 123 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | // don't add a comma on the end of the last field |
| 127 | if (++$current_field_count < count($fields)) |
| 128 | { |
| 129 | $sql .= ','; |
| 130 | } |
| 131 | } |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 132 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 133 | return $sql; |
| 134 | } |
| 135 | |
| 136 | // -------------------------------------------------------------------- |
| 137 | |
| 138 | /** |
| 139 | * Create Table |
| 140 | * |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 141 | * @param string the table name |
| 142 | * @param array the fields |
| 143 | * @param mixed primary key(s) |
| 144 | * @param mixed key(s) |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 145 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 146 | * @return bool |
| 147 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 148 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 149 | { |
| 150 | $sql = 'CREATE TABLE '; |
| 151 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 152 | // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually |
| 153 | if ($if_not_exists === TRUE && $this->db->table_exists($table)) |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 154 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 155 | return TRUE; |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 156 | } |
| 157 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 158 | $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | |
| 160 | if (count($primary_keys) > 0) |
| 161 | { |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 162 | $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 165 | $sql .= "\n);"; |
| 166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | if (is_array($keys) && count($keys) > 0) |
| 168 | { |
| 169 | foreach ($keys as $key) |
| 170 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 171 | $key = is_array($key) |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 172 | ? $this->db->escape_identifiers($key) |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 173 | : array($this->db->escape_identifiers($key)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 175 | foreach ($key as $field) |
| 176 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 177 | $sql .= "\nCREATE INDEX ".$this->db->escape_identifiers($table.'_'.str_replace(array('"', "'"), '', $field).'_index') |
| 178 | .' ON '.$this->db->escape_identifiers($table).' ('.$this->db->escape_identifiers($field).');'; |
Greg Aker | 678256c | 2010-12-21 12:05:12 -0600 | [diff] [blame] | 179 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | return $sql; |
| 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | * Alter table query |
| 190 | * |
| 191 | * Generates a platform-specific query so that a table can be altered |
| 192 | * Called by add_column(), drop_column(), and column_alter(), |
| 193 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame^] | 194 | * @param string $alter_type the ALTER type (ADD, DROP, CHANGE) |
| 195 | * @param string $table the table name |
| 196 | * @param string $fields the column definition |
| 197 | * @param string $after_field = '' |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 198 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 200 | protected function _alter_table($alter_type, $table, $fields, $after_field = '') |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 201 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 202 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 204 | // DROP has everything it needs now. |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 205 | if ($alter_type === 'DROP') |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 206 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 207 | return $sql.$this->db->escape_identifiers($fields); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 208 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 210 | return $sql.$this->_process_fields($fields) |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 211 | .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 212 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* End of file postgre_forge.php */ |
Andrey Andreev | 135efb2 | 2012-03-20 15:30:56 +0200 | [diff] [blame] | 217 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |