blob: ad578f830d60cb38eb16dad9909f9ef73179099b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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 Andreev342a4662012-01-24 15:24:00 +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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Oracle Forge Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_oci8_forge extends CI_DB_forge {
37
Andrey Andreevc98e93a2012-11-02 02:04:59 +020038 /**
39 * CREATE DATABASE statement
40 *
41 * @var string
42 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030043 protected $_create_database = FALSE;
Andrey Andreevc98e93a2012-11-02 02:04:59 +020044
45 /**
46 * DROP DATABASE statement
47 *
48 * @var string
49 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030050 protected $_drop_database = FALSE;
Andrey Andreevc98e93a2012-11-02 02:04:59 +020051
52 /**
53 * DROP TABLE statement
54 *
55 * @var string
56 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030057 protected $_drop_table = 'DROP TABLE %s';
Derek Allard2067d1a2008-11-13 22:59:24 +000058
Andrey Andreevc98e93a2012-11-02 02:04:59 +020059 // --------------------------------------------------------------------
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061 /**
62 * Create Table
63 *
Derek Allard2067d1a2008-11-13 22:59:24 +000064 * @param string the table name
65 * @param array the fields
66 * @param mixed primary key(s)
67 * @param mixed key(s)
Andrey Andreevaa786c92012-01-16 12:14:45 +020068 * @param bool should 'IF NOT EXISTS' be added to the SQL
Andrey Andreev24abcb92012-01-05 20:40:15 +020069 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000070 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +030071 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
73 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 if ($if_not_exists === TRUE)
76 {
77 $sql .= 'IF NOT EXISTS ';
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Andrey Andreevea09a8a2012-04-06 20:50:07 +030080 $sql .= $this->db->escape_identifiers($table).' (';
Derek Allard2067d1a2008-11-13 22:59:24 +000081 $current_field_count = 0;
82
Andrey Andreev8f220572012-03-02 13:05:45 +020083 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
85 // Numeric field names aren't allowed in databases, so if the key is
86 // numeric, we know it was assigned by PHP and the developer manually
87 // entered the field information, so we'll simply add it to the list
88 if (is_numeric($field))
89 {
Andrey Andreevaa786c92012-01-16 12:14:45 +020090 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
92 else
93 {
94 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020095
Andrey Andreev5ef194d2012-06-08 01:12:54 +030096 $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
97
98 if (isset($attributes['UNSINGED']) && $attributes['UNSIGNED'] === TRUE)
99 {
100 $sql .= ' UNSIGNED';
101 }
102
103 if (isset($attributes['DEFAULT']))
104 {
105 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
106 }
107
108 $sql .= (isset($attributes['NULL']) && $attributes['NULL'] === TRUE)
109 ? '' : ' NOT NULL';
110
111 empty($attributes['CONSTRAINT']) OR ' CONSTRAINT '.$attributes['CONSTRAINT'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // don't add a comma on the end of the last field
115 if (++$current_field_count < count($fields))
116 {
117 $sql .= ',';
118 }
119 }
120
121 if (count($primary_keys) > 0)
122 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300123 $sql .= ",\n\tCONSTRAINT ".$table.' PRIMARY KEY ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
125
126 if (is_array($keys) && count($keys) > 0)
127 {
128 foreach ($keys as $key)
129 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300130 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300131 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300132 : array($this->db->escape_identifiers($key));
Barry Mienydd671972010-10-04 16:33:58 +0200133
Andrey Andreevaa786c92012-01-16 12:14:45 +0200134 $sql .= ",\n\tUNIQUE COLUMNS (".implode(', ', $key).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Andrey Andreevaa786c92012-01-16 12:14:45 +0200138 return $sql."\n)";
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * Alter table query
145 *
146 * Generates a platform-specific query so that a table can be altered
147 * Called by add_column(), drop_column(), and column_alter(),
148 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 * @param string the ALTER type (ADD, DROP, CHANGE)
150 * @param string the column name
151 * @param string the table name
152 * @param string the column definition
153 * @param string the default value
Andrey Andreevaa786c92012-01-16 12:14:45 +0200154 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param string the field after which we should add the new field
Andrey Andreev24abcb92012-01-05 20:40:15 +0200156 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300158 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300160 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161
162 // DROP has everything it needs now.
Andrey Andreevaa786c92012-01-16 12:14:45 +0200163 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return $sql;
166 }
167
Andrey Andreevaa786c92012-01-16 12:14:45 +0200168 return $sql.' '.$column_definition
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100169 .($default_value !== '' ? ' DEFAULT "'.$default_value.'"' : '')
Andrey Andreevaa786c92012-01-16 12:14:45 +0200170 .($null === NULL ? ' NULL' : ' NOT NULL')
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300171 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175}
176
177/* End of file oci8_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400178/* Location: ./system/database/drivers/oci8/oci8_forge.php */