blob: 9448f052cd81208663982e4fca7efab9596d8d79 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
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 *
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
28// ------------------------------------------------------------------------
29
30/**
31 * MS SQL Database Adapter Class
32 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_mssql_driver extends CI_DB {
44
45 var $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // The character used for escaping
48 var $_escape_char = '';
Derek Jonese4ed5832009-02-20 21:44:59 +000049
50 // clause and character used for LIKE escape sequences
51 var $_like_escape_str = " ESCAPE '%s' ";
52 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
57 * used for the count_all() and count_all_results() functions.
58 */
59 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword = ' ASC'; // not currently supported
61
62 /**
63 * Non-persistent database connection
64 *
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -040067 protected function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
69 if ($this->port != '')
70 {
71 $this->hostname .= ','.$this->port;
72 }
73
74 return @mssql_connect($this->hostname, $this->username, $this->password);
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // --------------------------------------------------------------------
78
79 /**
80 * Persistent database connection
81 *
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020083 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -040084 protected function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
86 if ($this->port != '')
87 {
88 $this->hostname .= ','.$this->port;
89 }
90
91 return @mssql_pconnect($this->hostname, $this->username, $this->password);
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Jones87cbafc2009-02-27 16:29:59 +000096 /**
97 * Reconnect
98 *
99 * Keep / reestablish the db connection if no queries have been
100 * sent for a length of time exceeding the server's idle timeout
101 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000102 * @return void
103 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400104 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000105 {
106 // not implemented in MSSQL
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Derek Jones87cbafc2009-02-27 16:29:59 +0000109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 /**
112 * Select the database
113 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200114 * @param string database name
115 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200116 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200117 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200119 if ($database === '')
120 {
121 $database = $this->database;
122 }
123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // Note: The brackets are required in the event that the DB name
125 // contains reserved characters
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200126 if (@mssql_select_db('['.$database.']', $this->conn_id))
127 {
128 $this->database = $database;
129 return TRUE;
130 }
131
132 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
134
135 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * Execute the query
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string an SQL query
141 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200142 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400143 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 return @mssql_query($sql, $this->conn_id);
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
149
150 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * Begin Transaction
152 *
Barry Mienydd671972010-10-04 16:33:58 +0200153 * @return bool
154 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400155 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 if ( ! $this->trans_enabled)
158 {
159 return TRUE;
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // When transactions are nested we only begin/commit/rollback the outermost ones
163 if ($this->_trans_depth > 0)
164 {
165 return TRUE;
166 }
167
168 // Reset the transaction failure flag.
169 // If the $test_mode flag is set to TRUE transactions will be rolled back
170 // even if the queries produce a successful result.
171 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
172
173 $this->simple_query('BEGIN TRAN');
174 return TRUE;
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Commit Transaction
181 *
Barry Mienydd671972010-10-04 16:33:58 +0200182 * @return bool
183 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400184 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 if ( ! $this->trans_enabled)
187 {
188 return TRUE;
189 }
190
191 // When transactions are nested we only begin/commit/rollback the outermost ones
192 if ($this->_trans_depth > 0)
193 {
194 return TRUE;
195 }
196
197 $this->simple_query('COMMIT TRAN');
198 return TRUE;
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Rollback Transaction
205 *
Barry Mienydd671972010-10-04 16:33:58 +0200206 * @return bool
207 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400208 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 if ( ! $this->trans_enabled)
211 {
212 return TRUE;
213 }
214
215 // When transactions are nested we only begin/commit/rollback the outermost ones
216 if ($this->_trans_depth > 0)
217 {
218 return TRUE;
219 }
220
221 $this->simple_query('ROLLBACK TRAN');
222 return TRUE;
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Escape String
229 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000231 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return string
233 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400234 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000236 if (is_array($str))
237 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500238 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200239 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000240 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200241 }
242
243 return $str;
244 }
245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500247 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Jonese4ed5832009-02-20 21:44:59 +0000249 // escape LIKE condition wildcards
250 if ($like === TRUE)
251 {
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100252 $str = str_replace(
253 array($this->_like_escape_chr, '%', '_'),
254 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
255 $str
256 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Jonese4ed5832009-02-20 21:44:59 +0000259 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * Affected Rows
266 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 * @return integer
268 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400269 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 return @mssql_rows_affected($this->conn_id);
272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // --------------------------------------------------------------------
275
276 /**
277 * Insert ID
278 *
279 * Returns the last id created in the Identity column.
280 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @return integer
282 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400283 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
285 $ver = self::_parse_major_version($this->version());
286 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
287 $query = $this->query($sql);
288 $row = $query->row();
289 return $row->last_id;
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Parse major version
296 *
Barry Mienydd671972010-10-04 16:33:58 +0200297 * Grabs the major version number from the
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * database server version string passed in.
299 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @param string $version
301 * @return int16 major version number
302 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400303 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
306 return $ver_info[1]; // return the major version b/c that's all we're interested in.
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Version number query string
313 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200314 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400316 protected public function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200318 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * "Count All" query
325 *
326 * Generates a platform-specific query string that counts all records in
327 * the specified database
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @param string
330 * @return string
331 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400332 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000335 {
336 return 0;
337 }
338
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200339 $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 +0000340 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000341 {
342 return 0;
343 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000344
345 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500346 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000347 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * List table query
354 *
355 * Generates a platform-specific query string so that the table names can be fetched
356 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * @param boolean
358 * @return string
359 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400360 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // for future compatibility
365 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
366 {
Greg Aker0d424892010-01-26 02:14:44 +0000367 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 return FALSE; // not currently supported
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 return $sql;
372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * List column query
378 *
379 * Generates a platform-specific query string so that the column names can be fetched
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string the table name
382 * @return string
383 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400384 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Barry Mienydd671972010-10-04 16:33:58 +0200386 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * Field data query
393 *
394 * Generates a platform-specific query so that the column data can be retrieved
395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param string the table name
397 * @return object
398 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400399 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Barry Mienydd671972010-10-04 16:33:58 +0200401 return "SELECT TOP 1 * FROM ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
405
406 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200407 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 * Returns an array containing code and message of the last
410 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200416 $query = $this->query('SELECT @@ERROR AS code');
417 $query = $query->row();
418 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Escape the SQL Identifiers
425 *
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400426 * This public function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param string
429 * @return string
430 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400431 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 if ($this->_escape_char == '')
434 {
435 return $item;
436 }
437
438 foreach ($this->_reserved_identifiers as $id)
439 {
440 if (strpos($item, '.'.$id) !== FALSE)
441 {
Barry Mienydd671972010-10-04 16:33:58 +0200442 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // remove duplicates if the user already included the escape
445 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200446 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
448
449 if (strpos($item, '.') !== FALSE)
450 {
Barry Mienydd671972010-10-04 16:33:58 +0200451 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453 else
454 {
455 $str = $this->_escape_char.$item.$this->_escape_char;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // remove duplicates if the user already included the escape
459 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
463
464 /**
465 * From Tables
466 *
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400467 * This public function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * about operator precedence in harmony with SQL standards
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param type
471 * @return type
472 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400473 public function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 if ( ! is_array($tables))
476 {
477 $tables = array($tables);
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 return implode(', ', $tables);
481 }
482
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Insert statement
487 *
488 * Generates a platform-specific insert string from the supplied data
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param string the table name
491 * @param array the insert keys
492 * @param array the insert values
493 * @return string
494 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400495 public function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // --------------------------------------------------------------------
501
502 /**
503 * Update statement
504 *
505 * Generates a platform-specific update string from the supplied data
506 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 * @param string the table name
508 * @param array the update data
509 * @param array the where clause
510 * @param array the orderby clause
511 * @param array the limit clause
512 * @return string
513 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400514 public function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500516 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 $valstr[] = $key." = ".$val;
519 }
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
526
527 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
528
529 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 return $sql;
532 }
533
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 // --------------------------------------------------------------------
536
537 /**
538 * Truncate statement
539 *
540 * Generates a platform-specific truncate string from the supplied data
541 * If the database does not support the truncate() command
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400542 * This public function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string the table name
545 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200546 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400547 public function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
549 return "TRUNCATE ".$table;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // --------------------------------------------------------------------
553
554 /**
555 * Delete statement
556 *
557 * Generates a platform-specific delete string from the supplied data
558 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 * @param string the table name
560 * @param array the where clause
561 * @param string the limit clause
562 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200563 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400564 public function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 $conditions = '';
567
568 if (count($where) > 0 OR count($like) > 0)
569 {
570 $conditions = "\nWHERE ";
571 $conditions .= implode("\n", $this->ar_where);
572
573 if (count($where) > 0 && count($like) > 0)
574 {
575 $conditions .= " AND ";
576 }
577 $conditions .= implode("\n", $like);
578 }
579
580 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 return "DELETE FROM ".$table.$conditions.$limit;
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Limit string
589 *
590 * Generates a platform-specific LIMIT clause
591 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * @param string the sql query string
593 * @param integer the number of rows to limit the query to
594 * @param integer the offset value
595 * @return string
596 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400597 public function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 $i = $limit + $offset;
Barry Mienydd671972010-10-04 16:33:58 +0200600
601 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
603
604 // --------------------------------------------------------------------
605
606 /**
607 * Close DB Connection
608 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param resource
610 * @return void
611 */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400612 public function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200615 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000616
617}
618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619/* End of file mssql_driver.php */
Timothy Warrenba1ebbe2012-03-19 17:48:46 -0400620/* Location: ./system/database/drivers/mssql/mssql_driver.php */