blob: 7634be2bb32abd9ff9ad69aa97e26168cd32217f [file] [log] [blame]
Andrey Andreev4da24f82012-01-25 21:54:23 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * MS SQL Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_mssql_driver extends CI_DB {
42
Andrey Andreev4da24f82012-01-25 21:54:23 +020043 public $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used for escaping
Andrey Andreev082ee2b2012-06-08 15:26:34 +030046 protected $_escape_char = '"';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreev1f619a82012-03-20 16:03:04 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevdd7242b2012-01-26 00:43:38 +020050 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
Andrey Andreev4da24f82012-01-25 21:54:23 +020055 * used for the count_all() and count_all_results() methods.
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
Andrey Andreevdd7242b2012-01-26 00:43:38 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' NEWID()';
Derek Allard2067d1a2008-11-13 22:59:24 +000059
Andrey Andreev082ee2b2012-06-08 15:26:34 +030060 // MSSQL-specific properties
61 protected $_quoted_identifier = TRUE;
62
63 /*
64 * Constructor
65 *
66 * Appends the port number to the hostname, if needed.
67 *
68 * @param array
69 * @return void
70 */
Andrey Andreevbd601d32012-02-12 21:16:51 +020071 public function __construct($params)
72 {
73 parent::__construct($params);
74
Andrey Andreev2cb262f2012-03-28 13:45:04 +030075 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020076 {
77 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
78 }
79 }
80
Andrey Andreev082ee2b2012-06-08 15:26:34 +030081 // --------------------------------------------------------------------
82
Derek Allard2067d1a2008-11-13 22:59:24 +000083 /**
84 * Non-persistent database connection
85 *
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020087 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020088 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Andrey Andreev082ee2b2012-06-08 15:26:34 +030090 return $this->_mssql_connect();
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
96 * Persistent database connection
97 *
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200100 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300102 return $this->_mssql_connect(TRUE);
103 }
104
105 // --------------------------------------------------------------------
106
107 /*
108 * MSSQL Connect
109 *
110 * @param bool
111 * @return resource
112 */
113 protected function _mssql_connect($persistent = FALSE)
114 {
115 $conn_id = ($persistent)
116 ? @mssql_pconnect($this->hostname, $this->username, $this->password)
117 : @mssql_connect($this->hostname, $this->username, $this->password);
118
119 if ( ! $conn_id)
120 {
121 return FALSE;
122 }
123
124 // Determine how identifiers are escaped
125 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
126 $query = $query->row_array();
Andrey Andreev70c72c92012-06-25 00:04:51 +0300127 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300128 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
129
130 return $conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jones87cbafc2009-02-27 16:29:59 +0000135 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * Select the database
137 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200138 * @param string database name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200139 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200140 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200141 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200143 if ($database === '')
144 {
145 $database = $this->database;
146 }
147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // Note: The brackets are required in the event that the DB name
149 // contains reserved characters
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300150 if (@mssql_select_db($this->escape_identifiers($database), $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200151 {
152 $this->database = $database;
153 return TRUE;
154 }
155
156 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * Execute the query
163 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200165 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200166 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200167 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 return @mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Begin Transaction
176 *
Barry Mienydd671972010-10-04 16:33:58 +0200177 * @return bool
178 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200179 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200182 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 return TRUE;
185 }
186
187 // Reset the transaction failure flag.
188 // If the $test_mode flag is set to TRUE transactions will be rolled back
189 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200190 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
Andrey Andreev4da24f82012-01-25 21:54:23 +0200192 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Commit Transaction
199 *
Barry Mienydd671972010-10-04 16:33:58 +0200200 * @return bool
201 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200202 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 return TRUE;
208 }
209
Andrey Andreev4da24f82012-01-25 21:54:23 +0200210 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Rollback Transaction
217 *
Barry Mienydd671972010-10-04 16:33:58 +0200218 * @return bool
219 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200220 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200223 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 return TRUE;
226 }
227
Andrey Andreev4da24f82012-01-25 21:54:23 +0200228 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // --------------------------------------------------------------------
232
233 /**
234 * Escape String
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000237 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @return string
239 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200240 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000242 if (is_array($str))
243 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500244 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200245 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000246 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200247 }
248
249 return $str;
250 }
251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500253 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Jonese4ed5832009-02-20 21:44:59 +0000255 // escape LIKE condition wildcards
256 if ($like === TRUE)
257 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200258 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100259 array($this->_like_escape_chr, '%', '_'),
260 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
261 $str
262 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Jonese4ed5832009-02-20 21:44:59 +0000265 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
269
270 /**
271 * Affected Rows
272 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200273 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200275 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 return @mssql_rows_affected($this->conn_id);
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // --------------------------------------------------------------------
281
282 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200283 * Insert ID
284 *
285 * Returns the last id created in the Identity column.
286 *
287 * @return string
288 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200289 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300291 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200292 ? 'SELECT SCOPE_IDENTITY() AS last_id'
293 : 'SELECT @@IDENTITY AS last_id';
294
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200295 $query = $this->query($query);
296 $query = $query->row();
297 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200303 * Version number query string
304 *
305 * @return string
306 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200307 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200309 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * List table query
316 *
317 * Generates a platform-specific query string so that the table names can be fetched
318 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200319 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @return string
321 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200322 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300324 $sql = 'SELECT '.$this->escape_identifiers('name')
325 .' FROM '.$this->escape_identifiers('sysobjects')
326 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200327
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100328 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300330 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
331 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Andrey Andreev70c72c92012-06-25 00:04:51 +0300334 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * List column query
341 *
342 * Generates a platform-specific query string so that the column names can be fetched
343 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @param string the table name
345 * @return string
346 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200347 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Barry Mienydd671972010-10-04 16:33:58 +0200349 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Field data query
356 *
357 * Generates a platform-specific query so that the column data can be retrieved
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200360 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200362 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300364 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // --------------------------------------------------------------------
368
369 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200372 * Returns an array containing code and message of the last
373 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200379 $query = $this->query('SELECT @@ERROR AS code');
380 $query = $query->row();
381 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * From Tables
388 *
389 * This function implicitly groups FROM tables so there is no confusion
390 * about operator precedence in harmony with SQL standards
391 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200392 * @param array
393 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200395 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300397 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300403 * Update statement
404 *
405 * Generates a platform-specific update string from the supplied data
406 *
407 * @param string the table name
408 * @param array the update data
409 * @param array the where clause
410 * @param array the orderby clause (ignored)
411 * @param array the limit clause (ignored)
412 * @param array the like clause
413 * @return string
414 */
415 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
416 {
417 foreach($values as $key => $val)
418 {
419 $valstr[] = $key.' = '.$val;
420 }
421
422 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
423
424 if ( ! empty($like))
425 {
426 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
427 }
428
429 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where;
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * Truncate statement
436 *
437 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300438 *
439 * If the database does not support the truncate() command,
440 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param string the table name
443 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200444 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200445 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300447 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // --------------------------------------------------------------------
451
452 /**
453 * Delete statement
454 *
455 * Generates a platform-specific delete string from the supplied data
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param string the table name
458 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300459 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @param string the limit clause
461 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200462 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200463 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300465 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300467 empty($where) OR $conditions[] = implode(' ', $where);
468 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300470 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000471
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300472 return ($limit)
473 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
474 : 'DELETE FROM '.$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // --------------------------------------------------------------------
478
479 /**
480 * Limit string
481 *
482 * Generates a platform-specific LIMIT clause
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200485 * @param int the number of rows to limit the query to
486 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @return string
488 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200489 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreev71379ca2012-06-11 16:12:43 +0300491 // As of SQL Server 2012 (11.0.*) OFFSET is supported
492 if (version_compare($this->version(), '11', '>='))
493 {
494 return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
495 }
496
497 $limit = $offset + $limit;
498
499 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
500 // however an ORDER BY clause is required for it to work
501 if (version_compare($this->version(), '9', '>=') && $offset && ! empty($this->qb_orderby))
502 {
503 $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby);
504
505 // We have to strip the ORDER BY clause
506 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
507
508 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
509 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
510 ."\n) ".$this->escape_identifiers('CI_subquery')
511 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit;
512 }
513
514 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
516
517 // --------------------------------------------------------------------
518
519 /**
520 * Close DB Connection
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @return void
523 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300524 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300526 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200527 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000528
529}
530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300532/* Location: ./system/database/drivers/mssql/mssql_driver.php */