blob: 9f2f886990df47bdb6e950624455636a1b320842 [file] [log] [blame]
Andrey Andreeve6297342012-03-20 16:25:07 +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 Andreeve6297342012-03-20 16:25:07 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve6297342012-03-20 16:25:07 +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
24 * @since Version 1.0
25 * @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
32 * creates dynamically based on whether the active record
33 * 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 Andreeve6297342012-03-20 16:25:07 +020043 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000044
45 // The character used for escaping
Andrey Andreeve6297342012-03-20 16:25:07 +020046 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' ";
50 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
Timothy Warren0e20da12012-03-19 19:05:46 -040055 * used for the count_all() and count_all_results() functions.
Alex Bilbie84445d02011-03-10 16:43:39 +000056 */
Andrey Andreeve6297342012-03-20 16:25:07 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' ASC'; // not currently supported
Alex Bilbie84445d02011-03-10 16:43:39 +000059
60 /**
61 * Non-persistent database connection
62 *
Alex Bilbie84445d02011-03-10 16:43:39 +000063 * @return resource
64 */
Andrey Andreeve6297342012-03-20 16:25:07 +020065 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000066 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000067 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
68 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
69
70 $connection = array(
71 'UID' => empty($this->username) ? '' : $this->username,
72 'PWD' => empty($this->password) ? '' : $this->password,
73 'Database' => $this->database,
74 'ConnectionPooling' => $pooling ? 1 : 0,
75 'CharacterSet' => $character_set,
76 'ReturnDatesAsStrings' => 1
77 );
Andrey Andreeve6297342012-03-20 16:25:07 +020078
79 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000080 // 'Windows Authentication Mode' connection.
Andrey Andreeve6297342012-03-20 16:25:07 +020081 if (empty($connection['UID']) && empty($connection['PWD']))
82 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000083 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000084 }
85
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000086 return sqlsrv_connect($this->hostname, $connection);
Alex Bilbie84445d02011-03-10 16:43:39 +000087 }
88
89 // --------------------------------------------------------------------
90
91 /**
92 * Persistent database connection
93 *
Alex Bilbie84445d02011-03-10 16:43:39 +000094 * @return resource
95 */
Andrey Andreeve6297342012-03-20 16:25:07 +020096 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +000097 {
Kyle Farris37e351f2011-09-07 11:14:46 -030098 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +000099 }
100
101 // --------------------------------------------------------------------
102
103 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000104 * Select the database
105 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200106 * @param string database name
107 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000108 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200109 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000110 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200111 if ($database === '')
112 {
113 $database = $this->database;
114 }
115
116 if ($this->_execute('USE '.$database))
117 {
118 $this->database = $database;
119 return TRUE;
120 }
121
122 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000123 }
124
125 // --------------------------------------------------------------------
126
127 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000128 * Execute the query
129 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000130 * @param string an SQL query
131 * @return resource
132 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200133 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000134 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200135 return sqlsrv_query($this->conn_id,
136 $sql,
137 NULL,
138 array('Scrollable'=> SQLSRV_CURSOR_STATIC, 'SendStreamParamsAtExec' => TRUE)
139 );
Alex Bilbie84445d02011-03-10 16:43:39 +0000140 }
141
142 // --------------------------------------------------------------------
143
144 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 * Begin Transaction
146 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000147 * @return bool
148 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200149 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 {
151 if ( ! $this->trans_enabled)
152 {
153 return TRUE;
154 }
155
156 // When transactions are nested we only begin/commit/rollback the outermost ones
157 if ($this->_trans_depth > 0)
158 {
159 return TRUE;
160 }
161
162 // Reset the transaction failure flag.
163 // If the $test_mode flag is set to TRUE transactions will be rolled back
164 // even if the queries produce a successful result.
165 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
166
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000167 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Commit Transaction
174 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000175 * @return bool
176 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200177 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000178 {
179 if ( ! $this->trans_enabled)
180 {
181 return TRUE;
182 }
183
184 // When transactions are nested we only begin/commit/rollback the outermost ones
185 if ($this->_trans_depth > 0)
186 {
187 return TRUE;
188 }
189
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000190 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Rollback Transaction
197 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000198 * @return bool
199 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200200 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000201 {
202 if ( ! $this->trans_enabled)
203 {
204 return TRUE;
205 }
206
207 // When transactions are nested we only begin/commit/rollback the outermost ones
208 if ($this->_trans_depth > 0)
209 {
210 return TRUE;
211 }
212
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000213 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Escape String
220 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000221 * @param string
222 * @param bool whether or not the string will be used in a LIKE condition
223 * @return string
224 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200225 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000226 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000228 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Affected Rows
235 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200236 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000237 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200238 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000239 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000240 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200246 * Insert ID
247 *
248 * Returns the last id created in the Identity column.
249 *
250 * @return string
251 */
252 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000253 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200254 $query = $this->query('SELECT @@IDENTITY AS insert_id');
255 $query = $query->row();
256 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200262 * Parse major version
263 *
264 * Grabs the major version number from the
265 * database server version string passed in.
266 *
267 * @param string $version
268 * @return int major version number
269 */
270 protected function _parse_major_version($version)
Alex Bilbie84445d02011-03-10 16:43:39 +0000271 {
272 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
273 return $ver_info[1]; // return the major version b/c that's all we're interested in.
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200279 * Database version number
280 *
281 * @return string
282 */
283 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000284 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200285 if (isset($this->data_cache['version']))
286 {
287 return $this->data_cache['version'];
288 }
289
290 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
291 {
292 return FALSE;
293 }
294
295 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * "Count All" query
302 *
303 * Generates a platform-specific query string that counts all records in
304 * the specified database
305 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000306 * @param string
Andrey Andreeve6297342012-03-20 16:25:07 +0200307 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000308 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200309 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000310 {
311 if ($table == '')
Andrey Andreeve6297342012-03-20 16:25:07 +0200312 {
313 return 0;
314 }
315
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000316 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000317 if ($query->num_rows() == 0)
Andrey Andreeve6297342012-03-20 16:25:07 +0200318 {
319 return 0;
320 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000321
322 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500323 $this->_reset_select();
Andrey Andreeve6297342012-03-20 16:25:07 +0200324 return (int) $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * List table query
331 *
332 * Generates a platform-specific query string so that the table names can be fetched
333 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200334 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000335 * @return string
336 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200337 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000338 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000339 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * List column query
346 *
347 * Generates a platform-specific query string so that the column names can be fetched
348 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000349 * @param string the table name
350 * @return string
351 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200352 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000353 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200354 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Field data query
361 *
362 * Generates a platform-specific query so that the column data can be retrieved
363 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000364 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200365 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000366 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200367 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000368 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200369 return 'SELECT TOP 1 * FROM '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000376 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 * Returns an array containing code and message of the last
378 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000379 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000381 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200382 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000383 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200384 $error = array('code' => '00000', 'message' => '');
385 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
386
387 if ( ! is_array($sqlsrv_errors))
Andrey Andreev850f6012012-03-01 15:58:25 +0200388 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200389 return $error;
Andrey Andreev850f6012012-03-01 15:58:25 +0200390 }
391
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 $sqlsrv_error = array_shift($sqlsrv_errors);
393 if (isset($sqlsrv_error['SQLSTATE']))
394 {
395 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
396 }
397 elseif (isset($sqlsrv_error['code']))
398 {
399 $error['code'] = $sqlsrv_error['code'];
400 }
401
402 if (isset($sqlsrv_error['message']))
403 {
404 $error['message'] = $sqlsrv_error['message'];
405 }
406
407 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Escape the SQL Identifiers
414 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400415 * This function escapes column and table names
Alex Bilbie84445d02011-03-10 16:43:39 +0000416 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000417 * @param string
418 * @return string
419 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200420 public function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000421 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000422 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000423 }
424
Timothy Warren0e20da12012-03-19 19:05:46 -0400425 // --------------------------------------------------------------------
Alex Bilbie84445d02011-03-10 16:43:39 +0000426
427 /**
428 * From Tables
429 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400430 * This function implicitly groups FROM tables so there is no confusion
Alex Bilbie84445d02011-03-10 16:43:39 +0000431 * about operator precedence in harmony with SQL standards
432 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200433 * @param array
434 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000435 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200436 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000437 {
438 if ( ! is_array($tables))
439 {
440 $tables = array($tables);
441 }
442
443 return implode(', ', $tables);
444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * Insert statement
450 *
451 * Generates a platform-specific insert string from the supplied data
452 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000453 * @param string the table name
454 * @param array the insert keys
455 * @param array the insert values
456 * @return string
457 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200458 protected function _insert($table, $keys, $values)
459 {
460 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Alex Bilbie84445d02011-03-10 16:43:39 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Update statement
467 *
468 * Generates a platform-specific update string from the supplied data
469 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000470 * @param string the table name
471 * @param array the update data
472 * @param array the where clause
473 * @param array the orderby clause
474 * @param array the limit clause
475 * @return string
476 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200477 protected function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000478 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000479 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000480 {
481 $valstr[] = $key." = ".$val;
482 }
Andrey Andreeve6297342012-03-20 16:25:07 +0200483
484 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000485 }
Andrey Andreeve6297342012-03-20 16:25:07 +0200486
Alex Bilbie84445d02011-03-10 16:43:39 +0000487 // --------------------------------------------------------------------
488
489 /**
490 * Truncate statement
491 *
492 * Generates a platform-specific truncate string from the supplied data
493 * If the database does not support the truncate() command
Timothy Warren0e20da12012-03-19 19:05:46 -0400494 * This function maps to "DELETE FROM table"
Alex Bilbie84445d02011-03-10 16:43:39 +0000495 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000496 * @param string the table name
497 * @return string
498 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200499 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000500 {
501 return "TRUNCATE ".$table;
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Delete statement
508 *
509 * Generates a platform-specific delete string from the supplied data
510 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000511 * @param string the table name
512 * @param array the where clause
513 * @param string the limit clause
514 * @return string
515 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200516 protected function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000517 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200518 return 'DELETE FROM '.$table.' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Limit string
525 *
526 * Generates a platform-specific LIMIT clause
527 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000528 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200529 * @param int the number of rows to limit the query to
530 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000531 * @return string
532 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200533 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000534 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200535 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Close DB Connection
542 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000543 * @param resource
544 * @return void
545 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200546 protected function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000547 {
548 @sqlsrv_close($conn_id);
549 }
550
551}
552
Andrey Andreeve6297342012-03-20 16:25:07 +0200553/* End of file sqlsrv_driver.php */
554/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */