source: sourcecode/system/database/drivers/postgre/postgre_forge.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 7.2 KB
Line 
1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package             CodeIgniter
8 * @author              ExpressionEngine Dev Team
9 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
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 */
25class 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 ';
69
70                if ($if_not_exists === TRUE)
71                {
72                        if ($this->db->table_exists($table))
73                        {
74                                return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement
75                        }
76                }
77
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);
93
94                                $sql .= "\n\t".$this->db->_protect_identifiers($field);
95
96                                $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
97
98                                // Convert datatypes to be PostgreSQL-compatible
99                                switch (strtoupper($attributes['TYPE']))
100                                {
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;
128                                }
129
130                                // If this is an auto-incrementing primary key, use the serial data type instead
131                                if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
132                                        && $attributes['AUTO_INCREMENT'] === TRUE)
133                                {
134                                        $sql .= ' SERIAL';
135                                }
136                                else
137                                {
138                                        $sql .=  ' '.$attributes['TYPE'];
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'].')';
145                                }
146
147                                if (array_key_exists('DEFAULT', $attributes))
148                                {
149                                        $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
150                                }
151
152                                if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
153                                {
154                                        $sql .= ' NULL';
155                                }
156                                else
157                                {
158                                        $sql .= ' NOT NULL';
159                                }
160
161                                // Added new attribute to create unqite fields. Also works with MySQL
162                                if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
163                                {
164                                        $sql .= ' UNIQUE';
165                                }
166                        }
167
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                {
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
183                        $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
184                }
185
186                $sql .= "\n);";
187
188                if (is_array($keys) && count($keys) > 0)
189                {
190                        foreach ($keys as $key)
191                        {
192                                if (is_array($key))
193                                {
194                                        $key = $this->db->_protect_identifiers($key);
195                                }
196                                else
197                                {
198                                        $key = array($this->db->_protect_identifiers($key));
199                                }
200
201                                foreach ($key as $field)
202                                {
203                                        $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
204                                }
205                        }
206                }
207
208                return $sql;
209        }
210
211        // --------------------------------------------------------------------
212
213        /**
214         * Drop Table
215         *
216         * @access    private
217         * @return    bool
218         */
219        function _drop_table($table)
220        {
221                return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
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                }
272
273                return $sql;
274
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 */
299/* Location: ./system/database/drivers/postgre/postgre_forge.php */
Note: See TracBrowser for help on using the repository browser.