blob: 6b793a37a471eacf5ed138a5ac2f90855a68411c [file] [log] [blame]
Andrey Andreev4da24f82012-01-25 21:54:23 +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 Andreev4da24f82012-01-25 21:54:23 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4da24f82012-01-25 21:54:23 +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 Andreev4da24f82012-01-25 21:54:23 +020041 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000042 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020043 public function _create_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
Andrey Andreev4da24f82012-01-25 21:54:23 +020045 return 'CREATE DATABASE '.$name;
Derek Allard2067d1a2008-11-13 22:59:24 +000046 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Drop database
52 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @param string the database name
Andrey Andreev4da24f82012-01-25 21:54:23 +020054 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000055 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020056 public function _drop_database($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
Andrey Andreev4da24f82012-01-25 21:54:23 +020058 return 'DROP DATABASE '.$name;
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Drop Table
65 *
Andrey Andreevc0664812012-03-20 16:07:59 +020066 * @param string table name
Andrey Andreev4da24f82012-01-25 21:54:23 +020067 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000068 */
Andrey Andreev4da24f82012-01-25 21:54:23 +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 Andreev7a189e82012-01-27 21:13:52 +020083 * @param bool should 'IF NOT EXISTS' be added to the SQL
Andrey Andreev4da24f82012-01-25 21:54:23 +020084 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000085 */
Andrey Andreev4da24f82012-01-25 21:54:23 +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 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200105 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107 else
108 {
109 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +0200110
Andrey Andreev4da24f82012-01-25 21:54:23 +0200111 $sql .= "\n\t".$this->db->protect_identifiers($field)
112 .' '.$attributes['TYPE']
Andrey Andreev7a189e82012-01-27 21:13:52 +0200113 .( ! empty($attributes['CONSTRAINT']) ? '('.$attributes['CONSTRAINT'].')' : '')
114 .(( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
Andrey Andreevfb862952012-01-26 14:08:47 +0200115 .(isset($attributes['DEFAULT']) ? ' DEFAULT \''.$attributes['DEFAULT'].'\'' : '')
Andrey Andreev7a189e82012-01-27 21:13:52 +0200116 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
117 .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // don't add a comma on the end of the last field
121 if (++$current_field_count < count($fields))
122 {
123 $sql .= ',';
124 }
125 }
126
127 if (count($primary_keys) > 0)
128 {
Andrey Andreev242500d2012-03-09 14:37:16 +0200129 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->protect_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 if (is_array($keys) && count($keys) > 0)
133 {
134 foreach ($keys as $key)
135 {
136 if (is_array($key))
137 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200138 $key = $this->db->protect_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140 else
141 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200142 $key = array($this->db->protect_identifiers($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Andrey Andreev4da24f82012-01-25 21:54:23 +0200145 $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Andrey Andreev4da24f82012-01-25 21:54:23 +0200149 return $sql."\n)";
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Alter table query
156 *
157 * Generates a platform-specific query so that a table can be altered
158 * Called by add_column(), drop_column(), and column_alter(),
159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @param string the ALTER type (ADD, DROP, CHANGE)
161 * @param string the column name
162 * @param string the table name
163 * @param string the column definition
164 * @param string the default value
Andrey Andreev7a189e82012-01-27 21:13:52 +0200165 * @param bool should 'NOT NULL' be added
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string the field after which we should add the new field
Andrey Andreev7a189e82012-01-27 21:13:52 +0200167 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200169 public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200171 $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
173 // DROP has everything it needs now.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200174 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 return $sql;
177 }
178
Andrey Andreev4da24f82012-01-25 21:54:23 +0200179 return $sql.' '.$column_definition
180 .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '')
181 .($null === NULL ? ' NULL' : ' NOT NULL')
182 .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Rename a table
189 *
190 * Generates a platform-specific query so that a table can be renamed
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the old table name
193 * @param string the new table name
194 * @return string
195 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200196 public function _rename_table($table_name, $new_table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
Andrey Andreev4da24f82012-01-25 21:54:23 +0200199 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 +0000200 }
201
202}
203
204/* End of file mssql_forge.php */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200205/* Location: ./system/database/drivers/mssql/mssql_forge.php */