blob: b2b17d0be06855c3790509c695d8a5e925c1e73c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev4da24f82012-01-25 21:54:23 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev4da24f82012-01-25 21:54:23 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MS SQL Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
53class CI_DB_mssql_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020060 public $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreeva24e52e2012-11-02 03:54:12 +020062 // --------------------------------------------------------------------
Andrey Andreev082ee2b2012-06-08 15:26:34 +030063
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030064 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020065 * ORDER BY random keyword
66 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020067 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020068 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020069 protected $_random_keyword = array('NEWID()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020070
71 /**
72 * Quoted identifier flag
73 *
74 * Whether to use SQL-92 standard quoted identifier
75 * (double quotes) or brackets for identifier escaping.
76 *
77 * @var bool
78 */
79 protected $_quoted_identifier = TRUE;
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Class constructor
Andrey Andreev082ee2b2012-06-08 15:26:34 +030085 *
86 * Appends the port number to the hostname, if needed.
87 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030088 * @param array $params
Andrey Andreev082ee2b2012-06-08 15:26:34 +030089 * @return void
90 */
Andrey Andreevbd601d32012-02-12 21:16:51 +020091 public function __construct($params)
92 {
93 parent::__construct($params);
94
Andrey Andreev2cb262f2012-03-28 13:45:04 +030095 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020096 {
97 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
98 }
99 }
100
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300101 // --------------------------------------------------------------------
102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 /**
104 * Non-persistent database connection
105 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200106 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Andrey Andreevfac37612012-07-02 14:56:20 +0300109 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300111 $this->conn_id = ($persistent)
Andrey Andreevf2818bd2014-02-25 15:26:20 +0200112 ? mssql_pconnect($this->hostname, $this->username, $this->password)
113 : mssql_connect($this->hostname, $this->username, $this->password);
Andrey Andreevfac37612012-07-02 14:56:20 +0300114
115 if ( ! $this->conn_id)
116 {
117 return FALSE;
118 }
119
Andrey Andreev1a001492013-01-24 11:32:29 +0200120 // ----------------------------------------------------------------
121
122 // Select the DB... assuming a database name is specified in the config file
123 if ($this->database !== '' && ! $this->db_select())
124 {
125 log_message('error', 'Unable to select database: '.$this->database);
126
127 return ($this->db_debug === TRUE)
128 ? $this->display_error('db_unable_to_select', $this->database)
129 : FALSE;
130 }
131
Andrey Andreevfac37612012-07-02 14:56:20 +0300132 // Determine how identifiers are escaped
133 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
134 $query = $query->row_array();
135 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
136 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
137
138 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // --------------------------------------------------------------------
142
143 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * Select the database
145 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200146 * @param string $database
Andrey Andreev4da24f82012-01-25 21:54:23 +0200147 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200148 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200149 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200151 if ($database === '')
152 {
153 $database = $this->database;
154 }
155
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300156 // Note: Escaping is required in the event that the DB name
Andrey Andreev60f744b2014-07-01 08:33:30 +0300157 // contains reserved characters.
158 if (mssql_select_db('['.$database.']', $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200159 {
160 $this->database = $database;
Andrey Andreeved6f0102016-06-08 11:06:59 +0300161 $this->data_cache = array();
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200162 return TRUE;
163 }
164
165 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * Execute the query
172 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200173 * @param string $sql an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200174 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200176 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300178 return mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Begin Transaction
185 *
Barry Mienydd671972010-10-04 16:33:58 +0200186 * @return bool
187 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300188 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200190 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Commit Transaction
197 *
Barry Mienydd671972010-10-04 16:33:58 +0200198 * @return bool
199 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300200 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200202 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Rollback Transaction
209 *
Barry Mienydd671972010-10-04 16:33:58 +0200210 * @return bool
211 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300212 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200214 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
218
219 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * Affected Rows
221 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200222 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200224 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300226 return mssql_rows_affected($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // --------------------------------------------------------------------
230
231 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200232 * Insert ID
233 *
234 * Returns the last id created in the Identity column.
235 *
236 * @return string
237 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200238 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300240 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200241 ? 'SELECT SCOPE_IDENTITY() AS last_id'
242 : 'SELECT @@IDENTITY AS last_id';
243
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200244 $query = $this->query($query);
245 $query = $query->row();
246 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248
249 // --------------------------------------------------------------------
250
251 /**
Andrey Andreev4173fa02012-11-19 11:14:23 +0200252 * Set client character set
253 *
254 * @param string $charset
255 * @return bool
256 */
257 protected function _db_set_charset($charset)
258 {
Andrey Andreevca39f2e2014-02-20 02:30:14 +0200259 return (ini_set('mssql.charset', $charset) !== FALSE);
Andrey Andreev4173fa02012-11-19 11:14:23 +0200260 }
261
262 // --------------------------------------------------------------------
263
264 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200265 * Version number query string
266 *
267 * @return string
268 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200269 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreev71a65c32015-12-11 17:21:51 +0200271 return "SELECT SERVERPROPERTY('ProductVersion') AS ver";
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * List table query
278 *
279 * Generates a platform-specific query string so that the table names can be fetched
280 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200281 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * @return string
283 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200284 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300286 $sql = 'SELECT '.$this->escape_identifiers('name')
287 .' FROM '.$this->escape_identifiers('sysobjects')
288 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200289
darwinelcf63aee2014-02-09 02:04:23 +0100290 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300292 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
293 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Andrey Andreev70c72c92012-06-25 00:04:51 +0300296 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * List column query
303 *
304 * Generates a platform-specific query string so that the column names can be fetched
305 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200306 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return string
308 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200309 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200311 return 'SELECT COLUMN_NAME
312 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200313 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200319 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200321 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200322 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200324 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200326 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
327 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200328 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200329
330 if (($query = $this->query($sql)) === FALSE)
331 {
332 return FALSE;
333 }
334 $query = $query->result_object();
335
336 $retval = array();
337 for ($i = 0, $c = count($query); $i < $c; $i++)
338 {
339 $retval[$i] = new stdClass();
340 $retval[$i]->name = $query[$i]->COLUMN_NAME;
341 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200342 $retval[$i]->max_length = ($query[$i]->CHARACTER_MAXIMUM_LENGTH > 0) ? $query[$i]->CHARACTER_MAXIMUM_LENGTH : $query[$i]->NUMERIC_PRECISION;
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200343 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
344 }
345
346 return $retval;
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
Andrey Andreev71d8f722017-01-17 12:01:00 +0200355 * database error that has occurred.
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 Andreevad4882f2015-09-16 11:29:50 +0300361 // We need this because the error info is discarded by the
362 // server the first time you request it, and query() already
363 // calls error() once for logging purposes when a query fails.
364 static $error = array('code' => 0, 'message' => NULL);
365
366 $message = mssql_get_last_message();
367 if ( ! empty($message))
368 {
369 $error['code'] = $this->query('SELECT @@ERROR AS code')->row()->code;
370 $error['message'] = $message;
371 }
372
373 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300379 * Update statement
380 *
381 * Generates a platform-specific update string from the supplied data
382 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200383 * @param string $table
384 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300385 * @return string
386 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300387 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300388 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300389 $this->qb_limit = FALSE;
390 $this->qb_orderby = array();
391 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300392 }
393
394 // --------------------------------------------------------------------
395
396 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * Truncate statement
398 *
399 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300400 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200401 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300402 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200404 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200406 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200407 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300409 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
413
414 /**
415 * Delete statement
416 *
417 * Generates a platform-specific delete string from the supplied data
418 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200419 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200421 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300422 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300424 if ($this->qb_limit)
425 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300426 return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete';
Andrey Andreevb0478652012-07-18 15:34:46 +0300427 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000428
Andrey Andreevb0478652012-07-18 15:34:46 +0300429 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200435 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 *
437 * Generates a platform-specific LIMIT clause
438 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200439 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * @return string
441 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300442 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300444 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300445
446 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
447 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300448 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300449 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300450 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300451
452 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300453 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300454
Andrey Andreev44514542012-10-23 15:35:09 +0300455 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
Andrey Andreev613b8982017-08-25 13:24:51 +0300456 if (count($this->qb_select) === 0 OR strpos(implode(',', $this->qb_select), '*') !== FALSE)
Andrey Andreev44514542012-10-23 15:35:09 +0300457 {
458 $select = '*'; // Inevitable
459 }
460 else
461 {
462 // Use only field names and their aliases, everything else is out of our scope.
463 $select = array();
464 $field_regexp = ($this->_quoted_identifier)
465 ? '("[^\"]+")' : '(\[[^\]]+\])';
466 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
467 {
468 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
469 ? $m[1] : $this->qb_select[$i];
470 }
471 $select = implode(', ', $select);
472 }
473
474 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300475 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
Andrey Andreev44514542012-10-23 15:35:09 +0300476 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300477 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300478 }
479
480 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200486 * Insert batch statement
487 *
488 * Generates a platform-specific insert string from the supplied data.
489 *
490 * @param string $table Table name
491 * @param array $keys INSERT keys
492 * @param array $values INSERT values
493 * @return string|bool
494 */
495 protected function _insert_batch($table, $keys, $values)
496 {
497 // Multiple-value inserts are only supported as of SQL Server 2008
498 if (version_compare($this->version(), '10', '>='))
499 {
500 return parent::_insert_batch($table, $keys, $values);
501 }
502
Andrey Andreevea073522017-03-14 18:42:12 +0200503 return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * Close DB Connection
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @return void
512 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300513 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300515 mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200516 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000517
518}