blob: cdd178261f55b81b2807bc2744ce207af03129f0 [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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 Andreev0e8968a2012-01-26 02:04:37 +020049 protected $_like_escape_str = ' ESCAPE \'%s\' ';
50 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 Andreev0e8968a2012-01-26 02:04:37 +020065 public function db_connect($pooling = TRUE)
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 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000121 * @return resource
122 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200123 public function db_select()
Alex Bilbie84445d02011-03-10 16:43:39 +0000124 {
Alex Bilbie5336ee22011-03-12 23:35:29 +0000125 return $this->_execute('USE ' . $this->database);
Alex Bilbie84445d02011-03-10 16:43:39 +0000126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * Set client character set
132 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000133 * @param string
134 * @param string
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200135 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000136 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200137 public function db_set_charset($charset, $collation)
Alex Bilbie84445d02011-03-10 16:43:39 +0000138 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200139 // This is done upon connect
Alex Bilbie84445d02011-03-10 16:43:39 +0000140 return TRUE;
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Execute the query
147 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000148 * @param string an SQL query
149 * @return resource
150 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200151 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000152 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200153 return sqlsrv_query($this->conn_id, $this->_prep_query($sql), NULL,
154 array('Scrollable' => SQLSRV_CURSOR_STATIC, 'SendStreamParamsAtExec' => TRUE));
Alex Bilbie84445d02011-03-10 16:43:39 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Prep the query
161 *
162 * If needed, each database adapter can prep the query string
163 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000164 * @param string an SQL query
165 * @return string
166 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200167 protected function _prep_query($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000168 {
169 return $sql;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Begin Transaction
176 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000177 * @return bool
178 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200179 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000180 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000181 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200182 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +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 Andreev0e8968a2012-01-26 02:04:37 +0200190 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000191
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000192 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Commit Transaction
199 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000200 * @return bool
201 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200202 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000203 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000206 {
207 return TRUE;
208 }
209
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000210 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Rollback Transaction
217 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000218 * @return bool
219 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200220 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000221 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000222 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200223 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000224 {
225 return TRUE;
226 }
227
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000228 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Escape String
235 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000236 * @param string
237 * @param bool whether or not the string will be used in a LIKE condition
238 * @return string
239 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200240 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000242 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000243 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Affected Rows
250 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200251 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000252 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200253 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000254 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000255 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Insert ID
262 *
263 * Returns the last id created in the Identity column.
264 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200265 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000266 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200267 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000268 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200269 $query = $this->query('SELECT @@IDENTITY AS insert_id');
270 $query = $query->row();
271 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Version number query string
278 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200279 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000280 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200281 protected function _version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000282 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000283 $info = sqlsrv_server_info($this->conn_id);
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200284 return $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * "Count All" query
291 *
292 * Generates a platform-specific query string that counts all records in
293 * the specified database
294 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000295 * @param string
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200296 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000297 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200298 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000299 {
300 if ($table == '')
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200301 {
302 return 0;
303 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000304
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200305 $query = $this->query('SELECT COUNT(*) AS numrows FROM '.$this->dbprefix.$table);
306 if ($query->num_rows() === 0)
307 {
308 return 0;
309 }
310
311 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500312 $this->_reset_select();
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200313 return (int) $query->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * List table query
320 *
321 * Generates a platform-specific query string so that the table names can be fetched
322 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200323 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000324 * @return string
325 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200326 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000327 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000328 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * List column query
335 *
336 * Generates a platform-specific query string so that the column names can be fetched
337 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000338 * @param string the table name
339 * @return string
340 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200341 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000342 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000343 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000344 }
345
346 // --------------------------------------------------------------------
347
348 /**
349 * Field data query
350 *
351 * Generates a platform-specific query so that the column data can be retrieved
352 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000353 * @param string the table name
354 * @return object
355 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200356 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000357 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200358 return 'SELECT TOP 1 * FROM '.$this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * The error message string
365 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000366 * @return string
367 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200368 protected function _error_message()
Alex Bilbie84445d02011-03-10 16:43:39 +0000369 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200370 $error = sqlsrv_errors();
371 if ( ! is_array($error))
372 {
373 return '';
374 }
375
376 $error = array_shift($error);
377 return isset($error['message']) ? $error['message'] : '';
Alex Bilbie84445d02011-03-10 16:43:39 +0000378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * The error message number
384 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200385 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000386 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200387 protected function _error_number()
Alex Bilbie84445d02011-03-10 16:43:39 +0000388 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200389 $error = sqlsrv_errors();
390 if ( ! is_array($error))
391 {
392 return '';
393 }
394 elseif (isset($error['SQLSTATE'], $error['code']))
395 {
396 return $error['SQLSTATE'].'/'.$error['code'];
397 }
398 elseif (isset($error['SQLSTATE']))
399 {
400 return $error['SQLSTATE'];
401 }
402 elseif (isset($error['code']))
403 {
404 return $error['code'];
405 }
406
407 return '';
Alex Bilbie84445d02011-03-10 16:43:39 +0000408 }
409
410 // --------------------------------------------------------------------
411
412 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000413 * Escape Table Name
414 *
415 * This function adds backticks if the table name has a period
416 * in it. Some DBs will get cranky unless periods are escaped
417 *
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000418 * @param string the table name
419 * @return string
420 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200421 protected function _escape_table($table)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000422 {
423 return $table;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200424 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000425
426 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000427 * Escape the SQL Identifiers
428 *
429 * This function escapes column and table names
430 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000431 * @param string
432 * @return string
433 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200434 public function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000435 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000436 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * From Tables
443 *
444 * This function implicitly groups FROM tables so there is no confusion
445 * about operator precedence in harmony with SQL standards
446 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200447 * @param array
448 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000449 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200450 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000451 {
452 if ( ! is_array($tables))
453 {
454 $tables = array($tables);
455 }
456
457 return implode(', ', $tables);
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Insert statement
464 *
465 * Generates a platform-specific insert string from the supplied data
466 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000467 * @param string the table name
468 * @param array the insert keys
469 * @param array the insert values
470 * @return string
471 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200472 protected function _insert($table, $keys, $values)
473 {
474 return 'INSERT INTO '.$this->_escape_table($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Alex Bilbie84445d02011-03-10 16:43:39 +0000475 }
476
477 // --------------------------------------------------------------------
478
479 /**
480 * Update statement
481 *
482 * Generates a platform-specific update string from the supplied data
483 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000484 * @param string the table name
485 * @param array the update data
486 * @param array the where clause
487 * @param array the orderby clause
488 * @param array the limit clause
489 * @return string
490 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200491 protected function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000492 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200493 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000494 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200495 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000496 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200497
498 return 'UPDATE '.$this->_escape_table($table).' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000499 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200500
Alex Bilbie84445d02011-03-10 16:43:39 +0000501 // --------------------------------------------------------------------
502
503 /**
504 * Truncate statement
505 *
506 * Generates a platform-specific truncate string from the supplied data
507 * If the database does not support the truncate() command
508 * This function maps to "DELETE FROM table"
509 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000510 * @param string the table name
511 * @return string
512 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200513 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000514 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200515 return 'TRUNCATE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * Delete statement
522 *
523 * Generates a platform-specific delete string from the supplied data
524 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000525 * @param string the table name
526 * @param array the where clause
527 * @param string the limit clause
528 * @return string
529 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200530 protected function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000531 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200532 return 'DELETE FROM '.$this->_escape_table($table).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Limit string
539 *
540 * Generates a platform-specific LIMIT clause
541 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000542 * @param string the sql query string
543 * @param integer the number of rows to limit the query to
544 * @param integer the offset value
545 * @return string
546 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200547 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000548 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200549 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Close DB Connection
556 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000557 * @param resource
558 * @return void
559 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200560 protected function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000561 {
562 @sqlsrv_close($conn_id);
563 }
564
565}
566
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200567/* End of file sqlsrv_driver.php */
568/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */