blob: 2759bac0b05f3a80fdb863fccbb887646af513e1 [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 Andreevba8bf562014-01-21 19:04:18 +020051 /**
52 * Scrollable flag
53 *
54 * Determines what cursor type to use when executing queries.
55 *
56 * FALSE or SQLSRV_CURSOR_FORWARD would increase performance,
57 * but would disable num_rows() (and possibly insert_id())
58 *
59 * @var mixed
60 */
61 public $scrollable;
62
Andrey Andreeva24e52e2012-11-02 03:54:12 +020063 // --------------------------------------------------------------------
Andrey Andreev082ee2b2012-06-08 15:26:34 +030064
Alex Bilbie84445d02011-03-10 16:43:39 +000065 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020066 * ORDER BY random keyword
Alex Bilbie84445d02011-03-10 16:43:39 +000067 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020068 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020070 protected $_random_keyword = array('NEWID()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020071
72 /**
73 * Quoted identifier flag
74 *
75 * Whether to use SQL-92 standard quoted identifier
76 * (double quotes) or brackets for identifier escaping.
77 *
78 * @var bool
79 */
80 protected $_quoted_identifier = TRUE;
81
82 // --------------------------------------------------------------------
83
84 /**
Andrey Andreevba8bf562014-01-21 19:04:18 +020085 * Class constructor
86 *
87 * @param array $params
88 * @return void
89 */
90 public function __construct($params)
91 {
92 parent::__construct($params);
93
94 // This is only supported as of SQLSRV 3.0
95 if ($this->scrollable === NULL)
96 {
97 $this->scrollable = defined('SQLSRV_CURSOR_CLIENT_BUFFERED')
98 ? SQLSRV_CURSOR_CLIENT_BUFFERED
99 : FALSE;
100 }
101 }
102
103 // --------------------------------------------------------------------
104
105 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200106 * Database connection
107 *
108 * @param bool $pooling
Alex Bilbie84445d02011-03-10 16:43:39 +0000109 * @return resource
110 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200111 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000112 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300113 $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE)
114 ? 'UTF-8' : SQLSRV_ENC_CHAR;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000115
116 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200117 'UID' => empty($this->username) ? '' : $this->username,
118 'PWD' => empty($this->password) ? '' : $this->password,
119 'Database' => $this->database,
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300120 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0,
121 'CharacterSet' => $charset,
122 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0,
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200123 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000124 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200125
126 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000127 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200128 if (empty($connection['UID']) && empty($connection['PWD']))
129 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000130 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +0000131 }
132
Andrey Andreevfac37612012-07-02 14:56:20 +0300133 $this->conn_id = sqlsrv_connect($this->hostname, $connection);
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300134
135 // Determine how identifiers are escaped
136 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
137 $query = $query->row_array();
Andrey Andreev70c72c92012-06-25 00:04:51 +0300138 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300139 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
140
Andrey Andreevfac37612012-07-02 14:56:20 +0300141 return $this->conn_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Persistent database connection
148 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000149 * @return resource
150 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200151 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000152 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300153 return $this->db_connect(TRUE);
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;
174 return TRUE;
175 }
176
177 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000178 }
179
180 // --------------------------------------------------------------------
181
182 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000183 * Execute the query
184 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200185 * @param string $sql an SQL query
Alex Bilbie84445d02011-03-10 16:43:39 +0000186 * @return resource
187 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200188 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000189 {
Andrey Andreevba8bf562014-01-21 19:04:18 +0200190 return ($this->scrollable === FALSE OR $this->is_write_type($sql))
Andrey Andreev846acc72012-05-24 23:27:46 +0300191 ? sqlsrv_query($this->conn_id, $sql)
Andrey Andreevba8bf562014-01-21 19:04:18 +0200192 : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => $this->scrollable));
Alex Bilbie84445d02011-03-10 16:43:39 +0000193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Begin Transaction
199 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200200 * @param bool $test_mode
Alex Bilbie84445d02011-03-10 16:43:39 +0000201 * @return bool
202 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200203 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000204 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000205 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200206 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000207 {
208 return TRUE;
209 }
210
211 // Reset the transaction failure flag.
212 // If the $test_mode flag is set to TRUE transactions will be rolled back
213 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200214 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000215
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000216 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Commit Transaction
223 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000224 * @return bool
225 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200226 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000228 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200229 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000230 {
231 return TRUE;
232 }
233
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000234 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Rollback Transaction
241 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000242 * @return bool
243 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200244 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000245 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000246 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200247 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000248 {
249 return TRUE;
250 }
251
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000252 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000253 }
254
255 // --------------------------------------------------------------------
256
257 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000258 * Affected Rows
259 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200260 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000261 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200262 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000263 {
Andrey Andreevede49ba2012-07-23 16:06:36 +0300264 return sqlsrv_rows_affected($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000265 }
266
267 // --------------------------------------------------------------------
268
269 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200270 * Insert ID
271 *
272 * Returns the last id created in the Identity column.
273 *
274 * @return string
275 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200276 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000277 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200278 $query = $this->query('SELECT @@IDENTITY AS insert_id');
279 $query = $query->row();
280 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000281 }
282
283 // --------------------------------------------------------------------
284
285 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200286 * Database version number
287 *
288 * @return string
289 */
290 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000291 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200292 if (isset($this->data_cache['version']))
293 {
294 return $this->data_cache['version'];
295 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200296 elseif ( ! $this->conn_id)
297 {
298 $this->initialize();
299 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200300
Andrey Andreev2b730372012-11-05 17:01:11 +0200301 if ( ! $this->conn_id OR ($info = sqlsrv_server_info($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200302 {
303 return FALSE;
304 }
305
306 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000312 * List table query
313 *
314 * Generates a platform-specific query string so that the table names can be fetched
315 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200316 * @param bool
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200317 * @return string $prefix_limit
Alex Bilbie84445d02011-03-10 16:43:39 +0000318 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200319 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000320 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300321 $sql = 'SELECT '.$this->escape_identifiers('name')
322 .' FROM '.$this->escape_identifiers('sysobjects')
323 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
324
325 if ($prefix_limit === TRUE && $this->dbprefix !== '')
326 {
327 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
328 .sprintf($this->_escape_like_str, $this->_escape_like_chr);
329 }
330
331 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Alex Bilbie84445d02011-03-10 16:43:39 +0000332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * List column query
338 *
339 * Generates a platform-specific query string so that the column names can be fetched
340 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200341 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000342 * @return string
343 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200344 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000345 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200346 return 'SELECT COLUMN_NAME
347 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200348 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Alex Bilbie84445d02011-03-10 16:43:39 +0000349 }
350
351 // --------------------------------------------------------------------
352
353 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200354 * Returns an object with field data
Alex Bilbie84445d02011-03-10 16:43:39 +0000355 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200356 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200357 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000358 */
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200359 public function field_data($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000360 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200361 if ($table === '')
362 {
363 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
364 }
365
366 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
367 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200368 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200369
370 if (($query = $this->query($sql)) === FALSE)
371 {
372 return FALSE;
373 }
374 $query = $query->result_object();
375
376 $retval = array();
377 for ($i = 0, $c = count($query); $i < $c; $i++)
378 {
379 $retval[$i] = new stdClass();
380 $retval[$i]->name = $query[$i]->COLUMN_NAME;
381 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200382 $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 +0200383 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
384 }
385
386 return $retval;
Alex Bilbie84445d02011-03-10 16:43:39 +0000387 }
388
389 // --------------------------------------------------------------------
390
391 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000393 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200394 * Returns an array containing code and message of the last
395 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000396 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200397 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000398 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200399 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000400 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200401 $error = array('code' => '00000', 'message' => '');
402 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
403
404 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200405 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200406 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200407 }
408
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 $sqlsrv_error = array_shift($sqlsrv_errors);
410 if (isset($sqlsrv_error['SQLSTATE']))
411 {
412 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
413 }
414 elseif (isset($sqlsrv_error['code']))
415 {
416 $error['code'] = $sqlsrv_error['code'];
417 }
418
419 if (isset($sqlsrv_error['message']))
420 {
421 $error['message'] = $sqlsrv_error['message'];
422 }
423
424 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000430 * Update statement
431 *
432 * Generates a platform-specific update string from the supplied data
433 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200434 * @param string $table
435 * @param array $values
Alex Bilbie84445d02011-03-10 16:43:39 +0000436 * @return string
437 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300438 protected function _update($table, $values)
Alex Bilbie84445d02011-03-10 16:43:39 +0000439 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300440 $this->qb_limit = FALSE;
441 $this->qb_orderby = array();
442 return parent::_update($table, $values);
Alex Bilbie84445d02011-03-10 16:43:39 +0000443 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200444
Alex Bilbie84445d02011-03-10 16:43:39 +0000445 // --------------------------------------------------------------------
446
447 /**
448 * Truncate statement
449 *
450 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300451 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200452 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300453 * then this method maps to 'DELETE FROM table'
Alex Bilbie84445d02011-03-10 16:43:39 +0000454 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200455 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000456 * @return string
457 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200458 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000459 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300460 return 'TRUNCATE TABLE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Delete statement
467 *
468 * Generates a platform-specific delete string from the supplied data
469 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200470 * @param string $table
Alex Bilbie84445d02011-03-10 16:43:39 +0000471 * @return string
472 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300473 protected function _delete($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000474 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300475 if ($this->qb_limit)
476 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300477 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 +0300478 }
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300479
Andrey Andreevb0478652012-07-18 15:34:46 +0300480 return parent::_delete($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200486 * LIMIT
Alex Bilbie84445d02011-03-10 16:43:39 +0000487 *
488 * Generates a platform-specific LIMIT clause
489 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200490 * @param string $sql SQL Query
Alex Bilbie84445d02011-03-10 16:43:39 +0000491 * @return string
492 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300493 protected function _limit($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000494 {
Andrey Andreevd25c5892012-06-08 16:23:01 +0300495 // As of SQL Server 2012 (11.0.*) OFFSET is supported
Andrey Andreev71379ca2012-06-11 16:12:43 +0300496 if (version_compare($this->version(), '11', '>='))
497 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300498 return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
Andrey Andreev71379ca2012-06-11 16:12:43 +0300499 }
500
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300501 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300502
503 // An ORDER BY clause is required for ROW_NUMBER() to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300504 if ($this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300505 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300506 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300507
508 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300509 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300510
Andrey Andreev44514542012-10-23 15:35:09 +0300511 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
512 if (count($this->qb_select) === 0)
513 {
514 $select = '*'; // Inevitable
515 }
516 else
517 {
518 // Use only field names and their aliases, everything else is out of our scope.
519 $select = array();
520 $field_regexp = ($this->_quoted_identifier)
521 ? '("[^\"]+")' : '(\[[^\]]+\])';
522 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
523 {
524 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
525 ? $m[1] : $this->qb_select[$i];
526 }
527 $select = implode(', ', $select);
528 }
529
530 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300531 .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 +0300532 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300533 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300534 }
535
536 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200542 * Insert batch statement
543 *
544 * Generates a platform-specific insert string from the supplied data.
545 *
546 * @param string $table Table name
547 * @param array $keys INSERT keys
548 * @param array $values INSERT values
549 * @return string|bool
550 */
551 protected function _insert_batch($table, $keys, $values)
552 {
553 // Multiple-value inserts are only supported as of SQL Server 2008
554 if (version_compare($this->version(), '10', '>='))
555 {
556 return parent::_insert_batch($table, $keys, $values);
557 }
558
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200559 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200560 }
561
562 // --------------------------------------------------------------------
563
564 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000565 * Close DB Connection
566 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000567 * @return void
568 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300569 protected function _close()
Alex Bilbie84445d02011-03-10 16:43:39 +0000570 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300571 @sqlsrv_close($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000572 }
573
574}
575
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200576/* End of file sqlsrv_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300577/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */