blob: e49b4b17db801428e118ac8a1ce2865fd9eab267 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +000028
Alex Bilbie84445d02011-03-10 16:43:39 +000029/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000030 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000031 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Alex Bilbie84445d02011-03-10 16:43:39 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000042class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000043
Andrey Andreev0e8968a2012-01-26 02:04:37 +020044 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000045
46 // The character used for escaping
Andrey Andreev082ee2b2012-06-08 15:26:34 +030047 protected $_escape_char = '"';
Alex Bilbie84445d02011-03-10 16:43:39 +000048
Andrey Andreev0e8968a2012-01-26 02:04:37 +020049 protected $_random_keyword = ' NEWID()';
Alex Bilbie84445d02011-03-10 16:43:39 +000050
Andrey Andreev082ee2b2012-06-08 15:26:34 +030051 // SQLSRV-specific properties
52 protected $_quoted_identifier = TRUE;
53
Alex Bilbie84445d02011-03-10 16:43:39 +000054 /**
55 * Non-persistent database connection
56 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030057 * @param bool $pooling = FALSE
Alex Bilbie84445d02011-03-10 16:43:39 +000058 * @return resource
59 */
Andrey Andreeve6297342012-03-20 16:25:07 +020060 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000061 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030062 $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE)
63 ? 'UTF-8' : SQLSRV_ENC_CHAR;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000064
65 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +020066 'UID' => empty($this->username) ? '' : $this->username,
67 'PWD' => empty($this->password) ? '' : $this->password,
68 'Database' => $this->database,
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030069 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0,
70 'CharacterSet' => $charset,
71 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0,
Andrey Andreev0e8968a2012-01-26 02:04:37 +020072 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000073 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +020074
75 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000076 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +020077 if (empty($connection['UID']) && empty($connection['PWD']))
78 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000079 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000080 }
81
Andrey Andreevfac37612012-07-02 14:56:20 +030082 $this->conn_id = sqlsrv_connect($this->hostname, $connection);
Andrey Andreev082ee2b2012-06-08 15:26:34 +030083
84 // Determine how identifiers are escaped
85 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
86 $query = $query->row_array();
Andrey Andreev70c72c92012-06-25 00:04:51 +030087 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +030088 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
89
Andrey Andreevfac37612012-07-02 14:56:20 +030090 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +000091 }
92
93 // --------------------------------------------------------------------
94
95 /**
96 * Persistent database connection
97 *
Alex Bilbie84445d02011-03-10 16:43:39 +000098 * @return resource
99 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200100 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000101 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300102 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000103 }
104
105 // --------------------------------------------------------------------
106
107 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000108 * Select the database
109 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200110 * @param string database name
111 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000112 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200113 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000114 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200115 if ($database === '')
116 {
117 $database = $this->database;
118 }
119
120 if ($this->_execute('USE '.$database))
121 {
122 $this->database = $database;
123 return TRUE;
124 }
125
126 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000127 }
128
129 // --------------------------------------------------------------------
130
131 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000132 * Execute the query
133 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000134 * @param string an SQL query
135 * @return resource
136 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200137 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000138 {
Andrey Andreevb2f60332012-07-03 14:45:00 +0300139 return ($this->is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
Andrey Andreev846acc72012-05-24 23:27:46 +0300140 ? sqlsrv_query($this->conn_id, $sql)
141 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
Alex Bilbie84445d02011-03-10 16:43:39 +0000142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Begin Transaction
148 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300149 * @param bool $test_mode = FALSE
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 Andreevede49ba2012-07-23 16:06:36 +0300228 return sqlsrv_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 * Update statement
366 *
367 * Generates a platform-specific update string from the supplied data
368 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000369 * @param string the table name
370 * @param array the update data
Alex Bilbie84445d02011-03-10 16:43:39 +0000371 * @return string
372 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300373 protected function _update($table, $values)
Alex Bilbie84445d02011-03-10 16:43:39 +0000374 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300375 $this->qb_limit = FALSE;
376 $this->qb_orderby = array();
377 return parent::_update($table, $values);
Alex Bilbie84445d02011-03-10 16:43:39 +0000378 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200379
Alex Bilbie84445d02011-03-10 16:43:39 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * Truncate statement
384 *
385 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300386 *
387 * If the database does not support the truncate() command,
388 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000389 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000390 * @param string the table name
391 * @return string
392 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200393 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000394 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300395 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Delete statement
402 *
403 * Generates a platform-specific delete string from the supplied data
404 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000405 * @param string the table name
Alex Bilbie84445d02011-03-10 16:43:39 +0000406 * @return string
407 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300408 protected function _delete($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000409 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300410 if ($this->qb_limit)
411 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300412 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 +0300413 }
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300414
Andrey Andreevb0478652012-07-18 15:34:46 +0300415 return parent::_delete($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Limit string
422 *
423 * Generates a platform-specific LIMIT clause
424 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000425 * @param string the sql query string
Alex Bilbie84445d02011-03-10 16:43:39 +0000426 * @return string
427 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300428 protected function _limit($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000429 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300430 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300431 if (version_compare($this->version(), '11', '>='))
432 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300433 return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
Andrey Andreev71379ca2012-06-11 16:12:43 +0300434 }
435
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300436 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300437
438 // An ORDER BY clause is required for ROW_NUMBER() to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300439 if ($this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300440 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300441 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300442
443 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300444 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300445
Andrey Andreev44514542012-10-23 15:35:09 +0300446 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
447 if (count($this->qb_select) === 0)
448 {
449 $select = '*'; // Inevitable
450 }
451 else
452 {
453 // Use only field names and their aliases, everything else is out of our scope.
454 $select = array();
455 $field_regexp = ($this->_quoted_identifier)
456 ? '("[^\"]+")' : '(\[[^\]]+\])';
457 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
458 {
459 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
460 ? $m[1] : $this->qb_select[$i];
461 }
462 $select = implode(', ', $select);
463 }
464
465 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300466 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
Andrey Andreev44514542012-10-23 15:35:09 +0300467 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300468 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300469 }
470
471 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Close DB Connection
478 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000479 * @return void
480 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300481 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000482 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300483 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000484 }
485
486}
487
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200488/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300489/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */