blob: 825c0245270966428ae9f5d9a2894b435bc3cc28 [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
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' NEWID()';
Alex Bilbie84445d02011-03-10 16:43:39 +000059
Andrey Andreev082ee2b2012-06-08 15:26:34 +030060 // SQLSRV-specific properties
61 protected $_quoted_identifier = TRUE;
62
Alex Bilbie84445d02011-03-10 16:43:39 +000063 /**
64 * Non-persistent database connection
65 *
Alex Bilbie84445d02011-03-10 16:43:39 +000066 * @return resource
67 */
Andrey Andreeve6297342012-03-20 16:25:07 +020068 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000069 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000070 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
71 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
72
73 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +020074 'UID' => empty($this->username) ? '' : $this->username,
75 'PWD' => empty($this->password) ? '' : $this->password,
76 'Database' => $this->database,
77 'ConnectionPooling' => $pooling ? 1 : 0,
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000078 'CharacterSet' => $character_set,
Andrey Andreev0e8968a2012-01-26 02:04:37 +020079 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000080 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +020081
82 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000083 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +020084 if (empty($connection['UID']) && empty($connection['PWD']))
85 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000086 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000087 }
88
Andrey Andreev082ee2b2012-06-08 15:26:34 +030089 $conn_id = sqlsrv_connect($this->hostname, $connection);
90
91 // Determine how identifiers are escaped
92 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
93 $query = $query->row_array();
94 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query->qi;
95 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
96
97 return $conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +000098 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Persistent database connection
104 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000105 * @return resource
106 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200107 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000108 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300109 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000110 }
111
112 // --------------------------------------------------------------------
113
114 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000115 * Select the database
116 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200117 * @param string database name
118 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000119 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200120 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000121 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200122 if ($database === '')
123 {
124 $database = $this->database;
125 }
126
127 if ($this->_execute('USE '.$database))
128 {
129 $this->database = $database;
130 return TRUE;
131 }
132
133 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000134 }
135
136 // --------------------------------------------------------------------
137
138 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000139 * Execute the query
140 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000141 * @param string an SQL query
142 * @return resource
143 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200144 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 {
Andrey Andreev846acc72012-05-24 23:27:46 +0300146 return (is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
147 ? sqlsrv_query($this->conn_id, $sql)
148 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
Alex Bilbie84445d02011-03-10 16:43:39 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Begin Transaction
155 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000156 * @return bool
157 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200158 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000159 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000160 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200161 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000162 {
163 return TRUE;
164 }
165
166 // Reset the transaction failure flag.
167 // If the $test_mode flag is set to TRUE transactions will be rolled back
168 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200169 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000170
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000171 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Commit Transaction
178 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000179 * @return bool
180 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200181 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000182 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000183 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200184 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000185 {
186 return TRUE;
187 }
188
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000189 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Rollback Transaction
196 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000197 * @return bool
198 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200199 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000200 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000201 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200202 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000203 {
204 return TRUE;
205 }
206
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000207 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Escape String
214 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000215 * @param string
216 * @param bool whether or not the string will be used in a LIKE condition
217 * @return string
218 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200219 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000220 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000221 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000222 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Affected Rows
229 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200230 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000231 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200232 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000233 {
Andrey Andreev846acc72012-05-24 23:27:46 +0300234 return sqlrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000235 }
236
237 // --------------------------------------------------------------------
238
239 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200240 * Insert ID
241 *
242 * Returns the last id created in the Identity column.
243 *
244 * @return string
245 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200246 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000247 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200248 $query = $this->query('SELECT @@IDENTITY AS insert_id');
249 $query = $query->row();
250 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000251 }
252
253 // --------------------------------------------------------------------
254
255 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200256 * Database version number
257 *
258 * @return string
259 */
260 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000261 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200262 if (isset($this->data_cache['version']))
263 {
264 return $this->data_cache['version'];
265 }
266
267 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
268 {
269 return FALSE;
270 }
271
272 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000273 }
274
275 // --------------------------------------------------------------------
276
277 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000278 * List table query
279 *
280 * Generates a platform-specific query string so that the table names can be fetched
281 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200282 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000283 * @return string
284 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200285 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000286 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000287 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY 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 Andreeve6297342012-03-20 16:25:07 +0200317 return 'SELECT TOP 1 * FROM '.$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 * From Tables
362 *
363 * This function implicitly groups FROM tables so there is no confusion
364 * about operator precedence in harmony with SQL standards
365 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200366 * @param array
367 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000368 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200369 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000370 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300371 return is_array($tables) ? implode(', ', $tables) : $tables;
Alex Bilbie84445d02011-03-10 16:43:39 +0000372 }
373
374 // --------------------------------------------------------------------
375
376 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000377 * Update statement
378 *
379 * Generates a platform-specific update string from the supplied data
380 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000381 * @param string the table name
382 * @param array the update data
383 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300384 * @param array the orderby clause (ignored)
385 * @param array the limit clause (ignored)
386 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000387 * @return string
388 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300389 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Alex Bilbie84445d02011-03-10 16:43:39 +0000390 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200391 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000392 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200393 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000394 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200395
Andrey Andreev00541ae2012-04-09 11:43:10 +0300396 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
397
398 if ( ! empty($like))
399 {
400 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
401 }
402
st24b47e982012-04-24 17:45:44 +0800403 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where;
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200405
Alex Bilbie84445d02011-03-10 16:43:39 +0000406 // --------------------------------------------------------------------
407
408 /**
409 * Truncate statement
410 *
411 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300412 *
413 * If the database does not support the truncate() command,
414 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000415 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000416 * @param string the table name
417 * @return string
418 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200419 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000420 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300421 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Delete statement
428 *
429 * Generates a platform-specific delete string from the supplied data
430 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000431 * @param string the table name
432 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300433 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000434 * @param string the limit clause
435 * @return string
436 */
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300437 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000438 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300439 $conditions = array();
440
441 empty($where) OR $conditions[] = implode(' ', $where);
442 empty($like) OR $conditions[] = implode(' ', $like);
443
444 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
445
446 return ($limit)
447 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
448 : 'DELETE FROM '.$table.$conditions;
Alex Bilbie84445d02011-03-10 16:43:39 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Limit string
455 *
456 * Generates a platform-specific LIMIT clause
457 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000458 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200459 * @param int the number of rows to limit the query to
460 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000461 * @return string
462 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200463 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000464 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300465 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300466 if (version_compare($this->version(), '11', '>='))
467 {
468 return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
469 }
470
471 $limit = $offset + $limit;
472
473 // An ORDER BY clause is required for ROW_NUMBER() to work
474 if ($offset && ! empty($this->qb_orderby))
475 {
476 $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby);
477
478 // We have to strip the ORDER BY clause
479 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
480
481 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
482 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
483 ."\n) ".$this->escape_identifiers('CI_subquery')
484 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit;
485 }
486
487 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Close DB Connection
494 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000495 * @return void
496 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300497 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000498 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300499 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000500 }
501
502}
503
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200504/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300505/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */