blob: 914de499fb24d3b52d46fbd994a1d53cfc276d30 [file] [log] [blame]
Andrey Andreev1f619a82012-03-20 16:03:04 +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 Andreev1f619a82012-03-20 16:03:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1f619a82012-03-20 16:03:04 +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
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * 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 Andreev1f619a82012-03-20 16:03:04 +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 Andreev1f619a82012-03-20 16:03:04 +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' ";
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 Andreev3b2587e2012-03-28 13:39:34 +030055 * used for the count_all() and count_all_results() methods.
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
Andrey Andreev1f619a82012-03-20 16:03:04 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
Andrey Andreev3b2587e2012-03-28 13:39:34 +030058 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 Andreev1f619a82012-03-20 16:03:04 +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 Andreev1f619a82012-03-20 16:03:04 +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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000095 * Select the database
96 *
Andrey Andreev11454e02012-02-22 16:05:47 +020097 * @param string database name
98 * @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
124 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Andrey Andreev1f619a82012-03-20 16:03:04 +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);
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // --------------------------------------------------------------------
132
133 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * Begin Transaction
135 *
Barry Mienydd671972010-10-04 16:33:58 +0200136 * @return bool
137 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200138 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 if ( ! $this->trans_enabled)
141 {
142 return TRUE;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // When transactions are nested we only begin/commit/rollback the outermost ones
146 if ($this->_trans_depth > 0)
147 {
148 return TRUE;
149 }
150
151 // Reset the transaction failure flag.
152 // If the $test_mode flag is set to TRUE transactions will be rolled back
153 // even if the queries produce a successful result.
154 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
155
156 $this->simple_query('BEGIN TRAN');
157 return TRUE;
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * Commit Transaction
164 *
Barry Mienydd671972010-10-04 16:33:58 +0200165 * @return bool
166 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200167 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 if ( ! $this->trans_enabled)
170 {
171 return TRUE;
172 }
173
174 // When transactions are nested we only begin/commit/rollback the outermost ones
175 if ($this->_trans_depth > 0)
176 {
177 return TRUE;
178 }
179
180 $this->simple_query('COMMIT TRAN');
181 return TRUE;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Rollback Transaction
188 *
Barry Mienydd671972010-10-04 16:33:58 +0200189 * @return bool
190 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200191 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 if ( ! $this->trans_enabled)
194 {
195 return TRUE;
196 }
197
198 // When transactions are nested we only begin/commit/rollback the outermost ones
199 if ($this->_trans_depth > 0)
200 {
201 return TRUE;
202 }
203
204 $this->simple_query('ROLLBACK TRAN');
205 return TRUE;
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 // --------------------------------------------------------------------
209
210 /**
211 * Escape String
212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000214 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @return string
216 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200217 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000219 if (is_array($str))
220 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500221 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200222 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000223 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200224 }
225
226 return $str;
227 }
228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500230 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Jonese4ed5832009-02-20 21:44:59 +0000232 // escape LIKE condition wildcards
233 if ($like === TRUE)
234 {
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100235 $str = str_replace(
236 array($this->_like_escape_chr, '%', '_'),
237 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
238 $str
239 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Jonese4ed5832009-02-20 21:44:59 +0000242 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // --------------------------------------------------------------------
246
247 /**
248 * Affected Rows
249 *
Andrey Andreev1f619a82012-03-20 16:03:04 +0200250 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200252 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 return @mssql_rows_affected($this->conn_id);
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // --------------------------------------------------------------------
258
259 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200260 * Insert ID
261 *
262 * Returns the last id created in the Identity column.
263 *
264 * @return string
265 */
266 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 $ver = self::_parse_major_version($this->version());
269 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
270 $query = $this->query($sql);
271 $row = $query->row();
272 return $row->last_id;
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200278 * Parse major version
279 *
280 * Grabs the major version number from the
281 * database server version string passed in.
282 *
283 * @param string $version
284 * @return int major version number
285 */
286 protected function _parse_major_version($version)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
288 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
289 return $ver_info[1]; // return the major version b/c that's all we're interested in.
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
Andrey Andreev1f619a82012-03-20 16:03:04 +0200295 * Version number query string
296 *
297 * @return string
298 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200299 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200301 return 'SELECT @@VERSION AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * "Count All" query
308 *
309 * Generates a platform-specific query string that counts all records in
310 * the specified database
311 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @param string
313 * @return string
314 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200315 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000318 {
319 return 0;
320 }
321
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200322 $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 +0000323 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000324 {
325 return 0;
326 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
328 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500329 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000330 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * List table query
337 *
338 * Generates a platform-specific query string so that the table names can be fetched
339 *
Andrey Andreev1f619a82012-03-20 16:03:04 +0200340 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return string
342 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200343 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // for future compatibility
348 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
349 {
Greg Aker0d424892010-01-26 02:14:44 +0000350 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 return FALSE; // not currently supported
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 return $sql;
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * List column query
361 *
362 * Generates a platform-specific query string so that the column names can be fetched
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param string the table name
365 * @return string
366 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200367 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Barry Mienydd671972010-10-04 16:33:58 +0200369 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Field data query
376 *
377 * Generates a platform-specific query so that the column data can be retrieved
378 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * @param string the table name
Andrey Andreev1f619a82012-03-20 16:03:04 +0200380 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200382 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Barry Mienydd671972010-10-04 16:33:58 +0200384 return "SELECT TOP 1 * FROM ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387 // --------------------------------------------------------------------
388
389 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 * Returns an array containing code and message of the last
393 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200395 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200397 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200399 $query = $this->query('SELECT @@ERROR AS code');
400 $query = $query->row();
401 return array('code' => $query->code, 'message' => mssql_get_last_message());
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
405
406 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * From Tables
408 *
409 * This function implicitly groups FROM tables so there is no confusion
410 * about operator precedence in harmony with SQL standards
411 *
Andrey Andreev1f619a82012-03-20 16:03:04 +0200412 * @param array
413 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200415 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 if ( ! is_array($tables))
418 {
419 $tables = array($tables);
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 return implode(', ', $tables);
423 }
424
425 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * Update statement
429 *
430 * Generates a platform-specific update string from the supplied data
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string the table name
433 * @param array the update data
434 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300435 * @param array the orderby clause (ignored)
436 * @param array the limit clause (ignored)
437 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @return string
439 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300440 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Andrey Andreev00541ae2012-04-09 11:43:10 +0300442 foreach($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev00541ae2012-04-09 11:43:10 +0300444 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Andrey Andreev00541ae2012-04-09 11:43:10 +0300447 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
Barry Mienydd671972010-10-04 16:33:58 +0200448
Andrey Andreev00541ae2012-04-09 11:43:10 +0300449 if ( ! empty($like))
450 {
451 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Andrey Andreev00541ae2012-04-09 11:43:10 +0300454 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).' WHERE '.$where;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // --------------------------------------------------------------------
458
459 /**
460 * Truncate statement
461 *
462 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 *
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300464 * If the database does not support the truncate() command,
465 * then this method maps to 'DELETE FROM table'
466 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @param string the table name
468 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200469 */
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300470 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreev6d83cde2012-04-05 16:20:50 +0300472 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Delete statement
479 *
480 * Generates a platform-specific delete string from the supplied data
481 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string the table name
483 * @param array the where clause
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300484 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @param string the limit clause
486 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200487 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300488 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300490 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000491
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300492 empty($where) OR $conditions[] = implode(' ', $where);
493 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300495 $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000496
Andrey Andreev5c0e9fe2012-04-09 12:28:11 +0300497 return ($limit)
498 ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete'
499 : 'DELETE FROM '.$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Limit string
506 *
507 * Generates a platform-specific LIMIT clause
508 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * @param string the sql query string
Andrey Andreev1f619a82012-03-20 16:03:04 +0200510 * @param int the number of rows to limit the query to
511 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @return string
513 */
Andrey Andreev1f619a82012-03-20 16:03:04 +0200514 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 $i = $limit + $offset;
Barry Mienydd671972010-10-04 16:33:58 +0200517
518 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Close DB Connection
525 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * @return void
527 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300528 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300530 @mssql_close($this->conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200531 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000532
533}
534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535/* End of file mssql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300536/* Location: ./system/database/drivers/mssql/mssql_driver.php */