blob: 672c3161cc87822986a8e4ed6cfdb7a27c775086 [file] [log] [blame]
Andrey Andreev4da24f82012-01-25 21:54:23 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +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
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4da24f82012-01-25 21:54:23 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4da24f82012-01-25 21:54:23 +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 *
Derek Allard2067d1a2008-11-13 22:59:24 +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)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * MS SQL Database Adapter Class
30 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +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
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_mssql_driver extends CI_DB {
42
Andrey Andreev4da24f82012-01-25 21:54:23 +020043 public $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used for escaping
Andrey Andreev082ee2b2012-06-08 15:26:34 +030046 protected $_escape_char = '"';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreev1f619a82012-03-20 16:03:04 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevdd7242b2012-01-26 00:43:38 +020050 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Andrey Andreevdd7242b2012-01-26 00:43:38 +020052 protected $_random_keyword = ' NEWID()';
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Andrey Andreev082ee2b2012-06-08 15:26:34 +030054 // MSSQL-specific properties
55 protected $_quoted_identifier = TRUE;
56
57 /*
58 * Constructor
59 *
60 * Appends the port number to the hostname, if needed.
61 *
62 * @param array
63 * @return void
64 */
Andrey Andreevbd601d32012-02-12 21:16:51 +020065 public function __construct($params)
66 {
67 parent::__construct($params);
68
Andrey Andreev2cb262f2012-03-28 13:45:04 +030069 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020070 {
71 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
72 }
73 }
74
Andrey Andreev082ee2b2012-06-08 15:26:34 +030075 // --------------------------------------------------------------------
76
Derek Allard2067d1a2008-11-13 22:59:24 +000077 /**
78 * Non-persistent database connection
79 *
Andrey Andreevfac37612012-07-02 14:56:20 +030080 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020082 */
Andrey Andreevfac37612012-07-02 14:56:20 +030083 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Andrey Andreevfac37612012-07-02 14:56:20 +030085 $this->conn_id = ($persistent)
86 ? @mssql_pconnect($this->hostname, $this->username, $this->password)
87 : @mssql_connect($this->hostname, $this->username, $this->password);
88
89 if ( ! $this->conn_id)
90 {
91 return FALSE;
92 }
93
94 // Determine how identifiers are escaped
95 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
96 $query = $query->row_array();
97 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
98 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
99
100 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
106 * Persistent database connection
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200109 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200110 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300112 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * Select the database
119 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200120 * @param string database name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200121 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200122 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200123 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200125 if ($database === '')
126 {
127 $database = $this->database;
128 }
129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // Note: The brackets are required in the event that the DB name
131 // contains reserved characters
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300132 if (@mssql_select_db($this->escape_identifiers($database), $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200133 {
134 $this->database = $database;
135 return TRUE;
136 }
137
138 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * Execute the query
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200147 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200148 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200149 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 return @mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Begin Transaction
158 *
Barry Mienydd671972010-10-04 16:33:58 +0200159 * @return bool
160 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200161 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200164 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 return TRUE;
167 }
168
169 // Reset the transaction failure flag.
170 // If the $test_mode flag is set to TRUE transactions will be rolled back
171 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200172 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173
Andrey Andreev4da24f82012-01-25 21:54:23 +0200174 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Commit Transaction
181 *
Barry Mienydd671972010-10-04 16:33:58 +0200182 * @return bool
183 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200184 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200187 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 return TRUE;
190 }
191
Andrey Andreev4da24f82012-01-25 21:54:23 +0200192 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Rollback Transaction
199 *
Barry Mienydd671972010-10-04 16:33:58 +0200200 * @return bool
201 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200202 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 return TRUE;
208 }
209
Andrey Andreev4da24f82012-01-25 21:54:23 +0200210 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // --------------------------------------------------------------------
214
215 /**
216 * Escape String
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000219 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @return string
221 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200222 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000224 if (is_array($str))
225 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500226 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000228 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200229 }
230
231 return $str;
232 }
233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500235 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jonese4ed5832009-02-20 21:44:59 +0000237 // escape LIKE condition wildcards
238 if ($like === TRUE)
239 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200240 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100241 array($this->_like_escape_chr, '%', '_'),
242 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
243 $str
244 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Jonese4ed5832009-02-20 21:44:59 +0000247 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // --------------------------------------------------------------------
251
252 /**
253 * Affected Rows
254 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200255 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200257 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 return @mssql_rows_affected($this->conn_id);
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200265 * Insert ID
266 *
267 * Returns the last id created in the Identity column.
268 *
269 * @return string
270 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200271 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300273 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200274 ? 'SELECT SCOPE_IDENTITY() AS last_id'
275 : 'SELECT @@IDENTITY AS last_id';
276
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200277 $query = $this->query($query);
278 $query = $query->row();
279 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
282 // --------------------------------------------------------------------
283
284 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200285 * Version number query string
286 *
287 * @return string
288 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200289 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200291 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293
294 // --------------------------------------------------------------------
295
296 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * List table query
298 *
299 * Generates a platform-specific query string so that the table names can be fetched
300 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200301 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @return string
303 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200304 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300306 $sql = 'SELECT '.$this->escape_identifiers('name')
307 .' FROM '.$this->escape_identifiers('sysobjects')
308 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200309
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100310 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300312 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
313 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Andrey Andreev70c72c92012-06-25 00:04:51 +0300316 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * List column query
323 *
324 * Generates a platform-specific query string so that the column names can be fetched
325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @param string the table name
327 * @return string
328 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200329 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Barry Mienydd671972010-10-04 16:33:58 +0200331 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Field data query
338 *
339 * Generates a platform-specific query so that the column data can be retrieved
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200342 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200344 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300346 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 // --------------------------------------------------------------------
350
351 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200352 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200354 * Returns an array containing code and message of the last
355 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200357 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200359 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 $query = $this->query('SELECT @@ERROR AS code');
362 $query = $query->row();
363 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 // --------------------------------------------------------------------
367
368 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * From Tables
370 *
371 * This function implicitly groups FROM tables so there is no confusion
372 * about operator precedence in harmony with SQL standards
373 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200374 * @param array
375 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200377 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300379 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300385 * Update statement
386 *
387 * Generates a platform-specific update string from the supplied data
388 *
389 * @param string the table name
390 * @param array the update data
Andrey Andreev00541ae2012-04-09 11:43:10 +0300391 * @return string
392 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300393 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300394 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300395 $this->qb_limit = FALSE;
396 $this->qb_orderby = array();
397 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300398 }
399
400 // --------------------------------------------------------------------
401
402 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * Truncate statement
404 *
405 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300406 *
407 * If the database does not support the truncate() command,
408 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param string the table name
411 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200412 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200413 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300415 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
421 * Delete statement
422 *
423 * Generates a platform-specific delete string from the supplied data
424 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200427 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300428 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300430 if ($this->qb_limit)
431 {
432 return 'WITH ci_delete AS (SELECT TOP '.(int) $this->qb_limit.' * FROM '.$table.$this->_compile_where().') DELETE FROM ci_delete';
433 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000434
Andrey Andreevb0478652012-07-18 15:34:46 +0300435 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Limit string
442 *
443 * Generates a platform-specific LIMIT clause
444 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200446 * @param int the number of rows to limit the query to
447 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @return string
449 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200450 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Andrey Andreev71379ca2012-06-11 16:12:43 +0300452 // As of SQL Server 2012 (11.0.*) OFFSET is supported
453 if (version_compare($this->version(), '11', '>='))
454 {
455 return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
456 }
457
458 $limit = $offset + $limit;
459
460 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
461 // however an ORDER BY clause is required for it to work
462 if (version_compare($this->version(), '9', '>=') && $offset && ! empty($this->qb_orderby))
463 {
464 $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby);
465
466 // We have to strip the ORDER BY clause
467 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
468
469 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
470 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
471 ."\n) ".$this->escape_identifiers('CI_subquery')
472 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit;
473 }
474
475 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Close DB Connection
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return void
484 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300485 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300487 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200488 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000489
490}
491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300493/* Location: ./system/database/drivers/mssql/mssql_driver.php */