Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +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 | 4da24f8 | 2012-01-25 21:54:23 +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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * MS SQL Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_mssql_driver extends CI_DB { |
| 42 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 43 | public $dbdriver = 'mssql'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | // The character used for escaping |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 46 | protected $_escape_char = '"'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 47 | |
| 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | 1f619a8 | 2012-03-20 16:03:04 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = " ESCAPE '%s' "; |
Andrey Andreev | dd7242b | 2012-01-26 00:43:38 +0200 | [diff] [blame] | 50 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 55 | * used for the count_all() and count_all_results() methods. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | */ |
Andrey Andreev | dd7242b | 2012-01-26 00:43:38 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword = ' NEWID()'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 60 | // MSSQL-specific properties |
| 61 | protected $_quoted_identifier = TRUE; |
| 62 | |
| 63 | /* |
| 64 | * Constructor |
| 65 | * |
| 66 | * Appends the port number to the hostname, if needed. |
| 67 | * |
| 68 | * @param array |
| 69 | * @return void |
| 70 | */ |
Andrey Andreev | bd601d3 | 2012-02-12 21:16:51 +0200 | [diff] [blame] | 71 | public function __construct($params) |
| 72 | { |
| 73 | parent::__construct($params); |
| 74 | |
Andrey Andreev | 2cb262f | 2012-03-28 13:45:04 +0300 | [diff] [blame] | 75 | if ( ! empty($this->port)) |
Andrey Andreev | bd601d3 | 2012-02-12 21:16:51 +0200 | [diff] [blame] | 76 | { |
| 77 | $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port; |
| 78 | } |
| 79 | } |
| 80 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 81 | // -------------------------------------------------------------------- |
| 82 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | /** |
| 84 | * Non-persistent database connection |
| 85 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 88 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | { |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 90 | return $this->_mssql_connect(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | // -------------------------------------------------------------------- |
| 94 | |
| 95 | /** |
| 96 | * Persistent database connection |
| 97 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 100 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | { |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 102 | return $this->_mssql_connect(TRUE); |
| 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /* |
| 108 | * MSSQL Connect |
| 109 | * |
| 110 | * @param bool |
| 111 | * @return resource |
| 112 | */ |
| 113 | protected function _mssql_connect($persistent = FALSE) |
| 114 | { |
| 115 | $conn_id = ($persistent) |
| 116 | ? @mssql_pconnect($this->hostname, $this->username, $this->password) |
| 117 | : @mssql_connect($this->hostname, $this->username, $this->password); |
| 118 | |
| 119 | if ( ! $conn_id) |
| 120 | { |
| 121 | return FALSE; |
| 122 | } |
| 123 | |
| 124 | // Determine how identifiers are escaped |
| 125 | $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi'); |
| 126 | $query = $query->row_array(); |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame^] | 127 | $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi']; |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 128 | $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']'); |
| 129 | |
| 130 | return $conn_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 135 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | * Select the database |
| 137 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 138 | * @param string database name |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 139 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 141 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 143 | if ($database === '') |
| 144 | { |
| 145 | $database = $this->database; |
| 146 | } |
| 147 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | // Note: The brackets are required in the event that the DB name |
| 149 | // contains reserved characters |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 150 | if (@mssql_select_db($this->escape_identifiers($database), $this->conn_id)) |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 151 | { |
| 152 | $this->database = $database; |
| 153 | return TRUE; |
| 154 | } |
| 155 | |
| 156 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | * Execute the query |
| 163 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | * @param string an SQL query |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 165 | * @return mixed resource if rows are returned, bool otherwise |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 167 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | return @mssql_query($sql, $this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Begin Transaction |
| 176 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | * @return bool |
| 178 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 179 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 182 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | { |
| 184 | return TRUE; |
| 185 | } |
| 186 | |
| 187 | // Reset the transaction failure flag. |
| 188 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 189 | // even if the queries produce a successful result. |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 190 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 192 | return $this->simple_query('BEGIN TRAN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // -------------------------------------------------------------------- |
| 196 | |
| 197 | /** |
| 198 | * Commit Transaction |
| 199 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | * @return bool |
| 201 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 202 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 205 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
| 207 | return TRUE; |
| 208 | } |
| 209 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 210 | return $this->simple_query('COMMIT TRAN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Rollback Transaction |
| 217 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | * @return bool |
| 219 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 220 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 223 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | { |
| 225 | return TRUE; |
| 226 | } |
| 227 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 228 | return $this->simple_query('ROLLBACK TRAN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | // -------------------------------------------------------------------- |
| 232 | |
| 233 | /** |
| 234 | * Escape String |
| 235 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 237 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | * @return string |
| 239 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 240 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 242 | if (is_array($str)) |
| 243 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 244 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 246 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return $str; |
| 250 | } |
| 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | // Escape single quotes |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 253 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 254 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 255 | // escape LIKE condition wildcards |
| 256 | if ($like === TRUE) |
| 257 | { |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 258 | return str_replace( |
Phil Sturgeon | 36b0c94 | 2011-04-02 12:16:41 +0100 | [diff] [blame] | 259 | array($this->_like_escape_chr, '%', '_'), |
| 260 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 261 | $str |
| 262 | ); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 263 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 265 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * Affected Rows |
| 272 | * |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 273 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 275 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
| 277 | return @mssql_rows_affected($this->conn_id); |
| 278 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
Andrey Andreev | 1f619a8 | 2012-03-20 16:03:04 +0200 | [diff] [blame] | 283 | * Insert ID |
| 284 | * |
| 285 | * Returns the last id created in the Identity column. |
| 286 | * |
| 287 | * @return string |
| 288 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 289 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame^] | 291 | $query = version_compare($this->version(), '8', '>=') |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 292 | ? 'SELECT SCOPE_IDENTITY() AS last_id' |
| 293 | : 'SELECT @@IDENTITY AS last_id'; |
| 294 | |
Andrey Andreev | b7a47a7 | 2012-01-26 02:13:33 +0200 | [diff] [blame] | 295 | $query = $this->query($query); |
| 296 | $query = $query->row(); |
| 297 | return $query->last_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
Andrey Andreev | 1f619a8 | 2012-03-20 16:03:04 +0200 | [diff] [blame] | 303 | * Version number query string |
| 304 | * |
| 305 | * @return string |
| 306 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 307 | protected function _version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | { |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 309 | return 'SELECT @@VERSION AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
| 314 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | * List table query |
| 316 | * |
| 317 | * Generates a platform-specific query string so that the table names can be fetched |
| 318 | * |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 319 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | * @return string |
| 321 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 322 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame^] | 324 | $sql = 'SELECT '.$this->escape_identifiers('name') |
| 325 | .' FROM '.$this->escape_identifiers('sysobjects') |
| 326 | .' WHERE '.$this->escape_identifiers('type')." = 'U'"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 328 | if ($prefix_limit !== FALSE AND $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame^] | 330 | $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' " |
| 331 | .sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame^] | 334 | return $sql.' ORDER BY '.$this->escape_identifiers('name'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // -------------------------------------------------------------------- |
| 338 | |
| 339 | /** |
| 340 | * List column query |
| 341 | * |
| 342 | * Generates a platform-specific query string so that the column names can be fetched |
| 343 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | * @param string the table name |
| 345 | * @return string |
| 346 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 347 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // -------------------------------------------------------------------- |
| 353 | |
| 354 | /** |
| 355 | * Field data query |
| 356 | * |
| 357 | * Generates a platform-specific query so that the column data can be retrieved |
| 358 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | * @param string the table name |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 360 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 362 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame^] | 364 | return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 370 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 372 | * Returns an array containing code and message of the last |
| 373 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 375 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 377 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 379 | $query = $this->query('SELECT @@ERROR AS code'); |
| 380 | $query = $query->row(); |
| 381 | return array('code' => $query->code, 'message' => mssql_get_last_message()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
| 385 | |
| 386 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | * From Tables |
| 388 | * |
| 389 | * This function implicitly groups FROM tables so there is no confusion |
| 390 | * about operator precedence in harmony with SQL standards |
| 391 | * |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 392 | * @param array |
| 393 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 395 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 397 | return is_array($tables) ? implode(', ', $tables) : $tables; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | /** |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 403 | * Update statement |
| 404 | * |
| 405 | * Generates a platform-specific update string from the supplied data |
| 406 | * |
| 407 | * @param string the table name |
| 408 | * @param array the update data |
| 409 | * @param array the where clause |
| 410 | * @param array the orderby clause (ignored) |
| 411 | * @param array the limit clause (ignored) |
| 412 | * @param array the like clause |
| 413 | * @return string |
| 414 | */ |
| 415 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) |
| 416 | { |
| 417 | foreach($values as $key => $val) |
| 418 | { |
| 419 | $valstr[] = $key.' = '.$val; |
| 420 | } |
| 421 | |
| 422 | $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); |
| 423 | |
| 424 | if ( ! empty($like)) |
| 425 | { |
| 426 | $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); |
| 427 | } |
| 428 | |
| 429 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where; |
| 430 | } |
| 431 | |
| 432 | // -------------------------------------------------------------------- |
| 433 | |
| 434 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | * Truncate statement |
| 436 | * |
| 437 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 438 | * |
| 439 | * If the database does not support the truncate() command, |
| 440 | * then this method maps to 'DELETE FROM table' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | * @param string the table name |
| 443 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 445 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | { |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 447 | return 'TRUNCATE TABLE '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 449 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | // -------------------------------------------------------------------- |
| 451 | |
| 452 | /** |
| 453 | * Delete statement |
| 454 | * |
| 455 | * Generates a platform-specific delete string from the supplied data |
| 456 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | * @param string the table name |
| 458 | * @param array the where clause |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 459 | * @param array the like clause |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | * @param string the limit clause |
| 461 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 463 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | { |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 465 | $conditions = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 467 | empty($where) OR $conditions[] = implode(' ', $where); |
| 468 | empty($like) OR $conditions[] = implode(' ', $like); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 470 | $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 472 | return ($limit) |
| 473 | ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' |
| 474 | : 'DELETE FROM '.$table.$conditions; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // -------------------------------------------------------------------- |
| 478 | |
| 479 | /** |
| 480 | * Limit string |
| 481 | * |
| 482 | * Generates a platform-specific LIMIT clause |
| 483 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | * @param string the sql query string |
Andrey Andreev | 1f619a8 | 2012-03-20 16:03:04 +0200 | [diff] [blame] | 485 | * @param int the number of rows to limit the query to |
| 486 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | * @return string |
| 488 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 489 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | { |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 491 | // As of SQL Server 2012 (11.0.*) OFFSET is supported |
| 492 | if (version_compare($this->version(), '11', '>=')) |
| 493 | { |
| 494 | return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY'; |
| 495 | } |
| 496 | |
| 497 | $limit = $offset + $limit; |
| 498 | |
| 499 | // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported, |
| 500 | // however an ORDER BY clause is required for it to work |
| 501 | if (version_compare($this->version(), '9', '>=') && $offset && ! empty($this->qb_orderby)) |
| 502 | { |
| 503 | $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby); |
| 504 | |
| 505 | // We have to strip the ORDER BY clause |
| 506 | $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); |
| 507 | |
| 508 | return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" |
| 509 | .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) |
| 510 | ."\n) ".$this->escape_identifiers('CI_subquery') |
| 511 | ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit; |
| 512 | } |
| 513 | |
| 514 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // -------------------------------------------------------------------- |
| 518 | |
| 519 | /** |
| 520 | * Close DB Connection |
| 521 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | * @return void |
| 523 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 524 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 526 | @mssql_close($this->conn_id); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 527 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | |
| 529 | } |
| 530 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | /* End of file mssql_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 532 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |