blob: fff9f92e391c7670e83b1601c18cdcad1aabdd24 [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 Andreevdd7242b2012-01-26 00:43:38 +020046 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 Andreevbd601d32012-02-12 21:16:51 +020060 public function __construct($params)
61 {
62 parent::__construct($params);
63
Andrey Andreev2cb262f2012-03-28 13:45:04 +030064 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020065 {
66 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
67 }
68 }
69
Derek Allard2067d1a2008-11-13 22:59:24 +000070 /**
71 * Non-persistent database connection
72 *
Derek Allard2067d1a2008-11-13 22:59:24 +000073 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020074 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020075 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
Derek Allard2067d1a2008-11-13 22:59:24 +000077 return @mssql_connect($this->hostname, $this->username, $this->password);
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 // --------------------------------------------------------------------
81
82 /**
83 * Persistent database connection
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020087 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Derek Allard2067d1a2008-11-13 22:59:24 +000089 return @mssql_pconnect($this->hostname, $this->username, $this->password);
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Jones87cbafc2009-02-27 16:29:59 +000094 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000095 * Select the database
96 *
Andrey Andreev11454e02012-02-22 16:05:47 +020097 * @param string database name
Andrey Andreev4da24f82012-01-25 21:54:23 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200100 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200102 if ($database === '')
103 {
104 $database = $this->database;
105 }
106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // Note: The brackets are required in the event that the DB name
108 // contains reserved characters
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200109 if (@mssql_select_db('['.$database.']', $this->conn_id))
110 {
111 $this->database = $database;
112 return TRUE;
113 }
114
115 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
118 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * Execute the query
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200124 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200126 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 return @mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Begin Transaction
135 *
Barry Mienydd671972010-10-04 16:33:58 +0200136 * @return bool
137 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200138 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200141 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 return TRUE;
144 }
145
146 // Reset the transaction failure flag.
147 // If the $test_mode flag is set to TRUE transactions will be rolled back
148 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200149 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150
Andrey Andreev4da24f82012-01-25 21:54:23 +0200151 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Commit Transaction
158 *
Barry Mienydd671972010-10-04 16:33:58 +0200159 * @return bool
160 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200161 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200164 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 return TRUE;
167 }
168
Andrey Andreev4da24f82012-01-25 21:54:23 +0200169 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Rollback Transaction
176 *
Barry Mienydd671972010-10-04 16:33:58 +0200177 * @return bool
178 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200179 public function trans_rollback()
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
Andrey Andreev4da24f82012-01-25 21:54:23 +0200187 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // --------------------------------------------------------------------
191
192 /**
193 * Escape String
194 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000196 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @return string
198 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200199 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000201 if (is_array($str))
202 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500203 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000205 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
207
208 return $str;
209 }
210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500212 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jonese4ed5832009-02-20 21:44:59 +0000214 // escape LIKE condition wildcards
215 if ($like === TRUE)
216 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200217 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100218 array($this->_like_escape_chr, '%', '_'),
219 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
220 $str
221 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Jonese4ed5832009-02-20 21:44:59 +0000224 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // --------------------------------------------------------------------
228
229 /**
230 * Affected Rows
231 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200232 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200234 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 return @mssql_rows_affected($this->conn_id);
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // --------------------------------------------------------------------
240
241 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200242 * Insert ID
243 *
244 * Returns the last id created in the Identity column.
245 *
246 * @return string
247 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200248 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200250 $query = (self::_parse_major_version($this->version()) > 7)
Andrey Andreev4da24f82012-01-25 21:54:23 +0200251 ? 'SELECT SCOPE_IDENTITY() AS last_id'
252 : 'SELECT @@IDENTITY AS last_id';
253
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200254 $query = $this->query($query);
255 $query = $query->row();
256 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200262 * Parse major version
263 *
264 * Grabs the major version number from the
265 * database server version string passed in.
266 *
267 * @param string $version
268 * @return int major version number
269 */
Andrey Andreev7a189e82012-01-27 21:13:52 +0200270 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
273 return $ver_info[1]; // return the major version b/c that's all we're interested in.
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200279 * Version number query string
280 *
281 * @return string
282 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200283 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200285 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * List table query
292 *
293 * Generates a platform-specific query string so that the table names can be fetched
294 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200295 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @return string
297 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200298 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
300 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // for future compatibility
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100303 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Greg Aker0d424892010-01-26 02:14:44 +0000305 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 return FALSE; // not currently supported
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 return $sql;
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * List column query
316 *
317 * Generates a platform-specific query string so that the column names can be fetched
318 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @param string the table name
320 * @return string
321 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200322 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Barry Mienydd671972010-10-04 16:33:58 +0200324 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Field data query
331 *
332 * Generates a platform-specific query so that the column data can be retrieved
333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200335 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200337 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200339 return 'SELECT TOP 1 * FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200345 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200347 * Returns an array containing code and message of the last
348 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200350 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200352 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200354 $query = $this->query('SELECT @@ERROR AS code');
355 $query = $query->row();
356 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358
359 // --------------------------------------------------------------------
360
361 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * From Tables
363 *
364 * This function implicitly groups FROM tables so there is no confusion
365 * about operator precedence in harmony with SQL standards
366 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200367 * @param array
368 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200370 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 if ( ! is_array($tables))
373 {
374 $tables = array($tables);
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 return implode(', ', $tables);
378 }
379
380 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300383 * Update statement
384 *
385 * Generates a platform-specific update string from the supplied data
386 *
387 * @param string the table name
388 * @param array the update data
389 * @param array the where clause
390 * @param array the orderby clause (ignored)
391 * @param array the limit clause (ignored)
392 * @param array the like clause
393 * @return string
394 */
395 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
396 {
397 foreach($values as $key => $val)
398 {
399 $valstr[] = $key.' = '.$val;
400 }
401
402 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
403
404 if ( ! empty($like))
405 {
406 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
407 }
408
409 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where;
410 }
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 *
419 * If the database does not support the truncate() command,
420 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 * @param string the table name
423 * @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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string the table name
438 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300439 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * @param string the limit clause
441 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200443 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300445 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000446
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300447 empty($where) OR $conditions[] = implode(' ', $where);
448 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300450 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300452 return ($limit)
453 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
454 : 'DELETE FROM '.$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Limit string
461 *
462 * Generates a platform-specific LIMIT clause
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200465 * @param int the number of rows to limit the query to
466 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @return string
468 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200469 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200471 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Close DB Connection
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @return void
480 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300481 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300483 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200484 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000485
486}
487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300489/* Location: ./system/database/drivers/mssql/mssql_driver.php */