blob: 645274232972cde68b1dcddb54944391d933609a [file] [log] [blame]
Alex Bilbie84445d02011-03-10 16:43:39 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000031 * SQLSRV Forge Class
Alex Bilbie84445d02011-03-10 16:43:39 +000032 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000037class CI_DB_sqlsrv_forge extends CI_DB_forge {
Alex Bilbie84445d02011-03-10 16:43:39 +000038
39 /**
40 * Create database
41 *
Alex Bilbie84445d02011-03-10 16:43:39 +000042 * @param string the database name
43 * @return bool
44 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040045 protected function _create_database($name)
Alex Bilbie84445d02011-03-10 16:43:39 +000046 {
47 return "CREATE DATABASE ".$name;
48 }
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Drop database
54 *
Alex Bilbie84445d02011-03-10 16:43:39 +000055 * @param string the database name
56 * @return bool
57 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040058 protected function _drop_database($name)
Alex Bilbie84445d02011-03-10 16:43:39 +000059 {
60 return "DROP DATABASE ".$name;
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
66 * Drop Table
67 *
Alex Bilbie84445d02011-03-10 16:43:39 +000068 * @return bool
69 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040070 protected function _drop_table($table)
Alex Bilbie84445d02011-03-10 16:43:39 +000071 {
72 return "DROP TABLE ".$this->db->_escape_identifiers($table);
73 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Create Table
79 *
Alex Bilbie84445d02011-03-10 16:43:39 +000080 * @param string the table name
81 * @param array the fields
82 * @param mixed primary key(s)
83 * @param mixed key(s)
84 * @param boolean should 'IF NOT EXISTS' be added to the SQL
85 * @return bool
86 */
Timothy Warren5137ebc2012-03-19 19:05:25 -040087 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Alex Bilbie84445d02011-03-10 16:43:39 +000088 {
89 $sql = 'CREATE TABLE ';
90
91 if ($if_not_exists === TRUE)
92 {
93 $sql .= 'IF NOT EXISTS ';
94 }
95
96 $sql .= $this->db->_escape_identifiers($table)." (";
97 $current_field_count = 0;
98
99 foreach ($fields as $field=>$attributes)
100 {
101 // Numeric field names aren't allowed in databases, so if the key is
102 // numeric, we know it was assigned by PHP and the developer manually
103 // entered the field information, so we'll simply add it to the list
104 if (is_numeric($field))
105 {
106 $sql .= "\n\t$attributes";
107 }
108 else
109 {
110 $attributes = array_change_key_case($attributes, CASE_UPPER);
111
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200112 $sql .= "\n\t".$this->db->protect_identifiers($field);
Alex Bilbie84445d02011-03-10 16:43:39 +0000113
114 $sql .= ' '.$attributes['TYPE'];
115
116 if (array_key_exists('CONSTRAINT', $attributes))
117 {
118 $sql .= '('.$attributes['CONSTRAINT'].')';
119 }
120
121 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
122 {
123 $sql .= ' UNSIGNED';
124 }
125
126 if (array_key_exists('DEFAULT', $attributes))
127 {
128 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
129 }
130
131 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
132 {
133 $sql .= ' NULL';
134 }
135 else
136 {
137 $sql .= ' NOT NULL';
138 }
139
140 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
141 {
142 $sql .= ' AUTO_INCREMENT';
143 }
144 }
145
146 // don't add a comma on the end of the last field
147 if (++$current_field_count < count($fields))
148 {
149 $sql .= ',';
150 }
151 }
152
153 if (count($primary_keys) > 0)
154 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200155 $primary_keys = $this->db->protect_identifiers($primary_keys);
Alex Bilbie84445d02011-03-10 16:43:39 +0000156 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
157 }
158
159 if (is_array($keys) && count($keys) > 0)
160 {
161 foreach ($keys as $key)
162 {
163 if (is_array($key))
164 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200165 $key = $this->db->protect_identifiers($key);
Alex Bilbie84445d02011-03-10 16:43:39 +0000166 }
167 else
168 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200169 $key = array($this->db->protect_identifiers($key));
Alex Bilbie84445d02011-03-10 16:43:39 +0000170 }
171
172 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
173 }
174 }
175
176 $sql .= "\n)";
177
178 return $sql;
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Alter table query
185 *
186 * Generates a platform-specific query so that a table can be altered
187 * Called by add_column(), drop_column(), and column_alter(),
188 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000189 * @param string the ALTER type (ADD, DROP, CHANGE)
190 * @param string the column name
191 * @param string the table name
192 * @param string the column definition
193 * @param string the default value
194 * @param boolean should 'NOT NULL' be added
195 * @param string the field after which we should add the new field
196 * @return object
197 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400198 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000199 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200200 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Alex Bilbie84445d02011-03-10 16:43:39 +0000201
202 // DROP has everything it needs now.
203 if ($alter_type == 'DROP')
204 {
205 return $sql;
206 }
207
208 $sql .= " $column_definition";
209
210 if ($default_value != '')
211 {
212 $sql .= " DEFAULT \"$default_value\"";
213 }
214
215 if ($null === NULL)
216 {
217 $sql .= ' NULL';
218 }
219 else
220 {
221 $sql .= ' NOT NULL';
222 }
223
224 if ($after_field != '')
225 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200226 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 }
228
229 return $sql;
230
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Rename a table
237 *
238 * Generates a platform-specific query so that a table can be renamed
239 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000240 * @param string the old table name
241 * @param string the new table name
242 * @return string
243 */
Timothy Warren5137ebc2012-03-19 19:05:25 -0400244 protected function _rename_table($table_name, $new_table_name)
Alex Bilbie84445d02011-03-10 16:43:39 +0000245 {
246 // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200247 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 +0000248 }
249
250}
251
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200252/* End of file sqlsrv_forge.php */
Timothy Warrenb4172692012-03-19 18:34:11 -0400253/* Location: ./system/database/drivers/sqlsrv/sqlsrv_forge.php */