blob: 1c1b84582b4f9a015b4ad5b6a02dfd5b69830b39 [file] [log] [blame]
Andrey Andreev1f619a82012-03-20 16:03:04 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +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
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1f619a82012-03-20 16:03:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1f619a82012-03-20 16:03:04 +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 *
Derek Allard2067d1a2008-11-13 22:59:24 +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)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * MS SQL Database Adapter Class
30 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_mssql_driver extends CI_DB {
42
Andrey Andreev1f619a82012-03-20 16:03:04 +020043 public $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used for escaping
Andrey Andreev1f619a82012-03-20 16:03:04 +020046 protected $_escape_char = '';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreev1f619a82012-03-20 16:03:04 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
50 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
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 Andreev1f619a82012-03-20 16:03:04 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' ASC'; // not currently supported
Derek Allard2067d1a2008-11-13 22:59:24 +000059
60 /**
61 * Non-persistent database connection
62 *
Derek Allard2067d1a2008-11-13 22:59:24 +000063 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev1f619a82012-03-20 16:03:04 +020065 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
67 if ($this->port != '')
68 {
69 $this->hostname .= ','.$this->port;
70 }
71
72 return @mssql_connect($this->hostname, $this->username, $this->password);
73 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 // --------------------------------------------------------------------
76
77 /**
78 * Persistent database connection
79 *
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020081 */
Andrey Andreev1f619a82012-03-20 16:03:04 +020082 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
84 if ($this->port != '')
85 {
86 $this->hostname .= ','.$this->port;
87 }
88
89 return @mssql_pconnect($this->hostname, $this->username, $this->password);
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Jones87cbafc2009-02-27 16:29:59 +000094 /**
95 * Reconnect
96 *
97 * Keep / reestablish the db connection if no queries have been
98 * sent for a length of time exceeding the server's idle timeout
99 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000100 * @return void
101 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200102 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000103 {
104 // not implemented in MSSQL
105 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000106
Derek Jones87cbafc2009-02-27 16:29:59 +0000107 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 /**
110 * Select the database
111 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200112 * @param string database name
113 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200115 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200117 if ($database === '')
118 {
119 $database = $this->database;
120 }
121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // Note: The brackets are required in the event that the DB name
123 // contains reserved characters
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200124 if (@mssql_select_db('['.$database.']', $this->conn_id))
125 {
126 $this->database = $database;
127 return TRUE;
128 }
129
130 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132
133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * Execute the query
137 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @param string an SQL query
139 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200140 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200141 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 return @mssql_query($sql, $this->conn_id);
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
147
148 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 * Begin Transaction
150 *
Barry Mienydd671972010-10-04 16:33:58 +0200151 * @return bool
152 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200153 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
155 if ( ! $this->trans_enabled)
156 {
157 return TRUE;
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // When transactions are nested we only begin/commit/rollback the outermost ones
161 if ($this->_trans_depth > 0)
162 {
163 return TRUE;
164 }
165
166 // Reset the transaction failure flag.
167 // If the $test_mode flag is set to TRUE transactions will be rolled back
168 // even if the queries produce a successful result.
169 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
170
171 $this->simple_query('BEGIN TRAN');
172 return TRUE;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Commit Transaction
179 *
Barry Mienydd671972010-10-04 16:33:58 +0200180 * @return bool
181 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200182 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 if ( ! $this->trans_enabled)
185 {
186 return TRUE;
187 }
188
189 // When transactions are nested we only begin/commit/rollback the outermost ones
190 if ($this->_trans_depth > 0)
191 {
192 return TRUE;
193 }
194
195 $this->simple_query('COMMIT TRAN');
196 return TRUE;
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Rollback Transaction
203 *
Barry Mienydd671972010-10-04 16:33:58 +0200204 * @return bool
205 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200206 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 if ( ! $this->trans_enabled)
209 {
210 return TRUE;
211 }
212
213 // When transactions are nested we only begin/commit/rollback the outermost ones
214 if ($this->_trans_depth > 0)
215 {
216 return TRUE;
217 }
218
219 $this->simple_query('ROLLBACK TRAN');
220 return TRUE;
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // --------------------------------------------------------------------
224
225 /**
226 * Escape String
227 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000229 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @return string
231 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200232 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000234 if (is_array($str))
235 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500236 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200237 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000238 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200239 }
240
241 return $str;
242 }
243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500245 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Jonese4ed5832009-02-20 21:44:59 +0000247 // escape LIKE condition wildcards
248 if ($like === TRUE)
249 {
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100250 $str = str_replace(
251 array($this->_like_escape_chr, '%', '_'),
252 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
253 $str
254 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Jonese4ed5832009-02-20 21:44:59 +0000257 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // --------------------------------------------------------------------
261
262 /**
263 * Affected Rows
264 *
Andrey Andreev1f619a82012-03-20 16:03:04 +0200265 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200267 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 return @mssql_rows_affected($this->conn_id);
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200275 * Insert ID
276 *
277 * Returns the last id created in the Identity column.
278 *
279 * @return string
280 */
281 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 $ver = self::_parse_major_version($this->version());
284 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
285 $query = $this->query($sql);
286 $row = $query->row();
287 return $row->last_id;
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200293 * Parse major version
294 *
295 * Grabs the major version number from the
296 * database server version string passed in.
297 *
298 * @param string $version
299 * @return int major version number
300 */
301 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
304 return $ver_info[1]; // return the major version b/c that's all we're interested in.
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200310 * Version number query string
311 *
312 * @return string
313 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400314 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200316 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * "Count All" query
323 *
324 * Generates a platform-specific query string that counts all records in
325 * the specified database
326 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 * @param string
328 * @return string
329 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200330 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000333 {
334 return 0;
335 }
336
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200337 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000339 {
340 return 0;
341 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
343 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500344 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000345 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * List table query
352 *
353 * Generates a platform-specific query string so that the table names can be fetched
354 *
Andrey Andreev1f619a82012-03-20 16:03:04 +0200355 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return string
357 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200358 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // for future compatibility
363 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
364 {
Greg Aker0d424892010-01-26 02:14:44 +0000365 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 return FALSE; // not currently supported
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 return $sql;
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * List column query
376 *
377 * Generates a platform-specific query string so that the column names can be fetched
378 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * @param string the table name
380 * @return string
381 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200382 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Barry Mienydd671972010-10-04 16:33:58 +0200384 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * Field data query
391 *
392 * Generates a platform-specific query so that the column data can be retrieved
393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param string the table name
Andrey Andreev1f619a82012-03-20 16:03:04 +0200395 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200397 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Barry Mienydd671972010-10-04 16:33:58 +0200399 return "SELECT TOP 1 * FROM ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401
402 // --------------------------------------------------------------------
403
404 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200405 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200407 * Returns an array containing code and message of the last
408 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 $query = $this->query('SELECT @@ERROR AS code');
415 $query = $query->row();
416 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Escape the SQL Identifiers
423 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400424 * This function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @param string
427 * @return string
428 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200429 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 if ($this->_escape_char == '')
432 {
433 return $item;
434 }
435
436 foreach ($this->_reserved_identifiers as $id)
437 {
438 if (strpos($item, '.'.$id) !== FALSE)
439 {
Barry Mienydd671972010-10-04 16:33:58 +0200440 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // remove duplicates if the user already included the escape
443 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200444 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446
447 if (strpos($item, '.') !== FALSE)
448 {
Barry Mienydd671972010-10-04 16:33:58 +0200449 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451 else
452 {
453 $str = $this->_escape_char.$item.$this->_escape_char;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // remove duplicates if the user already included the escape
457 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // --------------------------------------------------------------------
461
462 /**
463 * From Tables
464 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400465 * This function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * about operator precedence in harmony with SQL standards
467 *
Andrey Andreev1f619a82012-03-20 16:03:04 +0200468 * @param array
469 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200471 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 if ( ! is_array($tables))
474 {
475 $tables = array($tables);
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 return implode(', ', $tables);
479 }
480
481 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 /**
484 * Insert statement
485 *
486 * Generates a platform-specific insert string from the supplied data
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @param string the table name
489 * @param array the insert keys
490 * @param array the insert values
491 * @return string
492 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200493 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200494 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // --------------------------------------------------------------------
499
500 /**
501 * Update statement
502 *
503 * Generates a platform-specific update string from the supplied data
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @param string the table name
506 * @param array the update data
507 * @param array the where clause
508 * @param array the orderby clause
509 * @param array the limit clause
510 * @return string
511 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200512 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500514 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 $valstr[] = $key." = ".$val;
517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
524
525 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
526
527 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 return $sql;
530 }
531
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // --------------------------------------------------------------------
534
535 /**
536 * Truncate statement
537 *
538 * Generates a platform-specific truncate string from the supplied data
539 * If the database does not support the truncate() command
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400540 * This function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @param string the table name
543 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200544 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200545 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
547 return "TRUNCATE ".$table;
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
553 * Delete statement
554 *
555 * Generates a platform-specific delete string from the supplied data
556 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400557 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @param string the table name
559 * @param array the where clause
560 * @param string the limit clause
561 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200562 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400563 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
565 $conditions = '';
566
567 if (count($where) > 0 OR count($like) > 0)
568 {
569 $conditions = "\nWHERE ";
570 $conditions .= implode("\n", $this->ar_where);
571
572 if (count($where) > 0 && count($like) > 0)
573 {
574 $conditions .= " AND ";
575 }
576 $conditions .= implode("\n", $like);
577 }
578
579 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 return "DELETE FROM ".$table.$conditions.$limit;
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
587 * Limit string
588 *
589 * Generates a platform-specific LIMIT clause
590 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200592 * @param int the number of rows to limit the query to
593 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @return string
595 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200596 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 $i = $limit + $offset;
Barry Mienydd671972010-10-04 16:33:58 +0200599
600 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
602
603 // --------------------------------------------------------------------
604
605 /**
606 * Close DB Connection
607 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 * @param resource
609 * @return void
610 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200611 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200614 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000615
616}
617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618/* End of file mssql_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400619/* Location: ./system/database/drivers/mssql/mssql_driver.php */