blob: 6b9a8e8fd89123fcf619d7629d4c1c00d1be7631 [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
Andrey Andreevaa786c92012-01-16 12:14:45 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Oracle Forge Class
30 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
35class CI_DB_oci8_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @param string the database name
41 * @return bool
42 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020043 public function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
Andrey Andreev24abcb92012-01-05 20:40:15 +020045 // Not supported - schemas in Oracle are actual usernames
Derek Allard2067d1a2008-11-13 22:59:24 +000046 return FALSE;
47 }
48
49 // --------------------------------------------------------------------
50
51 /**
52 * Drop database
53 *
Derek Allard2067d1a2008-11-13 22:59:24 +000054 * @param string the database name
55 * @return bool
56 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020057 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
Andrey Andreev24abcb92012-01-05 20:40:15 +020059 // Not supported - schemas in Oracle are actual usernames
Derek Allard2067d1a2008-11-13 22:59:24 +000060 return FALSE;
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
66 * Create Table
67 *
Derek Allard2067d1a2008-11-13 22:59:24 +000068 * @param string the table name
69 * @param array the fields
70 * @param mixed primary key(s)
71 * @param mixed key(s)
Andrey Andreevaa786c92012-01-16 12:14:45 +020072 * @param bool should 'IF NOT EXISTS' be added to the SQL
Andrey Andreev24abcb92012-01-05 20:40:15 +020073 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000074 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020075 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
77 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 if ($if_not_exists === TRUE)
80 {
81 $sql .= 'IF NOT EXISTS ';
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Andrey Andreevaa786c92012-01-16 12:14:45 +020084 $sql .= $this->db->_escape_identifiers($table).' (';
Derek Allard2067d1a2008-11-13 22:59:24 +000085 $current_field_count = 0;
86
87 foreach ($fields as $field=>$attributes)
88 {
89 // Numeric field names aren't allowed in databases, so if the key is
90 // numeric, we know it was assigned by PHP and the developer manually
91 // entered the field information, so we'll simply add it to the list
92 if (is_numeric($field))
93 {
Andrey Andreevaa786c92012-01-16 12:14:45 +020094 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
96 else
97 {
98 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020099
Andrey Andreevaa786c92012-01-16 12:14:45 +0200100 $sql .= "\n\t".$this->db->_protect_identifiers($field).' '.$attributes['TYPE']
101 .(array_key_exists('CONSTRAINT', $attributes) ? '('.$attributes['CONSTRAINT'].')' : '')
102 .((array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
103 .(array_key_exists('DEFAULT', $attributes) ? ' DEFAULT \''.$attributes['DEFAULT'].'\'' : '')
104 .((array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL');
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // don't add a comma on the end of the last field
108 if (++$current_field_count < count($fields))
109 {
110 $sql .= ',';
111 }
112 }
113
114 if (count($primary_keys) > 0)
115 {
116 $primary_keys = $this->db->_protect_identifiers($primary_keys);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200117 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119
120 if (is_array($keys) && count($keys) > 0)
121 {
122 foreach ($keys as $key)
123 {
124 if (is_array($key))
125 {
Barry Mienydd671972010-10-04 16:33:58 +0200126 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
128 else
129 {
130 $key = array($this->db->_protect_identifiers($key));
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Andrey Andreevaa786c92012-01-16 12:14:45 +0200133 $sql .= ",\n\tUNIQUE COLUMNS (".implode(', ', $key).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Andrey Andreevaa786c92012-01-16 12:14:45 +0200137 return $sql."\n)";
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Drop Table
144 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200145 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200147 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200149 return 'DROP TABLE '.$this->db->_protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Alter table query
156 *
157 * Generates a platform-specific query so that a table can be altered
158 * Called by add_column(), drop_column(), and column_alter(),
159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @param string the ALTER type (ADD, DROP, CHANGE)
161 * @param string the column name
162 * @param string the table name
163 * @param string the column definition
164 * @param string the default value
Andrey Andreevaa786c92012-01-16 12:14:45 +0200165 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string the field after which we should add the new field
Andrey Andreev24abcb92012-01-05 20:40:15 +0200167 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200169 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200171 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table).' '.$alter_type.' '.$this->db->_protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
173 // DROP has everything it needs now.
Andrey Andreevaa786c92012-01-16 12:14:45 +0200174 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 return $sql;
177 }
178
Andrey Andreevaa786c92012-01-16 12:14:45 +0200179 return $sql.' '.$column_definition
180 .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '')
181 .($null === NULL ? ' NULL' : ' NOT NULL')
182 .($after_field != '' ? ' AFTER '.$this->db->_protect_identifiers($after_field) : '');
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Rename a table
190 *
191 * Generates a platform-specific query so that a table can be renamed
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string the old table name
194 * @param string the new table name
195 * @return string
196 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200197 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200199 return 'ALTER TABLE '.$this->db->_protect_identifiers($table_name).' RENAME TO '.$this->db->_protect_identifiers($new_table_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202}
203
204/* End of file oci8_forge.php */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200205/* Location: ./system/database/drivers/oci8/oci8_forge.php */