blob: 87094e76e072f2aef96a3bb639b7ba2d58aa836d [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();
127 $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query->qi;
128 $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 Andreevb7a47a72012-01-26 02:13:33 +0200291 $query = (self::_parse_major_version($this->version()) > 7)
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 * Parse major version
304 *
305 * Grabs the major version number from the
306 * database server version string passed in.
307 *
308 * @param string $version
309 * @return int major version number
310 */
Andrey Andreev7a189e82012-01-27 21:13:52 +0200311 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
314 return $ver_info[1]; // return the major version b/c that's all we're interested in.
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200320 * Version number query string
321 *
322 * @return string
323 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200324 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200326 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328
329 // --------------------------------------------------------------------
330
331 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * List table query
333 *
334 * Generates a platform-specific query string so that the table names can be fetched
335 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200336 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * @return string
338 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200339 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // for future compatibility
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100344 if ($prefix_limit !== FALSE AND $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Greg Aker0d424892010-01-26 02:14:44 +0000346 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 return FALSE; // not currently supported
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 return $sql;
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * List column query
357 *
358 * Generates a platform-specific query string so that the column names can be fetched
359 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @param string the table name
361 * @return string
362 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200363 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Barry Mienydd671972010-10-04 16:33:58 +0200365 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Field data query
372 *
373 * Generates a platform-specific query so that the column data can be retrieved
374 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200376 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200378 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200380 return 'SELECT TOP 1 * FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200386 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200388 * Returns an array containing code and message of the last
389 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200391 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200393 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200395 $query = $this->query('SELECT @@ERROR AS code');
396 $query = $query->row();
397 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 // --------------------------------------------------------------------
401
402 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * From Tables
404 *
405 * This function implicitly groups FROM tables so there is no confusion
406 * about operator precedence in harmony with SQL standards
407 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200408 * @param array
409 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200411 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300413 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 /**
Andrey Andreev00541ae2012-04-09 11:43:10 +0300419 * Update statement
420 *
421 * Generates a platform-specific update string from the supplied data
422 *
423 * @param string the table name
424 * @param array the update data
425 * @param array the where clause
426 * @param array the orderby clause (ignored)
427 * @param array the limit clause (ignored)
428 * @param array the like clause
429 * @return string
430 */
431 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
432 {
433 foreach($values as $key => $val)
434 {
435 $valstr[] = $key.' = '.$val;
436 }
437
438 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
439
440 if ( ! empty($like))
441 {
442 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
443 }
444
445 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where;
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 * Truncate statement
452 *
453 * Generates a platform-specific truncate string from the supplied data
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300454 *
455 * If the database does not support the truncate() command,
456 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @param string the table name
459 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200460 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200461 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300463 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
467
468 /**
469 * Delete statement
470 *
471 * Generates a platform-specific delete string from the supplied data
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string the table name
474 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300475 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @param string the limit clause
477 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200478 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200479 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300481 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300483 empty($where) OR $conditions[] = implode(' ', $where);
484 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000485
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300486 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300488 return ($limit)
489 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
490 : 'DELETE FROM '.$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Limit string
497 *
498 * Generates a platform-specific LIMIT clause
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200501 * @param int the number of rows to limit the query to
502 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @return string
504 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200505 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200507 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Close DB Connection
514 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @return void
516 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300517 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300519 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200520 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000521
522}
523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300525/* Location: ./system/database/drivers/mssql/mssql_driver.php */