blob: 49711fec939e55c6c05294b188ce27ace7adadfc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +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
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4da24f82012-01-25 21:54:23 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4da24f82012-01-25 21:54:23 +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 *
Derek Allard2067d1a2008-11-13 22:59:24 +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)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * MS SQL Database Adapter Class
31 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +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
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_mssql_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020049 public $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreeva24e52e2012-11-02 03:54:12 +020051 // --------------------------------------------------------------------
Andrey Andreev082ee2b2012-06-08 15:26:34 +030052
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030053 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020054 * ORDER BY random keyword
55 *
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 * Class constructor
Andrey Andreev082ee2b2012-06-08 15:26:34 +030074 *
75 * Appends the port number to the hostname, if needed.
76 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030077 * @param array $params
Andrey Andreev082ee2b2012-06-08 15:26:34 +030078 * @return void
79 */
Andrey Andreevbd601d32012-02-12 21:16:51 +020080 public function __construct($params)
81 {
82 parent::__construct($params);
83
Andrey Andreev2cb262f2012-03-28 13:45:04 +030084 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020085 {
86 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
87 }
88 }
89
Andrey Andreev082ee2b2012-06-08 15:26:34 +030090 // --------------------------------------------------------------------
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
93 * Non-persistent database connection
94 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020095 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +000096 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020097 */
Andrey Andreevfac37612012-07-02 14:56:20 +030098 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300100 $this->conn_id = ($persistent)
101 ? @mssql_pconnect($this->hostname, $this->username, $this->password)
102 : @mssql_connect($this->hostname, $this->username, $this->password);
103
104 if ( ! $this->conn_id)
105 {
106 return FALSE;
107 }
108
Andrey Andreev1a001492013-01-24 11:32:29 +0200109 // ----------------------------------------------------------------
110
111 // Select the DB... assuming a database name is specified in the config file
112 if ($this->database !== '' && ! $this->db_select())
113 {
114 log_message('error', 'Unable to select database: '.$this->database);
115
116 return ($this->db_debug === TRUE)
117 ? $this->display_error('db_unable_to_select', $this->database)
118 : FALSE;
119 }
120
Andrey Andreevfac37612012-07-02 14:56:20 +0300121 // Determine how identifiers are escaped
122 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
123 $query = $query->row_array();
124 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
125 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
126
127 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // --------------------------------------------------------------------
131
132 /**
133 * Persistent database connection
134 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200137 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300139 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jones87cbafc2009-02-27 16:29:59 +0000144 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * Select the database
146 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200147 * @param string $database
Andrey Andreev4da24f82012-01-25 21:54:23 +0200148 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200150 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200152 if ($database === '')
153 {
154 $database = $this->database;
155 }
156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 // Note: The brackets are required in the event that the DB name
158 // contains reserved characters
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300159 if (@mssql_select_db($this->escape_identifiers($database), $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200160 {
161 $this->database = $database;
162 return TRUE;
163 }
164
165 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * Execute the query
172 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200173 * @param string $sql an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200174 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200176 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 return @mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Begin Transaction
185 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200186 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200187 * @return bool
188 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200189 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200192 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
194 return TRUE;
195 }
196
197 // Reset the transaction failure flag.
198 // If the $test_mode flag is set to TRUE transactions will be rolled back
199 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200200 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
Andrey Andreev4da24f82012-01-25 21:54:23 +0200202 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Commit Transaction
209 *
Barry Mienydd671972010-10-04 16:33:58 +0200210 * @return bool
211 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200212 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200215 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 return TRUE;
218 }
219
Andrey Andreev4da24f82012-01-25 21:54:23 +0200220 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Rollback Transaction
227 *
Barry Mienydd671972010-10-04 16:33:58 +0200228 * @return bool
229 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200230 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200233 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 return TRUE;
236 }
237
Andrey Andreev4da24f82012-01-25 21:54:23 +0200238 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // --------------------------------------------------------------------
242
243 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * Affected Rows
245 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200246 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200248 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 return @mssql_rows_affected($this->conn_id);
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // --------------------------------------------------------------------
254
255 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200256 * Insert ID
257 *
258 * Returns the last id created in the Identity column.
259 *
260 * @return string
261 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200262 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300264 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200265 ? 'SELECT SCOPE_IDENTITY() AS last_id'
266 : 'SELECT @@IDENTITY AS last_id';
267
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200268 $query = $this->query($query);
269 $query = $query->row();
270 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272
273 // --------------------------------------------------------------------
274
275 /**
Andrey Andreev4173fa02012-11-19 11:14:23 +0200276 * Set client character set
277 *
278 * @param string $charset
279 * @return bool
280 */
281 protected function _db_set_charset($charset)
282 {
283 return (@ini_set('mssql.charset', $charset) !== FALSE);
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200289 * Version number query string
290 *
291 * @return string
292 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200293 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200295 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * List table query
302 *
303 * Generates a platform-specific query string so that the table names can be fetched
304 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200305 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * @return string
307 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200308 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300310 $sql = 'SELECT '.$this->escape_identifiers('name')
311 .' FROM '.$this->escape_identifiers('sysobjects')
312 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200313
darwinelcf63aee2014-02-09 02:04:23 +0100314 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300316 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
317 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Andrey Andreev70c72c92012-06-25 00:04:51 +0300320 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * List column query
327 *
328 * Generates a platform-specific query string so that the column names can be fetched
329 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200330 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @return string
332 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200333 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200335 return 'SELECT COLUMN_NAME
336 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200337 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200343 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200345 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200346 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 */
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200348 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200350 if ($table === '')
351 {
352 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
353 }
354
355 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
356 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200357 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200358
359 if (($query = $this->query($sql)) === FALSE)
360 {
361 return FALSE;
362 }
363 $query = $query->result_object();
364
365 $retval = array();
366 for ($i = 0, $c = count($query); $i < $c; $i++)
367 {
368 $retval[$i] = new stdClass();
369 $retval[$i]->name = $query[$i]->COLUMN_NAME;
370 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200371 $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 +0200372 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
373 }
374
375 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377
378 // --------------------------------------------------------------------
379
380 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200381 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 * Returns an array containing code and message of the last
384 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200386 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200388 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 $query = $this->query('SELECT @@ERROR AS code');
391 $query = $query->row();
392 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394
395 // --------------------------------------------------------------------
396
397 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300398 * Update statement
399 *
400 * Generates a platform-specific update string from the supplied data
401 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200402 * @param string $table
403 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300404 * @return string
405 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300406 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300407 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300408 $this->qb_limit = FALSE;
409 $this->qb_orderby = array();
410 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300411 }
412
413 // --------------------------------------------------------------------
414
415 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * Truncate statement
417 *
418 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300419 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200420 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300421 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200423 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200425 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200426 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300428 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // --------------------------------------------------------------------
432
433 /**
434 * Delete statement
435 *
436 * Generates a platform-specific delete string from the supplied data
437 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200438 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200440 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300441 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300443 if ($this->qb_limit)
444 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300445 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 +0300446 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000447
Andrey Andreevb0478652012-07-18 15:34:46 +0300448 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200454 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 *
456 * Generates a platform-specific LIMIT clause
457 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200458 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * @return string
460 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300461 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300463 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300464
465 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
466 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300467 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300468 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300469 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300470
471 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300472 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300473
Andrey Andreev44514542012-10-23 15:35:09 +0300474 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
475 if (count($this->qb_select) === 0)
476 {
477 $select = '*'; // Inevitable
478 }
479 else
480 {
481 // Use only field names and their aliases, everything else is out of our scope.
482 $select = array();
483 $field_regexp = ($this->_quoted_identifier)
484 ? '("[^\"]+")' : '(\[[^\]]+\])';
485 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
486 {
487 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
488 ? $m[1] : $this->qb_select[$i];
489 }
490 $select = implode(', ', $select);
491 }
492
493 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300494 .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 +0300495 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300496 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300497 }
498
499 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501
502 // --------------------------------------------------------------------
503
504 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200505 * Insert batch statement
506 *
507 * Generates a platform-specific insert string from the supplied data.
508 *
509 * @param string $table Table name
510 * @param array $keys INSERT keys
511 * @param array $values INSERT values
512 * @return string|bool
513 */
514 protected function _insert_batch($table, $keys, $values)
515 {
516 // Multiple-value inserts are only supported as of SQL Server 2008
517 if (version_compare($this->version(), '10', '>='))
518 {
519 return parent::_insert_batch($table, $keys, $values);
520 }
521
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200522 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200523 }
524
525 // --------------------------------------------------------------------
526
527 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * Close DB Connection
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return void
531 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300532 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300534 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200535 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000536
537}
538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300540/* Location: ./system/database/drivers/mssql/mssql_driver.php */