blob: 2053ddb60fc5ec2660262439835549cb1e403484 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey 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 Andreev1f619a82012-03-20 16:03:04 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevdd7242b2012-01-26 00:43:38 +020050 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
Andrey Andreev2cb262f2012-03-28 13:45:04 +030064 if ( ! empty($this->port))
Andrey Andreevbd601d32012-02-12 21:16:51 +020065 {
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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000095 * Select the database
96 *
Andrey Andreev11454e02012-02-22 16:05:47 +020097 * @param string database name
Andrey Andreev4da24f82012-01-25 21:54:23 +020098 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200100 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200102 if ($database === '')
103 {
104 $database = $this->database;
105 }
106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // Note: The brackets are required in the event that the DB name
108 // contains reserved characters
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200109 if (@mssql_select_db('['.$database.']', $this->conn_id))
110 {
111 $this->database = $database;
112 return TRUE;
113 }
114
115 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
118 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * Execute the query
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string an SQL query
Andrey Andreev4da24f82012-01-25 21:54:23 +0200124 * @return mixed resource if rows are returned, bool otherwise
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200126 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 return @mssql_query($sql, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Begin Transaction
135 *
Barry Mienydd671972010-10-04 16:33:58 +0200136 * @return bool
137 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200138 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200141 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 return TRUE;
144 }
145
146 // Reset the transaction failure flag.
147 // If the $test_mode flag is set to TRUE transactions will be rolled back
148 // even if the queries produce a successful result.
Andrey Andreev4da24f82012-01-25 21:54:23 +0200149 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150
Andrey Andreev4da24f82012-01-25 21:54:23 +0200151 return $this->simple_query('BEGIN TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Commit Transaction
158 *
Barry Mienydd671972010-10-04 16:33:58 +0200159 * @return bool
160 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200161 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200164 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 return TRUE;
167 }
168
Andrey Andreev4da24f82012-01-25 21:54:23 +0200169 return $this->simple_query('COMMIT TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Rollback Transaction
176 *
Barry Mienydd671972010-10-04 16:33:58 +0200177 * @return bool
178 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200179 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev4da24f82012-01-25 21:54:23 +0200182 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 return TRUE;
185 }
186
Andrey Andreev4da24f82012-01-25 21:54:23 +0200187 return $this->simple_query('ROLLBACK TRAN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // --------------------------------------------------------------------
191
192 /**
193 * Escape String
194 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000196 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @return string
198 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200199 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000201 if (is_array($str))
202 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500203 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000205 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
207
208 return $str;
209 }
210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500212 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jonese4ed5832009-02-20 21:44:59 +0000214 // escape LIKE condition wildcards
215 if ($like === TRUE)
216 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200217 return str_replace(
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100218 array($this->_like_escape_chr, '%', '_'),
219 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
220 $str
221 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Jonese4ed5832009-02-20 21:44:59 +0000224 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // --------------------------------------------------------------------
228
229 /**
230 * Affected Rows
231 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200232 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200234 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 return @mssql_rows_affected($this->conn_id);
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // --------------------------------------------------------------------
240
241 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200242 * Insert ID
243 *
244 * Returns the last id created in the Identity column.
245 *
246 * @return string
247 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200248 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200250 $query = (self::_parse_major_version($this->version()) > 7)
Andrey Andreev4da24f82012-01-25 21:54:23 +0200251 ? 'SELECT SCOPE_IDENTITY() AS last_id'
252 : 'SELECT @@IDENTITY AS last_id';
253
Andrey Andreevb7a47a72012-01-26 02:13:33 +0200254 $query = $this->query($query);
255 $query = $query->row();
256 return $query->last_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200262 * Parse major version
263 *
264 * Grabs the major version number from the
265 * database server version string passed in.
266 *
267 * @param string $version
268 * @return int major version number
269 */
Andrey Andreev7a189e82012-01-27 21:13:52 +0200270 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
273 return $ver_info[1]; // return the major version b/c that's all we're interested in.
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200279 * Version number query string
280 *
281 * @return string
282 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200283 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200285 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * "Count All" query
292 *
293 * Generates a platform-specific query string that counts all records in
294 * the specified database
295 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @param string
297 * @return string
298 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200299 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000302 {
303 return 0;
304 }
305
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200306 $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 +0000307 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000308 {
309 return 0;
310 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
312 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500313 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000314 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * List table query
321 *
322 * Generates a platform-specific query string so that the table names can be fetched
323 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200324 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @return string
326 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200327 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // for future compatibility
332 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
333 {
Greg Aker0d424892010-01-26 02:14:44 +0000334 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 return FALSE; // not currently supported
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 return $sql;
339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * List column query
345 *
346 * Generates a platform-specific query string so that the column names can be fetched
347 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 * @param string the table name
349 * @return string
350 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200351 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Barry Mienydd671972010-10-04 16:33:58 +0200353 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Field data query
360 *
361 * Generates a platform-specific query so that the column data can be retrieved
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param string the table name
Andrey Andreev4da24f82012-01-25 21:54:23 +0200364 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200366 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200368 return 'SELECT TOP 1 * FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370
371 // --------------------------------------------------------------------
372
373 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200374 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200376 * Returns an array containing code and message of the last
377 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200379 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200381 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 $query = $this->query('SELECT @@ERROR AS code');
384 $query = $query->row();
385 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Escape the SQL Identifiers
392 *
393 * This function escapes column and table names
394 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @param string
396 * @return string
397 */
Andrey Andreev3318ab82012-01-26 01:59:08 +0200398 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 if ($this->_escape_char == '')
401 {
402 return $item;
403 }
404
405 foreach ($this->_reserved_identifiers as $id)
406 {
407 if (strpos($item, '.'.$id) !== FALSE)
408 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200409 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // remove duplicates if the user already included the escape
Andrey Andreev4da24f82012-01-25 21:54:23 +0200412 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200413 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 if (strpos($item, '.') !== FALSE)
417 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200418 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // remove duplicates if the user already included the escape
Andrey Andreev4da24f82012-01-25 21:54:23 +0200422 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // --------------------------------------------------------------------
426
427 /**
428 * From Tables
429 *
430 * This function implicitly groups FROM tables so there is no confusion
431 * about operator precedence in harmony with SQL standards
432 *
Andrey Andreev4da24f82012-01-25 21:54:23 +0200433 * @param array
434 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200436 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 if ( ! is_array($tables))
439 {
440 $tables = array($tables);
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 return implode(', ', $tables);
444 }
445
446 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 /**
449 * Insert statement
450 *
451 * Generates a platform-specific insert string from the supplied data
452 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @param string the table name
454 * @param array the insert keys
455 * @param array the insert values
456 * @return string
457 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200458 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200459 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200460 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // --------------------------------------------------------------------
464
465 /**
466 * Update statement
467 *
468 * Generates a platform-specific update string from the supplied data
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string the table name
471 * @param array the update data
472 * @param array the where clause
473 * @param array the orderby clause
474 * @param array the limit clause
475 * @return string
476 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200477 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500479 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200481 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreev4da24f82012-01-25 21:54:23 +0200484 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
485 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
486 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
487 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Truncate statement
495 *
496 * Generates a platform-specific truncate string from the supplied data
497 * If the database does not support the truncate() command
498 * This function maps to "DELETE FROM table"
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param string the table name
501 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200502 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200503 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200505 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 // --------------------------------------------------------------------
509
510 /**
511 * Delete statement
512 *
513 * Generates a platform-specific delete string from the supplied data
514 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @param string the table name
516 * @param array the where clause
517 * @param string the limit clause
518 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200519 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200520 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
522 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 if (count($where) > 0 OR count($like) > 0)
524 {
Andrey Andreev2cb262f2012-03-28 13:45:04 +0300525 $conditions .= "\nWHERE ".implode("\n", $this->ar_where)
526 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
527 .implode("\n", $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
Andrey Andreev4da24f82012-01-25 21:54:23 +0200530 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Limit string
537 *
538 * Generates a platform-specific LIMIT clause
539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200541 * @param int the number of rows to limit the query to
542 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @return string
544 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200545 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Andrey Andreev4da24f82012-01-25 21:54:23 +0200547 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
549
550 // --------------------------------------------------------------------
551
552 /**
553 * Close DB Connection
554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @param resource
556 * @return void
557 */
Andrey Andreev4da24f82012-01-25 21:54:23 +0200558 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
560 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200561 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000562
563}
564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565/* End of file mssql_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400566/* Location: ./system/database/drivers/mssql/mssql_driver.php */