blob: 95ab61b9ffa7b3e3509a7eb2d4f9b1334adec468 [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 Warrenb4172692012-03-19 18:34:11 -040045 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000046
47 // The character used for escaping
Timothy Warrenb4172692012-03-19 18:34:11 -040048 protected $_escape_char = '';
Alex Bilbie84445d02011-03-10 16:43:39 +000049
50 // clause and character used for LIKE escape sequences
Timothy Warrenb4172692012-03-19 18:34:11 -040051 protected $_like_escape_str = " ESCAPE '%s' ";
52 protected $_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 Warrenb4172692012-03-19 18:34:11 -040057 * used for the count_all() and count_all_results() public functions.
Alex Bilbie84445d02011-03-10 16:43:39 +000058 */
Timothy Warrenb4172692012-03-19 18:34:11 -040059 protected $_count_string = "SELECT COUNT(*) AS ";
60 protected $_random_keyword = ' ASC'; // not currently supported
Alex Bilbie84445d02011-03-10 16:43:39 +000061
62 /**
63 * Non-persistent database connection
64 *
Alex Bilbie84445d02011-03-10 16:43:39 +000065 * @return resource
66 */
Timothy Warrenb4172692012-03-19 18:34:11 -040067 protected function db_connect($pooling = false)
Alex Bilbie84445d02011-03-10 16:43:39 +000068 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000069 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
70 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
71
72 $connection = array(
73 'UID' => empty($this->username) ? '' : $this->username,
74 'PWD' => empty($this->password) ? '' : $this->password,
75 'Database' => $this->database,
76 'ConnectionPooling' => $pooling ? 1 : 0,
77 'CharacterSet' => $character_set,
78 'ReturnDatesAsStrings' => 1
79 );
80
81 // If the username and password are both empty, assume this is a
82 // 'Windows Authentication Mode' connection.
83 if(empty($connection['UID']) && empty($connection['PWD'])) {
84 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000085 }
86
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000087 return sqlsrv_connect($this->hostname, $connection);
Alex Bilbie84445d02011-03-10 16:43:39 +000088 }
89
90 // --------------------------------------------------------------------
91
92 /**
93 * Persistent database connection
94 *
Alex Bilbie84445d02011-03-10 16:43:39 +000095 * @return resource
96 */
Timothy Warrenb4172692012-03-19 18:34:11 -040097 protected function db_pconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +000098 {
Kyle Farris37e351f2011-09-07 11:14:46 -030099 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Reconnect
106 *
107 * Keep / reestablish the db connection if no queries have been
108 * sent for a length of time exceeding the server's idle timeout
109 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000110 * @return void
111 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400112 public function reconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000113 {
114 // not implemented in MSSQL
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Select the database
121 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200122 * @param string database name
123 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000124 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200125 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000126 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200127 if ($database === '')
128 {
129 $database = $this->database;
130 }
131
132 if ($this->_execute('USE '.$database))
133 {
134 $this->database = $database;
135 return TRUE;
136 }
137
138 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000144 * Execute the query
145 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000146 * @param string an SQL query
147 * @return resource
148 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400149 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000151 return sqlsrv_query($this->conn_id, $sql, null, array(
152 'Scrollable' => SQLSRV_CURSOR_STATIC,
153 'SendStreamParamsAtExec' => true
154 ));
Alex Bilbie84445d02011-03-10 16:43:39 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000160 * Begin Transaction
161 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000162 * @return bool
163 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400164 public function trans_begin($test_mode = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000165 {
166 if ( ! $this->trans_enabled)
167 {
168 return TRUE;
169 }
170
171 // When transactions are nested we only begin/commit/rollback the outermost ones
172 if ($this->_trans_depth > 0)
173 {
174 return TRUE;
175 }
176
177 // Reset the transaction failure flag.
178 // If the $test_mode flag is set to TRUE transactions will be rolled back
179 // even if the queries produce a successful result.
180 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
181
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000182 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Commit Transaction
189 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000190 * @return bool
191 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400192 public function trans_commit()
Alex Bilbie84445d02011-03-10 16:43:39 +0000193 {
194 if ( ! $this->trans_enabled)
195 {
196 return TRUE;
197 }
198
199 // When transactions are nested we only begin/commit/rollback the outermost ones
200 if ($this->_trans_depth > 0)
201 {
202 return TRUE;
203 }
204
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000205 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Rollback Transaction
212 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000213 * @return bool
214 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400215 public function trans_rollback()
Alex Bilbie84445d02011-03-10 16:43:39 +0000216 {
217 if ( ! $this->trans_enabled)
218 {
219 return TRUE;
220 }
221
222 // When transactions are nested we only begin/commit/rollback the outermost ones
223 if ($this->_trans_depth > 0)
224 {
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 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400240 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 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000251 * @return integer
252 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400253 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 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000265 * @return integer
266 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400267 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000268 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000269 return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
Alex Bilbie84445d02011-03-10 16:43:39 +0000270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Parse major version
276 *
277 * Grabs the major version number from the
278 * database server version string passed in.
279 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000280 * @param string $version
281 * @return int16 major version number
282 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400283 protected function _parse_major_version($version)
Alex Bilbie84445d02011-03-10 16:43:39 +0000284 {
285 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
286 return $ver_info[1]; // return the major version b/c that's all we're interested in.
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200292 * Database version number
293 *
294 * @return string
295 */
296 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000297 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200298 if (isset($this->data_cache['version']))
299 {
300 return $this->data_cache['version'];
301 }
302
303 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
304 {
305 return FALSE;
306 }
307
308 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * "Count All" query
315 *
316 * Generates a platform-specific query string that counts all records in
317 * the specified database
318 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000319 * @param string
320 * @return string
321 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400322 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000323 {
324 if ($table == '')
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000325 return '0';
326
327 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
328
Alex Bilbie84445d02011-03-10 16:43:39 +0000329 if ($query->num_rows() == 0)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000330 return '0';
Alex Bilbie84445d02011-03-10 16:43:39 +0000331
332 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500333 $this->_reset_select();
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000334 return $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * List table query
341 *
342 * Generates a platform-specific query string so that the table names can be fetched
343 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000344 * @param boolean
345 * @return string
346 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400347 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000348 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000349 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * List column query
356 *
357 * Generates a platform-specific query string so that the column names can be fetched
358 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000359 * @param string the table name
360 * @return string
361 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400362 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000363 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000364 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Field data query
371 *
372 * Generates a platform-specific query so that the column data can be retrieved
373 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000374 * @param string the table name
375 * @return object
376 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400377 public function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000378 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000379 return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000380 }
381
382 // --------------------------------------------------------------------
383
384 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200385 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000386 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200387 * Returns an array containing code and message of the last
388 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000389 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000391 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000393 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200394 $error = array('code' => '00000', 'message' => '');
395 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
396
397 if ( ! is_array($sqlsrv_errors))
Andrey Andreev850f6012012-03-01 15:58:25 +0200398 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200399 return $error;
Andrey Andreev850f6012012-03-01 15:58:25 +0200400 }
401
Andrey Andreev4be5de12012-03-02 15:45:41 +0200402 $sqlsrv_error = array_shift($sqlsrv_errors);
403 if (isset($sqlsrv_error['SQLSTATE']))
404 {
405 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
406 }
407 elseif (isset($sqlsrv_error['code']))
408 {
409 $error['code'] = $sqlsrv_error['code'];
410 }
411
412 if (isset($sqlsrv_error['message']))
413 {
414 $error['message'] = $sqlsrv_error['message'];
415 }
416
417 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000423 * Escape Table Name
424 *
Timothy Warrenb4172692012-03-19 18:34:11 -0400425 * This public function adds backticks if the table name has a period
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000426 * in it. Some DBs will get cranky unless periods are escaped
427 *
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000428 * @param string the table name
429 * @return string
430 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400431 protected function _escape_table($table)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000432 {
433 return $table;
Timothy Warrenb4172692012-03-19 18:34:11 -0400434 }
435
436 // --------------------------------------------------------------------------
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000437
438 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000439 * Escape the SQL Identifiers
440 *
Timothy Warrenb4172692012-03-19 18:34:11 -0400441 * This public function escapes column and table names
Alex Bilbie84445d02011-03-10 16:43:39 +0000442 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000443 * @param string
444 * @return string
445 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400446 protected 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
Timothy Warrenb4172692012-03-19 18:34:11 -0400451 // --------------------------------------------------------------------------
Alex Bilbie84445d02011-03-10 16:43:39 +0000452
453 /**
454 * From Tables
455 *
Timothy Warrenb4172692012-03-19 18:34:11 -0400456 * This public function implicitly groups FROM tables so there is no confusion
Alex Bilbie84445d02011-03-10 16:43:39 +0000457 * about operator precedence in harmony with SQL standards
458 *
Timothy Warrenb4172692012-03-19 18:34:11 -0400459 * @param string the table name
460 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000461 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400462 public 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 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400484 public function _insert($table, $keys, $values)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000485 {
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 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400503 public function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000504 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000505 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000506 {
507 $valstr[] = $key." = ".$val;
508 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000509
510 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000511 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000512
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
Timothy Warrenb4172692012-03-19 18:34:11 -0400520 * This public function maps to "DELETE FROM table"
Alex Bilbie84445d02011-03-10 16:43:39 +0000521 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000522 * @param string the table name
523 * @return string
524 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400525 public function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000526 {
527 return "TRUNCATE ".$table;
528 }
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 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400542 public function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000543 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000544 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 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400559 public function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000560 {
561 $i = $limit + $offset;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000562
563 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000564 }
565
566 // --------------------------------------------------------------------
567
568 /**
569 * Close DB Connection
570 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000571 * @param resource
572 * @return void
573 */
Timothy Warrenb4172692012-03-19 18:34:11 -0400574 public function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000575 {
576 @sqlsrv_close($conn_id);
577 }
578
579}
580
Alex Bilbie84445d02011-03-10 16:43:39 +0000581/* End of file mssql_driver.php */
Timothy Warrenb4172692012-03-19 18:34:11 -0400582/* Location: ./system/database/drivers/mssql/mssql_driver.php */