blob: 2fd21dbf3c786dc722a24f9dcda440762d125842 [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 Andreevf1e1b772012-11-16 01:27:00 +0200347 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200349 if ($table === '')
350 {
351 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
352 }
353
354 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
355 FROM INFORMATION_SCHEMA.Columns
Andrey Andreev46583072012-11-16 16:44:31 +0200356 WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200357
358 if (($query = $this->query($sql)) === FALSE)
359 {
360 return FALSE;
361 }
362 $query = $query->result_object();
363
364 $retval = array();
365 for ($i = 0, $c = count($query); $i < $c; $i++)
366 {
367 $retval[$i] = new stdClass();
368 $retval[$i]->name = $query[$i]->COLUMN_NAME;
369 $retval[$i]->type = $query[$i]->DATA_TYPE;
Andrey Andreeve1580572012-11-16 16:17:54 +0200370 $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 +0200371 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
372 }
373
374 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 // --------------------------------------------------------------------
378
379 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200380 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200382 * Returns an array containing code and message of the last
383 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200387 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200389 $query = $this->query('SELECT @@ERROR AS code');
390 $query = $query->row();
391 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393
394 // --------------------------------------------------------------------
395
396 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300397 * Update statement
398 *
399 * Generates a platform-specific update string from the supplied data
400 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200401 * @param string $table
402 * @param array $values
Andrey Andreev00541ae2012-04-09 11:43:10 +0300403 * @return string
404 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300405 protected function _update($table, $values)
Andrey Andreev00541ae2012-04-09 11:43:10 +0300406 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300407 $this->qb_limit = FALSE;
408 $this->qb_orderby = array();
409 return parent::_update($table, $values);
Andrey Andreev00541ae2012-04-09 11:43:10 +0300410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * Truncate statement
416 *
417 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300418 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200419 * If the database does not support the TRUNCATE statement,
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300420 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200422 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200424 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200425 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300427 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // --------------------------------------------------------------------
431
432 /**
433 * Delete statement
434 *
435 * Generates a platform-specific delete string from the supplied data
436 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200437 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200439 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300440 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300442 if ($this->qb_limit)
443 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300444 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 +0300445 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000446
Andrey Andreevb0478652012-07-18 15:34:46 +0300447 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 // --------------------------------------------------------------------
451
452 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200453 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 *
455 * Generates a platform-specific LIMIT clause
456 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200457 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @return string
459 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300460 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300462 $limit = $this->qb_offset + $this->qb_limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300463
464 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
465 // however an ORDER BY clause is required for it to work
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300466 if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
Andrey Andreev71379ca2012-06-11 16:12:43 +0300467 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300468 $orderby = $this->_compile_order_by();
Andrey Andreev71379ca2012-06-11 16:12:43 +0300469
470 // We have to strip the ORDER BY clause
Andrey Andreev2d486232012-07-19 14:46:51 +0300471 $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
Andrey Andreev71379ca2012-06-11 16:12:43 +0300472
Andrey Andreev44514542012-10-23 15:35:09 +0300473 // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
474 if (count($this->qb_select) === 0)
475 {
476 $select = '*'; // Inevitable
477 }
478 else
479 {
480 // Use only field names and their aliases, everything else is out of our scope.
481 $select = array();
482 $field_regexp = ($this->_quoted_identifier)
483 ? '("[^\"]+")' : '(\[[^\]]+\])';
484 for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
485 {
486 $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
487 ? $m[1] : $this->qb_select[$i];
488 }
489 $select = implode(', ', $select);
490 }
491
492 return 'SELECT '.$select." FROM (\n\n"
Andrey Andreev2d486232012-07-19 14:46:51 +0300493 .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 +0300494 ."\n\n) ".$this->escape_identifiers('CI_subquery')
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300495 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
Andrey Andreev71379ca2012-06-11 16:12:43 +0300496 }
497
498 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 // --------------------------------------------------------------------
502
503 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200504 * Insert batch statement
505 *
506 * Generates a platform-specific insert string from the supplied data.
507 *
508 * @param string $table Table name
509 * @param array $keys INSERT keys
510 * @param array $values INSERT values
511 * @return string|bool
512 */
513 protected function _insert_batch($table, $keys, $values)
514 {
515 // Multiple-value inserts are only supported as of SQL Server 2008
516 if (version_compare($this->version(), '10', '>='))
517 {
518 return parent::_insert_batch($table, $keys, $values);
519 }
520
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200521 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreev083e3c82012-11-06 12:48:32 +0200522 }
523
524 // --------------------------------------------------------------------
525
526 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * Close DB Connection
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @return void
530 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300531 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300533 mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200534 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000535
536}
537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300539/* Location: ./system/database/drivers/mssql/mssql_driver.php */