blob: 49e5c3e725a48f7415da896da2b076f3ca0929bc [file] [log] [blame]
Andrey Andreevb76029d2012-01-26 15:13:19 +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 Andreevb76029d2012-01-26 15:13:19 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevb76029d2012-01-26 15:13:19 +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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * ODBC 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/database/
34 */
35class CI_DB_odbc_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 Andreevb76029d2012-01-26 15:13:19 +020043 public function _create_database()
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
45 // ODBC has no "create database" command since it's
46 // designed to connect to an existing database
Andrey Andreevb76029d2012-01-26 15:13:19 +020047 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000048 }
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Drop database
54 *
Derek Allard2067d1a2008-11-13 22:59:24 +000055 * @param string the database name
56 * @return bool
57 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020058 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
60 // ODBC has no "drop database" command since it's
Barry Mienydd671972010-10-04 16:33:58 +020061 // designed to connect to an existing database
Andrey Andreevb76029d2012-01-26 15:13:19 +020062 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000063 }
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
75 * @return bool
76 */
Andrey Andreevb76029d2012-01-26 15:13:19 +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
Andrey Andreevb76029d2012-01-26 15:13:19 +020086 $sql .= $this->db->_escape_identifiers($table).' (';
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $current_field_count = 0;
88
Andrey Andreevb76029d2012-01-26 15:13:19 +020089 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
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 {
Andrey Andreevb76029d2012-01-26 15:13:19 +020096 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
98 else
99 {
100 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200101
Andrey Andreevb76029d2012-01-26 15:13:19 +0200102 $sql .= "\n\t".$this->db->protect_identifiers($field)
103 .' '.$attributes['TYPE']
104 .( ! empty($attributes['CONSTRAINT']) ? '('.$attributes['CONSTRAINT'].')' : '')
105 .(( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
106 .(isset($attributes['DEFAULT'], $attributes) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
107 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
108 .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // don't add a comma on the end of the last field
112 if (++$current_field_count < count($fields))
113 {
114 $sql .= ',';
115 }
116 }
117
118 if (count($primary_keys) > 0)
119 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200120 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->protect_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 if (is_array($keys) && count($keys) > 0)
124 {
125 foreach ($keys as $key)
126 {
127 if (is_array($key))
128 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200129 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131 else
132 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200133 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Andrey Andreevb76029d2012-01-26 15:13:19 +0200136 $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Andrey Andreevb76029d2012-01-26 15:13:19 +0200140 return $sql."\n)";
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Drop Table
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @return bool
149 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200150 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Barry Mienydd671972010-10-04 16:33:58 +0200152 // Not a supported ODBC feature
Andrey Andreevb76029d2012-01-26 15:13:19 +0200153 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Alter table query
160 *
161 * Generates a platform-specific query so that a table can be altered
162 * Called by add_column(), drop_column(), and column_alter(),
163 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 * @param string the ALTER type (ADD, DROP, CHANGE)
165 * @param string the column name
166 * @param string the table name
167 * @param string the column definition
168 * @param string the default value
Andrey Andreevb76029d2012-01-26 15:13:19 +0200169 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string the field after which we should add the new field
Andrey Andreevb76029d2012-01-26 15:13:19 +0200171 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200173 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200175 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176
177 // DROP has everything it needs now.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200178 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 return $sql;
181 }
182
Andrey Andreevb76029d2012-01-26 15:13:19 +0200183 return $sql.' '.$column_definition
184 .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '')
185 .($null === NULL ? ' NULL' : ' NOT NULL')
186 .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Rename a table
194 *
195 * Generates a platform-specific query so that a table can be renamed
196 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @param string the old table name
198 * @param string the new table name
199 * @return string
200 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200201 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200203 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 +0000204 }
205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206}
207
208/* End of file odbc_forge.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200209/* Location: ./system/database/drivers/odbc/odbc_forge.php */