blob: 4eb12f910e3e995b2361545cbd93e909a07a1a73 [file] [log] [blame]
Andrey Andreev0e8968a2012-01-26 02:04:37 +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 Andreev0e8968a2012-01-26 02:04:37 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0e8968a2012-01-26 02:04:37 +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
Andrey Andreevbf940582012-06-10 07:05:05 +030024 * @since Version 2.0.3
Alex Bilbie84445d02011-03-10 16:43:39 +000025 * @filesource
26 */
27
Alex Bilbie84445d02011-03-10 16:43:39 +000028/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000029 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000030 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Alex Bilbie84445d02011-03-10 16:43:39 +000033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000041class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000042
Andrey Andreev0e8968a2012-01-26 02:04:37 +020043 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000044
45 // The character used for escaping
Andrey Andreev082ee2b2012-06-08 15:26:34 +030046 protected $_escape_char = '"';
Alex Bilbie84445d02011-03-10 16:43:39 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreeve6297342012-03-20 16:25:07 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreev0e8968a2012-01-26 02:04:37 +020050 protected $_like_escape_chr = '!';
Alex Bilbie84445d02011-03-10 16:43:39 +000051
Andrey Andreev0e8968a2012-01-26 02:04:37 +020052 protected $_random_keyword = ' NEWID()';
Alex Bilbie84445d02011-03-10 16:43:39 +000053
Andrey Andreev082ee2b2012-06-08 15:26:34 +030054 // SQLSRV-specific properties
55 protected $_quoted_identifier = TRUE;
56
Alex Bilbie84445d02011-03-10 16:43:39 +000057 /**
58 * Non-persistent database connection
59 *
Alex Bilbie84445d02011-03-10 16:43:39 +000060 * @return resource
61 */
Andrey Andreeve6297342012-03-20 16:25:07 +020062 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000063 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000064 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
65 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
66
67 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +020068 'UID' => empty($this->username) ? '' : $this->username,
69 'PWD' => empty($this->password) ? '' : $this->password,
70 'Database' => $this->database,
71 'ConnectionPooling' => $pooling ? 1 : 0,
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000072 'CharacterSet' => $character_set,
Andrey Andreev0e8968a2012-01-26 02:04:37 +020073 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000074 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +020075
76 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000077 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +020078 if (empty($connection['UID']) && empty($connection['PWD']))
79 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000080 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000081 }
82
Andrey Andreevfac37612012-07-02 14:56:20 +030083 $this->conn_id = sqlsrv_connect($this->hostname, $connection);
Andrey Andreev082ee2b2012-06-08 15:26:34 +030084
85 // Determine how identifiers are escaped
86 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
87 $query = $query->row_array();
Andrey Andreev70c72c92012-06-25 00:04:51 +030088 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +030089 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
90
Andrey Andreevfac37612012-07-02 14:56:20 +030091 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +000092 }
93
94 // --------------------------------------------------------------------
95
96 /**
97 * Persistent database connection
98 *
Alex Bilbie84445d02011-03-10 16:43:39 +000099 * @return resource
100 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200101 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000102 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300103 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000104 }
105
106 // --------------------------------------------------------------------
107
108 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000109 * Select the database
110 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200111 * @param string database name
112 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000113 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200114 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000115 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200116 if ($database === '')
117 {
118 $database = $this->database;
119 }
120
121 if ($this->_execute('USE '.$database))
122 {
123 $this->database = $database;
124 return TRUE;
125 }
126
127 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000133 * Execute the query
134 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000135 * @param string an SQL query
136 * @return resource
137 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200138 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000139 {
Andrey Andreevb2f60332012-07-03 14:45:00 +0300140 return ($this->is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
Andrey Andreev846acc72012-05-24 23:27:46 +0300141 ? sqlsrv_query($this->conn_id, $sql)
142 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
Alex Bilbie84445d02011-03-10 16:43:39 +0000143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Begin Transaction
149 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 * @return bool
151 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200152 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000153 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000154 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200155 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000156 {
157 return TRUE;
158 }
159
160 // Reset the transaction failure flag.
161 // If the $test_mode flag is set to TRUE transactions will be rolled back
162 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200163 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000164
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000165 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Commit Transaction
172 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000173 * @return bool
174 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200175 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000176 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000177 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200178 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000179 {
180 return TRUE;
181 }
182
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000183 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Rollback Transaction
190 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000191 * @return bool
192 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200193 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000194 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000195 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200196 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000197 {
198 return TRUE;
199 }
200
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000201 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Escape String
208 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000209 * @param string
210 * @param bool whether or not the string will be used in a LIKE condition
211 * @return string
212 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200213 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000214 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000215 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000216 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Affected Rows
223 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200224 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000225 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200226 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 {
Andrey Andreev846acc72012-05-24 23:27:46 +0300228 return sqlrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 }
230
231 // --------------------------------------------------------------------
232
233 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200234 * Insert ID
235 *
236 * Returns the last id created in the Identity column.
237 *
238 * @return string
239 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200240 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200242 $query = $this->query('SELECT @@IDENTITY AS insert_id');
243 $query = $query->row();
244 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000245 }
246
247 // --------------------------------------------------------------------
248
249 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200250 * Database version number
251 *
252 * @return string
253 */
254 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000255 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200256 if (isset($this->data_cache['version']))
257 {
258 return $this->data_cache['version'];
259 }
260
261 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
262 {
263 return FALSE;
264 }
265
266 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000267 }
268
269 // --------------------------------------------------------------------
270
271 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000272 * List table query
273 *
274 * Generates a platform-specific query string so that the table names can be fetched
275 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200276 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000277 * @return string
278 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200279 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000280 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300281 $sql = 'SELECT '.$this->escape_identifiers('name')
282 .' FROM '.$this->escape_identifiers('sysobjects')
283 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
284
285 if ($prefix_limit === TRUE && $this->dbprefix !== '')
286 {
287 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
288 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
289 }
290
291 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * List column query
298 *
299 * Generates a platform-specific query string so that the column names can be fetched
300 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000301 * @param string the table name
302 * @return string
303 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200304 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000305 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200306 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Field data query
313 *
314 * Generates a platform-specific query so that the column data can be retrieved
315 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000316 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200317 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000318 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200319 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000320 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300321 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000322 }
323
324 // --------------------------------------------------------------------
325
326 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200327 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000328 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200329 * Returns an array containing code and message of the last
330 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000331 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200332 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000333 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200334 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000335 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200336 $error = array('code' => '00000', 'message' => '');
337 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
338
339 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200340 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200341 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200342 }
343
Andrey Andreev4be5de12012-03-02 15:45:41 +0200344 $sqlsrv_error = array_shift($sqlsrv_errors);
345 if (isset($sqlsrv_error['SQLSTATE']))
346 {
347 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
348 }
349 elseif (isset($sqlsrv_error['code']))
350 {
351 $error['code'] = $sqlsrv_error['code'];
352 }
353
354 if (isset($sqlsrv_error['message']))
355 {
356 $error['message'] = $sqlsrv_error['message'];
357 }
358
359 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000360 }
361
362 // --------------------------------------------------------------------
363
364 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000365 * From Tables
366 *
367 * This function implicitly groups FROM tables so there is no confusion
368 * about operator precedence in harmony with SQL standards
369 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200370 * @param array
371 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000372 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200373 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000374 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300375 return is_array($tables) ? implode(', ', $tables) : $tables;
Alex Bilbie84445d02011-03-10 16:43:39 +0000376 }
377
378 // --------------------------------------------------------------------
379
380 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000381 * Update statement
382 *
383 * Generates a platform-specific update string from the supplied data
384 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000385 * @param string the table name
386 * @param array the update data
Alex Bilbie84445d02011-03-10 16:43:39 +0000387 * @return string
388 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300389 protected function _update($table, $values)
Alex Bilbie84445d02011-03-10 16:43:39 +0000390 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300391 $this->qb_limit = FALSE;
392 $this->qb_orderby = array();
393 return parent::_update($table, $values);
Alex Bilbie84445d02011-03-10 16:43:39 +0000394 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200395
Alex Bilbie84445d02011-03-10 16:43:39 +0000396 // --------------------------------------------------------------------
397
398 /**
399 * Truncate statement
400 *
401 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300402 *
403 * If the database does not support the truncate() command,
404 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000405 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000406 * @param string the table name
407 * @return string
408 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200409 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000410 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300411 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * Delete statement
418 *
419 * Generates a platform-specific delete string from the supplied data
420 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000421 * @param string the table name
Alex Bilbie84445d02011-03-10 16:43:39 +0000422 * @return string
423 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300424 protected function _delete($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000425 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300426 if ($this->qb_limit)
427 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300428 return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete';
Andrey Andreevb0478652012-07-18 15:34:46 +0300429 }
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300430
Andrey Andreevb0478652012-07-18 15:34:46 +0300431 return parent::_delete($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Limit string
438 *
439 * Generates a platform-specific LIMIT clause
440 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000441 * @param string the sql query string
Alex Bilbie84445d02011-03-10 16:43:39 +0000442 * @return string
443 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300444 protected function _limit($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000445 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300446 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300447 if (version_compare($this->version(), '11', '>='))
448 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300449 return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
Andrey Andreev71379ca2012-06-11 16:12:43 +0300450 }
451
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300452 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300453
454 // An ORDER BY clause is required for ROW_NUMBER() to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300455 if ($this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300456 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300457 $orderby = trim($this->_compile_order_by());
Andrey Andreev71379ca2012-06-11 16:12:43 +0300458
459 // We have to strip the ORDER BY clause
460 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
461
462 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
463 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
464 ."\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300465 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300466 }
467
468 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Close DB Connection
475 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000476 * @return void
477 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300478 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000479 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300480 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000481 }
482
483}
484
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200485/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300486/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */