Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 28 | /** |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 29 | * SQLSRV Forge Class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 30 | * |
| 31 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 32 | * @author EllisLab Dev Team |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 35 | class CI_DB_sqlsrv_forge extends CI_DB_forge { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Create database |
| 39 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 40 | * @param string the database name |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 41 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 42 | */ |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 43 | public function _create_database($name) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 44 | { |
| 45 | return "CREATE DATABASE ".$name; |
| 46 | } |
| 47 | |
| 48 | // -------------------------------------------------------------------- |
| 49 | |
| 50 | /** |
| 51 | * Drop database |
| 52 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 53 | * @param string the database name |
| 54 | * @return bool |
| 55 | */ |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 56 | public function _drop_database($name) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 57 | { |
| 58 | return "DROP DATABASE ".$name; |
| 59 | } |
| 60 | |
| 61 | // -------------------------------------------------------------------- |
| 62 | |
| 63 | /** |
| 64 | * Drop Table |
| 65 | * |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame^] | 66 | * @param string table name |
| 67 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 68 | */ |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 69 | public function _drop_table($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 70 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame^] | 71 | return 'DROP TABLE '.$this->db->escape_identifiers($table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // -------------------------------------------------------------------- |
| 75 | |
| 76 | /** |
| 77 | * Create Table |
| 78 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 79 | * @param string the table name |
| 80 | * @param array the fields |
| 81 | * @param mixed primary key(s) |
| 82 | * @param mixed key(s) |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 83 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 84 | * @return bool |
| 85 | */ |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 86 | public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 87 | { |
| 88 | $sql = 'CREATE TABLE '; |
| 89 | |
| 90 | if ($if_not_exists === TRUE) |
| 91 | { |
| 92 | $sql .= 'IF NOT EXISTS '; |
| 93 | } |
| 94 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame^] | 95 | $sql .= $this->db->escape_identifiers($table).' ('; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 96 | $current_field_count = 0; |
| 97 | |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 98 | foreach ($fields as $field => $attributes) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 99 | { |
| 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); |
| 110 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 111 | $sql .= "\n\t".$this->db->protect_identifiers($field); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 112 | |
| 113 | $sql .= ' '.$attributes['TYPE']; |
| 114 | |
| 115 | if (array_key_exists('CONSTRAINT', $attributes)) |
| 116 | { |
| 117 | $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 118 | } |
| 119 | |
| 120 | if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) |
| 121 | { |
| 122 | $sql .= ' UNSIGNED'; |
| 123 | } |
| 124 | |
| 125 | if (array_key_exists('DEFAULT', $attributes)) |
| 126 | { |
| 127 | $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; |
| 128 | } |
| 129 | |
| 130 | if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE) |
| 131 | { |
| 132 | $sql .= ' NULL'; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | $sql .= ' NOT NULL'; |
| 137 | } |
| 138 | |
| 139 | if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 140 | { |
| 141 | $sql .= ' AUTO_INCREMENT'; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // 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 Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 154 | $primary_keys = $this->db->protect_identifiers($primary_keys); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 155 | $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")"; |
| 156 | } |
| 157 | |
| 158 | if (is_array($keys) && count($keys) > 0) |
| 159 | { |
| 160 | foreach ($keys as $key) |
| 161 | { |
| 162 | if (is_array($key)) |
| 163 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 164 | $key = $this->db->protect_identifiers($key); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 165 | } |
| 166 | else |
| 167 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 168 | $key = array($this->db->protect_identifiers($key)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")"; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $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 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 188 | * @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 Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 193 | * @param bool should 'NOT NULL' be added |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 194 | * @param string the field after which we should add the new field |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 195 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 196 | */ |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 197 | public function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 198 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 199 | $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 200 | |
| 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 Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 225 | return $sql.' AFTER '.$this->db->protect_identifiers($after_field); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return $sql; |
| 229 | |
| 230 | } |
| 231 | |
| 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
| 235 | * Rename a table |
| 236 | * |
| 237 | * Generates a platform-specific query so that a table can be renamed |
| 238 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 239 | * @param string the old table name |
| 240 | * @param string the new table name |
| 241 | * @return string |
| 242 | */ |
Andrey Andreev | 4d11671 | 2012-03-20 16:30:57 +0200 | [diff] [blame] | 243 | public function _rename_table($table_name, $new_table_name) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 244 | { |
| 245 | // I think this syntax will work, but can find little documentation on renaming tables in MSSQL |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 246 | return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | } |
| 250 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 251 | /* End of file sqlsrv_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 252 | /* Location: ./system/database/drivers/sqlsrv/sqlsrv_forge.php */ |