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