blob: 390554c7a5f5c0b52dd46d5eb27e1bb956bc3d2a [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 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200406 elseif (isset($error['SQLSTATE']))
407 {
Andrey Andreev850f6012012-03-01 15:58:25 +0200408 return isset($error['code']) ? $error['SQLSTATE'].'/'.$error['code'] : $error['SQLSTATE'];
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200409 }
410 elseif (isset($error['code']))
411 {
412 return $error['code'];
413 }
414
415 return '';
Alex Bilbie84445d02011-03-10 16:43:39 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000421 * Escape Table Name
422 *
423 * This function adds backticks if the table name has a period
424 * in it. Some DBs will get cranky unless periods are escaped
425 *
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000426 * @param string the table name
427 * @return string
428 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200429 protected function _escape_table($table)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000430 {
431 return $table;
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200432 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000433
434 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000435 * Escape the SQL Identifiers
436 *
437 * This function escapes column and table names
438 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000439 * @param string
440 * @return string
441 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200442 public function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000443 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000444 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * From Tables
451 *
452 * This function implicitly groups FROM tables so there is no confusion
453 * about operator precedence in harmony with SQL standards
454 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200455 * @param array
456 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000457 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200458 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000459 {
460 if ( ! is_array($tables))
461 {
462 $tables = array($tables);
463 }
464
465 return implode(', ', $tables);
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Insert statement
472 *
473 * Generates a platform-specific insert string from the supplied data
474 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000475 * @param string the table name
476 * @param array the insert keys
477 * @param array the insert values
478 * @return string
479 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200480 protected function _insert($table, $keys, $values)
481 {
482 return 'INSERT INTO '.$this->_escape_table($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Alex Bilbie84445d02011-03-10 16:43:39 +0000483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * Update statement
489 *
490 * Generates a platform-specific update string from the supplied data
491 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000492 * @param string the table name
493 * @param array the update data
494 * @param array the where clause
495 * @param array the orderby clause
496 * @param array the limit clause
497 * @return string
498 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200499 protected function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000500 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200501 foreach ($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000502 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200503 $valstr[] = $key.' = '.$val;
Alex Bilbie84445d02011-03-10 16:43:39 +0000504 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200505
506 return 'UPDATE '.$this->_escape_table($table).' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000507 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200508
Alex Bilbie84445d02011-03-10 16:43:39 +0000509 // --------------------------------------------------------------------
510
511 /**
512 * Truncate statement
513 *
514 * Generates a platform-specific truncate string from the supplied data
515 * If the database does not support the truncate() command
516 * This function maps to "DELETE FROM table"
517 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000518 * @param string the table name
519 * @return string
520 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200521 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000522 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200523 return 'TRUNCATE '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Delete statement
530 *
531 * Generates a platform-specific delete string from the supplied data
532 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000533 * @param string the table name
534 * @param array the where clause
535 * @param string the limit clause
536 * @return string
537 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200538 protected function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000539 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200540 return 'DELETE FROM '.$this->_escape_table($table).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000541 }
542
543 // --------------------------------------------------------------------
544
545 /**
546 * Limit string
547 *
548 * Generates a platform-specific LIMIT clause
549 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000550 * @param string the sql query string
551 * @param integer the number of rows to limit the query to
552 * @param integer the offset value
553 * @return string
554 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200555 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000556 {
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200557 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Close DB Connection
564 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000565 * @param resource
566 * @return void
567 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200568 protected function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000569 {
570 @sqlsrv_close($conn_id);
571 }
572
573}
574
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200575/* End of file sqlsrv_driver.php */
576/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */