blob: 9d61afc4629595799c15c1c730e58311f7192cc5 [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 *
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 /**
143 * Set client character set
144 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 * @param string
146 * @param string
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200147 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000148 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200149 public function db_set_charset($charset, $collation)
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200151 // This is done upon connect
Alex Bilbie84445d02011-03-10 16:43:39 +0000152 return TRUE;
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Execute the query
159 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000160 * @param string an SQL query
161 * @return resource
162 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200163 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000164 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200165 return sqlsrv_query($this->conn_id, $this->_prep_query($sql), NULL,
166 array('Scrollable' => SQLSRV_CURSOR_STATIC, 'SendStreamParamsAtExec' => TRUE));
Alex Bilbie84445d02011-03-10 16:43:39 +0000167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Prep the query
173 *
174 * If needed, each database adapter can prep the query string
175 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000176 * @param string an SQL query
177 * @return string
178 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200179 protected function _prep_query($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000180 {
181 return $sql;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Begin Transaction
188 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000189 * @return bool
190 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200191 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000192 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000193 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200194 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000195 {
196 return TRUE;
197 }
198
199 // Reset the transaction failure flag.
200 // If the $test_mode flag is set to TRUE transactions will be rolled back
201 // even if the queries produce a successful result.
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200202 $this->_trans_failure = ($test_mode === TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000203
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000204 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Commit Transaction
211 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000212 * @return bool
213 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200214 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000215 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000216 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200217 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000218 {
219 return TRUE;
220 }
221
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000222 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Rollback Transaction
229 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000230 * @return bool
231 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200232 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000233 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000234 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200235 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Alex Bilbie84445d02011-03-10 16:43:39 +0000236 {
237 return TRUE;
238 }
239
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000240 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Escape String
247 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000248 * @param string
249 * @param bool whether or not the string will be used in a LIKE condition
250 * @return string
251 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200252 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000253 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000254 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000255 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Affected Rows
262 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200263 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000264 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200265 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000266 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000267 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Insert ID
274 *
275 * Returns the last id created in the Identity column.
276 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200277 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000278 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200279 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000280 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200281 $query = $this->query('SELECT @@IDENTITY AS insert_id');
282 $query = $query->row();
283 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Version number query string
290 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200291 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000292 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200293 protected function _version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000294 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000295 $info = sqlsrv_server_info($this->conn_id);
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200296 return $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * "Count All" query
303 *
304 * Generates a platform-specific query string that counts all records in
305 * the specified database
306 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000307 * @param string
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200308 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000309 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200310 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000311 {
312 if ($table == '')
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200313 {
314 return 0;
315 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000316
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200317 $query = $this->query('SELECT COUNT(*) AS numrows FROM '.$this->dbprefix.$table);
Andrey Andreev993f9322012-01-26 14:49:23 +0200318 if ($query->num_rows() == 0)
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200319 {
320 return 0;
321 }
322
323 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500324 $this->_reset_select();
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200325 return (int) $query->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * List table query
332 *
333 * Generates a platform-specific query string so that the table names can be fetched
334 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200335 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000336 * @return string
337 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200338 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000339 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000340 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * List column query
347 *
348 * Generates a platform-specific query string so that the column names can be fetched
349 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000350 * @param string the table name
351 * @return string
352 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200353 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000354 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000355 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Field data query
362 *
363 * Generates a platform-specific query so that the column data can be retrieved
364 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000365 * @param string the table name
366 * @return object
367 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200368 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000369 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200370 return 'SELECT TOP 1 * FROM '.$this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * The error message string
377 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000378 * @return string
379 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200380 protected function _error_message()
Alex Bilbie84445d02011-03-10 16:43:39 +0000381 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200382 $error = sqlsrv_errors();
383 if ( ! is_array($error))
384 {
385 return '';
386 }
387
388 $error = array_shift($error);
389 return isset($error['message']) ? $error['message'] : '';
Alex Bilbie84445d02011-03-10 16:43:39 +0000390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * The error message number
396 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200397 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000398 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200399 protected function _error_number()
Alex Bilbie84445d02011-03-10 16:43:39 +0000400 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200401 $error = sqlsrv_errors();
402 if ( ! is_array($error))
403 {
404 return '';
405 }
406 elseif (isset($error['SQLSTATE'], $error['code']))
407 {
408 return $error['SQLSTATE'].'/'.$error['code'];
409 }
410 elseif (isset($error['SQLSTATE']))
411 {
412 return $error['SQLSTATE'];
413 }
414 elseif (isset($error['code']))
415 {
416 return $error['code'];
417 }
418
419 return '';
Alex Bilbie84445d02011-03-10 16:43:39 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000425 * Escape Table Name
426 *
427 * This function adds backticks if the table name has a period
428 * in it. Some DBs will get cranky unless periods are escaped
429 *
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000430 * @param string the table name
431 * @return string
432 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200433 protected function _escape_table($table)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000434 {
435 return $table;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200436 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000437
438 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000439 * Escape the SQL Identifiers
440 *
441 * This function escapes column and table names
442 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000443 * @param string
444 * @return string
445 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200446 public function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000447 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000448 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * From Tables
455 *
456 * This function implicitly groups FROM tables so there is no confusion
457 * about operator precedence in harmony with SQL standards
458 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200459 * @param array
460 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000461 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200462 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000463 {
464 if ( ! is_array($tables))
465 {
466 $tables = array($tables);
467 }
468
469 return implode(', ', $tables);
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Insert statement
476 *
477 * Generates a platform-specific insert string from the supplied data
478 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000479 * @param string the table name
480 * @param array the insert keys
481 * @param array the insert values
482 * @return string
483 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200484 protected function _insert($table, $keys, $values)
485 {
486 return 'INSERT INTO '.$this->_escape_table($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Alex Bilbie84445d02011-03-10 16:43:39 +0000487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Update statement
493 *
494 * Generates a platform-specific update string from the supplied data
495 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000496 * @param string the table name
497 * @param array the update data
498 * @param array the where clause
499 * @param array the orderby clause
500 * @param array the limit clause
501 * @return string
502 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200503 protected function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000504 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200505 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000506 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200507 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000508 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200509
510 return 'UPDATE '.$this->_escape_table($table).' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000511 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200512
Alex Bilbie84445d02011-03-10 16:43:39 +0000513 // --------------------------------------------------------------------
514
515 /**
516 * Truncate statement
517 *
518 * Generates a platform-specific truncate string from the supplied data
519 * If the database does not support the truncate() command
520 * This function maps to "DELETE FROM table"
521 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000522 * @param string the table name
523 * @return string
524 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200525 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000526 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200527 return 'TRUNCATE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000528 }
529
530 // --------------------------------------------------------------------
531
532 /**
533 * Delete statement
534 *
535 * Generates a platform-specific delete string from the supplied data
536 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000537 * @param string the table name
538 * @param array the where clause
539 * @param string the limit clause
540 * @return string
541 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200542 protected function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000543 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200544 return 'DELETE FROM '.$this->_escape_table($table).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Limit string
551 *
552 * Generates a platform-specific LIMIT clause
553 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000554 * @param string the sql query string
555 * @param integer the number of rows to limit the query to
556 * @param integer the offset value
557 * @return string
558 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200559 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000560 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200561 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000562 }
563
564 // --------------------------------------------------------------------
565
566 /**
567 * Close DB Connection
568 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000569 * @param resource
570 * @return void
571 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200572 protected function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000573 {
574 @sqlsrv_close($conn_id);
575 }
576
577}
578
Andrey Andreev5ac69992012-02-24 11:53:08 +0200579<<<<<<< HEAD
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200580/* End of file sqlsrv_driver.php */
581/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */
Andrey Andreev5ac69992012-02-24 11:53:08 +0200582=======
Alex Bilbie84445d02011-03-10 16:43:39 +0000583
584
585/* End of file mssql_driver.php */
Andrey Andreev11454e02012-02-22 16:05:47 +0200586/* Location: ./system/database/drivers/mssql/mssql_driver.php */
Andrey Andreev5ac69992012-02-24 11:53:08 +0200587>>>>>>> upstream/develop