blob: 05e5418c38d19d62c34287ff329eaff2ea4b8e1b [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 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200185 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200186 * @return bool
187 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200188 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200191 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 return TRUE;
194 }
195
196 // Reset the transaction failure flag.
197 // If the $test_mode flag is set to TRUE transactions will be rolled back
198 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200199 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
Andrey Andreev4da24f82012-01-25 21:54:23 +0200201 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Commit Transaction
208 *
Barry Mienydd671972010-10-04 16:33:58 +0200209 * @return bool
210 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200211 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200214 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 return TRUE;
217 }
218
Andrey Andreev4da24f82012-01-25 21:54:23 +0200219 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Rollback Transaction
226 *
Barry Mienydd671972010-10-04 16:33:58 +0200227 * @return bool
228 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200229 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200232 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 return TRUE;
235 }
236
Andrey Andreev4da24f82012-01-25 21:54:23 +0200237 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // --------------------------------------------------------------------
241
242 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * Affected Rows
244 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200245 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200247 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300249 return mssql_rows_affected($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // --------------------------------------------------------------------
253
254 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200255 * Insert ID
256 *
257 * Returns the last id created in the Identity column.
258 *
259 * @return string
260 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200261 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300263 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200264 ? 'SELECT SCOPE_IDENTITY() AS last_id'
265 : 'SELECT @@IDENTITY AS last_id';
266
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200267 $query = $this->query($query);
268 $query = $query->row();
269 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271
272 // --------------------------------------------------------------------
273
274 /**
Andrey Andreev4173fa02012-11-19 11:14:23 +0200275 * Set client character set
276 *
277 * @param string $charset
278 * @return bool
279 */
280 protected function _db_set_charset($charset)
281 {
Andrey Andreevca39f2e2014-02-20 02:30:14 +0200282 return (ini_set('mssql.charset', $charset) !== FALSE);
Andrey Andreev4173fa02012-11-19 11:14:23 +0200283 }
284
285 // --------------------------------------------------------------------
286
287 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200288 * Version number query string
289 *
290 * @return string
291 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200292 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200294 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * List table query
301 *
302 * Generates a platform-specific query string so that the table names can be fetched
303 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200304 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @return string
306 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200307 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300309 $sql = 'SELECT '.$this->escape_identifiers('name')
310 .' FROM '.$this->escape_identifiers('sysobjects')
311 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200312
darwinelcf63aee2014-02-09 02:04:23 +0100313 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300315 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
316 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Andrey Andreev70c72c92012-06-25 00:04:51 +0300319 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * List column query
326 *
327 * Generates a platform-specific query string so that the column names can be fetched
328 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200329 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @return string
331 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200332 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200334 return 'SELECT COLUMN_NAME
335 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200336 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
338
339 // --------------------------------------------------------------------
340
341 /**
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200342 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200344 * @param string $table
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200345 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200347 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200349 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
350 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200351 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200352
353 if (($query = $this->query($sql)) === FALSE)
354 {
355 return FALSE;
356 }
357 $query = $query->result_object();
358
359 $retval = array();
360 for ($i = 0, $c = count($query); $i < $c; $i++)
361 {
362 $retval[$i] = new stdClass();
363 $retval[$i]->name = $query[$i]->COLUMN_NAME;
364 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200365 $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 +0200366 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
367 }
368
369 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 * Returns an array containing code and message of the last
378 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200382 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreevad4882f2015-09-16 11:29:50 +0300384 // We need this because the error info is discarded by the
385 // server the first time you request it, and query() already
386 // calls error() once for logging purposes when a query fails.
387 static $error = array('code' => 0, 'message' => NULL);
388
389 $message = mssql_get_last_message();
390 if ( ! empty($message))
391 {
392 $error['code'] = $this->query('SELECT @@ERROR AS code')->row()->code;
393 $error['message'] = $message;
394 }
395
396 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398
399 // --------------------------------------------------------------------
400
401 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300402 * Update statement
403 *
404 * Generates a platform-specific update string from the supplied data
405 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200406 * @param string $table
407 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300408 * @return string
409 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300410 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300411 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300412 $this->qb_limit = FALSE;
413 $this->qb_orderby = array();
414 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300415 }
416
417 // --------------------------------------------------------------------
418
419 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * Truncate statement
421 *
422 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300423 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200424 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300425 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200427 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200429 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200430 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300432 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // --------------------------------------------------------------------
436
437 /**
438 * Delete statement
439 *
440 * Generates a platform-specific delete string from the supplied data
441 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200442 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200444 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300445 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300447 if ($this->qb_limit)
448 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300449 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 +0300450 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
Andrey Andreevb0478652012-07-18 15:34:46 +0300452 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454
455 // --------------------------------------------------------------------
456
457 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200458 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 *
460 * Generates a platform-specific LIMIT clause
461 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200462 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @return string
464 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300465 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300467 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300468
469 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
470 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300471 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300472 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300473 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300474
475 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300476 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300477
Andrey Andreev44514542012-10-23 15:35:09 +0300478 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
479 if (count($this->qb_select) === 0)
480 {
481 $select = '*'; // Inevitable
482 }
483 else
484 {
485 // Use only field names and their aliases, everything else is out of our scope.
486 $select = array();
487 $field_regexp = ($this->_quoted_identifier)
488 ? '("[^\"]+")' : '(\[[^\]]+\])';
489 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
490 {
491 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
492 ? $m[1] : $this->qb_select[$i];
493 }
494 $select = implode(', ', $select);
495 }
496
497 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300498 .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 +0300499 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300500 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300501 }
502
503 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200509 * Insert batch statement
510 *
511 * Generates a platform-specific insert string from the supplied data.
512 *
513 * @param string $table Table name
514 * @param array $keys INSERT keys
515 * @param array $values INSERT values
516 * @return string|bool
517 */
518 protected function _insert_batch($table, $keys, $values)
519 {
520 // Multiple-value inserts are only supported as of SQL Server 2008
521 if (version_compare($this->version(), '10', '>='))
522 {
523 return parent::_insert_batch($table, $keys, $values);
524 }
525
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200526 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200527 }
528
529 // --------------------------------------------------------------------
530
531 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * Close DB Connection
533 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @return void
535 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300536 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300538 mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200539 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000540
541}