blob: a6739d1921851bd5f8c398546782d5709de3fb03 [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
Andrey Andreev0e8968a2012-01-26 02:04:37 +020048 protected $_random_keyword = ' NEWID()';
Alex Bilbie84445d02011-03-10 16:43:39 +000049
Andrey Andreev082ee2b2012-06-08 15:26:34 +030050 // SQLSRV-specific properties
51 protected $_quoted_identifier = TRUE;
52
Alex Bilbie84445d02011-03-10 16:43:39 +000053 /**
54 * Non-persistent database connection
55 *
Alex Bilbie84445d02011-03-10 16:43:39 +000056 * @return resource
57 */
Andrey Andreeve6297342012-03-20 16:25:07 +020058 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000059 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000060 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
61 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
62
63 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +020064 'UID' => empty($this->username) ? '' : $this->username,
65 'PWD' => empty($this->password) ? '' : $this->password,
66 'Database' => $this->database,
67 'ConnectionPooling' => $pooling ? 1 : 0,
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000068 'CharacterSet' => $character_set,
Andrey Andreev0e8968a2012-01-26 02:04:37 +020069 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000070 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +020071
72 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000073 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +020074 if (empty($connection['UID']) && empty($connection['PWD']))
75 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000076 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000077 }
78
Andrey Andreevfac37612012-07-02 14:56:20 +030079 $this->conn_id = sqlsrv_connect($this->hostname, $connection);
Andrey Andreev082ee2b2012-06-08 15:26:34 +030080
81 // Determine how identifiers are escaped
82 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
83 $query = $query->row_array();
Andrey Andreev70c72c92012-06-25 00:04:51 +030084 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +030085 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
86
Andrey Andreevfac37612012-07-02 14:56:20 +030087 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +000088 }
89
90 // --------------------------------------------------------------------
91
92 /**
93 * Persistent database connection
94 *
Alex Bilbie84445d02011-03-10 16:43:39 +000095 * @return resource
96 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020097 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +000098 {
Kyle Farris37e351f2011-09-07 11:14:46 -030099 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000100 }
101
102 // --------------------------------------------------------------------
103
104 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000105 * Select the database
106 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200107 * @param string database name
108 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000109 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200110 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000111 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200112 if ($database === '')
113 {
114 $database = $this->database;
115 }
116
117 if ($this->_execute('USE '.$database))
118 {
119 $this->database = $database;
120 return TRUE;
121 }
122
123 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000124 }
125
126 // --------------------------------------------------------------------
127
128 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000129 * Execute the query
130 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000131 * @param string an SQL query
132 * @return resource
133 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200134 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000135 {
Andrey Andreevb2f60332012-07-03 14:45:00 +0300136 return ($this->is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
Andrey Andreev846acc72012-05-24 23:27:46 +0300137 ? sqlsrv_query($this->conn_id, $sql)
138 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
Alex Bilbie84445d02011-03-10 16:43:39 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Begin Transaction
145 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000146 * @return bool
147 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200148 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000149 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200151 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000152 {
153 return TRUE;
154 }
155
156 // Reset the transaction failure flag.
157 // If the $test_mode flag is set to TRUE transactions will be rolled back
158 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200159 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000160
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000161 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Commit Transaction
168 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000169 * @return bool
170 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200171 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000172 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000173 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200174 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000175 {
176 return TRUE;
177 }
178
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000179 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Rollback Transaction
186 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000187 * @return bool
188 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200189 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000190 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000191 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200192 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000193 {
194 return TRUE;
195 }
196
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000197 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Escape String
204 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000205 * @param string
206 * @param bool whether or not the string will be used in a LIKE condition
207 * @return string
208 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200209 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000210 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000211 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000212 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Affected Rows
219 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200220 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000221 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200222 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000223 {
Andrey Andreevede49ba2012-07-23 16:06:36 +0300224 return sqlsrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000225 }
226
227 // --------------------------------------------------------------------
228
229 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200230 * Insert ID
231 *
232 * Returns the last id created in the Identity column.
233 *
234 * @return string
235 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200236 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000237 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200238 $query = $this->query('SELECT @@IDENTITY AS insert_id');
239 $query = $query->row();
240 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200246 * Database version number
247 *
248 * @return string
249 */
250 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000251 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200252 if (isset($this->data_cache['version']))
253 {
254 return $this->data_cache['version'];
255 }
256
257 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
258 {
259 return FALSE;
260 }
261
262 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000263 }
264
265 // --------------------------------------------------------------------
266
267 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000268 * List table query
269 *
270 * Generates a platform-specific query string so that the table names can be fetched
271 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200272 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000273 * @return string
274 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200275 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000276 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300277 $sql = 'SELECT '.$this->escape_identifiers('name')
278 .' FROM '.$this->escape_identifiers('sysobjects')
279 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
280
281 if ($prefix_limit === TRUE && $this->dbprefix !== '')
282 {
283 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
284 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
285 }
286
287 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * List column query
294 *
295 * Generates a platform-specific query string so that the column names can be fetched
296 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000297 * @param string the table name
298 * @return string
299 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200300 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000301 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200302 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Field data query
309 *
310 * Generates a platform-specific query so that the column data can be retrieved
311 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000312 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200313 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000314 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200315 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000316 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300317 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000318 }
319
320 // --------------------------------------------------------------------
321
322 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200323 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000324 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200325 * Returns an array containing code and message of the last
326 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000327 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200328 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000329 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200330 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000331 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200332 $error = array('code' => '00000', 'message' => '');
333 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
334
335 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200336 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200337 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200338 }
339
Andrey Andreev4be5de12012-03-02 15:45:41 +0200340 $sqlsrv_error = array_shift($sqlsrv_errors);
341 if (isset($sqlsrv_error['SQLSTATE']))
342 {
343 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
344 }
345 elseif (isset($sqlsrv_error['code']))
346 {
347 $error['code'] = $sqlsrv_error['code'];
348 }
349
350 if (isset($sqlsrv_error['message']))
351 {
352 $error['message'] = $sqlsrv_error['message'];
353 }
354
355 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000361 * Update statement
362 *
363 * Generates a platform-specific update string from the supplied data
364 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000365 * @param string the table name
366 * @param array the update data
367 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300368 * @param array the orderby clause (ignored)
369 * @param array the limit clause (ignored)
370 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000371 * @return string
372 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300373 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Alex Bilbie84445d02011-03-10 16:43:39 +0000374 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200375 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000376 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200377 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000378 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200379
Andrey Andreev00541ae2012-04-09 11:43:10 +0300380 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
381
382 if ( ! empty($like))
383 {
384 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
385 }
386
st24b47e982012-04-24 17:45:44 +0800387 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where;
Alex Bilbie84445d02011-03-10 16:43:39 +0000388 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200389
Alex Bilbie84445d02011-03-10 16:43:39 +0000390 // --------------------------------------------------------------------
391
392 /**
393 * Truncate statement
394 *
395 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300396 *
397 * If the database does not support the truncate() command,
398 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000399 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000400 * @param string the table name
401 * @return string
402 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200403 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300405 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Delete statement
412 *
413 * Generates a platform-specific delete string from the supplied data
414 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000415 * @param string the table name
416 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300417 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000418 * @param string the limit clause
419 * @return string
420 */
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300421 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000422 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300423 $conditions = array();
424
425 empty($where) OR $conditions[] = implode(' ', $where);
426 empty($like) OR $conditions[] = implode(' ', $like);
427
428 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
429
430 return ($limit)
431 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
432 : 'DELETE FROM '.$table.$conditions;
Alex Bilbie84445d02011-03-10 16:43:39 +0000433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Limit string
439 *
440 * Generates a platform-specific LIMIT clause
441 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000442 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200443 * @param int the number of rows to limit the query to
444 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000445 * @return string
446 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200447 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000448 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300449 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300450 if (version_compare($this->version(), '11', '>='))
451 {
452 return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
453 }
454
455 $limit = $offset + $limit;
456
457 // An ORDER BY clause is required for ROW_NUMBER() to work
458 if ($offset && ! empty($this->qb_orderby))
459 {
460 $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby);
461
462 // We have to strip the ORDER BY clause
463 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
464
465 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
466 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
467 ."\n) ".$this->escape_identifiers('CI_subquery')
468 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit;
469 }
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 */