blob: 0a893f68f8b147f43021857dc9e1a4d8234a5f3a [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * MS SQL Forge Class
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_mssql_forge extends CI_DB_forge {
38
39 /**
40 * Create database
41 *
42 * @access private
43 * @param string the database name
44 * @return bool
45 */
46 function _create_database($name)
47 {
48 return "CREATE DATABASE ".$name;
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Drop database
55 *
56 * @access private
57 * @param string the database name
58 * @return bool
59 */
60 function _drop_database($name)
61 {
62 return "DROP DATABASE ".$name;
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Drop Table
69 *
70 * @access private
71 * @return bool
72 */
73 function _drop_table($table)
74 {
75 return "DROP TABLE ".$this->db->_escape_identifiers($table);
76 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Create Table
82 *
83 * @access private
84 * @param string the table name
85 * @param array the fields
86 * @param mixed primary key(s)
87 * @param mixed key(s)
88 * @param boolean should 'IF NOT EXISTS' be added to the SQL
89 * @return bool
90 */
91 function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
92 {
93 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 if ($if_not_exists === TRUE)
96 {
97 $sql .= 'IF NOT EXISTS ';
98 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 $sql .= $this->db->_escape_identifiers($table)." (";
101 $current_field_count = 0;
102
103 foreach ($fields as $field=>$attributes)
104 {
105 // Numeric field names aren't allowed in databases, so if the key is
106 // numeric, we know it was assigned by PHP and the developer manually
107 // entered the field information, so we'll simply add it to the list
108 if (is_numeric($field))
109 {
110 $sql .= "\n\t$attributes";
111 }
112 else
113 {
114 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 $sql .= "\n\t".$this->db->_protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Jones37f4b9c2011-07-01 17:56:50 -0500118 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if (array_key_exists('CONSTRAINT', $attributes))
121 {
122 $sql .= '('.$attributes['CONSTRAINT'].')';
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
126 {
127 $sql .= ' UNSIGNED';
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if (array_key_exists('DEFAULT', $attributes))
131 {
132 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
136 {
137 $sql .= ' NULL';
138 }
139 else
140 {
Barry Mienydd671972010-10-04 16:33:58 +0200141 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
145 {
146 $sql .= ' AUTO_INCREMENT';
147 }
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // don't add a comma on the end of the last field
151 if (++$current_field_count < count($fields))
152 {
153 $sql .= ',';
154 }
155 }
156
157 if (count($primary_keys) > 0)
158 {
159 $primary_keys = $this->db->_protect_identifiers($primary_keys);
160 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 if (is_array($keys) && count($keys) > 0)
164 {
165 foreach ($keys as $key)
166 {
167 if (is_array($key))
168 {
Barry Mienydd671972010-10-04 16:33:58 +0200169 $key = $this->db->_protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171 else
172 {
173 $key = array($this->db->_protect_identifiers($key));
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
177 }
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $sql .= "\n)";
181
182 return $sql;
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Alter table query
189 *
190 * Generates a platform-specific query so that a table can be altered
191 * Called by add_column(), drop_column(), and column_alter(),
192 *
193 * @access private
194 * @param string the ALTER type (ADD, DROP, CHANGE)
195 * @param string the column name
196 * @param string the table name
197 * @param string the column definition
198 * @param string the default value
199 * @param boolean should 'NOT NULL' be added
200 * @param string the field after which we should add the new field
201 * @return object
202 */
203 function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
204 {
205 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
206
207 // DROP has everything it needs now.
208 if ($alter_type == 'DROP')
209 {
210 return $sql;
211 }
212
213 $sql .= " $column_definition";
214
215 if ($default_value != '')
216 {
217 $sql .= " DEFAULT \"$default_value\"";
218 }
219
220 if ($null === NULL)
221 {
222 $sql .= ' NULL';
223 }
224 else
225 {
226 $sql .= ' NOT NULL';
227 }
228
229 if ($after_field != '')
230 {
231 $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rename a table
242 *
243 * Generates a platform-specific query so that a table can be renamed
244 *
245 * @access private
246 * @param string the old table name
247 * @param string the new table name
248 * @return string
249 */
250 function _rename_table($table_name, $new_table_name)
251 {
252 // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
253 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
254 return $sql;
255 }
256
257}
258
259/* End of file mssql_forge.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000260/* Location: ./system/database/drivers/mssql/mssql_forge.php */