blob: fed8141b145f19a7edb61d9864612d89604c5c5f [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 Andreevd947eba2012-04-09 14:58:28 +030038 protected $_create_database = FALSE;
39 protected $_drop_database = FALSE;
40 protected $_drop_table = 'DROP TABLE %s';
Derek Allard2067d1a2008-11-13 22:59:24 +000041
42 /**
43 * Create Table
44 *
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @param string the table name
46 * @param array the fields
47 * @param mixed primary key(s)
48 * @param mixed key(s)
Andrey Andreevaa786c92012-01-16 12:14:45 +020049 * @param bool should 'IF NOT EXISTS' be added to the SQL
Andrey Andreev24abcb92012-01-05 20:40:15 +020050 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000051 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +030052 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
54 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 if ($if_not_exists === TRUE)
57 {
58 $sql .= 'IF NOT EXISTS ';
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Andrey Andreevea09a8a2012-04-06 20:50:07 +030061 $sql .= $this->db->escape_identifiers($table).' (';
Derek Allard2067d1a2008-11-13 22:59:24 +000062 $current_field_count = 0;
63
Andrey Andreev8f220572012-03-02 13:05:45 +020064 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
66 // Numeric field names aren't allowed in databases, so if the key is
67 // numeric, we know it was assigned by PHP and the developer manually
68 // entered the field information, so we'll simply add it to the list
69 if (is_numeric($field))
70 {
Andrey Andreevaa786c92012-01-16 12:14:45 +020071 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000072 }
73 else
74 {
75 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreev5ef194d2012-06-08 01:12:54 +030077 $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
78
79 if (isset($attributes['UNSINGED']) && $attributes['UNSIGNED'] === TRUE)
80 {
81 $sql .= ' UNSIGNED';
82 }
83
84 if (isset($attributes['DEFAULT']))
85 {
86 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
87 }
88
89 $sql .= (isset($attributes['NULL']) && $attributes['NULL'] === TRUE)
90 ? '' : ' NOT NULL';
91
92 empty($attributes['CONSTRAINT']) OR ' CONSTRAINT '.$attributes['CONSTRAINT'];
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // don't add a comma on the end of the last field
96 if (++$current_field_count < count($fields))
97 {
98 $sql .= ',';
99 }
100 }
101
102 if (count($primary_keys) > 0)
103 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300104 $sql .= ",\n\tCONSTRAINT ".$table.' PRIMARY KEY ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
106
107 if (is_array($keys) && count($keys) > 0)
108 {
109 foreach ($keys as $key)
110 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300111 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300112 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300113 : array($this->db->escape_identifiers($key));
Barry Mienydd671972010-10-04 16:33:58 +0200114
Andrey Andreevaa786c92012-01-16 12:14:45 +0200115 $sql .= ",\n\tUNIQUE COLUMNS (".implode(', ', $key).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Andrey Andreevaa786c92012-01-16 12:14:45 +0200119 return $sql."\n)";
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121
122 // --------------------------------------------------------------------
123
124 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * Alter table query
126 *
127 * Generates a platform-specific query so that a table can be altered
128 * Called by add_column(), drop_column(), and column_alter(),
129 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * @param string the ALTER type (ADD, DROP, CHANGE)
131 * @param string the column name
132 * @param string the table name
133 * @param string the column definition
134 * @param string the default value
Andrey Andreevaa786c92012-01-16 12:14:45 +0200135 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @param string the field after which we should add the new field
Andrey Andreev24abcb92012-01-05 20:40:15 +0200137 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300139 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300141 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 // DROP has everything it needs now.
Andrey Andreevaa786c92012-01-16 12:14:45 +0200144 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return $sql;
147 }
148
Andrey Andreevaa786c92012-01-16 12:14:45 +0200149 return $sql.' '.$column_definition
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100150 .($default_value !== '' ? ' DEFAULT "'.$default_value.'"' : '')
Andrey Andreevaa786c92012-01-16 12:14:45 +0200151 .($null === NULL ? ' NULL' : ' NOT NULL')
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300152 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156}
157
158/* End of file oci8_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400159/* Location: ./system/database/drivers/oci8/oci8_forge.php */