blob: d787b3764de4bf7d18ebbf0e31ed83de9c713b86 [file] [log] [blame]
Andrey Andreevc0664812012-03-20 16:07:59 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 Andreevc0664812012-03-20 16:07:59 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevc0664812012-03-20 16:07:59 +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 * MS SQL 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/user_guide/database/
34 */
35class CI_DB_mssql_forge extends CI_DB_forge {
36
37 /**
38 * Create database
39 *
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @param string the database name
Andrey Andreevc0664812012-03-20 16:07:59 +020041 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000042 */
Andrey Andreevc0664812012-03-20 16:07:59 +020043 public function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
45 return "CREATE DATABASE ".$name;
46 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Drop database
52 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @param string the database name
Andrey Andreevc0664812012-03-20 16:07:59 +020054 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000055 */
Andrey Andreevc0664812012-03-20 16:07:59 +020056 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
58 return "DROP DATABASE ".$name;
59 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Drop Table
65 *
Andrey Andreevc0664812012-03-20 16:07:59 +020066 * @param string table name
67 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000068 */
Andrey Andreevc0664812012-03-20 16:07:59 +020069 public function _drop_table($table)
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Andrey Andreevea09a8a2012-04-06 20:50:07 +030071 return 'DROP TABLE '.$this->db->escape_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +000072 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * Create Table
78 *
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * @param string the table name
80 * @param array the fields
81 * @param mixed primary key(s)
82 * @param mixed key(s)
Andrey Andreevc0664812012-03-20 16:07:59 +020083 * @param bool should 'IF NOT EXISTS' be added to the SQL
84 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000085 */
Andrey Andreevc0664812012-03-20 16:07:59 +020086 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if ($if_not_exists === TRUE)
91 {
92 $sql .= 'IF NOT EXISTS ';
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Andrey Andreevea09a8a2012-04-06 20:50:07 +030095 $sql .= $this->db->escape_identifiers($table).' (';
Derek Allard2067d1a2008-11-13 22:59:24 +000096 $current_field_count = 0;
97
Andrey Andreevc0664812012-03-20 16:07:59 +020098 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +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);
Barry Mienydd671972010-10-04 16:33:58 +0200110
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200111 $sql .= "\n\t".$this->db->protect_identifiers($field);
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Jones37f4b9c2011-07-01 17:56:50 -0500113 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if (array_key_exists('CONSTRAINT', $attributes))
116 {
117 $sql .= '('.$attributes['CONSTRAINT'].')';
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
121 {
122 $sql .= ' UNSIGNED';
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if (array_key_exists('DEFAULT', $attributes))
126 {
127 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
131 {
132 $sql .= ' NULL';
133 }
134 else
135 {
Barry Mienydd671972010-10-04 16:33:58 +0200136 $sql .= ' NOT NULL';
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
140 {
141 $sql .= ' AUTO_INCREMENT';
142 }
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // 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);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 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);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166 else
167 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200168 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
172 }
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 $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 *
Derek Allard2067d1a2008-11-13 22:59:24 +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 Andreevc0664812012-03-20 16:07:59 +0200193 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string the field after which we should add the new field
Andrey Andreevc0664812012-03-20 16:07:59 +0200195 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreevc0664812012-03-20 16:07:59 +0200197 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +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);
Derek Allard2067d1a2008-11-13 22:59:24 +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);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Rename a table
236 *
237 * Generates a platform-specific query so that a table can be renamed
238 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * @param string the old table name
240 * @param string the new table name
241 * @return string
242 */
Andrey Andreevc0664812012-03-20 16:07:59 +0200243 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +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);
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248
249}
250
251/* End of file mssql_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400252/* Location: ./system/database/drivers/mssql/mssql_forge.php */