blob: 2b9f822016b88f2d8e4cf3e54f7ccc86d859bbef [file] [log] [blame]
Andrey Andreeve6297342012-03-20 16:25:07 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +00002/**
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
Andrey Andreeve6297342012-03-20 16:25:07 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve6297342012-03-20 16:25:07 +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 Andreeve6297342012-03-20 16:25:07 +020043 public $dbdriver = 'sqlsrv';
Alex Bilbie84445d02011-03-10 16:43:39 +000044
45 // The character used for escaping
Andrey Andreeve6297342012-03-20 16:25:07 +020046 protected $_escape_char = '';
Alex Bilbie84445d02011-03-10 16:43:39 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreeve6297342012-03-20 16:25:07 +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
Timothy Warren0e20da12012-03-19 19:05:46 -040055 * used for the count_all() and count_all_results() functions.
Alex Bilbie84445d02011-03-10 16:43:39 +000056 */
Andrey Andreeve6297342012-03-20 16:25:07 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' ASC'; // not currently supported
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 Andreeve6297342012-03-20 16:25:07 +020065 public function db_connect($pooling = FALSE)
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(
71 'UID' => empty($this->username) ? '' : $this->username,
72 'PWD' => empty($this->password) ? '' : $this->password,
73 'Database' => $this->database,
74 'ConnectionPooling' => $pooling ? 1 : 0,
75 'CharacterSet' => $character_set,
76 'ReturnDatesAsStrings' => 1
77 );
Andrey Andreeve6297342012-03-20 16:25:07 +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 Andreeve6297342012-03-20 16:25:07 +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 Andreeve6297342012-03-20 16:25:07 +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 Andreeve6297342012-03-20 16:25:07 +0200111 public function reconnect()
Alex Bilbie84445d02011-03-10 16:43:39 +0000112 {
113 // not implemented in MSSQL
114 }
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 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000143 * Execute the query
144 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000145 * @param string an SQL query
146 * @return resource
147 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200148 protected function _execute($sql)
Alex Bilbie84445d02011-03-10 16:43:39 +0000149 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200150 return sqlsrv_query($this->conn_id,
151 $sql,
152 NULL,
153 array('Scrollable'=> SQLSRV_CURSOR_STATIC, '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 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200164 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 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200192 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 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200215 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 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200240 public function escape_str($str, $like = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000241 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000242 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000243 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Affected Rows
250 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200251 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000252 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200253 public function affected_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +0000254 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000255 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000256 }
257
258 // --------------------------------------------------------------------
259
260 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200261 * Insert ID
262 *
263 * Returns the last id created in the Identity column.
264 *
265 * @return string
266 */
267 public function insert_id()
Alex Bilbie84445d02011-03-10 16:43:39 +0000268 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200269 $query = $this->query('SELECT @@IDENTITY AS insert_id');
270 $query = $query->row();
271 return $query->insert_id;
Alex Bilbie84445d02011-03-10 16:43:39 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
Andrey Andreeve6297342012-03-20 16:25:07 +0200277 * Parse major version
278 *
279 * Grabs the major version number from the
280 * database server version string passed in.
281 *
282 * @param string $version
283 * @return int major version number
284 */
285 protected function _parse_major_version($version)
Alex Bilbie84445d02011-03-10 16:43:39 +0000286 {
287 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
288 return $ver_info[1]; // return the major version b/c that's all we're interested in.
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200294 * Database version number
295 *
296 * @return string
297 */
298 public function version()
Alex Bilbie84445d02011-03-10 16:43:39 +0000299 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200300 if (isset($this->data_cache['version']))
301 {
302 return $this->data_cache['version'];
303 }
304
305 if (($info = sqlsrv_server_info($this->conn_id)) === FALSE)
306 {
307 return FALSE;
308 }
309
310 return $this->data_cache['version'] = $info['SQLServerVersion'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * "Count All" query
317 *
318 * Generates a platform-specific query string that counts all records in
319 * the specified database
320 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000321 * @param string
Andrey Andreeve6297342012-03-20 16:25:07 +0200322 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +0000323 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200324 public function count_all($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000325 {
326 if ($table == '')
Andrey Andreeve6297342012-03-20 16:25:07 +0200327 {
328 return 0;
329 }
330
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000331 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000332 if ($query->num_rows() == 0)
Andrey Andreeve6297342012-03-20 16:25:07 +0200333 {
334 return 0;
335 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000336
337 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500338 $this->_reset_select();
Andrey Andreeve6297342012-03-20 16:25:07 +0200339 return (int) $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * List table query
346 *
347 * Generates a platform-specific query string so that the table names can be fetched
348 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200349 * @param bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000350 * @return string
351 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200352 protected function _list_tables($prefix_limit = FALSE)
Alex Bilbie84445d02011-03-10 16:43:39 +0000353 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000354 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * List column query
361 *
362 * Generates a platform-specific query string so that the column names can be fetched
363 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000364 * @param string the table name
365 * @return string
366 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200367 protected function _list_columns($table = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000368 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200369 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Field data query
376 *
377 * Generates a platform-specific query so that the column data can be retrieved
378 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000379 * @param string the table name
Andrey Andreeve6297342012-03-20 16:25:07 +0200380 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000381 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200382 protected function _field_data($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000383 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200384 return 'SELECT TOP 1 * FROM '.$table;
Alex Bilbie84445d02011-03-10 16:43:39 +0000385 }
386
387 // --------------------------------------------------------------------
388
389 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000391 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 * Returns an array containing code and message of the last
393 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000394 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200395 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000396 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200397 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000398 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200399 $error = array('code' => '00000', 'message' => '');
400 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
401
402 if ( ! is_array($sqlsrv_errors))
Andrey Andreev850f6012012-03-01 15:58:25 +0200403 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200404 return $error;
Andrey Andreev850f6012012-03-01 15:58:25 +0200405 }
406
Andrey Andreev4be5de12012-03-02 15:45:41 +0200407 $sqlsrv_error = array_shift($sqlsrv_errors);
408 if (isset($sqlsrv_error['SQLSTATE']))
409 {
410 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
411 }
412 elseif (isset($sqlsrv_error['code']))
413 {
414 $error['code'] = $sqlsrv_error['code'];
415 }
416
417 if (isset($sqlsrv_error['message']))
418 {
419 $error['message'] = $sqlsrv_error['message'];
420 }
421
422 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Escape the SQL Identifiers
429 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400430 * This function escapes column and table names
Alex Bilbie84445d02011-03-10 16:43:39 +0000431 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000432 * @param string
433 * @return string
434 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200435 public function _escape_identifiers($item)
Alex Bilbie84445d02011-03-10 16:43:39 +0000436 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000437 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000438 }
439
Timothy Warren0e20da12012-03-19 19:05:46 -0400440 // --------------------------------------------------------------------
Alex Bilbie84445d02011-03-10 16:43:39 +0000441
442 /**
443 * From Tables
444 *
Timothy Warren0e20da12012-03-19 19:05:46 -0400445 * This function implicitly groups FROM tables so there is no confusion
Alex Bilbie84445d02011-03-10 16:43:39 +0000446 * about operator precedence in harmony with SQL standards
447 *
Andrey Andreeve6297342012-03-20 16:25:07 +0200448 * @param array
449 * @return string
Alex Bilbie84445d02011-03-10 16:43:39 +0000450 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200451 protected function _from_tables($tables)
Alex Bilbie84445d02011-03-10 16:43:39 +0000452 {
453 if ( ! is_array($tables))
454 {
455 $tables = array($tables);
456 }
457
458 return implode(', ', $tables);
459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Insert statement
465 *
466 * Generates a platform-specific insert string from the supplied data
467 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000468 * @param string the table name
469 * @param array the insert keys
470 * @param array the insert values
471 * @return string
472 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200473 protected function _insert($table, $keys, $values)
474 {
475 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Alex Bilbie84445d02011-03-10 16:43:39 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Update statement
482 *
483 * Generates a platform-specific update string from the supplied data
484 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000485 * @param string the table name
486 * @param array the update data
487 * @param array the where clause
488 * @param array the orderby clause
489 * @param array the limit clause
490 * @return string
491 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200492 protected function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000493 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000494 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000495 {
496 $valstr[] = $key." = ".$val;
497 }
Andrey Andreeve6297342012-03-20 16:25:07 +0200498
499 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000500 }
Andrey Andreeve6297342012-03-20 16:25:07 +0200501
Alex Bilbie84445d02011-03-10 16:43:39 +0000502 // --------------------------------------------------------------------
503
504 /**
505 * Truncate statement
506 *
507 * Generates a platform-specific truncate string from the supplied data
508 * If the database does not support the truncate() command
Timothy Warren0e20da12012-03-19 19:05:46 -0400509 * This function maps to "DELETE FROM table"
Alex Bilbie84445d02011-03-10 16:43:39 +0000510 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000511 * @param string the table name
512 * @return string
513 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200514 protected function _truncate($table)
Alex Bilbie84445d02011-03-10 16:43:39 +0000515 {
516 return "TRUNCATE ".$table;
517 }
518
519 // --------------------------------------------------------------------
520
521 /**
522 * Delete statement
523 *
524 * Generates a platform-specific delete string from the supplied data
525 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000526 * @param string the table name
527 * @param array the where clause
528 * @param string the limit clause
529 * @return string
530 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200531 protected function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000532 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200533 return 'DELETE FROM '.$table.' WHERE '.implode(' ', $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Limit string
540 *
541 * Generates a platform-specific LIMIT clause
542 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000543 * @param string the sql query string
Andrey Andreeve6297342012-03-20 16:25:07 +0200544 * @param int the number of rows to limit the query to
545 * @param int the offset value
Alex Bilbie84445d02011-03-10 16:43:39 +0000546 * @return string
547 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200548 protected function _limit($sql, $limit, $offset)
Alex Bilbie84445d02011-03-10 16:43:39 +0000549 {
Andrey Andreeve6297342012-03-20 16:25:07 +0200550 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000551 }
552
553 // --------------------------------------------------------------------
554
555 /**
556 * Close DB Connection
557 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000558 * @param resource
559 * @return void
560 */
Andrey Andreeve6297342012-03-20 16:25:07 +0200561 protected function _close($conn_id)
Alex Bilbie84445d02011-03-10 16:43:39 +0000562 {
563 @sqlsrv_close($conn_id);
564 }
565
566}
567
Andrey Andreeve6297342012-03-20 16:25:07 +0200568/* End of file sqlsrv_driver.php */
569/* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */