blob: 3026b36dc1f2884a0d232a5a4295094f005d2503 [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 *
Andrey Andreevfac37612012-07-02 14:56:20 +030086 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Andrey Andreevfac37612012-07-02 14:56:20 +030089 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Andrey Andreevfac37612012-07-02 14:56:20 +030091 $this->conn_id = ($persistent)
92 ? @mssql_pconnect($this->hostname, $this->username, $this->password)
93 : @mssql_connect($this->hostname, $this->username, $this->password);
94
95 if ( ! $this->conn_id)
96 {
97 return FALSE;
98 }
99
100 // Determine how identifiers are escaped
101 $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
102 $query = $query->row_array();
103 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
104 $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
105
106 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
110
111 /**
112 * Persistent database connection
113 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200115 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200116 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Andrey Andreevfac37612012-07-02 14:56:20 +0300118 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Jones87cbafc2009-02-27 16:29:59 +0000123 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * Select the database
125 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200126 * @param string database name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200127 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200129 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200131 if ($database === '')
132 {
133 $database = $this->database;
134 }
135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // Note: The brackets are required in the event that the DB name
137 // contains reserved characters
Andrey Andreev082ee2b2012-06-08 15:26:34 +0300138 if (@mssql_select_db($this->escape_identifiers($database), $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200139 {
140 $this->database = $database;
141 return TRUE;
142 }
143
144 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
146
147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * Execute the query
151 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200153 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200154 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200155 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 return @mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * Begin Transaction
164 *
Barry Mienydd671972010-10-04 16:33:58 +0200165 * @return bool
166 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200167 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200170 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 return TRUE;
173 }
174
175 // Reset the transaction failure flag.
176 // If the $test_mode flag is set to TRUE transactions will be rolled back
177 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200178 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
Andrey Andreev4da24f82012-01-25 21:54:23 +0200180 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Commit Transaction
187 *
Barry Mienydd671972010-10-04 16:33:58 +0200188 * @return bool
189 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200190 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200193 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 return TRUE;
196 }
197
Andrey Andreev4da24f82012-01-25 21:54:23 +0200198 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Rollback Transaction
205 *
Barry Mienydd671972010-10-04 16:33:58 +0200206 * @return bool
207 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200208 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200211 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 return TRUE;
214 }
215
Andrey Andreev4da24f82012-01-25 21:54:23 +0200216 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // --------------------------------------------------------------------
220
221 /**
222 * Escape String
223 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000225 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @return string
227 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200228 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000230 if (is_array($str))
231 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500232 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200233 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000234 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200235 }
236
237 return $str;
238 }
239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500241 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jonese4ed5832009-02-20 21:44:59 +0000243 // escape LIKE condition wildcards
244 if ($like === TRUE)
245 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200246 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100247 array($this->_like_escape_chr, '%', '_'),
248 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
249 $str
250 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Jonese4ed5832009-02-20 21:44:59 +0000253 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
257
258 /**
259 * Affected Rows
260 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200261 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200263 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 return @mssql_rows_affected($this->conn_id);
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
269
270 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200271 * Insert ID
272 *
273 * Returns the last id created in the Identity column.
274 *
275 * @return string
276 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200277 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300279 $query = version_compare($this->version(), '8', '>=')
Andrey Andreev4da24f82012-01-25 21:54:23 +0200280 ? 'SELECT SCOPE_IDENTITY() AS last_id'
281 : 'SELECT @@IDENTITY AS last_id';
282
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200283 $query = $this->query($query);
284 $query = $query->row();
285 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200291 * Version number query string
292 *
293 * @return string
294 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200295 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200297 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * List table query
304 *
305 * Generates a platform-specific query string so that the table names can be fetched
306 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200307 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @return string
309 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200310 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300312 $sql = 'SELECT '.$this->escape_identifiers('name')
313 .' FROM '.$this->escape_identifiers('sysobjects')
314 .' WHERE '.$this->escape_identifiers('type')." = 'U'";
Barry Mienydd671972010-10-04 16:33:58 +0200315
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100316 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300318 $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
319 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Andrey Andreev70c72c92012-06-25 00:04:51 +0300322 return $sql.' ORDER BY '.$this->escape_identifiers('name');
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * List column query
329 *
330 * Generates a platform-specific query string so that the column names can be fetched
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string the table name
333 * @return string
334 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200335 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Barry Mienydd671972010-10-04 16:33:58 +0200337 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Field data query
344 *
345 * Generates a platform-specific query so that the column data can be retrieved
346 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200348 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200350 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreev70c72c92012-06-25 00:04:51 +0300352 return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200360 * Returns an array containing code and message of the last
361 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200363 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200365 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200367 $query = $this->query('SELECT @@ERROR AS code');
368 $query = $query->row();
369 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * From Tables
376 *
377 * This function implicitly groups FROM tables so there is no confusion
378 * about operator precedence in harmony with SQL standards
379 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200380 * @param array
381 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200383 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300385 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300391 * Update statement
392 *
393 * Generates a platform-specific update string from the supplied data
394 *
395 * @param string the table name
396 * @param array the update data
397 * @param array the where clause
398 * @param array the orderby clause (ignored)
399 * @param array the limit clause (ignored)
400 * @param array the like clause
401 * @return string
402 */
403 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
404 {
405 foreach($values as $key => $val)
406 {
407 $valstr[] = $key.' = '.$val;
408 }
409
410 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
411
412 if ( ! empty($like))
413 {
414 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
415 }
416
417 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where;
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * Truncate statement
424 *
425 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300426 *
427 * If the database does not support the truncate() command,
428 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @param string the table name
431 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200432 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200433 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300435 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
439
440 /**
441 * Delete statement
442 *
443 * Generates a platform-specific delete string from the supplied data
444 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @param string the table name
446 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300447 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string the limit clause
449 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200450 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200451 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300453 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000454
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300455 empty($where) OR $conditions[] = implode(' ', $where);
456 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300458 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000459
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300460 return ($limit)
461 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
462 : 'DELETE FROM '.$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Limit string
469 *
470 * Generates a platform-specific LIMIT clause
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200473 * @param int the number of rows to limit the query to
474 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @return string
476 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200477 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Andrey Andreev71379ca2012-06-11 16:12:43 +0300479 // As of SQL Server 2012 (11.0.*) OFFSET is supported
480 if (version_compare($this->version(), '11', '>='))
481 {
482 return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
483 }
484
485 $limit = $offset + $limit;
486
487 // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
488 // however an ORDER BY clause is required for it to work
489 if (version_compare($this->version(), '9', '>=') && $offset && ! empty($this->qb_orderby))
490 {
491 $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby);
492
493 // We have to strip the ORDER BY clause
494 $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby)));
495
496 return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n"
497 .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
498 ."\n) ".$this->escape_identifiers('CI_subquery')
499 ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit;
500 }
501
502 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * 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 Andreev79922c02012-05-23 12:27:17 +0300514 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200515 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000516
517}
518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300520/* Location: ./system/database/drivers/mssql/mssql_driver.php */