blob: a6f2d55372b453d1281203b02964706c6f2afe3d [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0e8968a2012-01-26 02:04:37 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0e8968a2012-01-26 02:04:37 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Alex Bilbie84445d02011-03-10 16:43:39 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Alex Bilbie84445d02011-03-10 16:43:39 +000023 * @link http://codeigniter.com
Andrey Andreevbf940582012-06-10 07:05:05 +030024 * @since Version 2.0.3
Alex Bilbie84445d02011-03-10 16:43:39 +000025 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +000028
Alex Bilbie84445d02011-03-10 16:43:39 +000029/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000030 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000031 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Alex Bilbie84445d02011-03-10 16:43:39 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000042class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000043
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020049 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000050
Andrey Andreeva24e52e2012-11-02 03:54:12 +020051 // --------------------------------------------------------------------
Andrey Andreev082ee2b2012-06-08 15:26:34 +030052
Alex Bilbie84445d02011-03-10 16:43:39 +000053 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020054 * ORDER BY random keyword
Alex Bilbie84445d02011-03-10 16:43:39 +000055 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020056 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020057 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020058 protected $_random_keyword = array('NEWID()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020059
60 /**
61 * Quoted identifier flag
62 *
63 * Whether to use SQL-92 standard quoted identifier
64 * (double quotes) or brackets for identifier escaping.
65 *
66 * @var bool
67 */
68 protected $_quoted_identifier = TRUE;
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Database connection
74 *
75 * @param bool $pooling
Alex Bilbie84445d02011-03-10 16:43:39 +000076 * @return resource
77 */
Andrey Andreeve6297342012-03-20 16:25:07 +020078 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000079 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030080 $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE)
81 ? 'UTF-8' : SQLSRV_ENC_CHAR;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000082
83 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +020084 'UID' => empty($this->username) ? '' : $this->username,
85 'PWD' => empty($this->password) ? '' : $this->password,
86 'Database' => $this->database,
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030087 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0,
88 'CharacterSet' => $charset,
89 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0,
Andrey Andreev0e8968a2012-01-26 02:04:37 +020090 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000091 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +020092
93 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000094 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +020095 if (empty($connection['UID']) && empty($connection['PWD']))
96 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000097 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000098 }
99
Andrey Andreevfac37612012-07-02 14:56:20 +0300100 $this->conn_id = sqlsrv_connect($this->hostname, $connection);
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300101
102 // Determine how identifiers are escaped
103 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
104 $query = $query->row_array();
Andrey Andreev70c72c92012-06-25 00:04:51 +0300105 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300106 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
107
Andrey Andreevfac37612012-07-02 14:56:20 +0300108 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Persistent database connection
115 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000116 * @return resource
117 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200118 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000119 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300120 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000121 }
122
123 // --------------------------------------------------------------------
124
125 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000126 * Select the database
127 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200128 * @param string $database
Andrey Andreev11454e02012-02-22 16:05:47 +0200129 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000130 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200131 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000132 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200133 if ($database === '')
134 {
135 $database = $this->database;
136 }
137
Eco91d0822ce2012-11-19 20:49:38 +0100138 if ($this->_execute('USE '.$this->escape_identifiers($database)))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200139 {
140 $this->database = $database;
141 return TRUE;
142 }
143
144 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 }
146
147 // --------------------------------------------------------------------
148
149 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 * Execute the query
151 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200152 * @param string $sql an SQL query
Alex Bilbie84445d02011-03-10 16:43:39 +0000153 * @return resource
154 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200155 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000156 {
Andrey Andreevb2f60332012-07-03 14:45:00 +0300157 return ($this->is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
Andrey Andreev846acc72012-05-24 23:27:46 +0300158 ? sqlsrv_query($this->conn_id, $sql)
159 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
Alex Bilbie84445d02011-03-10 16:43:39 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Begin Transaction
166 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200167 * @param bool $test_mode
Alex Bilbie84445d02011-03-10 16:43:39 +0000168 * @return bool
169 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200170 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000171 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000172 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200173 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000174 {
175 return TRUE;
176 }
177
178 // Reset the transaction failure flag.
179 // If the $test_mode flag is set to TRUE transactions will be rolled back
180 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200181 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000182
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000183 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Commit Transaction
190 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000191 * @return bool
192 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200193 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000194 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000195 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200196 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000197 {
198 return TRUE;
199 }
200
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000201 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Rollback Transaction
208 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000209 * @return bool
210 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200211 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000212 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000213 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200214 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000215 {
216 return TRUE;
217 }
218
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000219 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000220 }
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Escape String
226 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200227 * @param string $str
228 * @param bool $like Whether or not the string will be used in a LIKE condition
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 * @return string
230 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200231 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000232 {
Andrey Andreev7545ffd2013-01-10 16:23:48 +0200233 if (is_array($str))
234 {
235 foreach ($str as $key => $val)
236 {
237 $str[$key] = $this->escape_str($val, $like);
238 }
239
240 return $str;
241 }
242
Alex Bilbie84445d02011-03-10 16:43:39 +0000243 // Escape single quotes
Andrey Andreev7545ffd2013-01-10 16:23:48 +0200244 $str = str_replace("'", "''", remove_invisible_characters($str));
245
246 // escape LIKE condition wildcards
247 if ($like === TRUE)
248 {
249 return str_replace(
250 array($this->_like_escape_chr, '%', '_'),
251 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
252 $str
253 );
254 }
255
256 return $str;
Alex Bilbie84445d02011-03-10 16:43:39 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Affected Rows
263 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200264 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000265 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200266 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000267 {
Andrey Andreevede49ba2012-07-23 16:06:36 +0300268 return sqlsrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000269 }
270
271 // --------------------------------------------------------------------
272
273 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200274 * Insert ID
275 *
276 * Returns the last id created in the Identity column.
277 *
278 * @return string
279 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200280 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000281 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200282 $query = $this->query('SELECT @@IDENTITY AS insert_id');
283 $query = $query->row();
284 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200290 * Database version number
291 *
292 * @return string
293 */
294 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000295 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200296 if (isset($this->data_cache['version']))
297 {
298 return $this->data_cache['version'];
299 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200300 elseif ( ! $this->conn_id)
301 {
302 $this->initialize();
303 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200304
Andrey Andreev2b730372012-11-05 17:01:11 +0200305 if ( ! $this->conn_id OR ($info = sqlsrv_server_info($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200306 {
307 return FALSE;
308 }
309
310 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000311 }
312
313 // --------------------------------------------------------------------
314
315 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000316 * List table query
317 *
318 * Generates a platform-specific query string so that the table names can be fetched
319 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200320 * @param bool
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200321 * @return string $prefix_limit
Alex Bilbie84445d02011-03-10 16:43:39 +0000322 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200323 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000324 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300325 $sql = 'SELECT '.$this->escape_identifiers('name')
326 .' FROM '.$this->escape_identifiers('sysobjects')
327 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
328
329 if ($prefix_limit === TRUE && $this->dbprefix !== '')
330 {
331 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
332 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
333 }
334
335 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * List column query
342 *
343 * Generates a platform-specific query string so that the column names can be fetched
344 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200345 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000346 * @return string
347 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200348 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000349 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200350 return 'SELECT COLUMN_NAME
351 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200352 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Alex Bilbie84445d02011-03-10 16:43:39 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200358 * Returns an object with field data
Alex Bilbie84445d02011-03-10 16:43:39 +0000359 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200360 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200361 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000362 */
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200363 public function field_data($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000364 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200365 if ($table === '')
366 {
367 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
368 }
369
370 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
371 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200372 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200373
374 if (($query = $this->query($sql)) === FALSE)
375 {
376 return FALSE;
377 }
378 $query = $query->result_object();
379
380 $retval = array();
381 for ($i = 0, $c = count($query); $i < $c; $i++)
382 {
383 $retval[$i] = new stdClass();
384 $retval[$i]->name = $query[$i]->COLUMN_NAME;
385 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200386 $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 +0200387 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
388 }
389
390 return $retval;
Alex Bilbie84445d02011-03-10 16:43:39 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200396 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000397 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200398 * Returns an array containing code and message of the last
399 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000400 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200401 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000402 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200403 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200405 $error = array('code' => '00000', 'message' => '');
406 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
407
408 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200409 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200411 }
412
Andrey Andreev4be5de12012-03-02 15:45:41 +0200413 $sqlsrv_error = array_shift($sqlsrv_errors);
414 if (isset($sqlsrv_error['SQLSTATE']))
415 {
416 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
417 }
418 elseif (isset($sqlsrv_error['code']))
419 {
420 $error['code'] = $sqlsrv_error['code'];
421 }
422
423 if (isset($sqlsrv_error['message']))
424 {
425 $error['message'] = $sqlsrv_error['message'];
426 }
427
428 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000429 }
430
431 // --------------------------------------------------------------------
432
433 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000434 * Update statement
435 *
436 * Generates a platform-specific update string from the supplied data
437 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200438 * @param string $table
439 * @param array $values
Alex Bilbie84445d02011-03-10 16:43:39 +0000440 * @return string
441 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300442 protected function _update($table, $values)
Alex Bilbie84445d02011-03-10 16:43:39 +0000443 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300444 $this->qb_limit = FALSE;
445 $this->qb_orderby = array();
446 return parent::_update($table, $values);
Alex Bilbie84445d02011-03-10 16:43:39 +0000447 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200448
Alex Bilbie84445d02011-03-10 16:43:39 +0000449 // --------------------------------------------------------------------
450
451 /**
452 * Truncate statement
453 *
454 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300455 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200456 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300457 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000458 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200459 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000460 * @return string
461 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200462 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000463 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300464 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000465 }
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Delete statement
471 *
472 * Generates a platform-specific delete string from the supplied data
473 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200474 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000475 * @return string
476 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300477 protected function _delete($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000478 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300479 if ($this->qb_limit)
480 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300481 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 +0300482 }
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300483
Andrey Andreevb0478652012-07-18 15:34:46 +0300484 return parent::_delete($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000485 }
486
487 // --------------------------------------------------------------------
488
489 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200490 * LIMIT
Alex Bilbie84445d02011-03-10 16:43:39 +0000491 *
492 * Generates a platform-specific LIMIT clause
493 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200494 * @param string $sql SQL Query
Alex Bilbie84445d02011-03-10 16:43:39 +0000495 * @return string
496 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300497 protected function _limit($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000498 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300499 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300500 if (version_compare($this->version(), '11', '>='))
501 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300502 return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
Andrey Andreev71379ca2012-06-11 16:12:43 +0300503 }
504
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300505 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300506
507 // An ORDER BY clause is required for ROW_NUMBER() to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300508 if ($this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300509 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300510 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300511
512 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300513 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300514
Andrey Andreev44514542012-10-23 15:35:09 +0300515 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
516 if (count($this->qb_select) === 0)
517 {
518 $select = '*'; // Inevitable
519 }
520 else
521 {
522 // Use only field names and their aliases, everything else is out of our scope.
523 $select = array();
524 $field_regexp = ($this->_quoted_identifier)
525 ? '("[^\"]+")' : '(\[[^\]]+\])';
526 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
527 {
528 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
529 ? $m[1] : $this->qb_select[$i];
530 }
531 $select = implode(', ', $select);
532 }
533
534 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300535 .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 +0300536 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300537 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300538 }
539
540 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000541 }
542
543 // --------------------------------------------------------------------
544
545 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200546 * Insert batch statement
547 *
548 * Generates a platform-specific insert string from the supplied data.
549 *
550 * @param string $table Table name
551 * @param array $keys INSERT keys
552 * @param array $values INSERT values
553 * @return string|bool
554 */
555 protected function _insert_batch($table, $keys, $values)
556 {
557 // Multiple-value inserts are only supported as of SQL Server 2008
558 if (version_compare($this->version(), '10', '>='))
559 {
560 return parent::_insert_batch($table, $keys, $values);
561 }
562
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200563 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200564 }
565
566 // --------------------------------------------------------------------
567
568 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000569 * Close DB Connection
570 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000571 * @return void
572 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300573 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000574 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300575 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000576 }
577
578}
579
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200580/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300581/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */