blob: 6afde6153afbc8e3e8da96b551cf2b83603c7dbe [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 *
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Alex Bilbie84445d02011-03-10 16:43:39 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
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 Andreevfac37612012-07-02 14:56:20 +0300144 $this->conn_id = sqlsrv_connect($this->hostname, $connection);
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300145
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();
Andrey Andreev70c72c92012-06-25 00:04:51 +0300149 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300150 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
151
Andrey Andreevfac37612012-07-02 14:56:20 +0300152 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000153 }
154
155 // --------------------------------------------------------------------
156
157 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000158 * Select the database
159 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200160 * @param string $database
Andrey Andreev11454e02012-02-22 16:05:47 +0200161 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000162 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200163 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000164 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200165 if ($database === '')
166 {
167 $database = $this->database;
168 }
169
Eco91d0822ce2012-11-19 20:49:38 +0100170 if ($this->_execute('USE '.$this->escape_identifiers($database)))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200171 {
172 $this->database = $database;
173 return TRUE;
174 }
175
176 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000177 }
178
179 // --------------------------------------------------------------------
180
181 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000182 * Execute the query
183 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200184 * @param string $sql an SQL query
Alex Bilbie84445d02011-03-10 16:43:39 +0000185 * @return resource
186 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200187 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000188 {
Andrey Andreevba8bf562014-01-21 19:04:18 +0200189 return ($this->scrollable === FALSE OR $this->is_write_type($sql))
Andrey Andreev846acc72012-05-24 23:27:46 +0300190 ? sqlsrv_query($this->conn_id, $sql)
Andrey Andreevba8bf562014-01-21 19:04:18 +0200191 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => $this->scrollable));
Alex Bilbie84445d02011-03-10 16:43:39 +0000192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Begin Transaction
198 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200199 * @param bool $test_mode
Alex Bilbie84445d02011-03-10 16:43:39 +0000200 * @return bool
201 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200202 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000203 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000206 {
207 return TRUE;
208 }
209
210 // Reset the transaction failure flag.
211 // If the $test_mode flag is set to TRUE transactions will be rolled back
212 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200213 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000214
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000215 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Commit Transaction
222 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000223 * @return bool
224 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200225 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000226 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200228 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 {
230 return TRUE;
231 }
232
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000233 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Rollback Transaction
240 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 * @return bool
242 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200243 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000244 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000245 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200246 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000247 {
248 return TRUE;
249 }
250
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000251 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000252 }
253
254 // --------------------------------------------------------------------
255
256 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000257 * Affected Rows
258 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200259 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000260 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200261 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000262 {
Andrey Andreevede49ba2012-07-23 16:06:36 +0300263 return sqlsrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000264 }
265
266 // --------------------------------------------------------------------
267
268 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200269 * Insert ID
270 *
271 * Returns the last id created in the Identity column.
272 *
273 * @return string
274 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200275 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000276 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200277 $query = $this->query('SELECT @@IDENTITY AS insert_id');
278 $query = $query->row();
279 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000280 }
281
282 // --------------------------------------------------------------------
283
284 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200285 * Database version number
286 *
287 * @return string
288 */
289 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000290 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200291 if (isset($this->data_cache['version']))
292 {
293 return $this->data_cache['version'];
294 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200295 elseif ( ! $this->conn_id)
296 {
297 $this->initialize();
298 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200299
Andrey Andreev2b730372012-11-05 17:01:11 +0200300 if ( ! $this->conn_id OR ($info = sqlsrv_server_info($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200301 {
302 return FALSE;
303 }
304
305 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000306 }
307
308 // --------------------------------------------------------------------
309
310 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000311 * List table query
312 *
313 * Generates a platform-specific query string so that the table names can be fetched
314 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200315 * @param bool
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200316 * @return string $prefix_limit
Alex Bilbie84445d02011-03-10 16:43:39 +0000317 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200318 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000319 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300320 $sql = 'SELECT '.$this->escape_identifiers('name')
321 .' FROM '.$this->escape_identifiers('sysobjects')
322 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
323
324 if ($prefix_limit === TRUE && $this->dbprefix !== '')
325 {
326 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
327 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
328 }
329
330 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * List column query
337 *
338 * Generates a platform-specific query string so that the column names can be fetched
339 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200340 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000341 * @return string
342 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200343 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000344 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200345 return 'SELECT COLUMN_NAME
346 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200347 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Alex Bilbie84445d02011-03-10 16:43:39 +0000348 }
349
350 // --------------------------------------------------------------------
351
352 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200353 * Returns an object with field data
Alex Bilbie84445d02011-03-10 16:43:39 +0000354 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200355 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200356 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000357 */
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200358 public function field_data($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000359 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200360 if ($table === '')
361 {
362 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
363 }
364
365 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
366 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200367 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200368
369 if (($query = $this->query($sql)) === FALSE)
370 {
371 return FALSE;
372 }
373 $query = $query->result_object();
374
375 $retval = array();
376 for ($i = 0, $c = count($query); $i < $c; $i++)
377 {
378 $retval[$i] = new stdClass();
379 $retval[$i]->name = $query[$i]->COLUMN_NAME;
380 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200381 $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 +0200382 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
383 }
384
385 return $retval;
Alex Bilbie84445d02011-03-10 16:43:39 +0000386 }
387
388 // --------------------------------------------------------------------
389
390 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200391 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000392 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200393 * Returns an array containing code and message of the last
394 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000395 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200396 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000397 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200398 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000399 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200400 $error = array('code' => '00000', 'message' => '');
401 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
402
403 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200404 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200405 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200406 }
407
Andrey Andreev4be5de12012-03-02 15:45:41 +0200408 $sqlsrv_error = array_shift($sqlsrv_errors);
409 if (isset($sqlsrv_error['SQLSTATE']))
410 {
411 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
412 }
413 elseif (isset($sqlsrv_error['code']))
414 {
415 $error['code'] = $sqlsrv_error['code'];
416 }
417
418 if (isset($sqlsrv_error['message']))
419 {
420 $error['message'] = $sqlsrv_error['message'];
421 }
422
423 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000429 * Update statement
430 *
431 * Generates a platform-specific update string from the supplied data
432 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200433 * @param string $table
434 * @param array $values
Alex Bilbie84445d02011-03-10 16:43:39 +0000435 * @return string
436 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300437 protected function _update($table, $values)
Alex Bilbie84445d02011-03-10 16:43:39 +0000438 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300439 $this->qb_limit = FALSE;
440 $this->qb_orderby = array();
441 return parent::_update($table, $values);
Alex Bilbie84445d02011-03-10 16:43:39 +0000442 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200443
Alex Bilbie84445d02011-03-10 16:43:39 +0000444 // --------------------------------------------------------------------
445
446 /**
447 * Truncate statement
448 *
449 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300450 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200451 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300452 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000453 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200454 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000455 * @return string
456 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200457 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000458 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300459 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Delete statement
466 *
467 * Generates a platform-specific delete string from the supplied data
468 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200469 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000470 * @return string
471 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300472 protected function _delete($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000473 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300474 if ($this->qb_limit)
475 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300476 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 +0300477 }
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300478
Andrey Andreevb0478652012-07-18 15:34:46 +0300479 return parent::_delete($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000480 }
481
482 // --------------------------------------------------------------------
483
484 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200485 * LIMIT
Alex Bilbie84445d02011-03-10 16:43:39 +0000486 *
487 * Generates a platform-specific LIMIT clause
488 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200489 * @param string $sql SQL Query
Alex Bilbie84445d02011-03-10 16:43:39 +0000490 * @return string
491 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300492 protected function _limit($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000493 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300494 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300495 if (version_compare($this->version(), '11', '>='))
496 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300497 return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
Andrey Andreev71379ca2012-06-11 16:12:43 +0300498 }
499
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300500 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300501
502 // An ORDER BY clause is required for ROW_NUMBER() to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300503 if ($this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300504 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300505 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300506
507 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300508 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300509
Andrey Andreev44514542012-10-23 15:35:09 +0300510 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
511 if (count($this->qb_select) === 0)
512 {
513 $select = '*'; // Inevitable
514 }
515 else
516 {
517 // Use only field names and their aliases, everything else is out of our scope.
518 $select = array();
519 $field_regexp = ($this->_quoted_identifier)
520 ? '("[^\"]+")' : '(\[[^\]]+\])';
521 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
522 {
523 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
524 ? $m[1] : $this->qb_select[$i];
525 }
526 $select = implode(', ', $select);
527 }
528
529 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300530 .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 +0300531 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300532 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300533 }
534
535 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000536 }
537
538 // --------------------------------------------------------------------
539
540 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200541 * Insert batch statement
542 *
543 * Generates a platform-specific insert string from the supplied data.
544 *
545 * @param string $table Table name
546 * @param array $keys INSERT keys
547 * @param array $values INSERT values
548 * @return string|bool
549 */
550 protected function _insert_batch($table, $keys, $values)
551 {
552 // Multiple-value inserts are only supported as of SQL Server 2008
553 if (version_compare($this->version(), '10', '>='))
554 {
555 return parent::_insert_batch($table, $keys, $values);
556 }
557
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200558 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200559 }
560
561 // --------------------------------------------------------------------
562
563 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000564 * Close DB Connection
565 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000566 * @return void
567 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300568 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000569 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300570 sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000571 }
572
573}
574
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200575/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300576/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */