blob: 377dcf154852a4a4584684f6415983e7adc6fcb7 [file] [log] [blame]
Andrey Andreev4d116712012-03-20 16:30:57 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +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
Alex Bilbie84445d02011-03-10 16:43:39 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4d116712012-03-20 16:30:57 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4d116712012-03-20 16:30:57 +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 *
Alex Bilbie84445d02011-03-10 16:43:39 +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)
Alex Bilbie84445d02011-03-10 16:43:39 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Alex Bilbie84445d02011-03-10 16:43:39 +000028/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000029 * SQLSRV Forge Class
Alex Bilbie84445d02011-03-10 16:43:39 +000030 *
31 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000033 * @link http://codeigniter.com/user_guide/database/
34 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000035class CI_DB_sqlsrv_forge extends CI_DB_forge {
Alex Bilbie84445d02011-03-10 16:43:39 +000036
37 /**
38 * Create database
39 *
Alex Bilbie84445d02011-03-10 16:43:39 +000040 * @param string the database name
Andrey Andreev4d116712012-03-20 16:30:57 +020041 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +000042 */
Andrey Andreev4d116712012-03-20 16:30:57 +020043 public function _create_database($name)
Alex Bilbie84445d02011-03-10 16:43:39 +000044 {
45 return "CREATE DATABASE ".$name;
46 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Drop database
52 *
Alex Bilbie84445d02011-03-10 16:43:39 +000053 * @param string the database name
54 * @return bool
55 */
Andrey Andreev4d116712012-03-20 16:30:57 +020056 public function _drop_database($name)
Alex Bilbie84445d02011-03-10 16:43:39 +000057 {
58 return "DROP DATABASE ".$name;
59 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Drop Table
65 *
Andrey Andreevea09a8a2012-04-06 20:50:07 +030066 * @param string table name
67 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +000068 */
Andrey Andreev4d116712012-03-20 16:30:57 +020069 public function _drop_table($table)
Alex Bilbie84445d02011-03-10 16:43:39 +000070 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +030071 return 'DROP TABLE '.$this->db->escape_identifiers($table);
Alex Bilbie84445d02011-03-10 16:43:39 +000072 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * Create Table
78 *
Alex Bilbie84445d02011-03-10 16:43:39 +000079 * @param string the table name
80 * @param array the fields
81 * @param mixed primary key(s)
82 * @param mixed key(s)
Andrey Andreev4d116712012-03-20 16:30:57 +020083 * @param bool should 'IF NOT EXISTS' be added to the SQL
Alex Bilbie84445d02011-03-10 16:43:39 +000084 * @return bool
85 */
Andrey Andreev4d116712012-03-20 16:30:57 +020086 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Alex Bilbie84445d02011-03-10 16:43:39 +000087 {
88 $sql = 'CREATE TABLE ';
89
90 if ($if_not_exists === TRUE)
91 {
92 $sql .= 'IF NOT EXISTS ';
93 }
94
Andrey Andreevea09a8a2012-04-06 20:50:07 +030095 $sql .= $this->db->escape_identifiers($table).' (';
Alex Bilbie84445d02011-03-10 16:43:39 +000096 $current_field_count = 0;
97
Andrey Andreev4d116712012-03-20 16:30:57 +020098 foreach ($fields as $field => $attributes)
Alex Bilbie84445d02011-03-10 16:43:39 +000099 {
100 // Numeric field names aren't allowed in databases, so if the key is
101 // numeric, we know it was assigned by PHP and the developer manually
102 // entered the field information, so we'll simply add it to the list
103 if (is_numeric($field))
104 {
105 $sql .= "\n\t$attributes";
106 }
107 else
108 {
109 $attributes = array_change_key_case($attributes, CASE_UPPER);
110
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200111 $sql .= "\n\t".$this->db->protect_identifiers($field);
Alex Bilbie84445d02011-03-10 16:43:39 +0000112
113 $sql .= ' '.$attributes['TYPE'];
114
115 if (array_key_exists('CONSTRAINT', $attributes))
116 {
117 $sql .= '('.$attributes['CONSTRAINT'].')';
118 }
119
120 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
121 {
122 $sql .= ' UNSIGNED';
123 }
124
125 if (array_key_exists('DEFAULT', $attributes))
126 {
127 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
128 }
129
130 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
131 {
132 $sql .= ' NULL';
133 }
134 else
135 {
136 $sql .= ' NOT NULL';
137 }
138
139 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
140 {
141 $sql .= ' AUTO_INCREMENT';
142 }
143 }
144
145 // don't add a comma on the end of the last field
146 if (++$current_field_count < count($fields))
147 {
148 $sql .= ',';
149 }
150 }
151
152 if (count($primary_keys) > 0)
153 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200154 $primary_keys = $this->db->protect_identifiers($primary_keys);
Alex Bilbie84445d02011-03-10 16:43:39 +0000155 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
156 }
157
158 if (is_array($keys) && count($keys) > 0)
159 {
160 foreach ($keys as $key)
161 {
162 if (is_array($key))
163 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200164 $key = $this->db->protect_identifiers($key);
Alex Bilbie84445d02011-03-10 16:43:39 +0000165 }
166 else
167 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200168 $key = array($this->db->protect_identifiers($key));
Alex Bilbie84445d02011-03-10 16:43:39 +0000169 }
170
171 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
172 }
173 }
174
175 $sql .= "\n)";
176
177 return $sql;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Alter table query
184 *
185 * Generates a platform-specific query so that a table can be altered
186 * Called by add_column(), drop_column(), and column_alter(),
187 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000188 * @param string the ALTER type (ADD, DROP, CHANGE)
189 * @param string the column name
190 * @param string the table name
191 * @param string the column definition
192 * @param string the default value
Andrey Andreev4d116712012-03-20 16:30:57 +0200193 * @param bool should 'NOT NULL' be added
Alex Bilbie84445d02011-03-10 16:43:39 +0000194 * @param string the field after which we should add the new field
Andrey Andreev4d116712012-03-20 16:30:57 +0200195 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000196 */
Andrey Andreev4d116712012-03-20 16:30:57 +0200197 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000198 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200199 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Alex Bilbie84445d02011-03-10 16:43:39 +0000200
201 // DROP has everything it needs now.
202 if ($alter_type == 'DROP')
203 {
204 return $sql;
205 }
206
207 $sql .= " $column_definition";
208
209 if ($default_value != '')
210 {
211 $sql .= " DEFAULT \"$default_value\"";
212 }
213
214 if ($null === NULL)
215 {
216 $sql .= ' NULL';
217 }
218 else
219 {
220 $sql .= ' NOT NULL';
221 }
222
223 if ($after_field != '')
224 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200225 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Alex Bilbie84445d02011-03-10 16:43:39 +0000226 }
227
228 return $sql;
229
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Rename a table
236 *
237 * Generates a platform-specific query so that a table can be renamed
238 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000239 * @param string the old table name
240 * @param string the new table name
241 * @return string
242 */
Andrey Andreev4d116712012-03-20 16:30:57 +0200243 public function _rename_table($table_name, $new_table_name)
Alex Bilbie84445d02011-03-10 16:43:39 +0000244 {
245 // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200246 return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name);
Alex Bilbie84445d02011-03-10 16:43:39 +0000247 }
248
249}
250
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200251/* End of file sqlsrv_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400252/* Location: ./system/database/drivers/sqlsrv/sqlsrv_forge.php */