Take two:
[www-register-wizard.git] / database / drivers / odbc / odbc_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  * ODBC Forge Class\r
20  *\r
21  * @category    Database\r
22  * @author              ExpressionEngine Dev Team\r
23  * @link                http://codeigniter.com/database/\r
24  */\r
25 class CI_DB_odbc_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()\r
35         {\r
36                 // ODBC has no "create database" command since it's\r
37                 // designed to connect to an existing database\r
38                 if ($this->db->db_debug)\r
39                 {\r
40                         return $this->db->display_error('db_unsuported_feature');\r
41                 }\r
42                 return FALSE;\r
43         }\r
44 \r
45         // --------------------------------------------------------------------\r
46 \r
47         /**\r
48          * Drop database\r
49          *\r
50          * @access      private\r
51          * @param       string  the database name\r
52          * @return      bool\r
53          */\r
54         function _drop_database($name)\r
55         {\r
56                 // ODBC has no "drop database" command since it's\r
57                 // designed to connect to an existing database          \r
58                 if ($this->db->db_debug)\r
59                 {\r
60                         return $this->db->display_error('db_unsuported_feature');\r
61                 }\r
62                 return FALSE;\r
63         }\r
64 \r
65         // --------------------------------------------------------------------\r
66 \r
67         /**\r
68          * Create Table\r
69          *\r
70          * @access      private\r
71          * @param       string  the table name\r
72          * @param       array   the fields\r
73          * @param       mixed   primary key(s)\r
74          * @param       mixed   key(s)\r
75          * @param       boolean should 'IF NOT EXISTS' be added to the SQL\r
76          * @return      bool\r
77          */\r
78         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)\r
79         {\r
80                 $sql = 'CREATE TABLE ';\r
81                 \r
82                 if ($if_not_exists === TRUE)\r
83                 {\r
84                         $sql .= 'IF NOT EXISTS ';\r
85                 }\r
86                 \r
87                 $sql .= $this->db->_escape_identifiers($table)." (";\r
88                 $current_field_count = 0;\r
89 \r
90                 foreach ($fields as $field=>$attributes)\r
91                 {\r
92                         // Numeric field names aren't allowed in databases, so if the key is\r
93                         // numeric, we know it was assigned by PHP and the developer manually\r
94                         // entered the field information, so we'll simply add it to the list\r
95                         if (is_numeric($field))\r
96                         {\r
97                                 $sql .= "\n\t$attributes";\r
98                         }\r
99                         else\r
100                         {\r
101                                 $attributes = array_change_key_case($attributes, CASE_UPPER);\r
102                                 \r
103                                 $sql .= "\n\t".$this->db->_protect_identifiers($field);\r
104                                 \r
105                                 $sql .=  ' '.$attributes['TYPE'];\r
106         \r
107                                 if (array_key_exists('CONSTRAINT', $attributes))\r
108                                 {\r
109                                         $sql .= '('.$attributes['CONSTRAINT'].')';\r
110                                 }\r
111         \r
112                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)\r
113                                 {\r
114                                         $sql .= ' UNSIGNED';\r
115                                 }\r
116         \r
117                                 if (array_key_exists('DEFAULT', $attributes))\r
118                                 {\r
119                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';\r
120                                 }\r
121         \r
122                                 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)\r
123                                 {\r
124                                         $sql .= ' NULL';\r
125                                 }\r
126                                 else\r
127                                 {\r
128                                         $sql .= ' NOT NULL';                    \r
129                                 }\r
130         \r
131                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)\r
132                                 {\r
133                                         $sql .= ' AUTO_INCREMENT';\r
134                                 }\r
135                         }\r
136                         \r
137                         // don't add a comma on the end of the last field\r
138                         if (++$current_field_count < count($fields))\r
139                         {\r
140                                 $sql .= ',';\r
141                         }\r
142                 }\r
143 \r
144                 if (count($primary_keys) > 0)\r
145                 {\r
146                         $primary_keys = $this->db->_protect_identifiers($primary_keys);\r
147                         $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";\r
148                 }\r
149                 \r
150                 if (is_array($keys) && count($keys) > 0)\r
151                 {\r
152                         foreach ($keys as $key)\r
153                         {\r
154                                 if (is_array($key))\r
155                                 {\r
156                                         $key = $this->db->_protect_identifiers($key);   \r
157                                 }\r
158                                 else\r
159                                 {\r
160                                         $key = array($this->db->_protect_identifiers($key));\r
161                                 }\r
162                                 \r
163                                 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";\r
164                         }\r
165                 }\r
166                 \r
167                 $sql .= "\n)";\r
168 \r
169                 return $sql;\r
170         }\r
171 \r
172         // --------------------------------------------------------------------\r
173 \r
174         /**\r
175          * Drop Table\r
176          *\r
177          * @access      private\r
178          * @return      bool\r
179          */\r
180         function _drop_table($table)\r
181         {\r
182                 // Not a supported ODBC feature \r
183                 if ($this->db->db_debug)\r
184                 {\r
185                         return $this->db->display_error('db_unsuported_feature');\r
186                 }\r
187                 return FALSE;\r
188         }\r
189 \r
190         // --------------------------------------------------------------------\r
191 \r
192         /**\r
193          * Alter table query\r
194          *\r
195          * Generates a platform-specific query so that a table can be altered\r
196          * Called by add_column(), drop_column(), and column_alter(),\r
197          *\r
198          * @access      private\r
199          * @param       string  the ALTER type (ADD, DROP, CHANGE)\r
200          * @param       string  the column name\r
201          * @param       string  the table name\r
202          * @param       string  the column definition\r
203          * @param       string  the default value\r
204          * @param       boolean should 'NOT NULL' be added\r
205          * @param       string  the field after which we should add the new field\r
206          * @return      object\r
207          */\r
208         function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')\r
209         {\r
210                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);\r
211 \r
212                 // DROP has everything it needs now.\r
213                 if ($alter_type == 'DROP')\r
214                 {\r
215                         return $sql;\r
216                 }\r
217 \r
218                 $sql .= " $column_definition";\r
219 \r
220                 if ($default_value != '')\r
221                 {\r
222                         $sql .= " DEFAULT \"$default_value\"";\r
223                 }\r
224 \r
225                 if ($null === NULL)\r
226                 {\r
227                         $sql .= ' NULL';\r
228                 }\r
229                 else\r
230                 {\r
231                         $sql .= ' NOT NULL';\r
232                 }\r
233 \r
234                 if ($after_field != '')\r
235                 {\r
236                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);\r
237                 }\r
238                 \r
239                 return $sql;\r
240                 \r
241         }\r
242 \r
243 \r
244         // --------------------------------------------------------------------\r
245 \r
246         /**\r
247          * Rename a table\r
248          *\r
249          * Generates a platform-specific query so that a table can be renamed\r
250          *\r
251          * @access      private\r
252          * @param       string  the old table name\r
253          * @param       string  the new table name\r
254          * @return      string\r
255          */\r
256         function _rename_table($table_name, $new_table_name)\r
257         {\r
258                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);\r
259                 return $sql;\r
260         }\r
261 \r
262 \r
263 }\r
264 \r
265 /* End of file odbc_forge.php */\r
266 /* Location: ./system/database/drivers/odbc/odbc_forge.php */