Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Postgre Forge Class |
| 31 | * |
| 32 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 33 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * @link http://codeigniter.com/user_guide/database/ |
| 35 | */ |
| 36 | class CI_DB_postgre_forge extends CI_DB_forge { |
| 37 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 38 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 39 | * UNSIGNED support |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | protected $_unsigned = array( |
| 44 | 'INT2' => 'INTEGER', |
| 45 | 'SMALLINT' => 'INTEGER', |
| 46 | 'INT' => 'BIGINT', |
| 47 | 'INT4' => 'BIGINT', |
| 48 | 'INTEGER' => 'BIGINT', |
| 49 | 'INT8' => 'NUMERIC', |
| 50 | 'BIGINT' => 'NUMERIC', |
| 51 | 'REAL' => 'DOUBLE PRECISION', |
| 52 | 'FLOAT' => 'DOUBLE PRECISION' |
| 53 | ); |
| 54 | |
| 55 | /** |
| 56 | * NULL value representation in CREATE/ALTER TABLE statements |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 57 | * |
| 58 | * @var string |
| 59 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 60 | protected $_null = 'NULL'; |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 61 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 62 | // -------------------------------------------------------------------- |
| 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 65 | * Class constructor |
Greg Aker | 6924e40 | 2011-12-27 17:19:50 -0600 | [diff] [blame] | 66 | * |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame^] | 67 | * @param object &$db Database object |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 68 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame^] | 70 | public function __construct(&$db) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame^] | 72 | parent::__construct($db); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 74 | if (version_compare($this->db->version(), '9.0', '>')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 76 | $this->create_table_if = 'CREATE TABLE IF NOT EXISTS'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | } |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 83 | * ALTER TABLE |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 84 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 85 | * @param string $alter_type ALTER type |
| 86 | * @param string $table Table name |
| 87 | * @param mixed $field Column definition |
| 88 | * @return string|string[] |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 89 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 90 | protected function _alter_table($alter_type, $table, $field) |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 91 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 92 | if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) |
| 93 | { |
| 94 | return parent::_alter_table($alter_type, $table, $field); |
| 95 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 97 | $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); |
| 98 | $sqls = array(); |
| 99 | for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++) |
| 100 | { |
| 101 | if ($field[$i]['_literal'] !== FALSE) |
| 102 | { |
| 103 | return FALSE; |
| 104 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 106 | if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type'])) |
| 107 | { |
| 108 | $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length']; |
| 109 | } |
| 110 | |
| 111 | if ( ! empty($field[$i]['default'])) |
| 112 | { |
| 113 | $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) |
| 114 | .' SET '.$field[$i]['default']; |
| 115 | } |
| 116 | |
| 117 | if (isset($field[$i]['null'])) |
| 118 | { |
| 119 | $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) |
| 120 | .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL'); |
| 121 | } |
| 122 | |
| 123 | if ( ! empty($field[$i]['new_name'])) |
| 124 | { |
| 125 | $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name']) |
| 126 | .' TO '.$this->db->escape_identifiers($field[$i]['new_name']); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return $sqls; |
Greg Aker | 0cbcc1a | 2011-12-27 17:11:38 -0600 | [diff] [blame] | 131 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 133 | // -------------------------------------------------------------------- |
| 134 | |
| 135 | /** |
| 136 | * Field attribute TYPE |
| 137 | * |
| 138 | * Performs a data type mapping between different databases. |
| 139 | * |
| 140 | * @param array &$attributes |
| 141 | * @return void |
| 142 | */ |
| 143 | protected function _attr_type(&$attributes) |
| 144 | { |
| 145 | // Reset field lenghts for data types that don't support it |
| 146 | if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE) |
| 147 | { |
| 148 | $attributes['CONSTRAINT'] = NULL; |
| 149 | } |
| 150 | |
| 151 | switch (strtoupper($attributes['TYPE'])) |
| 152 | { |
| 153 | case 'TINYINT': |
| 154 | $attributes['TYPE'] = 'SMALLINT'; |
| 155 | $attributes['UNSIGNED'] = FALSE; |
| 156 | return; |
| 157 | case 'MEDIUMINT': |
| 158 | $attributes['TYPE'] = 'INTEGER'; |
| 159 | $attributes['UNSIGNED'] = FALSE; |
| 160 | return; |
| 161 | default: return; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // -------------------------------------------------------------------- |
| 166 | |
| 167 | /** |
| 168 | * Field attribute AUTO_INCREMENT |
| 169 | * |
| 170 | * @param array &$attributes |
| 171 | * @param array &$field |
| 172 | * @return void |
| 173 | */ |
| 174 | protected function _attr_auto_increment(&$attributes, &$field) |
| 175 | { |
| 176 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 177 | { |
| 178 | $field['type'] = ($field['type'] === 'NUMERIC') |
| 179 | ? 'BIGSERIAL' |
| 180 | : 'SERIAL'; |
| 181 | } |
| 182 | } |
| 183 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* End of file postgre_forge.php */ |
Andrey Andreev | 135efb2 | 2012-03-20 15:30:56 +0200 | [diff] [blame] | 187 | /* Location: ./system/database/drivers/postgre/postgre_forge.php */ |