blob: 67557196ca86188a8410140631ac23a8cb9573d5 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 *
56 * @var string
57 */
58 protected $_random_keyword = ' NEWID()';
59
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
109 // Determine how identifiers are escaped
110 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
111 $query = $query->row_array();
112 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
113 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
114
115 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
119
120 /**
121 * Persistent database connection
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200125 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300127 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Jones87cbafc2009-02-27 16:29:59 +0000132 /**
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // Note: The brackets are required in the event that the DB name
146 // contains reserved characters
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300147 if (@mssql_select_db($this->escape_identifiers($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 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 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 /**
232 * Escape String
233 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200234 * @param string $str
235 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @return string
237 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200238 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000240 if (is_array($str))
241 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500242 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200243 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000244 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200245 }
246
247 return $str;
248 }
249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500251 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Jonese4ed5832009-02-20 21:44:59 +0000253 // escape LIKE condition wildcards
254 if ($like === TRUE)
255 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200256 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100257 array($this->_like_escape_chr, '%', '_'),
258 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
259 $str
260 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Jonese4ed5832009-02-20 21:44:59 +0000263 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * Affected Rows
270 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200271 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200273 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 return @mssql_rows_affected($this->conn_id);
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // --------------------------------------------------------------------
279
280 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200281 * Insert ID
282 *
283 * Returns the last id created in the Identity column.
284 *
285 * @return string
286 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200287 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300289 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200290 ? 'SELECT SCOPE_IDENTITY() AS last_id'
291 : 'SELECT @@IDENTITY AS last_id';
292
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200293 $query = $this->query($query);
294 $query = $query->row();
295 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200301 * Version number query string
302 *
303 * @return string
304 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200305 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200307 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309
310 // --------------------------------------------------------------------
311
312 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * List table query
314 *
315 * Generates a platform-specific query string so that the table names can be fetched
316 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200317 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return string
319 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200320 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300322 $sql = 'SELECT '.$this->escape_identifiers('name')
323 .' FROM '.$this->escape_identifiers('sysobjects')
324 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200325
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100326 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300328 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
329 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Andrey Andreev70c72c92012-06-25 00:04:51 +0300332 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * List column query
339 *
340 * Generates a platform-specific query string so that the column names can be fetched
341 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200342 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @return string
344 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200345 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Barry Mienydd671972010-10-04 16:33:58 +0200347 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Field data query
354 *
355 * Generates a platform-specific query so that the column data can be retrieved
356 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200357 * @param string $table
Andrey Andreev4da24f82012-01-25 21:54:23 +0200358 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200360 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300362 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200368 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 * Returns an array containing code and message of the last
371 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200373 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 $query = $this->query('SELECT @@ERROR AS code');
378 $query = $query->row();
379 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
383
384 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300385 * Update statement
386 *
387 * Generates a platform-specific update string from the supplied data
388 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200389 * @param string $table
390 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300391 * @return string
392 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300393 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300394 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300395 $this->qb_limit = FALSE;
396 $this->qb_orderby = array();
397 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300398 }
399
400 // --------------------------------------------------------------------
401
402 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * Truncate statement
404 *
405 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300406 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200407 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300408 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200410 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200412 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200413 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300415 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
421 * Delete statement
422 *
423 * Generates a platform-specific delete string from the supplied data
424 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200425 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200427 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300428 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300430 if ($this->qb_limit)
431 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300432 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 +0300433 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000434
Andrey Andreevb0478652012-07-18 15:34:46 +0300435 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200441 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 *
443 * Generates a platform-specific LIMIT clause
444 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200445 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @return string
447 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300448 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300450 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300451
452 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
453 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300454 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300455 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300456 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300457
458 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300459 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300460
Andrey Andreev44514542012-10-23 15:35:09 +0300461 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
462 if (count($this->qb_select) === 0)
463 {
464 $select = '*'; // Inevitable
465 }
466 else
467 {
468 // Use only field names and their aliases, everything else is out of our scope.
469 $select = array();
470 $field_regexp = ($this->_quoted_identifier)
471 ? '("[^\"]+")' : '(\[[^\]]+\])';
472 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
473 {
474 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
475 ? $m[1] : $this->qb_select[$i];
476 }
477 $select = implode(', ', $select);
478 }
479
480 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300481 .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 +0300482 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300483 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300484 }
485
486 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // --------------------------------------------------------------------
490
491 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200492 * Insert batch statement
493 *
494 * Generates a platform-specific insert string from the supplied data.
495 *
496 * @param string $table Table name
497 * @param array $keys INSERT keys
498 * @param array $values INSERT values
499 * @return string|bool
500 */
501 protected function _insert_batch($table, $keys, $values)
502 {
503 // Multiple-value inserts are only supported as of SQL Server 2008
504 if (version_compare($this->version(), '10', '>='))
505 {
506 return parent::_insert_batch($table, $keys, $values);
507 }
508
509 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * Close DB Connection
516 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 * @return void
518 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300519 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300521 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200522 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000523
524}
525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300527/* Location: ./system/database/drivers/mssql/mssql_driver.php */