blob: 4c26dc465c36540c37a4fc35e2ffe75d2fd2da4f [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
Andrey Andreevbd601d32012-02-12 21:16:51 +020060 public function __construct($params)
61 {
62 parent::__construct($params);
63
64 if ( ! empty($this->port) && ctype_digit($this->port))
65 {
66 $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
67 }
68 }
69
Derek Allard2067d1a2008-11-13 22:59:24 +000070 /**
71 * Non-persistent database connection
72 *
Derek Allard2067d1a2008-11-13 22:59:24 +000073 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020074 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020075 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
Derek Allard2067d1a2008-11-13 22:59:24 +000077 return @mssql_connect($this->hostname, $this->username, $this->password);
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 // --------------------------------------------------------------------
81
82 /**
83 * Persistent database connection
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Andrey Andreev4da24f82012-01-25 21:54:23 +020087 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Derek Allard2067d1a2008-11-13 22:59:24 +000089 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 Andreev11454e02012-02-22 16:05:47 +0200112 * @param string database name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200113 * @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 /**
136 * Set client character set
137 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @param string
139 * @param string
Andrey Andreev4da24f82012-01-25 21:54:23 +0200140 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200142 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200144 // Not supported in MSSQL
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 return TRUE;
146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 /**
151 * Execute the query
152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200154 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200156 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200158 return @mssql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // --------------------------------------------------------------------
162
163 /**
164 * Prep the query
165 *
166 * If needed, each database adapter can prep the query string
167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @param string an SQL query
169 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200170 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200171 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
173 return $sql;
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Begin Transaction
180 *
Barry Mienydd671972010-10-04 16:33:58 +0200181 * @return bool
182 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200183 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200186 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
188 return TRUE;
189 }
190
191 // Reset the transaction failure flag.
192 // If the $test_mode flag is set to TRUE transactions will be rolled back
193 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200194 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
Andrey Andreev4da24f82012-01-25 21:54:23 +0200196 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Commit Transaction
203 *
Barry Mienydd671972010-10-04 16:33:58 +0200204 * @return bool
205 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200206 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200209 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
211 return TRUE;
212 }
213
Andrey Andreev4da24f82012-01-25 21:54:23 +0200214 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Rollback Transaction
221 *
Barry Mienydd671972010-10-04 16:33:58 +0200222 * @return bool
223 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200224 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200227 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return TRUE;
230 }
231
Andrey Andreev4da24f82012-01-25 21:54:23 +0200232 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
236
237 /**
238 * Escape String
239 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000241 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @return string
243 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200244 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000246 if (is_array($str))
247 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500248 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200249 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000250 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200251 }
252
253 return $str;
254 }
255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500257 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Jonese4ed5832009-02-20 21:44:59 +0000259 // escape LIKE condition wildcards
260 if ($like === TRUE)
261 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200262 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100263 array($this->_like_escape_chr, '%', '_'),
264 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
265 $str
266 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Affected Rows
276 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200277 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200279 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 return @mssql_rows_affected($this->conn_id);
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // --------------------------------------------------------------------
285
286 /**
287 * Insert ID
288 *
289 * Returns the last id created in the Identity column.
290 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200291 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200293 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200295 $query = (self::_parse_major_version($this->version()) > 7)
Andrey Andreev4da24f82012-01-25 21:54:23 +0200296 ? 'SELECT SCOPE_IDENTITY() AS last_id'
297 : 'SELECT @@IDENTITY AS last_id';
298
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200299 $query = $this->query($query);
300 $query = $query->row();
301 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Parse major version
308 *
Barry Mienydd671972010-10-04 16:33:58 +0200309 * Grabs the major version number from the
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * database server version string passed in.
311 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200312 * @param string $version
313 * @return int major version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 */
Andrey Andreev7a189e82012-01-27 21:13:52 +0200315 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
318 return $ver_info[1]; // return the major version b/c that's all we're interested in.
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Version number query string
325 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200326 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200328 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200330 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * "Count All" query
337 *
338 * Generates a platform-specific query string that counts all records in
339 * the specified database
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @param string
342 * @return string
343 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200344 public 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 Andreev4da24f82012-01-25 21:54:23 +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 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200369 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @return string
371 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200372 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // for future compatibility
377 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
378 {
Greg Aker0d424892010-01-26 02:14:44 +0000379 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 return FALSE; // not currently supported
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 return $sql;
384 }
385
386 // --------------------------------------------------------------------
387
388 /**
389 * List column query
390 *
391 * Generates a platform-specific query string so that the column names can be fetched
392 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * @param string the table name
394 * @return string
395 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200396 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Barry Mienydd671972010-10-04 16:33:58 +0200398 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Field data query
405 *
406 * Generates a platform-specific query so that the column data can be retrieved
407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200409 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200411 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200413 return 'SELECT TOP 1 * FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * The error message string
420 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @return string
422 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200423 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Derek Allard64c398b2009-08-17 15:13:45 +0000425 return mssql_get_last_message();
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // --------------------------------------------------------------------
429
430 /**
431 * The error message number
432 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200433 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200435 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200437 // Not supported in MSSQL
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return '';
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Escape the SQL Identifiers
445 *
446 * This function escapes column and table names
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string
449 * @return string
450 */
Andrey Andreev3318ab82012-01-26 01:59:08 +0200451 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 if ($this->_escape_char == '')
454 {
455 return $item;
456 }
457
458 foreach ($this->_reserved_identifiers as $id)
459 {
460 if (strpos($item, '.'.$id) !== FALSE)
461 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200462 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // remove duplicates if the user already included the escape
Andrey Andreev4da24f82012-01-25 21:54:23 +0200465 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200466 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
469 if (strpos($item, '.') !== FALSE)
470 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200471 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
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
Andrey Andreev4da24f82012-01-25 21:54:23 +0200475 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * From Tables
482 *
483 * This function implicitly groups FROM tables so there is no confusion
484 * about operator precedence in harmony with SQL standards
485 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200486 * @param array
487 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200489 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 if ( ! is_array($tables))
492 {
493 $tables = array($tables);
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 return implode(', ', $tables);
497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 /**
502 * Insert statement
503 *
504 * Generates a platform-specific insert string from the supplied data
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string the table name
507 * @param array the insert keys
508 * @param array the insert values
509 * @return string
510 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200511 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200512 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200513 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
519 * Update statement
520 *
521 * Generates a platform-specific update string from the supplied data
522 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 * @param string the table name
524 * @param array the update data
525 * @param array the where clause
526 * @param array the orderby clause
527 * @param array the limit clause
528 * @return string
529 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200530 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500532 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200534 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Andrey Andreev4da24f82012-01-25 21:54:23 +0200537 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
538 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
539 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
540 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // --------------------------------------------------------------------
545
546 /**
547 * Truncate statement
548 *
549 * Generates a platform-specific truncate string from the supplied data
550 * If the database does not support the truncate() command
551 * This function maps to "DELETE FROM table"
552 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * @param string the table name
554 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200555 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200556 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200558 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // --------------------------------------------------------------------
562
563 /**
564 * Delete statement
565 *
566 * Generates a platform-specific delete string from the supplied data
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @param string the table name
569 * @param array the where clause
570 * @param string the limit clause
571 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200572 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200573 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 if (count($where) > 0 OR count($like) > 0)
577 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200578 $conditions .= "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000579
580 if (count($where) > 0 && count($like) > 0)
581 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200582 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584 $conditions .= implode("\n", $like);
585 }
586
Andrey Andreev4da24f82012-01-25 21:54:23 +0200587 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Limit string
594 *
595 * Generates a platform-specific LIMIT clause
596 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 * @param string the sql query string
598 * @param integer the number of rows to limit the query to
599 * @param integer the offset value
600 * @return string
601 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200602 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200604 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Close DB Connection
611 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 * @param resource
613 * @return void
614 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200615 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200618 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000619
620}
621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622/* End of file mssql_driver.php */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200623/* Location: ./system/database/drivers/mssql/mssql_driver.php */