blob: 0dc7b5242cc5a49a0d823d280d460e17524f19c9 [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 *
Alex Bilbie84445d02011-03-10 16:43:39 +000066 * @return bool
67 */
Andrey Andreev4d116712012-03-20 16:30:57 +020068 public function _drop_table($table)
Alex Bilbie84445d02011-03-10 16:43:39 +000069 {
70 return "DROP TABLE ".$this->db->_escape_identifiers($table);
71 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Create Table
77 *
Alex Bilbie84445d02011-03-10 16:43:39 +000078 * @param string the table name
79 * @param array the fields
80 * @param mixed primary key(s)
81 * @param mixed key(s)
Andrey Andreev4d116712012-03-20 16:30:57 +020082 * @param bool should 'IF NOT EXISTS' be added to the SQL
Alex Bilbie84445d02011-03-10 16:43:39 +000083 * @return bool
84 */
Andrey Andreev4d116712012-03-20 16:30:57 +020085 public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Alex Bilbie84445d02011-03-10 16:43:39 +000086 {
87 $sql = 'CREATE TABLE ';
88
89 if ($if_not_exists === TRUE)
90 {
91 $sql .= 'IF NOT EXISTS ';
92 }
93
94 $sql .= $this->db->_escape_identifiers($table)." (";
95 $current_field_count = 0;
96
Andrey Andreev4d116712012-03-20 16:30:57 +020097 foreach ($fields as $field => $attributes)
Alex Bilbie84445d02011-03-10 16:43:39 +000098 {
99 // Numeric field names aren't allowed in databases, so if the key is
100 // numeric, we know it was assigned by PHP and the developer manually
101 // entered the field information, so we'll simply add it to the list
102 if (is_numeric($field))
103 {
104 $sql .= "\n\t$attributes";
105 }
106 else
107 {
108 $attributes = array_change_key_case($attributes, CASE_UPPER);
109
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200110 $sql .= "\n\t".$this->db->protect_identifiers($field);
Alex Bilbie84445d02011-03-10 16:43:39 +0000111
112 $sql .= ' '.$attributes['TYPE'];
113
114 if (array_key_exists('CONSTRAINT', $attributes))
115 {
116 $sql .= '('.$attributes['CONSTRAINT'].')';
117 }
118
119 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
120 {
121 $sql .= ' UNSIGNED';
122 }
123
124 if (array_key_exists('DEFAULT', $attributes))
125 {
126 $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
127 }
128
129 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
130 {
131 $sql .= ' NULL';
132 }
133 else
134 {
135 $sql .= ' NOT NULL';
136 }
137
138 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
139 {
140 $sql .= ' AUTO_INCREMENT';
141 }
142 }
143
144 // don't add a comma on the end of the last field
145 if (++$current_field_count < count($fields))
146 {
147 $sql .= ',';
148 }
149 }
150
151 if (count($primary_keys) > 0)
152 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200153 $primary_keys = $this->db->protect_identifiers($primary_keys);
Alex Bilbie84445d02011-03-10 16:43:39 +0000154 $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
155 }
156
157 if (is_array($keys) && count($keys) > 0)
158 {
159 foreach ($keys as $key)
160 {
161 if (is_array($key))
162 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200163 $key = $this->db->protect_identifiers($key);
Alex Bilbie84445d02011-03-10 16:43:39 +0000164 }
165 else
166 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200167 $key = array($this->db->protect_identifiers($key));
Alex Bilbie84445d02011-03-10 16:43:39 +0000168 }
169
170 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
171 }
172 }
173
174 $sql .= "\n)";
175
176 return $sql;
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Alter table query
183 *
184 * Generates a platform-specific query so that a table can be altered
185 * Called by add_column(), drop_column(), and column_alter(),
186 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000187 * @param string the ALTER type (ADD, DROP, CHANGE)
188 * @param string the column name
189 * @param string the table name
190 * @param string the column definition
191 * @param string the default value
Andrey Andreev4d116712012-03-20 16:30:57 +0200192 * @param bool should 'NOT NULL' be added
Alex Bilbie84445d02011-03-10 16:43:39 +0000193 * @param string the field after which we should add the new field
Andrey Andreev4d116712012-03-20 16:30:57 +0200194 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000195 */
Andrey Andreev4d116712012-03-20 16:30:57 +0200196 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000197 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200198 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Alex Bilbie84445d02011-03-10 16:43:39 +0000199
200 // DROP has everything it needs now.
201 if ($alter_type == 'DROP')
202 {
203 return $sql;
204 }
205
206 $sql .= " $column_definition";
207
208 if ($default_value != '')
209 {
210 $sql .= " DEFAULT \"$default_value\"";
211 }
212
213 if ($null === NULL)
214 {
215 $sql .= ' NULL';
216 }
217 else
218 {
219 $sql .= ' NOT NULL';
220 }
221
222 if ($after_field != '')
223 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200224 return $sql.' AFTER '.$this->db->protect_identifiers($after_field);
Alex Bilbie84445d02011-03-10 16:43:39 +0000225 }
226
227 return $sql;
228
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Rename a table
235 *
236 * Generates a platform-specific query so that a table can be renamed
237 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000238 * @param string the old table name
239 * @param string the new table name
240 * @return string
241 */
Andrey Andreev4d116712012-03-20 16:30:57 +0200242 public function _rename_table($table_name, $new_table_name)
Alex Bilbie84445d02011-03-10 16:43:39 +0000243 {
244 // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200245 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 +0000246 }
247
248}
249
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200250/* End of file sqlsrv_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400251/* Location: ./system/database/drivers/sqlsrv/sqlsrv_forge.php */