blob: 5a24f5532de07ab4bb9d81c491e6b1b56e7714fa [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
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 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
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreeve6297342012-03-20 16:25:07 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
Andrey Andreev3b2587e2012-03-28 13:39:34 +030058 protected $_random_keyword = ' NEWID()'; // 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(
Andrey Andreev3b2587e2012-03-28 13:39:34 +030071 'UID' => empty($this->username) ? '' : $this->username,
72 'PWD' => empty($this->password) ? '' : $this->password,
73 'Database' => $this->database,
74 'ConnectionPooling' => $pooling ? 1 : 0,
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000075 'CharacterSet' => $character_set,
Andrey Andreev3b2587e2012-03-28 13:39:34 +030076 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000077 );
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 Andreev846acc72012-05-24 23:27:46 +0300135 return (is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
136 ? sqlsrv_query($this->conn_id, $sql)
137 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
Alex Bilbie84445d02011-03-10 16:43:39 +0000138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Begin Transaction
144 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 * @return bool
146 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200147 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000148 {
149 if ( ! $this->trans_enabled)
150 {
151 return TRUE;
152 }
153
154 // When transactions are nested we only begin/commit/rollback the outermost ones
155 if ($this->_trans_depth > 0)
156 {
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.
163 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
164
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 Andreeve6297342012-03-20 16:25:07 +0200175 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000176 {
177 if ( ! $this->trans_enabled)
178 {
179 return TRUE;
180 }
181
182 // When transactions are nested we only begin/commit/rollback the outermost ones
183 if ($this->_trans_depth > 0)
184 {
185 return TRUE;
186 }
187
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000188 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000189 }
190
191 // --------------------------------------------------------------------
192
193 /**
194 * Rollback Transaction
195 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000196 * @return bool
197 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200198 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000199 {
200 if ( ! $this->trans_enabled)
201 {
202 return TRUE;
203 }
204
205 // When transactions are nested we only begin/commit/rollback the outermost ones
206 if ($this->_trans_depth > 0)
207 {
208 return TRUE;
209 }
210
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000211 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Escape String
218 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000219 * @param string
220 * @param bool whether or not the string will be used in a LIKE condition
221 * @return string
222 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200223 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000224 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000225 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000226 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Affected Rows
233 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200234 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000235 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200236 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000237 {
Andrey Andreev846acc72012-05-24 23:27:46 +0300238 return sqlrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200244 * Insert ID
245 *
246 * Returns the last id created in the Identity column.
247 *
248 * @return string
249 */
250 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000251 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200252 $query = $this->query('SELECT @@IDENTITY AS insert_id');
253 $query = $query->row();
254 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000255 }
256
257 // --------------------------------------------------------------------
258
259 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200260 * Database version number
261 *
262 * @return string
263 */
264 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000265 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200266 if (isset($this->data_cache['version']))
267 {
268 return $this->data_cache['version'];
269 }
270
271 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
272 {
273 return FALSE;
274 }
275
276 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * "Count All" query
283 *
284 * Generates a platform-specific query string that counts all records in
285 * the specified database
286 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000287 * @param string
Andrey Andreeve6297342012-03-20 16:25:07 +0200288 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000289 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200290 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000291 {
292 if ($table == '')
Andrey Andreeve6297342012-03-20 16:25:07 +0200293 {
294 return 0;
295 }
296
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000297 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000298 if ($query->num_rows() == 0)
Andrey Andreeve6297342012-03-20 16:25:07 +0200299 {
300 return 0;
301 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000302
303 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500304 $this->_reset_select();
Andrey Andreeve6297342012-03-20 16:25:07 +0200305 return (int) $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * List table query
312 *
313 * Generates a platform-specific query string so that the table names can be fetched
314 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200315 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000316 * @return string
317 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200318 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000319 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000320 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * List column query
327 *
328 * Generates a platform-specific query string so that the column names can be fetched
329 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000330 * @param string the table name
331 * @return string
332 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200333 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000334 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200335 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Field data query
342 *
343 * Generates a platform-specific query so that the column data can be retrieved
344 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000345 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200346 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000347 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200348 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000349 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200350 return 'SELECT TOP 1 * FROM '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000351 }
352
353 // --------------------------------------------------------------------
354
355 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200356 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000357 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 * Returns an array containing code and message of the last
359 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000360 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000362 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200363 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000364 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200365 $error = array('code' => '00000', 'message' => '');
366 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
367
368 if ( ! is_array($sqlsrv_errors))
Andrey Andreev850f6012012-03-01 15:58:25 +0200369 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 return $error;
Andrey Andreev850f6012012-03-01 15:58:25 +0200371 }
372
Andrey Andreev4be5de12012-03-02 15:45:41 +0200373 $sqlsrv_error = array_shift($sqlsrv_errors);
374 if (isset($sqlsrv_error['SQLSTATE']))
375 {
376 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
377 }
378 elseif (isset($sqlsrv_error['code']))
379 {
380 $error['code'] = $sqlsrv_error['code'];
381 }
382
383 if (isset($sqlsrv_error['message']))
384 {
385 $error['message'] = $sqlsrv_error['message'];
386 }
387
388 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000389 }
390
391 // --------------------------------------------------------------------
392
393 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000394 * From Tables
395 *
396 * This function implicitly groups FROM tables so there is no confusion
397 * about operator precedence in harmony with SQL standards
398 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200399 * @param array
400 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000401 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200402 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000403 {
404 if ( ! is_array($tables))
405 {
406 $tables = array($tables);
407 }
408
409 return implode(', ', $tables);
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000415 * Update statement
416 *
417 * Generates a platform-specific update string from the supplied data
418 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000419 * @param string the table name
420 * @param array the update data
421 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300422 * @param array the orderby clause (ignored)
423 * @param array the limit clause (ignored)
424 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000425 * @return string
426 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300427 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Alex Bilbie84445d02011-03-10 16:43:39 +0000428 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000429 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000430 {
Andrey Andreev00541ae2012-04-09 11:43:10 +0300431 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000432 }
Andrey Andreeve6297342012-03-20 16:25:07 +0200433
Andrey Andreev00541ae2012-04-09 11:43:10 +0300434 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
435
436 if ( ! empty($like))
437 {
438 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
439 }
440
st24b47e982012-04-24 17:45:44 +0800441 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where;
Alex Bilbie84445d02011-03-10 16:43:39 +0000442 }
Andrey Andreeve6297342012-03-20 16:25:07 +0200443
Alex Bilbie84445d02011-03-10 16:43:39 +0000444 // --------------------------------------------------------------------
445
446 /**
447 * Truncate statement
448 *
449 * Generates a platform-specific truncate string from the supplied data
Alex Bilbie84445d02011-03-10 16:43:39 +0000450 *
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300451 * If the database does not support the truncate() command,
452 * then this method maps to 'DELETE FROM table'
453 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000454 * @param string the table name
455 * @return string
456 */
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300457 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000458 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300459 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Delete statement
466 *
467 * Generates a platform-specific delete string from the supplied data
468 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000469 * @param string the table name
470 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300471 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000472 * @param string the limit clause
473 * @return string
474 */
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300475 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000476 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300477 $conditions = array();
478
479 empty($where) OR $conditions[] = implode(' ', $where);
480 empty($like) OR $conditions[] = implode(' ', $like);
481
482 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
483
484 return ($limit)
485 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
486 : 'DELETE FROM '.$table.$conditions;
Alex Bilbie84445d02011-03-10 16:43:39 +0000487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Limit string
493 *
494 * Generates a platform-specific LIMIT clause
495 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000496 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200497 * @param int the number of rows to limit the query to
498 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000499 * @return string
500 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200501 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000502 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200503 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000504 }
505
506 // --------------------------------------------------------------------
507
508 /**
509 * Close DB Connection
510 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000511 * @return void
512 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300513 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000514 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300515 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000516 }
517
518}
519
Andrey Andreeve6297342012-03-20 16:25:07 +0200520/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300521/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */