blob: 4d29920d644d7a52bc2bd33c1a542f3f2e8db510 [file] [log] [blame]
Andrey Andreev4da24f82012-01-25 21:54:23 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4da24f82012-01-25 21:54:23 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4da24f82012-01-25 21:54:23 +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 Andreev4da24f82012-01-25 21:54:23 +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 Andreevdd7242b2012-01-26 00:43:38 +020046 protected $_escape_char = '';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences
Andrey Andreevdd7242b2012-01-26 00:43:38 +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
Andrey Andreev4da24f82012-01-25 21:54:23 +020055 * used for the count_all() and count_all_results() methods.
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
Andrey Andreevdd7242b2012-01-26 00:43:38 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' NEWID()';
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 Andreev4da24f82012-01-25 21:54:23 +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 Andreev4da24f82012-01-25 21:54:23 +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 Andreev4da24f82012-01-25 21:54:23 +0200102 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000103 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200104 // Not supported in MSSQL
Derek Jones87cbafc2009-02-27 16:29:59 +0000105 }
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 Andreev4da24f82012-01-25 21:54:23 +0200112 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200113 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200114 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 // Note: The brackets are required in the event that the DB name
117 // contains reserved characters
118 return @mssql_select_db('['.$this->database.']', $this->conn_id);
119 }
120
121 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 /**
124 * Set client character set
125 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * @param string
127 * @param string
Andrey Andreev4da24f82012-01-25 21:54:23 +0200128 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200130 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200132 // Not supported in MSSQL
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 return TRUE;
134 }
135
136 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 /**
139 * Execute the query
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200142 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200143 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200144 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200146 return @mssql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // --------------------------------------------------------------------
150
151 /**
152 * Prep the query
153 *
154 * If needed, each database adapter can prep the query string
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string an SQL query
157 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200158 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200159 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 return $sql;
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Begin Transaction
168 *
Barry Mienydd671972010-10-04 16:33:58 +0200169 * @return bool
170 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200171 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200174 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 return TRUE;
177 }
178
179 // Reset the transaction failure flag.
180 // If the $test_mode flag is set to TRUE transactions will be rolled back
181 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200182 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000183
Andrey Andreev4da24f82012-01-25 21:54:23 +0200184 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Commit Transaction
191 *
Barry Mienydd671972010-10-04 16:33:58 +0200192 * @return bool
193 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200194 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200197 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 return TRUE;
200 }
201
Andrey Andreev4da24f82012-01-25 21:54:23 +0200202 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Rollback Transaction
209 *
Barry Mienydd671972010-10-04 16:33:58 +0200210 * @return bool
211 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200212 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200215 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 return TRUE;
218 }
219
Andrey Andreev4da24f82012-01-25 21:54:23 +0200220 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
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 Andreev4da24f82012-01-25 21:54:23 +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 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200250 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100251 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 Andreev4da24f82012-01-25 21:54:23 +0200265 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 */
Andrey Andreev4da24f82012-01-25 21:54:23 +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 /**
275 * Insert ID
276 *
277 * Returns the last id created in the Identity column.
278 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200279 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200281 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200283 $query = (self::_parse_major_version($this->version()) > 7)
Andrey Andreev4da24f82012-01-25 21:54:23 +0200284 ? 'SELECT SCOPE_IDENTITY() AS last_id'
285 : 'SELECT @@IDENTITY AS last_id';
286
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200287 $query = $this->query($query);
288 $query = $query->row();
289 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
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 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200300 * @param string $version
301 * @return int major version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200303 private 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 Andreev4da24f82012-01-25 21:54:23 +0200314 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200316 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Andrey Andreev4da24f82012-01-25 21:54:23 +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 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200332 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 Andreev4da24f82012-01-25 21:54:23 +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 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200357 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * @return string
359 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200360 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 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200384 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
Andrey Andreev4da24f82012-01-25 21:54:23 +0200397 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200399 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200401 return 'SELECT TOP 1 * FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * The error message string
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @return string
410 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200411 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Derek Allard64c398b2009-08-17 15:13:45 +0000413 return mssql_get_last_message();
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // --------------------------------------------------------------------
417
418 /**
419 * The error message number
420 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200421 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200423 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200425 // Not supported in MSSQL
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 return '';
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Escape the SQL Identifiers
433 *
434 * This function escapes column and table names
435 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @param string
437 * @return string
438 */
Andrey Andreev3318ab82012-01-26 01:59:08 +0200439 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 if ($this->_escape_char == '')
442 {
443 return $item;
444 }
445
446 foreach ($this->_reserved_identifiers as $id)
447 {
448 if (strpos($item, '.'.$id) !== FALSE)
449 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200450 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // remove duplicates if the user already included the escape
Andrey Andreev4da24f82012-01-25 21:54:23 +0200453 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200454 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 if (strpos($item, '.') !== FALSE)
458 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200459 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // remove duplicates if the user already included the escape
Andrey Andreev4da24f82012-01-25 21:54:23 +0200463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
467
468 /**
469 * From Tables
470 *
471 * This function implicitly groups FROM tables so there is no confusion
472 * about operator precedence in harmony with SQL standards
473 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200474 * @param array
475 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200477 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 if ( ! is_array($tables))
480 {
481 $tables = array($tables);
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 return implode(', ', $tables);
485 }
486
487 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 /**
490 * Insert statement
491 *
492 * Generates a platform-specific insert string from the supplied data
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string the table name
495 * @param array the insert keys
496 * @param array the insert values
497 * @return string
498 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200499 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200500 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200501 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
505
506 /**
507 * Update statement
508 *
509 * Generates a platform-specific update string from the supplied data
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param string the table name
512 * @param array the update data
513 * @param array the where clause
514 * @param array the orderby clause
515 * @param array the limit clause
516 * @return string
517 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200518 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500520 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200522 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Andrey Andreev4da24f82012-01-25 21:54:23 +0200525 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
526 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
527 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
528 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 // --------------------------------------------------------------------
533
534 /**
535 * Truncate statement
536 *
537 * Generates a platform-specific truncate string from the supplied data
538 * If the database does not support the truncate() command
539 * This function maps to "DELETE FROM table"
540 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @param string the table name
542 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200543 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200544 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200546 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 // --------------------------------------------------------------------
550
551 /**
552 * Delete statement
553 *
554 * Generates a platform-specific delete string from the supplied data
555 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * @param string the table name
557 * @param array the where clause
558 * @param string the limit clause
559 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200560 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200561 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 if (count($where) > 0 OR count($like) > 0)
565 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200566 $conditions .= "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000567
568 if (count($where) > 0 && count($like) > 0)
569 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200570 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572 $conditions .= implode("\n", $like);
573 }
574
Andrey Andreev4da24f82012-01-25 21:54:23 +0200575 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Limit string
582 *
583 * Generates a platform-specific LIMIT clause
584 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @param string the sql query string
586 * @param integer the number of rows to limit the query to
587 * @param integer the offset value
588 * @return string
589 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200590 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200592 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * Close DB Connection
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param resource
601 * @return void
602 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200603 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200606 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000607
608}
609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610/* End of file mssql_driver.php */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200611/* Location: ./system/database/drivers/mssql/mssql_driver.php */