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