Take two:
[www-register-wizard.git] / database / drivers / postgre / postgre_forge.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * Postgre Forge Class\r
20  *\r
21  * @category    Database\r
22  * @author              ExpressionEngine Dev Team\r
23  * @link                http://codeigniter.com/user_guide/database/\r
24  */\r
25 class CI_DB_postgre_forge extends CI_DB_forge {\r
26 \r
27         /**\r
28          * Create database\r
29          *\r
30          * @access      private\r
31          * @param       string  the database name\r
32          * @return      bool\r
33          */\r
34         function _create_database($name)\r
35         {\r
36                 return "CREATE DATABASE ".$name;\r
37         }\r
38 \r
39         // --------------------------------------------------------------------\r
40 \r
41         /**\r
42          * Drop database\r
43          *\r
44          * @access      private\r
45          * @param       string  the database name\r
46          * @return      bool\r
47          */\r
48         function _drop_database($name)\r
49         {\r
50                 return "DROP DATABASE ".$name;\r
51         }\r
52 \r
53         // --------------------------------------------------------------------\r
54 \r
55         /**\r
56          * Create Table\r
57          *\r
58          * @access      private\r
59          * @param       string  the table name\r
60          * @param       array   the fields\r
61          * @param       mixed   primary key(s)\r
62          * @param       mixed   key(s)\r
63          * @param       boolean should 'IF NOT EXISTS' be added to the SQL\r
64          * @return      bool\r
65          */\r
66         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)\r
67         {\r
68                 $sql = 'CREATE TABLE ';\r
69                 \r
70                 if ($if_not_exists === TRUE)\r
71                 {\r
72                         $sql .= 'IF NOT EXISTS ';\r
73                 }\r
74                 \r
75                 $sql .= $this->db->_escape_identifiers($table)." (";\r
76                 $current_field_count = 0;\r
77 \r
78                 foreach ($fields as $field=>$attributes)\r
79                 {\r
80                         // Numeric field names aren't allowed in databases, so if the key is\r
81                         // numeric, we know it was assigned by PHP and the developer manually\r
82                         // entered the field information, so we'll simply add it to the list\r
83                         if (is_numeric($field))\r
84                         {\r
85                                 $sql .= "\n\t$attributes";\r
86                         }\r
87                         else\r
88                         {\r
89                                 $attributes = array_change_key_case($attributes, CASE_UPPER);\r
90                                 \r
91                                 $sql .= "\n\t".$this->db->_protect_identifiers($field);\r
92                                 \r
93                                 $sql .=  ' '.$attributes['TYPE'];\r
94         \r
95                                 if (array_key_exists('CONSTRAINT', $attributes))\r
96                                 {\r
97                                         $sql .= '('.$attributes['CONSTRAINT'].')';\r
98                                 }\r
99         \r
100                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)\r
101                                 {\r
102                                         $sql .= ' UNSIGNED';\r
103                                 }\r
104         \r
105                                 if (array_key_exists('DEFAULT', $attributes))\r
106                                 {\r
107                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';\r
108                                 }\r
109         \r
110                                 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)\r
111                                 {\r
112                                         $sql .= ' NULL';\r
113                                 }\r
114                                 else\r
115                                 {\r
116                                         $sql .= ' NOT NULL';                    \r
117                                 }\r
118         \r
119                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)\r
120                                 {\r
121                                         $sql .= ' AUTO_INCREMENT';\r
122                                 }\r
123                         }\r
124                         \r
125                         // don't add a comma on the end of the last field\r
126                         if (++$current_field_count < count($fields))\r
127                         {\r
128                                 $sql .= ',';\r
129                         }\r
130                 }\r
131 \r
132                 if (count($primary_keys) > 0)\r
133                 {\r
134                         $primary_keys = $this->db->_protect_identifiers($primary_keys);\r
135                         $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";\r
136                 }\r
137                 \r
138                 if (is_array($keys) && count($keys) > 0)\r
139                 {\r
140                         foreach ($keys as $key)\r
141                         {\r
142                                 if (is_array($key))\r
143                                 {\r
144                                         $key = $this->db->_protect_identifiers($key);   \r
145                                 }\r
146                                 else\r
147                                 {\r
148                                         $key = array($this->db->_protect_identifiers($key));\r
149                                 }\r
150                                 \r
151                                 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";\r
152                         }\r
153                 }\r
154 \r
155                 $sql .= "\n);";\r
156 \r
157                 return $sql;\r
158         }\r
159 \r
160         // --------------------------------------------------------------------\r
161 \r
162         /**\r
163          * Drop Table\r
164          *\r
165          * @access      private\r
166          * @return      bool\r
167          */\r
168         function _drop_table($table)\r
169         {\r
170                 return "DROP TABLE ".$this->db->_escape_identifiers($table)." CASCADE";\r
171         }\r
172 \r
173         // --------------------------------------------------------------------\r
174 \r
175         /**\r
176          * Alter table query\r
177          *\r
178          * Generates a platform-specific query so that a table can be altered\r
179          * Called by add_column(), drop_column(), and column_alter(),\r
180          *\r
181          * @access      private\r
182          * @param       string  the ALTER type (ADD, DROP, CHANGE)\r
183          * @param       string  the column name\r
184          * @param       string  the table name\r
185          * @param       string  the column definition\r
186          * @param       string  the default value\r
187          * @param       boolean should 'NOT NULL' be added\r
188          * @param       string  the field after which we should add the new field\r
189          * @return      object\r
190          */\r
191         function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')\r
192         {\r
193                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);\r
194 \r
195                 // DROP has everything it needs now.\r
196                 if ($alter_type == 'DROP')\r
197                 {\r
198                         return $sql;\r
199                 }\r
200 \r
201                 $sql .= " $column_definition";\r
202 \r
203                 if ($default_value != '')\r
204                 {\r
205                         $sql .= " DEFAULT \"$default_value\"";\r
206                 }\r
207 \r
208                 if ($null === NULL)\r
209                 {\r
210                         $sql .= ' NULL';\r
211                 }\r
212                 else\r
213                 {\r
214                         $sql .= ' NOT NULL';\r
215                 }\r
216 \r
217                 if ($after_field != '')\r
218                 {\r
219                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);\r
220                 }\r
221                 \r
222                 return $sql;\r
223                 \r
224         }\r
225 \r
226         // --------------------------------------------------------------------\r
227 \r
228         /**\r
229          * Rename a table\r
230          *\r
231          * Generates a platform-specific query so that a table can be renamed\r
232          *\r
233          * @access      private\r
234          * @param       string  the old table name\r
235          * @param       string  the new table name\r
236          * @return      string\r
237          */\r
238         function _rename_table($table_name, $new_table_name)\r
239         {\r
240                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);\r
241                 return $sql;\r
242         }\r
243 \r
244 \r
245 }\r
246 \r
247 /* End of file postgre_forge.php */\r
248 /* Location: ./system/database/drivers/postgre/postgre_forge.php */