blob: ea9f9483bf387a1255286e143f6501ca23b927a1 [file] [log] [blame]
Alex Bilbie84445d02011-03-10 16:43:39 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000031 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000032 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000043class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000044
Timothy Warren0e20da12012-03-19 19:05:46 -040045 var $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000046
47 // The character used for escaping
Timothy Warren0e20da12012-03-19 19:05:46 -040048 var $_escape_char = '';
Alex Bilbie84445d02011-03-10 16:43:39 +000049
50 // clause and character used for LIKE escape sequences
Timothy Warren0e20da12012-03-19 19:05:46 -040051 var $_like_escape_str = " ESCAPE '%s' ";
52 var $_like_escape_chr = '!';
Alex Bilbie84445d02011-03-10 16:43:39 +000053
54 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
Timothy Warren0e20da12012-03-19 19:05:46 -040057 * used for the count_all() and count_all_results() functions.
Alex Bilbie84445d02011-03-10 16:43:39 +000058 */
Timothy Warren0e20da12012-03-19 19:05:46 -040059 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword = ' ASC'; // not currently supported
Alex Bilbie84445d02011-03-10 16:43:39 +000061
62 /**
63 * Non-persistent database connection
64 *
Timothy Warren0e20da12012-03-19 19:05:46 -040065 * @access private called by the base class
Alex Bilbie84445d02011-03-10 16:43:39 +000066 * @return resource
67 */
Timothy Warren0e20da12012-03-19 19:05:46 -040068 function db_connect($pooling = false)
Alex Bilbie84445d02011-03-10 16:43:39 +000069 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000070 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
71 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
72
73 $connection = array(
74 'UID' => empty($this->username) ? '' : $this->username,
75 'PWD' => empty($this->password) ? '' : $this->password,
76 'Database' => $this->database,
77 'ConnectionPooling' => $pooling ? 1 : 0,
78 'CharacterSet' => $character_set,
79 'ReturnDatesAsStrings' => 1
80 );
81
82 // If the username and password are both empty, assume this is a
83 // 'Windows Authentication Mode' connection.
84 if(empty($connection['UID']) && empty($connection['PWD'])) {
85 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000086 }
87
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000088 return sqlsrv_connect($this->hostname, $connection);
Alex Bilbie84445d02011-03-10 16:43:39 +000089 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
95 *
Timothy Warren0e20da12012-03-19 19:05:46 -040096 * @access private called by the base class
Alex Bilbie84445d02011-03-10 16:43:39 +000097 * @return resource
98 */
Timothy Warren0e20da12012-03-19 19:05:46 -040099 function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000100 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300101 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Reconnect
108 *
109 * Keep / reestablish the db connection if no queries have been
110 * sent for a length of time exceeding the server's idle timeout
111 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400112 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000113 * @return void
114 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400115 function reconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000116 {
117 // not implemented in MSSQL
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Select the database
124 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200125 * @param string database name
126 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000127 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200128 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000129 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200130 if ($database === '')
131 {
132 $database = $this->database;
133 }
134
135 if ($this->_execute('USE '.$database))
136 {
137 $this->database = $database;
138 return TRUE;
139 }
140
141 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000142 }
143
144 // --------------------------------------------------------------------
145
146 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000147 * Execute the query
148 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400149 * @access private called by the base class
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 * @param string an SQL query
151 * @return resource
152 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400153 function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000154 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000155 return sqlsrv_query($this->conn_id, $sql, null, array(
156 'Scrollable' => SQLSRV_CURSOR_STATIC,
157 'SendStreamParamsAtExec' => true
158 ));
Alex Bilbie84445d02011-03-10 16:43:39 +0000159 }
160
161 // --------------------------------------------------------------------
162
163 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000164 * Begin Transaction
165 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400166 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000167 * @return bool
168 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400169 function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000170 {
171 if ( ! $this->trans_enabled)
172 {
173 return TRUE;
174 }
175
176 // When transactions are nested we only begin/commit/rollback the outermost ones
177 if ($this->_trans_depth > 0)
178 {
179 return TRUE;
180 }
181
182 // Reset the transaction failure flag.
183 // If the $test_mode flag is set to TRUE transactions will be rolled back
184 // even if the queries produce a successful result.
185 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
186
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000187 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Commit Transaction
194 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400195 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000196 * @return bool
197 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400198 function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000199 {
200 if ( ! $this->trans_enabled)
201 {
202 return TRUE;
203 }
204
205 // When transactions are nested we only begin/commit/rollback the outermost ones
206 if ($this->_trans_depth > 0)
207 {
208 return TRUE;
209 }
210
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000211 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Rollback Transaction
218 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400219 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000220 * @return bool
221 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400222 function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000223 {
224 if ( ! $this->trans_enabled)
225 {
226 return TRUE;
227 }
228
229 // When transactions are nested we only begin/commit/rollback the outermost ones
230 if ($this->_trans_depth > 0)
231 {
232 return TRUE;
233 }
234
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000235 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Escape String
242 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400243 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000244 * @param string
245 * @param bool whether or not the string will be used in a LIKE condition
246 * @return string
247 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400248 function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000249 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000250 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000251 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Affected Rows
258 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400259 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000260 * @return integer
261 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400262 function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000263 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000264 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Insert ID
271 *
272 * Returns the last id created in the Identity column.
273 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400274 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000275 * @return integer
276 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400277 function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000278 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000279 return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
Alex Bilbie84445d02011-03-10 16:43:39 +0000280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Parse major version
286 *
287 * Grabs the major version number from the
288 * database server version string passed in.
289 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400290 * @access private
Alex Bilbie84445d02011-03-10 16:43:39 +0000291 * @param string $version
292 * @return int16 major version number
293 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400294 function _parse_major_version($version)
Alex Bilbie84445d02011-03-10 16:43:39 +0000295 {
296 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
297 return $ver_info[1]; // return the major version b/c that's all we're interested in.
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200303 * Database version number
304 *
305 * @return string
306 */
307 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000308 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200309 if (isset($this->data_cache['version']))
310 {
311 return $this->data_cache['version'];
312 }
313
314 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
315 {
316 return FALSE;
317 }
318
319 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * "Count All" query
326 *
327 * Generates a platform-specific query string that counts all records in
328 * the specified database
329 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400330 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000331 * @param string
332 * @return string
333 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400334 function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000335 {
336 if ($table == '')
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000337 return '0';
338
339 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
340
Alex Bilbie84445d02011-03-10 16:43:39 +0000341 if ($query->num_rows() == 0)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000342 return '0';
Alex Bilbie84445d02011-03-10 16:43:39 +0000343
344 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500345 $this->_reset_select();
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000346 return $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * List table query
353 *
354 * Generates a platform-specific query string so that the table names can be fetched
355 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400356 * @access private
Alex Bilbie84445d02011-03-10 16:43:39 +0000357 * @param boolean
358 * @return string
359 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400360 function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000361 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000362 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * List column query
369 *
370 * Generates a platform-specific query string so that the column names can be fetched
371 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400372 * @access private
Alex Bilbie84445d02011-03-10 16:43:39 +0000373 * @param string the table name
374 * @return string
375 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400376 function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000377 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000378 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * Field data query
385 *
386 * Generates a platform-specific query so that the column data can be retrieved
387 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400388 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000389 * @param string the table name
390 * @return object
391 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400392 function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000393 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000394 return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200400 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000401 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200402 * Returns an array containing code and message of the last
403 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200405 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000406 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200407 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000408 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 $error = array('code' => '00000', 'message' => '');
410 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
411
412 if ( ! is_array($sqlsrv_errors))
Andrey Andreev850f6012012-03-01 15:58:25 +0200413 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 return $error;
Andrey Andreev850f6012012-03-01 15:58:25 +0200415 }
416
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 $sqlsrv_error = array_shift($sqlsrv_errors);
418 if (isset($sqlsrv_error['SQLSTATE']))
419 {
420 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
421 }
422 elseif (isset($sqlsrv_error['code']))
423 {
424 $error['code'] = $sqlsrv_error['code'];
425 }
426
427 if (isset($sqlsrv_error['message']))
428 {
429 $error['message'] = $sqlsrv_error['message'];
430 }
431
432 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000433 }
434
435 // --------------------------------------------------------------------
436
437 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000438 * Escape Table Name
439 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400440 * This function adds backticks if the table name has a period
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000441 * in it. Some DBs will get cranky unless periods are escaped
442 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400443 * @access private
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000444 * @param string the table name
445 * @return string
446 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400447 function _escape_table($table)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000448 {
449 return $table;
Timothy Warren0e20da12012-03-19 19:05:46 -0400450 }
451
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000452
453 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000454 * Escape the SQL Identifiers
455 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400456 * This function escapes column and table names
Alex Bilbie84445d02011-03-10 16:43:39 +0000457 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400458 * @access private
Alex Bilbie84445d02011-03-10 16:43:39 +0000459 * @param string
460 * @return string
461 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400462 function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000463 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000464 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000465 }
466
Timothy Warren0e20da12012-03-19 19:05:46 -0400467 // --------------------------------------------------------------------
Alex Bilbie84445d02011-03-10 16:43:39 +0000468
469 /**
470 * From Tables
471 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400472 * This function implicitly groups FROM tables so there is no confusion
Alex Bilbie84445d02011-03-10 16:43:39 +0000473 * about operator precedence in harmony with SQL standards
474 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400475 * @access public
476 * @param type
477 * @return type
Alex Bilbie84445d02011-03-10 16:43:39 +0000478 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400479 function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000480 {
481 if ( ! is_array($tables))
482 {
483 $tables = array($tables);
484 }
485
486 return implode(', ', $tables);
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Insert statement
493 *
494 * Generates a platform-specific insert string from the supplied data
495 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400496 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000497 * @param string the table name
498 * @param array the insert keys
499 * @param array the insert values
500 * @return string
501 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400502 function _insert($table, $keys, $values)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000503 {
504 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Alex Bilbie84445d02011-03-10 16:43:39 +0000505 }
506
507 // --------------------------------------------------------------------
508
509 /**
510 * Update statement
511 *
512 * Generates a platform-specific update string from the supplied data
513 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400514 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000515 * @param string the table name
516 * @param array the update data
517 * @param array the where clause
518 * @param array the orderby clause
519 * @param array the limit clause
520 * @return string
521 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400522 function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000523 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000524 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000525 {
526 $valstr[] = $key." = ".$val;
527 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000528
529 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000530 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000531
Alex Bilbie84445d02011-03-10 16:43:39 +0000532 // --------------------------------------------------------------------
533
534 /**
535 * Truncate statement
536 *
537 * Generates a platform-specific truncate string from the supplied data
538 * If the database does not support the truncate() command
Timothy Warren0e20da12012-03-19 19:05:46 -0400539 * This function maps to "DELETE FROM table"
Alex Bilbie84445d02011-03-10 16:43:39 +0000540 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400541 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000542 * @param string the table name
543 * @return string
544 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400545 function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000546 {
547 return "TRUNCATE ".$table;
548 }
549
550 // --------------------------------------------------------------------
551
552 /**
553 * Delete statement
554 *
555 * Generates a platform-specific delete string from the supplied data
556 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400557 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000558 * @param string the table name
559 * @param array the where clause
560 * @param string the limit clause
561 * @return string
562 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400563 function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000564 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000565 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Limit string
572 *
573 * Generates a platform-specific LIMIT clause
574 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400575 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000576 * @param string the sql query string
577 * @param integer the number of rows to limit the query to
578 * @param integer the offset value
579 * @return string
580 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400581 function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000582 {
583 $i = $limit + $offset;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000584
585 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Close DB Connection
592 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400593 * @access public
Alex Bilbie84445d02011-03-10 16:43:39 +0000594 * @param resource
595 * @return void
596 */
Timothy Warren0e20da12012-03-19 19:05:46 -0400597 function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000598 {
599 @sqlsrv_close($conn_id);
600 }
601
602}
603
Timothy Warren0e20da12012-03-19 19:05:46 -0400604
605
Alex Bilbie84445d02011-03-10 16:43:39 +0000606/* End of file mssql_driver.php */
Timothy Warren0e20da12012-03-19 19:05:46 -0400607/* Location: ./system/database/drivers/mssql/mssql_driver.php */