blob: 8d830fb51f8acb260e6e087b29057e39524f9916 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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)
Andrey Andreevf2818bd2014-02-25 15:26:20 +0200101 ? mssql_pconnect($this->hostname, $this->username, $this->password)
102 : mssql_connect($this->hostname, $this->username, $this->password);
Andrey Andreevfac37612012-07-02 14:56:20 +0300103
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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * Select the database
134 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200135 * @param string $database
Andrey Andreev4da24f82012-01-25 21:54:23 +0200136 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200138 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200140 if ($database === '')
141 {
142 $database = $this->database;
143 }
144
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300145 // Note: Escaping is required in the event that the DB name
Andrey Andreev60f744b2014-07-01 08:33:30 +0300146 // contains reserved characters.
147 if (mssql_select_db('['.$database.']', $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200148 {
149 $this->database = $database;
150 return TRUE;
151 }
152
153 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
156 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * Execute the query
160 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200161 * @param string $sql an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200162 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200163 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200164 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300166 return mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Begin Transaction
173 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200174 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200175 * @return bool
176 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200177 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200180 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 return TRUE;
183 }
184
185 // Reset the transaction failure flag.
186 // If the $test_mode flag is set to TRUE transactions will be rolled back
187 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200188 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000189
Andrey Andreev4da24f82012-01-25 21:54:23 +0200190 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Commit Transaction
197 *
Barry Mienydd671972010-10-04 16:33:58 +0200198 * @return bool
199 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200200 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200203 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 return TRUE;
206 }
207
Andrey Andreev4da24f82012-01-25 21:54:23 +0200208 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Rollback Transaction
215 *
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return bool
217 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200218 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200221 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return TRUE;
224 }
225
Andrey Andreev4da24f82012-01-25 21:54:23 +0200226 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // --------------------------------------------------------------------
230
231 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * Affected Rows
233 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200234 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200236 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300238 return mssql_rows_affected($this->conn_id);
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 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200244 * Insert ID
245 *
246 * Returns the last id created in the Identity column.
247 *
248 * @return string
249 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200250 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300252 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200253 ? 'SELECT SCOPE_IDENTITY() AS last_id'
254 : 'SELECT @@IDENTITY AS last_id';
255
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200256 $query = $this->query($query);
257 $query = $query->row();
258 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
Andrey Andreev4173fa02012-11-19 11:14:23 +0200264 * Set client character set
265 *
266 * @param string $charset
267 * @return bool
268 */
269 protected function _db_set_charset($charset)
270 {
Andrey Andreevca39f2e2014-02-20 02:30:14 +0200271 return (ini_set('mssql.charset', $charset) !== FALSE);
Andrey Andreev4173fa02012-11-19 11:14:23 +0200272 }
273
274 // --------------------------------------------------------------------
275
276 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200277 * Version number query string
278 *
279 * @return string
280 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200281 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200283 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
285
286 // --------------------------------------------------------------------
287
288 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * List table query
290 *
291 * Generates a platform-specific query string so that the table names can be fetched
292 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200293 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @return string
295 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200296 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300298 $sql = 'SELECT '.$this->escape_identifiers('name')
299 .' FROM '.$this->escape_identifiers('sysobjects')
300 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200301
darwinelcf63aee2014-02-09 02:04:23 +0100302 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300304 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
305 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Andrey Andreev70c72c92012-06-25 00:04:51 +0300308 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * List column query
315 *
316 * Generates a platform-specific query string so that the column names can be fetched
317 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200318 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return string
320 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200321 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200323 return 'SELECT COLUMN_NAME
324 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200325 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200331 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200333 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200334 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 */
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200336 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200338 if ($table === '')
339 {
340 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
341 }
342
343 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
344 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200345 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200346
347 if (($query = $this->query($sql)) === FALSE)
348 {
349 return FALSE;
350 }
351 $query = $query->result_object();
352
353 $retval = array();
354 for ($i = 0, $c = count($query); $i < $c; $i++)
355 {
356 $retval[$i] = new stdClass();
357 $retval[$i]->name = $query[$i]->COLUMN_NAME;
358 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200359 $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 +0200360 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
361 }
362
363 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 // --------------------------------------------------------------------
367
368 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200369 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200371 * Returns an array containing code and message of the last
372 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200374 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200376 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200378 $query = $this->query('SELECT @@ERROR AS code');
379 $query = $query->row();
380 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300386 * Update statement
387 *
388 * Generates a platform-specific update string from the supplied data
389 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200390 * @param string $table
391 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300392 * @return string
393 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300394 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300395 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300396 $this->qb_limit = FALSE;
397 $this->qb_orderby = array();
398 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300399 }
400
401 // --------------------------------------------------------------------
402
403 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * Truncate statement
405 *
406 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300407 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200408 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300409 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200411 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200413 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200414 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300416 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
420
421 /**
422 * Delete statement
423 *
424 * Generates a platform-specific delete string from the supplied data
425 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200426 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200428 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300429 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300431 if ($this->qb_limit)
432 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300433 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 +0300434 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000435
Andrey Andreevb0478652012-07-18 15:34:46 +0300436 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200442 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
444 * Generates a platform-specific LIMIT clause
445 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200446 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @return string
448 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300449 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300451 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300452
453 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
454 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300455 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300456 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300457 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300458
459 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300460 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300461
Andrey Andreev44514542012-10-23 15:35:09 +0300462 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
463 if (count($this->qb_select) === 0)
464 {
465 $select = '*'; // Inevitable
466 }
467 else
468 {
469 // Use only field names and their aliases, everything else is out of our scope.
470 $select = array();
471 $field_regexp = ($this->_quoted_identifier)
472 ? '("[^\"]+")' : '(\[[^\]]+\])';
473 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
474 {
475 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
476 ? $m[1] : $this->qb_select[$i];
477 }
478 $select = implode(', ', $select);
479 }
480
481 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300482 .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 +0300483 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300484 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300485 }
486
487 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
490 // --------------------------------------------------------------------
491
492 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200493 * Insert batch statement
494 *
495 * Generates a platform-specific insert string from the supplied data.
496 *
497 * @param string $table Table name
498 * @param array $keys INSERT keys
499 * @param array $values INSERT values
500 * @return string|bool
501 */
502 protected function _insert_batch($table, $keys, $values)
503 {
504 // Multiple-value inserts are only supported as of SQL Server 2008
505 if (version_compare($this->version(), '10', '>='))
506 {
507 return parent::_insert_batch($table, $keys, $values);
508 }
509
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200510 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200511 }
512
513 // --------------------------------------------------------------------
514
515 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * Close DB Connection
517 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 * @return void
519 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300520 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300522 mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200523 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000524
525}
526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300528/* Location: ./system/database/drivers/mssql/mssql_driver.php */