blob: 4fdc4aae07af42ee1df547887a601cb3c237dd5f [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();
Andrey Andreev70c72c92012-06-25 00:04:51 +030094 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +030095 $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 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300287 $sql = 'SELECT '.$this->escape_identifiers('name')
288 .' FROM '.$this->escape_identifiers('sysobjects')
289 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
290
291 if ($prefix_limit === TRUE && $this->dbprefix !== '')
292 {
293 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
294 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
295 }
296
297 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * List column query
304 *
305 * Generates a platform-specific query string so that the column names can be fetched
306 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000307 * @param string the table name
308 * @return string
309 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200310 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000311 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200312 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Field data query
319 *
320 * Generates a platform-specific query so that the column data can be retrieved
321 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000322 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200323 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000324 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200325 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000326 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300327 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000328 }
329
330 // --------------------------------------------------------------------
331
332 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200333 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000334 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200335 * Returns an array containing code and message of the last
336 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000337 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200338 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000339 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200340 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000341 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200342 $error = array('code' => '00000', 'message' => '');
343 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
344
345 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200346 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200347 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200348 }
349
Andrey Andreev4be5de12012-03-02 15:45:41 +0200350 $sqlsrv_error = array_shift($sqlsrv_errors);
351 if (isset($sqlsrv_error['SQLSTATE']))
352 {
353 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
354 }
355 elseif (isset($sqlsrv_error['code']))
356 {
357 $error['code'] = $sqlsrv_error['code'];
358 }
359
360 if (isset($sqlsrv_error['message']))
361 {
362 $error['message'] = $sqlsrv_error['message'];
363 }
364
365 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000366 }
367
368 // --------------------------------------------------------------------
369
370 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000371 * From Tables
372 *
373 * This function implicitly groups FROM tables so there is no confusion
374 * about operator precedence in harmony with SQL standards
375 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200376 * @param array
377 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000378 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200379 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000380 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300381 return is_array($tables) ? implode(', ', $tables) : $tables;
Alex Bilbie84445d02011-03-10 16:43:39 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000387 * Update statement
388 *
389 * Generates a platform-specific update string from the supplied data
390 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000391 * @param string the table name
392 * @param array the update data
393 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300394 * @param array the orderby clause (ignored)
395 * @param array the limit clause (ignored)
396 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000397 * @return string
398 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300399 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Alex Bilbie84445d02011-03-10 16:43:39 +0000400 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200401 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000402 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200403 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200405
Andrey Andreev00541ae2012-04-09 11:43:10 +0300406 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
407
408 if ( ! empty($like))
409 {
410 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
411 }
412
st24b47e982012-04-24 17:45:44 +0800413 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where;
Alex Bilbie84445d02011-03-10 16:43:39 +0000414 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200415
Alex Bilbie84445d02011-03-10 16:43:39 +0000416 // --------------------------------------------------------------------
417
418 /**
419 * Truncate statement
420 *
421 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300422 *
423 * If the database does not support the truncate() command,
424 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000425 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000426 * @param string the table name
427 * @return string
428 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200429 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000430 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300431 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Delete statement
438 *
439 * Generates a platform-specific delete string from the supplied data
440 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000441 * @param string the table name
442 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300443 * @param array the like clause
Alex Bilbie84445d02011-03-10 16:43:39 +0000444 * @param string the limit clause
445 * @return string
446 */
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300447 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000448 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300449 $conditions = array();
450
451 empty($where) OR $conditions[] = implode(' ', $where);
452 empty($like) OR $conditions[] = implode(' ', $like);
453
454 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
455
456 return ($limit)
457 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
458 : 'DELETE FROM '.$table.$conditions;
Alex Bilbie84445d02011-03-10 16:43:39 +0000459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Limit string
465 *
466 * Generates a platform-specific LIMIT clause
467 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000468 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200469 * @param int the number of rows to limit the query to
470 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000471 * @return string
472 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200473 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000474 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300475 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300476 if (version_compare($this->version(), '11', '>='))
477 {
478 return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
479 }
480
481 $limit = $offset + $limit;
482
483 // An ORDER BY clause is required for ROW_NUMBER() to work
484 if ($offset && ! empty($this->qb_orderby))
485 {
486 $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby);
487
488 // We have to strip the ORDER BY clause
489 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
490
491 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
492 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
493 ."\n) ".$this->escape_identifiers('CI_subquery')
494 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit;
495 }
496
497 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Close DB Connection
504 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000505 * @return void
506 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300507 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000508 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300509 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000510 }
511
512}
513
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200514/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300515/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */