blob: a93ac57feb72e26d5747a51a529c8101d3397c75 [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 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -040065 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020067 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -040068 function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
70 if ($this->port != '')
71 {
72 $this->hostname .= ','.$this->port;
73 }
74
75 return @mssql_connect($this->hostname, $this->username, $this->password);
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 // --------------------------------------------------------------------
79
80 /**
81 * Persistent database connection
82 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -040083 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020085 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -040086 function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 if ($this->port != '')
89 {
90 $this->hostname .= ','.$this->port;
91 }
92
93 return @mssql_pconnect($this->hostname, $this->username, $this->password);
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Jones87cbafc2009-02-27 16:29:59 +000098 /**
99 * Reconnect
100 *
101 * Keep / reestablish the db connection if no queries have been
102 * sent for a length of time exceeding the server's idle timeout
103 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400104 * @access public
Derek Jones87cbafc2009-02-27 16:29:59 +0000105 * @return void
106 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400107 function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000108 {
109 // not implemented in MSSQL
110 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000111
Derek Jones87cbafc2009-02-27 16:29:59 +0000112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 /**
115 * Select the database
116 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200117 * @param string database name
118 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200119 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200120 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200122 if ($database === '')
123 {
124 $database = $this->database;
125 }
126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // Note: The brackets are required in the event that the DB name
128 // contains reserved characters
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200129 if (@mssql_select_db('['.$database.']', $this->conn_id))
130 {
131 $this->database = $database;
132 return TRUE;
133 }
134
135 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137
138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * Execute the query
142 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400143 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @param string an SQL query
145 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200146 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400147 function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 return @mssql_query($sql, $this->conn_id);
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
153
154 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * Begin Transaction
156 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400157 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200158 * @return bool
159 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400160 function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 if ( ! $this->trans_enabled)
163 {
164 return TRUE;
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // When transactions are nested we only begin/commit/rollback the outermost ones
168 if ($this->_trans_depth > 0)
169 {
170 return TRUE;
171 }
172
173 // Reset the transaction failure flag.
174 // If the $test_mode flag is set to TRUE transactions will be rolled back
175 // even if the queries produce a successful result.
176 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
177
178 $this->simple_query('BEGIN TRAN');
179 return TRUE;
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Commit Transaction
186 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400187 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200188 * @return bool
189 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400190 function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
192 if ( ! $this->trans_enabled)
193 {
194 return TRUE;
195 }
196
197 // When transactions are nested we only begin/commit/rollback the outermost ones
198 if ($this->_trans_depth > 0)
199 {
200 return TRUE;
201 }
202
203 $this->simple_query('COMMIT TRAN');
204 return TRUE;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Rollback Transaction
211 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400212 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200213 * @return bool
214 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400215 function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +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
228 $this->simple_query('ROLLBACK TRAN');
229 return TRUE;
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
235 * Escape String
236 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400237 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000239 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @return string
241 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400242 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000244 if (is_array($str))
245 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500246 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200247 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000248 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200249 }
250
251 return $str;
252 }
253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500255 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Jonese4ed5832009-02-20 21:44:59 +0000257 // escape LIKE condition wildcards
258 if ($like === TRUE)
259 {
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100260 $str = str_replace(
261 array($this->_like_escape_chr, '%', '_'),
262 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
263 $str
264 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Jonese4ed5832009-02-20 21:44:59 +0000267 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Affected Rows
274 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400275 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @return integer
277 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400278 function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 return @mssql_rows_affected($this->conn_id);
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
284
285 /**
286 * Insert ID
287 *
288 * Returns the last id created in the Identity column.
289 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400290 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @return integer
292 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400293 function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 $ver = self::_parse_major_version($this->version());
296 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
297 $query = $this->query($sql);
298 $row = $query->row();
299 return $row->last_id;
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Parse major version
306 *
Barry Mienydd671972010-10-04 16:33:58 +0200307 * Grabs the major version number from the
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * database server version string passed in.
309 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400310 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 * @param string $version
312 * @return int16 major version number
313 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400314 function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
317 return $ver_info[1]; // return the major version b/c that's all we're interested in.
318 }
319
320 // --------------------------------------------------------------------
321
322 /**
323 * Version number query string
324 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200325 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400327 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200329 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * "Count All" query
336 *
337 * Generates a platform-specific query string that counts all records in
338 * the specified database
339 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400340 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @param string
342 * @return string
343 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400344 function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000347 {
348 return 0;
349 }
350
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200351 $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 +0000352 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000353 {
354 return 0;
355 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
357 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500358 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000359 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * List table query
366 *
367 * Generates a platform-specific query string so that the table names can be fetched
368 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400369 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @param boolean
371 * @return string
372 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400373 function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // for future compatibility
378 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
379 {
Greg Aker0d424892010-01-26 02:14:44 +0000380 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 return FALSE; // not currently supported
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 return $sql;
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * List column query
391 *
392 * Generates a platform-specific query string so that the column names can be fetched
393 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400394 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @param string the table name
396 * @return string
397 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400398 function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Barry Mienydd671972010-10-04 16:33:58 +0200400 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Field data query
407 *
408 * Generates a platform-specific query so that the column data can be retrieved
409 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400410 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param string the table name
412 * @return object
413 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400414 function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Barry Mienydd671972010-10-04 16:33:58 +0200416 return "SELECT TOP 1 * FROM ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200422 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200424 * Returns an array containing code and message of the last
425 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200427 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200429 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200431 $query = $this->query('SELECT @@ERROR AS code');
432 $query = $query->row();
433 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * Escape the SQL Identifiers
440 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400441 * This function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400443 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param string
445 * @return string
446 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400447 function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 if ($this->_escape_char == '')
450 {
451 return $item;
452 }
453
454 foreach ($this->_reserved_identifiers as $id)
455 {
456 if (strpos($item, '.'.$id) !== FALSE)
457 {
Barry Mienydd671972010-10-04 16:33:58 +0200458 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // remove duplicates if the user already included the escape
461 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200462 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464
465 if (strpos($item, '.') !== FALSE)
466 {
Barry Mienydd671972010-10-04 16:33:58 +0200467 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 else
470 {
471 $str = $this->_escape_char.$item.$this->_escape_char;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // remove duplicates if the user already included the escape
475 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * From Tables
482 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400483 * This function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * about operator precedence in harmony with SQL standards
485 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400486 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @param type
488 * @return type
489 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400490 function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 if ( ! is_array($tables))
493 {
494 $tables = array($tables);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 return implode(', ', $tables);
498 }
499
500 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 /**
503 * Insert statement
504 *
505 * Generates a platform-specific insert string from the supplied data
506 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400507 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @param string the table name
509 * @param array the insert keys
510 * @param array the insert values
511 * @return string
512 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400513 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200514 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Update statement
522 *
523 * Generates a platform-specific update string from the supplied data
524 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400525 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * @param string the table name
527 * @param array the update data
528 * @param array the where clause
529 * @param array the orderby clause
530 * @param array the limit clause
531 * @return string
532 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400533 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500535 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
537 $valstr[] = $key." = ".$val;
538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
545
546 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
547
548 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 return $sql;
551 }
552
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
555
556 /**
557 * Truncate statement
558 *
559 * Generates a platform-specific truncate string from the supplied data
560 * If the database does not support the truncate() command
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400561 * This function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400563 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @param string the table name
565 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200566 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400567 function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 return "TRUNCATE ".$table;
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
573
574 /**
575 * Delete statement
576 *
577 * Generates a platform-specific delete string from the supplied data
578 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400579 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param string the table name
581 * @param array the where clause
582 * @param string the limit clause
583 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200584 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400585 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $conditions = '';
588
589 if (count($where) > 0 OR count($like) > 0)
590 {
591 $conditions = "\nWHERE ";
592 $conditions .= implode("\n", $this->ar_where);
593
594 if (count($where) > 0 && count($like) > 0)
595 {
596 $conditions .= " AND ";
597 }
598 $conditions .= implode("\n", $like);
599 }
600
601 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 return "DELETE FROM ".$table.$conditions.$limit;
604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Limit string
610 *
611 * Generates a platform-specific LIMIT clause
612 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400613 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @param string the sql query string
615 * @param integer the number of rows to limit the query to
616 * @param integer the offset value
617 * @return string
618 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400619 function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
621 $i = $limit + $offset;
Barry Mienydd671972010-10-04 16:33:58 +0200622
623 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625
626 // --------------------------------------------------------------------
627
628 /**
629 * Close DB Connection
630 *
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400631 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @param resource
633 * @return void
634 */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400635 function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200638 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000639
640}
641
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400642
643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644/* End of file mssql_driver.php */
Timothy Warren8c1b2dc2012-03-19 19:07:18 -0400645/* Location: ./system/database/drivers/mssql/mssql_driver.php */