blob: b9e310a3af2714f1b72ddee22887cd27fe520366 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev4da24f82012-01-25 21:54:23 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev4da24f82012-01-25 21:54:23 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MS SQL Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
53class CI_DB_mssql_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020060 public $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreeva24e52e2012-11-02 03:54:12 +020062 // --------------------------------------------------------------------
Andrey Andreev082ee2b2012-06-08 15:26:34 +030063
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030064 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020065 * ORDER BY random keyword
66 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020067 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020068 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020069 protected $_random_keyword = array('NEWID()', 'RAND(%d)');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020070
71 /**
72 * Quoted identifier flag
73 *
74 * Whether to use SQL-92 standard quoted identifier
75 * (double quotes) or brackets for identifier escaping.
76 *
77 * @var bool
78 */
79 protected $_quoted_identifier = TRUE;
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Class constructor
Andrey Andreev082ee2b2012-06-08 15:26:34 +030085 *
86 * Appends the port number to the hostname, if needed.
87 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030088 * @param array $params
Andrey Andreev082ee2b2012-06-08 15:26:34 +030089 * @return void
90 */
Andrey Andreevbd601d32012-02-12 21:16:51 +020091 public function __construct($params)
92 {
93 parent::__construct($params);
94
Andrey Andreev2cb262f2012-03-28 13:45:04 +030095 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020096 {
97 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
98 }
99 }
100
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300101 // --------------------------------------------------------------------
102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 /**
104 * Non-persistent database connection
105 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200106 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Andrey Andreevfac37612012-07-02 14:56:20 +0300109 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300111 $this->conn_id = ($persistent)
Andrey Andreevf2818bd2014-02-25 15:26:20 +0200112 ? mssql_pconnect($this->hostname, $this->username, $this->password)
113 : mssql_connect($this->hostname, $this->username, $this->password);
Andrey Andreevfac37612012-07-02 14:56:20 +0300114
115 if ( ! $this->conn_id)
116 {
117 return FALSE;
118 }
119
Andrey Andreev1a001492013-01-24 11:32:29 +0200120 // ----------------------------------------------------------------
121
122 // Select the DB... assuming a database name is specified in the config file
123 if ($this->database !== '' && ! $this->db_select())
124 {
125 log_message('error', 'Unable to select database: '.$this->database);
126
127 return ($this->db_debug === TRUE)
128 ? $this->display_error('db_unable_to_select', $this->database)
129 : FALSE;
130 }
131
Andrey Andreevfac37612012-07-02 14:56:20 +0300132 // Determine how identifiers are escaped
133 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
134 $query = $query->row_array();
135 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
136 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
137
138 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // --------------------------------------------------------------------
142
143 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * Select the database
145 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200146 * @param string $database
Andrey Andreev4da24f82012-01-25 21:54:23 +0200147 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200148 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200149 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200151 if ($database === '')
152 {
153 $database = $this->database;
154 }
155
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300156 // Note: Escaping is required in the event that the DB name
Andrey Andreev60f744b2014-07-01 08:33:30 +0300157 // contains reserved characters.
158 if (mssql_select_db('['.$database.']', $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200159 {
160 $this->database = $database;
161 return TRUE;
162 }
163
164 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * Execute the query
171 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200172 * @param string $sql an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200173 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200174 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200175 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300177 return mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Begin Transaction
184 *
Barry Mienydd671972010-10-04 16:33:58 +0200185 * @return bool
186 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300187 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200189 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Commit Transaction
196 *
Barry Mienydd671972010-10-04 16:33:58 +0200197 * @return bool
198 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300199 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200201 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Rollback Transaction
208 *
Barry Mienydd671972010-10-04 16:33:58 +0200209 * @return bool
210 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300211 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200213 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
217
218 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * Affected Rows
220 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200221 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200223 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300225 return mssql_rows_affected($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // --------------------------------------------------------------------
229
230 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200231 * Insert ID
232 *
233 * Returns the last id created in the Identity column.
234 *
235 * @return string
236 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200237 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300239 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200240 ? 'SELECT SCOPE_IDENTITY() AS last_id'
241 : 'SELECT @@IDENTITY AS last_id';
242
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200243 $query = $this->query($query);
244 $query = $query->row();
245 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 // --------------------------------------------------------------------
249
250 /**
Andrey Andreev4173fa02012-11-19 11:14:23 +0200251 * Set client character set
252 *
253 * @param string $charset
254 * @return bool
255 */
256 protected function _db_set_charset($charset)
257 {
Andrey Andreevca39f2e2014-02-20 02:30:14 +0200258 return (ini_set('mssql.charset', $charset) !== FALSE);
Andrey Andreev4173fa02012-11-19 11:14:23 +0200259 }
260
261 // --------------------------------------------------------------------
262
263 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200264 * Version number query string
265 *
266 * @return string
267 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200268 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreev71a65c32015-12-11 17:21:51 +0200270 return "SELECT SERVERPROPERTY('ProductVersion') AS ver";
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272
273 // --------------------------------------------------------------------
274
275 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * List table query
277 *
278 * Generates a platform-specific query string so that the table names can be fetched
279 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200280 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @return string
282 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200283 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300285 $sql = 'SELECT '.$this->escape_identifiers('name')
286 .' FROM '.$this->escape_identifiers('sysobjects')
287 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200288
darwinelcf63aee2014-02-09 02:04:23 +0100289 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300291 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
292 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreev70c72c92012-06-25 00:04:51 +0300295 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * List column query
302 *
303 * Generates a platform-specific query string so that the column names can be fetched
304 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200305 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * @return string
307 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200308 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200310 return 'SELECT COLUMN_NAME
311 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200312 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314
315 // --------------------------------------------------------------------
316
317 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200318 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200320 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200321 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200323 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200325 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
326 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200327 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200328
329 if (($query = $this->query($sql)) === FALSE)
330 {
331 return FALSE;
332 }
333 $query = $query->result_object();
334
335 $retval = array();
336 for ($i = 0, $c = count($query); $i < $c; $i++)
337 {
338 $retval[$i] = new stdClass();
339 $retval[$i]->name = $query[$i]->COLUMN_NAME;
340 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200341 $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 +0200342 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
343 }
344
345 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
347
348 // --------------------------------------------------------------------
349
350 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200351 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200353 * Returns an array containing code and message of the last
354 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200356 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreevad4882f2015-09-16 11:29:50 +0300360 // We need this because the error info is discarded by the
361 // server the first time you request it, and query() already
362 // calls error() once for logging purposes when a query fails.
363 static $error = array('code' => 0, 'message' => NULL);
364
365 $message = mssql_get_last_message();
366 if ( ! empty($message))
367 {
368 $error['code'] = $this->query('SELECT @@ERROR AS code')->row()->code;
369 $error['message'] = $message;
370 }
371
372 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374
375 // --------------------------------------------------------------------
376
377 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300378 * Update statement
379 *
380 * Generates a platform-specific update string from the supplied data
381 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200382 * @param string $table
383 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300384 * @return string
385 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300386 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300387 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300388 $this->qb_limit = FALSE;
389 $this->qb_orderby = array();
390 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300391 }
392
393 // --------------------------------------------------------------------
394
395 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * Truncate statement
397 *
398 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300399 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200400 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300401 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200403 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200405 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200406 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300408 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
412
413 /**
414 * Delete statement
415 *
416 * Generates a platform-specific delete string from the supplied data
417 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200418 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200420 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300421 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300423 if ($this->qb_limit)
424 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300425 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 +0300426 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000427
Andrey Andreevb0478652012-07-18 15:34:46 +0300428 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
431 // --------------------------------------------------------------------
432
433 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200434 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
436 * Generates a platform-specific LIMIT clause
437 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200438 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return string
440 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300441 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300443 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300444
445 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
446 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300447 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300448 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300449 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300450
451 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300452 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300453
Andrey Andreev44514542012-10-23 15:35:09 +0300454 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
455 if (count($this->qb_select) === 0)
456 {
457 $select = '*'; // Inevitable
458 }
459 else
460 {
461 // Use only field names and their aliases, everything else is out of our scope.
462 $select = array();
463 $field_regexp = ($this->_quoted_identifier)
464 ? '("[^\"]+")' : '(\[[^\]]+\])';
465 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
466 {
467 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
468 ? $m[1] : $this->qb_select[$i];
469 }
470 $select = implode(', ', $select);
471 }
472
473 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300474 .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 +0300475 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300476 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300477 }
478
479 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481
482 // --------------------------------------------------------------------
483
484 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200485 * Insert batch statement
486 *
487 * Generates a platform-specific insert string from the supplied data.
488 *
489 * @param string $table Table name
490 * @param array $keys INSERT keys
491 * @param array $values INSERT values
492 * @return string|bool
493 */
494 protected function _insert_batch($table, $keys, $values)
495 {
496 // Multiple-value inserts are only supported as of SQL Server 2008
497 if (version_compare($this->version(), '10', '>='))
498 {
499 return parent::_insert_batch($table, $keys, $values);
500 }
501
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200502 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * Close DB Connection
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @return void
511 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300512 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300514 mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200515 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000516
517}