blob: 2c45183b901e1e4c676fe10db380c11adc2b0a24 [file] [log] [blame]
Andrey Andreev0e8968a2012-01-26 02:04:37 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +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
Alex Bilbie84445d02011-03-10 16:43:39 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0e8968a2012-01-26 02:04:37 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0e8968a2012-01-26 02:04:37 +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 *
Alex Bilbie84445d02011-03-10 16:43:39 +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)
Alex Bilbie84445d02011-03-10 16:43:39 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Alex Bilbie84445d02011-03-10 16:43:39 +000028/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000029 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000030 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * 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
Alex Bilbie84445d02011-03-10 16:43:39 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000041class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000042
Andrey Andreev0e8968a2012-01-26 02:04:37 +020043 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000044
45 // The character used for escaping
Andrey Andreev0e8968a2012-01-26 02:04:37 +020046 protected $_escape_char = '';
Alex Bilbie84445d02011-03-10 16:43:39 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreeve6297342012-03-20 16:25:07 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreev0e8968a2012-01-26 02:04:37 +020050 protected $_like_escape_chr = '!';
Alex Bilbie84445d02011-03-10 16:43:39 +000051
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' NEWID()';
Alex Bilbie84445d02011-03-10 16:43:39 +000059
60 /**
61 * Non-persistent database connection
62 *
Alex Bilbie84445d02011-03-10 16:43:39 +000063 * @return resource
64 */
Andrey Andreeve6297342012-03-20 16:25:07 +020065 public function db_connect($pooling = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +000066 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000067 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
68 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
69
70 $connection = array(
Andrey Andreev0e8968a2012-01-26 02:04:37 +020071 'UID' => empty($this->username) ? '' : $this->username,
72 'PWD' => empty($this->password) ? '' : $this->password,
73 'Database' => $this->database,
74 'ConnectionPooling' => $pooling ? 1 : 0,
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000075 'CharacterSet' => $character_set,
Andrey Andreev0e8968a2012-01-26 02:04:37 +020076 'ReturnDatesAsStrings' => 1
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000077 );
Andrey Andreev0e8968a2012-01-26 02:04:37 +020078
79 // If the username and password are both empty, assume this is a
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000080 // 'Windows Authentication Mode' connection.
Andrey Andreev0e8968a2012-01-26 02:04:37 +020081 if (empty($connection['UID']) && empty($connection['PWD']))
82 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000083 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000084 }
85
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000086 return sqlsrv_connect($this->hostname, $connection);
Alex Bilbie84445d02011-03-10 16:43:39 +000087 }
88
89 // --------------------------------------------------------------------
90
91 /**
92 * Persistent database connection
93 *
Alex Bilbie84445d02011-03-10 16:43:39 +000094 * @return resource
95 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020096 public function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +000097 {
Kyle Farris37e351f2011-09-07 11:14:46 -030098 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +000099 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Reconnect
105 *
106 * Keep / reestablish the db connection if no queries have been
107 * sent for a length of time exceeding the server's idle timeout
108 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000109 * @return void
110 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200111 public function reconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000112 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200113 // Not supported in MSSQL
Alex Bilbie84445d02011-03-10 16:43:39 +0000114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Select the database
120 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200121 * @param string database name
122 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000123 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200124 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000125 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200126 if ($database === '')
127 {
128 $database = $this->database;
129 }
130
131 if ($this->_execute('USE '.$database))
132 {
133 $this->database = $database;
134 return TRUE;
135 }
136
137 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000138 }
139
140 // --------------------------------------------------------------------
141
142 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000143 * Execute the query
144 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 * @param string an SQL query
146 * @return resource
147 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200148 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000149 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200150 return sqlsrv_query($this->conn_id,
151 $sql,
152 NULL,
153 array('Scrollable'=> SQLSRV_CURSOR_STATIC, 'SendStreamParamsAtExec' => TRUE)
154 );
Alex Bilbie84445d02011-03-10 16:43:39 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Begin Transaction
161 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000162 * @return bool
163 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200164 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000165 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000166 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200167 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000168 {
169 return TRUE;
170 }
171
172 // Reset the transaction failure flag.
173 // If the $test_mode flag is set to TRUE transactions will be rolled back
174 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200175 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000176
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000177 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Commit Transaction
184 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000185 * @return bool
186 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200187 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000188 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000189 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200190 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000191 {
192 return TRUE;
193 }
194
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000195 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Rollback Transaction
202 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000203 * @return bool
204 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200205 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000206 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000207 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200208 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000209 {
210 return TRUE;
211 }
212
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000213 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Escape String
220 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000221 * @param string
222 * @param bool whether or not the string will be used in a LIKE condition
223 * @return string
224 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200225 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000226 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000227 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000228 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Affected Rows
235 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200236 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000237 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200238 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000239 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000240 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200246 * Insert ID
247 *
248 * Returns the last id created in the Identity column.
249 *
250 * @return string
251 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200252 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000253 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200254 $query = $this->query('SELECT @@IDENTITY AS insert_id');
255 $query = $query->row();
256 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200262 * Database version number
263 *
264 * @return string
265 */
266 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000267 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200268 if (isset($this->data_cache['version']))
269 {
270 return $this->data_cache['version'];
271 }
272
273 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
274 {
275 return FALSE;
276 }
277
278 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * "Count All" query
285 *
286 * Generates a platform-specific query string that counts all records in
287 * the specified database
288 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000289 * @param string
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200290 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000291 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200292 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000293 {
294 if ($table == '')
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200295 {
296 return 0;
297 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000298
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200299 $query = $this->query('SELECT COUNT(*) AS numrows FROM '.$this->dbprefix.$table);
Andrey Andreev993f9322012-01-26 14:49:23 +0200300 if ($query->num_rows() == 0)
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200301 {
302 return 0;
303 }
304
305 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500306 $this->_reset_select();
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200307 return (int) $query->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * List table query
314 *
315 * Generates a platform-specific query string so that the table names can be fetched
316 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200317 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000318 * @return string
319 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200320 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000321 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000322 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +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 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000332 * @param string the table name
333 * @return string
334 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200335 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000336 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200337 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +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 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000347 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200348 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000349 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200350 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000351 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200352 return 'SELECT TOP 1 * FROM '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +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.
Alex Bilbie84445d02011-03-10 16:43:39 +0000362 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200363 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000364 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200365 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000366 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200367 $error = array('code' => '00000', 'message' => '');
368 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
369
370 if ( ! is_array($sqlsrv_errors))
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200371 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200372 return $error;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200373 }
374
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 $sqlsrv_error = array_shift($sqlsrv_errors);
376 if (isset($sqlsrv_error['SQLSTATE']))
377 {
378 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
379 }
380 elseif (isset($sqlsrv_error['code']))
381 {
382 $error['code'] = $sqlsrv_error['code'];
383 }
384
385 if (isset($sqlsrv_error['message']))
386 {
387 $error['message'] = $sqlsrv_error['message'];
388 }
389
390 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * Escape the SQL Identifiers
397 *
398 * This function escapes column and table names
399 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000400 * @param string
401 * @return string
402 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200403 public function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000405 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * From Tables
412 *
413 * This function implicitly groups FROM tables so there is no confusion
414 * about operator precedence in harmony with SQL standards
415 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200416 * @param array
417 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000418 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200419 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000420 {
421 if ( ! is_array($tables))
422 {
423 $tables = array($tables);
424 }
425
426 return implode(', ', $tables);
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Insert statement
433 *
434 * Generates a platform-specific insert string from the supplied data
435 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000436 * @param string the table name
437 * @param array the insert keys
438 * @param array the insert values
439 * @return string
440 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200441 protected function _insert($table, $keys, $values)
442 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200443 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Alex Bilbie84445d02011-03-10 16:43:39 +0000444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * Update statement
450 *
451 * Generates a platform-specific update string from the supplied data
452 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000453 * @param string the table name
454 * @param array the update data
455 * @param array the where clause
456 * @param array the orderby clause
457 * @param array the limit clause
458 * @return string
459 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200460 protected function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000461 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200462 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000463 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200464 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000465 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200466
Andrey Andreeve6297342012-03-20 16:25:07 +0200467 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000468 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200469
Alex Bilbie84445d02011-03-10 16:43:39 +0000470 // --------------------------------------------------------------------
471
472 /**
473 * Truncate statement
474 *
475 * Generates a platform-specific truncate string from the supplied data
476 * If the database does not support the truncate() command
477 * This function maps to "DELETE FROM table"
478 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000479 * @param string the table name
480 * @return string
481 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200482 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000483 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200484 return 'TRUNCATE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Delete statement
491 *
492 * Generates a platform-specific delete string from the supplied data
493 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000494 * @param string the table name
495 * @param array the where clause
496 * @param string the limit clause
497 * @return string
498 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200499 protected function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000500 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200501 return 'DELETE FROM '.$table.' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Limit string
508 *
509 * Generates a platform-specific LIMIT clause
510 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000511 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200512 * @param int the number of rows to limit the query to
513 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000514 * @return string
515 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200516 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000517 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200518 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Close DB Connection
525 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000526 * @param resource
527 * @return void
528 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200529 protected function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000530 {
531 @sqlsrv_close($conn_id);
532 }
533
534}
535
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200536/* End of file sqlsrv_driver.php */
Andrey Andreeve6297342012-03-20 16:25:07 +0200537/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */