blob: 05d35a42fcb3cc5510d709c942c075bfc9719c4e [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Alex Bilbie84445d02011-03-10 16:43:39 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Alex Bilbie84445d02011-03-10 16:43:39 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev0e8968a2012-01-26 02:04:37 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev0e8968a2012-01-26 02:04:37 +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 2.0.3
Alex Bilbie84445d02011-03-10 16:43:39 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +000039
Alex Bilbie84445d02011-03-10 16:43:39 +000040/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000041 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000042 *
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
Alex Bilbie84445d02011-03-10 16:43:39 +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/
Alex Bilbie84445d02011-03-10 16:43:39 +000052 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000053class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000054
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020060 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000061
Andrey Andreevba8bf562014-01-21 19:04:18 +020062 /**
63 * Scrollable flag
64 *
65 * Determines what cursor type to use when executing queries.
66 *
67 * FALSE or SQLSRV_CURSOR_FORWARD would increase performance,
68 * but would disable num_rows() (and possibly insert_id())
69 *
70 * @var mixed
71 */
72 public $scrollable;
73
Andrey Andreeva24e52e2012-11-02 03:54:12 +020074 // --------------------------------------------------------------------
Andrey Andreev082ee2b2012-06-08 15:26:34 +030075
Alex Bilbie84445d02011-03-10 16:43:39 +000076 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020077 * ORDER BY random keyword
Alex Bilbie84445d02011-03-10 16:43:39 +000078 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020079 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020080 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020081 protected $_random_keyword = array('NEWID()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020082
83 /**
84 * Quoted identifier flag
85 *
86 * Whether to use SQL-92 standard quoted identifier
87 * (double quotes) or brackets for identifier escaping.
88 *
89 * @var bool
90 */
91 protected $_quoted_identifier = TRUE;
92
93 // --------------------------------------------------------------------
94
95 /**
Andrey Andreevba8bf562014-01-21 19:04:18 +020096 * Class constructor
97 *
98 * @param array $params
99 * @return void
100 */
101 public function __construct($params)
102 {
103 parent::__construct($params);
104
105 // This is only supported as of SQLSRV 3.0
106 if ($this->scrollable === NULL)
107 {
108 $this->scrollable = defined('SQLSRV_CURSOR_CLIENT_BUFFERED')
109 ? SQLSRV_CURSOR_CLIENT_BUFFERED
110 : FALSE;
111 }
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200117 * Database connection
118 *
119 * @param bool $pooling
Alex Bilbie84445d02011-03-10 16:43:39 +0000120 * @return resource
121 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200122 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000123 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300124 $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE)
125 ? 'UTF-8' : SQLSRV_ENC_CHAR;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000126
127 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200128 'UID' => empty($this->username) ? '' : $this->username,
129 'PWD' => empty($this->password) ? '' : $this->password,
130 'Database' => $this->database,
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300131 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0,
132 'CharacterSet' => $charset,
133 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0,
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200134 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000135 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200136
137 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000138 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200139 if (empty($connection['UID']) && empty($connection['PWD']))
140 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000141 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +0000142 }
143
Andrey Andreeveb492582015-08-19 11:16:52 +0300144 if (FALSE !== ($this->conn_id = sqlsrv_connect($this->hostname, $connection)))
145 {
146 // Determine how identifiers are escaped
147 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
148 $query = $query->row_array();
149 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
150 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
151 }
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300152
Andrey Andreevfac37612012-07-02 14:56:20 +0300153 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000154 }
155
156 // --------------------------------------------------------------------
157
158 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000159 * Select the database
160 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200161 * @param string $database
Andrey Andreev11454e02012-02-22 16:05:47 +0200162 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000163 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200164 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000165 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200166 if ($database === '')
167 {
168 $database = $this->database;
169 }
170
Eco91d0822ce2012-11-19 20:49:38 +0100171 if ($this->_execute('USE '.$this->escape_identifiers($database)))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200172 {
173 $this->database = $database;
Andrey Andreeved6f0102016-06-08 11:06:59 +0300174 $this->data_cache = array();
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200175 return TRUE;
176 }
177
178 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000179 }
180
181 // --------------------------------------------------------------------
182
183 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000184 * Execute the query
185 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200186 * @param string $sql an SQL query
Alex Bilbie84445d02011-03-10 16:43:39 +0000187 * @return resource
188 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200189 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000190 {
Andrey Andreevba8bf562014-01-21 19:04:18 +0200191 return ($this->scrollable === FALSE OR $this->is_write_type($sql))
Andrey Andreev846acc72012-05-24 23:27:46 +0300192 ? sqlsrv_query($this->conn_id, $sql)
Andrey Andreevba8bf562014-01-21 19:04:18 +0200193 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => $this->scrollable));
Alex Bilbie84445d02011-03-10 16:43:39 +0000194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Begin Transaction
200 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000201 * @return bool
202 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300203 protected function _trans_begin()
Alex Bilbie84445d02011-03-10 16:43:39 +0000204 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000205 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Commit Transaction
212 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000213 * @return bool
214 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300215 protected function _trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000216 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000217 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Rollback Transaction
224 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000225 * @return bool
226 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300227 protected function _trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000228 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000229 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000230 }
231
232 // --------------------------------------------------------------------
233
234 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000235 * Affected Rows
236 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200237 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000238 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200239 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000240 {
Andrey Andreevede49ba2012-07-23 16:06:36 +0300241 return sqlsrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200247 * Insert ID
248 *
249 * Returns the last id created in the Identity column.
250 *
251 * @return string
252 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200253 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000254 {
Andrey Andreev9d02cea2015-10-13 14:38:30 +0300255 return $this->query('SELECT SCOPE_IDENTITY() AS insert_id')->row()->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000256 }
257
258 // --------------------------------------------------------------------
259
260 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200261 * Database version number
262 *
263 * @return string
264 */
265 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000266 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200267 if (isset($this->data_cache['version']))
268 {
269 return $this->data_cache['version'];
270 }
271
Andrey Andreev2b730372012-11-05 17:01:11 +0200272 if ( ! $this->conn_id OR ($info = sqlsrv_server_info($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200273 {
274 return FALSE;
275 }
276
277 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000278 }
279
280 // --------------------------------------------------------------------
281
282 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000283 * List table query
284 *
285 * Generates a platform-specific query string so that the table names can be fetched
286 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200287 * @param bool
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200288 * @return string $prefix_limit
Alex Bilbie84445d02011-03-10 16:43:39 +0000289 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200290 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000291 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300292 $sql = 'SELECT '.$this->escape_identifiers('name')
293 .' FROM '.$this->escape_identifiers('sysobjects')
294 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
295
296 if ($prefix_limit === TRUE && $this->dbprefix !== '')
297 {
298 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
299 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
300 }
301
302 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * List column query
309 *
310 * Generates a platform-specific query string so that the column names can be fetched
311 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200312 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000313 * @return string
314 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200315 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000316 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200317 return 'SELECT COLUMN_NAME
318 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200319 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Alex Bilbie84445d02011-03-10 16:43:39 +0000320 }
321
322 // --------------------------------------------------------------------
323
324 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200325 * Returns an object with field data
Alex Bilbie84445d02011-03-10 16:43:39 +0000326 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200327 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200328 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000329 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200330 public function field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000331 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200332 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
333 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200334 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200335
336 if (($query = $this->query($sql)) === FALSE)
337 {
338 return FALSE;
339 }
340 $query = $query->result_object();
341
342 $retval = array();
343 for ($i = 0, $c = count($query); $i < $c; $i++)
344 {
345 $retval[$i] = new stdClass();
346 $retval[$i]->name = $query[$i]->COLUMN_NAME;
347 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200348 $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 +0200349 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
350 }
351
352 return $retval;
Alex Bilbie84445d02011-03-10 16:43:39 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000359 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200360 * Returns an array containing code and message of the last
Andrey Andreev71d8f722017-01-17 12:01:00 +0200361 * database error that has occurred.
Alex Bilbie84445d02011-03-10 16:43:39 +0000362 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200363 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000364 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200365 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000366 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200367 $error = array('code' => '00000', 'message' => '');
368 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
369
370 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200371 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200372 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200373 }
374
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 $sqlsrv_error = array_shift($sqlsrv_errors);
376 if (isset($sqlsrv_error['SQLSTATE']))
377 {
378 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
379 }
380 elseif (isset($sqlsrv_error['code']))
381 {
382 $error['code'] = $sqlsrv_error['code'];
383 }
384
385 if (isset($sqlsrv_error['message']))
386 {
387 $error['message'] = $sqlsrv_error['message'];
388 }
389
390 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000396 * Update statement
397 *
398 * Generates a platform-specific update string from the supplied data
399 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200400 * @param string $table
401 * @param array $values
Alex Bilbie84445d02011-03-10 16:43:39 +0000402 * @return string
403 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300404 protected function _update($table, $values)
Alex Bilbie84445d02011-03-10 16:43:39 +0000405 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300406 $this->qb_limit = FALSE;
407 $this->qb_orderby = array();
408 return parent::_update($table, $values);
Alex Bilbie84445d02011-03-10 16:43:39 +0000409 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200410
Alex Bilbie84445d02011-03-10 16:43:39 +0000411 // --------------------------------------------------------------------
412
413 /**
414 * Truncate statement
415 *
416 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300417 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200418 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300419 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000420 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200421 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000422 * @return string
423 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200424 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000425 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300426 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Delete statement
433 *
434 * Generates a platform-specific delete string from the supplied data
435 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200436 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000437 * @return string
438 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300439 protected function _delete($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000440 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300441 if ($this->qb_limit)
442 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300443 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 +0300444 }
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300445
Andrey Andreevb0478652012-07-18 15:34:46 +0300446 return parent::_delete($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000447 }
448
449 // --------------------------------------------------------------------
450
451 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200452 * LIMIT
Alex Bilbie84445d02011-03-10 16:43:39 +0000453 *
454 * Generates a platform-specific LIMIT clause
455 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200456 * @param string $sql SQL Query
Alex Bilbie84445d02011-03-10 16:43:39 +0000457 * @return string
458 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300459 protected function _limit($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000460 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300461 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300462 if (version_compare($this->version(), '11', '>='))
463 {
Andrey Andreeved3fc512014-11-18 11:12:35 +0200464 // SQL Server OFFSET-FETCH can be used only with the ORDER BY clause
465 empty($this->qb_orderby) && $sql .= ' ORDER BY 1';
466
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300467 return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
Andrey Andreev71379ca2012-06-11 16:12:43 +0300468 }
469
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300470 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300471
472 // An ORDER BY clause is required for ROW_NUMBER() to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300473 if ($this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300474 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300475 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300476
477 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300478 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300479
Andrey Andreev44514542012-10-23 15:35:09 +0300480 // 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 +0300481 if (count($this->qb_select) === 0 OR strpos(implode(',', $this->qb_select), '*') !== FALSE)
Andrey Andreev44514542012-10-23 15:35:09 +0300482 {
483 $select = '*'; // Inevitable
484 }
485 else
486 {
487 // Use only field names and their aliases, everything else is out of our scope.
488 $select = array();
489 $field_regexp = ($this->_quoted_identifier)
490 ? '("[^\"]+")' : '(\[[^\]]+\])';
491 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
492 {
493 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
494 ? $m[1] : $this->qb_select[$i];
495 }
496 $select = implode(', ', $select);
497 }
498
499 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300500 .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 +0300501 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300502 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300503 }
504
505 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000506 }
507
508 // --------------------------------------------------------------------
509
510 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200511 * Insert batch statement
512 *
513 * Generates a platform-specific insert string from the supplied data.
514 *
515 * @param string $table Table name
516 * @param array $keys INSERT keys
517 * @param array $values INSERT values
518 * @return string|bool
519 */
520 protected function _insert_batch($table, $keys, $values)
521 {
522 // Multiple-value inserts are only supported as of SQL Server 2008
523 if (version_compare($this->version(), '10', '>='))
524 {
525 return parent::_insert_batch($table, $keys, $values);
526 }
527
Andrey Andreevea073522017-03-14 18:42:12 +0200528 return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200529 }
530
531 // --------------------------------------------------------------------
532
533 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000534 * Close DB Connection
535 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000536 * @return void
537 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300538 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000539 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300540 sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000541 }
542
543}