blob: 6975f2a5f5b8b45c1469888d54020177f47df5b2 [file] [log] [blame]
Andrey Andreev24abcb92012-01-05 20:40:15 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev24abcb92012-01-05 20:40:15 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24abcb92012-01-05 20:40:15 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Oracle Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_oci8_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
Derek Allard2067d1a2008-11-13 22:59:24 +000042 * @param string the database name
43 * @return bool
44 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020045 public function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000046 {
Andrey Andreev24abcb92012-01-05 20:40:15 +020047 // Not supported - schemas in Oracle are actual usernames
Derek Allard2067d1a2008-11-13 22:59:24 +000048 return FALSE;
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Drop database
55 *
Derek Allard2067d1a2008-11-13 22:59:24 +000056 * @param string the database name
57 * @return bool
58 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020059 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
Andrey Andreev24abcb92012-01-05 20:40:15 +020061 // Not supported - schemas in Oracle are actual usernames
Derek Allard2067d1a2008-11-13 22:59:24 +000062 return FALSE;
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Create Table
69 *
Derek Allard2067d1a2008-11-13 22:59:24 +000070 * @param string the table name
71 * @param array the fields
72 * @param mixed primary key(s)
73 * @param mixed key(s)
74 * @param boolean should 'IF NOT EXISTS' be added to the SQL
Andrey Andreev24abcb92012-01-05 20:40:15 +020075 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000076 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020077 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
79 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 if ($if_not_exists === TRUE)
82 {
83 $sql .= 'IF NOT EXISTS ';
84 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 $sql .= $this->db->_escape_identifiers($table)." (";
87 $current_field_count = 0;
88
89 foreach ($fields as $field=>$attributes)
90 {
91 // Numeric field names aren't allowed in databases, so if the key is
92 // numeric, we know it was assigned by PHP and the developer manually
93 // entered the field information, so we'll simply add it to the list
94 if (is_numeric($field))
95 {
96 $sql .= "\n\t$attributes";
97 }
98 else
99 {
100 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200101
Andrey Andreev24abcb92012-01-05 20:40:15 +0200102 $sql .= "\n\t".$this->db->_protect_identifiers($field).' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 if (array_key_exists('CONSTRAINT', $attributes))
105 {
106 $sql .= '('.$attributes['CONSTRAINT'].')';
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
110 {
111 $sql .= ' UNSIGNED';
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if (array_key_exists('DEFAULT', $attributes))
115 {
116 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
120 {
121 $sql .= ' NULL';
122 }
123 else
124 {
Barry Mienydd671972010-10-04 16:33:58 +0200125 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 // don't add a comma on the end of the last field
130 if (++$current_field_count < count($fields))
131 {
132 $sql .= ',';
133 }
134 }
135
136 if (count($primary_keys) > 0)
137 {
138 $primary_keys = $this->db->_protect_identifiers($primary_keys);
139 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
140 }
141
142 if (is_array($keys) && count($keys) > 0)
143 {
144 foreach ($keys as $key)
145 {
146 if (is_array($key))
147 {
Barry Mienydd671972010-10-04 16:33:58 +0200148 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150 else
151 {
152 $key = array($this->db->_protect_identifiers($key));
153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $sql .= ",\n\tUNIQUE COLUMNS (" . implode(', ', $key) . ")";
156 }
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 $sql .= "\n)";
160
161 return $sql;
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Drop Table
168 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200169 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200171 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200173 return 'DROP TABLE ' . $this->db->_protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Alter table query
180 *
181 * Generates a platform-specific query so that a table can be altered
182 * Called by add_column(), drop_column(), and column_alter(),
183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @param string the ALTER type (ADD, DROP, CHANGE)
185 * @param string the column name
186 * @param string the table name
187 * @param string the column definition
188 * @param string the default value
189 * @param boolean should 'NOT NULL' be added
190 * @param string the field after which we should add the new field
Andrey Andreev24abcb92012-01-05 20:40:15 +0200191 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200193 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
196
197 // DROP has everything it needs now.
198 if ($alter_type == 'DROP')
199 {
200 return $sql;
201 }
202
203 $sql .= " $column_definition";
204
205 if ($default_value != '')
206 {
207 $sql .= " DEFAULT \"$default_value\"";
208 }
209
210 if ($null === NULL)
211 {
212 $sql .= ' NULL';
213 }
214 else
215 {
216 $sql .= ' NOT NULL';
217 }
218
219 if ($after_field != '')
220 {
221 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Rename a table
232 *
233 * Generates a platform-specific query so that a table can be renamed
234 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * @param string the old table name
236 * @param string the new table name
237 * @return string
238 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200239 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
242 return $sql;
243 }
244
245
246}
247
248/* End of file oci8_forge.php */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200249/* Location: ./system/database/drivers/oci8/oci8_forge.php */