Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * MS SQL Database Adapter Class |
| 32 | * |
| 33 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame^] | 34 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * class is being used or not. |
| 36 | * |
| 37 | * @package CodeIgniter |
| 38 | * @subpackage Drivers |
| 39 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
| 43 | class CI_DB_mssql_driver extends CI_DB { |
| 44 | |
| 45 | var $dbdriver = 'mssql'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | // The character used for escaping |
| 48 | var $_escape_char = ''; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 49 | |
| 50 | // clause and character used for LIKE escape sequences |
| 51 | var $_like_escape_str = " ESCAPE '%s' "; |
| 52 | var $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | /** |
| 55 | * The syntax to count rows is slightly different across different |
| 56 | * database engines, so this string appears in each driver and is |
| 57 | * used for the count_all() and count_all_results() functions. |
| 58 | */ |
| 59 | var $_count_string = "SELECT COUNT(*) AS "; |
| 60 | var $_random_keyword = ' ASC'; // not currently supported |
| 61 | |
| 62 | /** |
| 63 | * Non-persistent database connection |
| 64 | * |
| 65 | * @access private called by the base class |
| 66 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | function db_connect() |
| 69 | { |
| 70 | if ($this->port != '') |
| 71 | { |
| 72 | $this->hostname .= ','.$this->port; |
| 73 | } |
| 74 | |
| 75 | return @mssql_connect($this->hostname, $this->username, $this->password); |
| 76 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | // -------------------------------------------------------------------- |
| 79 | |
| 80 | /** |
| 81 | * Persistent database connection |
| 82 | * |
| 83 | * @access private called by the base class |
| 84 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | function db_pconnect() |
| 87 | { |
| 88 | if ($this->port != '') |
| 89 | { |
| 90 | $this->hostname .= ','.$this->port; |
| 91 | } |
| 92 | |
| 93 | return @mssql_pconnect($this->hostname, $this->username, $this->password); |
| 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Reconnect |
| 100 | * |
| 101 | * Keep / reestablish the db connection if no queries have been |
| 102 | * sent for a length of time exceeding the server's idle timeout |
| 103 | * |
| 104 | * @access public |
| 105 | * @return void |
| 106 | */ |
| 107 | function reconnect() |
| 108 | { |
| 109 | // not implemented in MSSQL |
| 110 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 112 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Select the database |
| 116 | * |
| 117 | * @access private called by the base class |
| 118 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | function db_select() |
| 121 | { |
| 122 | // Note: The brackets are required in the event that the DB name |
| 123 | // contains reserved characters |
| 124 | return @mssql_select_db('['.$this->database.']', $this->conn_id); |
| 125 | } |
| 126 | |
| 127 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | /** |
| 130 | * Set client character set |
| 131 | * |
| 132 | * @access public |
| 133 | * @param string |
| 134 | * @param string |
| 135 | * @return resource |
| 136 | */ |
| 137 | function db_set_charset($charset, $collation) |
| 138 | { |
| 139 | // @todo - add support if needed |
| 140 | return TRUE; |
| 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | /** |
| 146 | * Execute the query |
| 147 | * |
| 148 | * @access private called by the base class |
| 149 | * @param string an SQL query |
| 150 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | function _execute($sql) |
| 153 | { |
| 154 | $sql = $this->_prep_query($sql); |
| 155 | return @mssql_query($sql, $this->conn_id); |
| 156 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | // -------------------------------------------------------------------- |
| 159 | |
| 160 | /** |
| 161 | * Prep the query |
| 162 | * |
| 163 | * If needed, each database adapter can prep the query string |
| 164 | * |
| 165 | * @access private called by execute() |
| 166 | * @param string an SQL query |
| 167 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | function _prep_query($sql) |
| 170 | { |
| 171 | return $sql; |
| 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Begin Transaction |
| 178 | * |
| 179 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | * @return bool |
| 181 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | function trans_begin($test_mode = FALSE) |
| 183 | { |
| 184 | if ( ! $this->trans_enabled) |
| 185 | { |
| 186 | return TRUE; |
| 187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 190 | if ($this->_trans_depth > 0) |
| 191 | { |
| 192 | return TRUE; |
| 193 | } |
| 194 | |
| 195 | // Reset the transaction failure flag. |
| 196 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 197 | // even if the queries produce a successful result. |
| 198 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 199 | |
| 200 | $this->simple_query('BEGIN TRAN'); |
| 201 | return TRUE; |
| 202 | } |
| 203 | |
| 204 | // -------------------------------------------------------------------- |
| 205 | |
| 206 | /** |
| 207 | * Commit Transaction |
| 208 | * |
| 209 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | * @return bool |
| 211 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | function trans_commit() |
| 213 | { |
| 214 | if ( ! $this->trans_enabled) |
| 215 | { |
| 216 | return TRUE; |
| 217 | } |
| 218 | |
| 219 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 220 | if ($this->_trans_depth > 0) |
| 221 | { |
| 222 | return TRUE; |
| 223 | } |
| 224 | |
| 225 | $this->simple_query('COMMIT TRAN'); |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
| 229 | // -------------------------------------------------------------------- |
| 230 | |
| 231 | /** |
| 232 | * Rollback Transaction |
| 233 | * |
| 234 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | * @return bool |
| 236 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | function trans_rollback() |
| 238 | { |
| 239 | if ( ! $this->trans_enabled) |
| 240 | { |
| 241 | return TRUE; |
| 242 | } |
| 243 | |
| 244 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 245 | if ($this->_trans_depth > 0) |
| 246 | { |
| 247 | return TRUE; |
| 248 | } |
| 249 | |
| 250 | $this->simple_query('ROLLBACK TRAN'); |
| 251 | return TRUE; |
| 252 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | // -------------------------------------------------------------------- |
| 255 | |
| 256 | /** |
| 257 | * Escape String |
| 258 | * |
| 259 | * @access public |
| 260 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 261 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | * @return string |
| 263 | */ |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 264 | function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 266 | if (is_array($str)) |
| 267 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 268 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 270 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return $str; |
| 274 | } |
| 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | // Escape single quotes |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 277 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 279 | // escape LIKE condition wildcards |
| 280 | if ($like === TRUE) |
| 281 | { |
Phil Sturgeon | 36b0c94 | 2011-04-02 12:16:41 +0100 | [diff] [blame] | 282 | $str = str_replace( |
| 283 | array($this->_like_escape_chr, '%', '_'), |
| 284 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 285 | $str |
| 286 | ); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 287 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 289 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | // -------------------------------------------------------------------- |
| 293 | |
| 294 | /** |
| 295 | * Affected Rows |
| 296 | * |
| 297 | * @access public |
| 298 | * @return integer |
| 299 | */ |
| 300 | function affected_rows() |
| 301 | { |
| 302 | return @mssql_rows_affected($this->conn_id); |
| 303 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * Insert ID |
| 309 | * |
| 310 | * Returns the last id created in the Identity column. |
| 311 | * |
| 312 | * @access public |
| 313 | * @return integer |
| 314 | */ |
| 315 | function insert_id() |
| 316 | { |
| 317 | $ver = self::_parse_major_version($this->version()); |
| 318 | $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id"); |
| 319 | $query = $this->query($sql); |
| 320 | $row = $query->row(); |
| 321 | return $row->last_id; |
| 322 | } |
| 323 | |
| 324 | // -------------------------------------------------------------------- |
| 325 | |
| 326 | /** |
| 327 | * Parse major version |
| 328 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | * Grabs the major version number from the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | * database server version string passed in. |
| 331 | * |
| 332 | * @access private |
| 333 | * @param string $version |
| 334 | * @return int16 major version number |
| 335 | */ |
| 336 | function _parse_major_version($version) |
| 337 | { |
| 338 | preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info); |
| 339 | return $ver_info[1]; // return the major version b/c that's all we're interested in. |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * Version number query string |
| 346 | * |
| 347 | * @access public |
| 348 | * @return string |
| 349 | */ |
| 350 | function _version() |
| 351 | { |
| 352 | return "SELECT @@VERSION AS ver"; |
| 353 | } |
| 354 | |
| 355 | // -------------------------------------------------------------------- |
| 356 | |
| 357 | /** |
| 358 | * "Count All" query |
| 359 | * |
| 360 | * Generates a platform-specific query string that counts all records in |
| 361 | * the specified database |
| 362 | * |
| 363 | * @access public |
| 364 | * @param string |
| 365 | * @return string |
| 366 | */ |
| 367 | function count_all($table = '') |
| 368 | { |
| 369 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 370 | { |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | |
| 376 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 377 | { |
| 378 | return 0; |
| 379 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | |
| 381 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 382 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 383 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // -------------------------------------------------------------------- |
| 387 | |
| 388 | /** |
| 389 | * List table query |
| 390 | * |
| 391 | * Generates a platform-specific query string so that the table names can be fetched |
| 392 | * |
| 393 | * @access private |
| 394 | * @param boolean |
| 395 | * @return string |
| 396 | */ |
| 397 | function _list_tables($prefix_limit = FALSE) |
| 398 | { |
| 399 | $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | // for future compatibility |
| 402 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 403 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 404 | //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | return FALSE; // not currently supported |
| 406 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | return $sql; |
| 409 | } |
| 410 | |
| 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * List column query |
| 415 | * |
| 416 | * Generates a platform-specific query string so that the column names can be fetched |
| 417 | * |
| 418 | * @access private |
| 419 | * @param string the table name |
| 420 | * @return string |
| 421 | */ |
| 422 | function _list_columns($table = '') |
| 423 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
| 430 | * Field data query |
| 431 | * |
| 432 | * Generates a platform-specific query so that the column data can be retrieved |
| 433 | * |
| 434 | * @access public |
| 435 | * @param string the table name |
| 436 | * @return object |
| 437 | */ |
| 438 | function _field_data($table) |
| 439 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | return "SELECT TOP 1 * FROM ".$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // -------------------------------------------------------------------- |
| 444 | |
| 445 | /** |
| 446 | * The error message string |
| 447 | * |
| 448 | * @access private |
| 449 | * @return string |
| 450 | */ |
| 451 | function _error_message() |
| 452 | { |
Derek Allard | 64c398b | 2009-08-17 15:13:45 +0000 | [diff] [blame] | 453 | return mssql_get_last_message(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 455 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | // -------------------------------------------------------------------- |
| 457 | |
| 458 | /** |
| 459 | * The error message number |
| 460 | * |
| 461 | * @access private |
| 462 | * @return integer |
| 463 | */ |
| 464 | function _error_number() |
| 465 | { |
| 466 | // Are error numbers supported? |
| 467 | return ''; |
| 468 | } |
| 469 | |
| 470 | // -------------------------------------------------------------------- |
| 471 | |
| 472 | /** |
| 473 | * Escape the SQL Identifiers |
| 474 | * |
| 475 | * This function escapes column and table names |
| 476 | * |
| 477 | * @access private |
| 478 | * @param string |
| 479 | * @return string |
| 480 | */ |
| 481 | function _escape_identifiers($item) |
| 482 | { |
| 483 | if ($this->_escape_char == '') |
| 484 | { |
| 485 | return $item; |
| 486 | } |
| 487 | |
| 488 | foreach ($this->_reserved_identifiers as $id) |
| 489 | { |
| 490 | if (strpos($item, '.'.$id) !== FALSE) |
| 491 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 493 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | // remove duplicates if the user already included the escape |
| 495 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 496 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | if (strpos($item, '.') !== FALSE) |
| 500 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 501 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | { |
| 505 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 506 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 507 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | // remove duplicates if the user already included the escape |
| 509 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 510 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | // -------------------------------------------------------------------- |
| 513 | |
| 514 | /** |
| 515 | * From Tables |
| 516 | * |
| 517 | * This function implicitly groups FROM tables so there is no confusion |
| 518 | * about operator precedence in harmony with SQL standards |
| 519 | * |
| 520 | * @access public |
| 521 | * @param type |
| 522 | * @return type |
| 523 | */ |
| 524 | function _from_tables($tables) |
| 525 | { |
| 526 | if ( ! is_array($tables)) |
| 527 | { |
| 528 | $tables = array($tables); |
| 529 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 530 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | return implode(', ', $tables); |
| 532 | } |
| 533 | |
| 534 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | /** |
| 537 | * Insert statement |
| 538 | * |
| 539 | * Generates a platform-specific insert string from the supplied data |
| 540 | * |
| 541 | * @access public |
| 542 | * @param string the table name |
| 543 | * @param array the insert keys |
| 544 | * @param array the insert values |
| 545 | * @return string |
| 546 | */ |
| 547 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 548 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 550 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 551 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | // -------------------------------------------------------------------- |
| 553 | |
| 554 | /** |
| 555 | * Update statement |
| 556 | * |
| 557 | * Generates a platform-specific update string from the supplied data |
| 558 | * |
| 559 | * @access public |
| 560 | * @param string the table name |
| 561 | * @param array the update data |
| 562 | * @param array the where clause |
| 563 | * @param array the orderby clause |
| 564 | * @param array the limit clause |
| 565 | * @return string |
| 566 | */ |
| 567 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 568 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 569 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | { |
| 571 | $valstr[] = $key." = ".$val; |
| 572 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 573 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 575 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 577 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 579 | |
| 580 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 581 | |
| 582 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 583 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | return $sql; |
| 585 | } |
| 586 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 587 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | // -------------------------------------------------------------------- |
| 589 | |
| 590 | /** |
| 591 | * Truncate statement |
| 592 | * |
| 593 | * Generates a platform-specific truncate string from the supplied data |
| 594 | * If the database does not support the truncate() command |
| 595 | * This function maps to "DELETE FROM table" |
| 596 | * |
| 597 | * @access public |
| 598 | * @param string the table name |
| 599 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 600 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | function _truncate($table) |
| 602 | { |
| 603 | return "TRUNCATE ".$table; |
| 604 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 605 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | // -------------------------------------------------------------------- |
| 607 | |
| 608 | /** |
| 609 | * Delete statement |
| 610 | * |
| 611 | * Generates a platform-specific delete string from the supplied data |
| 612 | * |
| 613 | * @access public |
| 614 | * @param string the table name |
| 615 | * @param array the where clause |
| 616 | * @param string the limit clause |
| 617 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 618 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 619 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 620 | { |
| 621 | $conditions = ''; |
| 622 | |
| 623 | if (count($where) > 0 OR count($like) > 0) |
| 624 | { |
| 625 | $conditions = "\nWHERE "; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame^] | 626 | $conditions .= implode("\n", $this->qb_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | |
| 628 | if (count($where) > 0 && count($like) > 0) |
| 629 | { |
| 630 | $conditions .= " AND "; |
| 631 | } |
| 632 | $conditions .= implode("\n", $like); |
| 633 | } |
| 634 | |
| 635 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 636 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | return "DELETE FROM ".$table.$conditions.$limit; |
| 638 | } |
| 639 | |
| 640 | // -------------------------------------------------------------------- |
| 641 | |
| 642 | /** |
| 643 | * Limit string |
| 644 | * |
| 645 | * Generates a platform-specific LIMIT clause |
| 646 | * |
| 647 | * @access public |
| 648 | * @param string the sql query string |
| 649 | * @param integer the number of rows to limit the query to |
| 650 | * @param integer the offset value |
| 651 | * @return string |
| 652 | */ |
| 653 | function _limit($sql, $limit, $offset) |
| 654 | { |
| 655 | $i = $limit + $offset; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 656 | |
| 657 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // -------------------------------------------------------------------- |
| 661 | |
| 662 | /** |
| 663 | * Close DB Connection |
| 664 | * |
| 665 | * @access public |
| 666 | * @param resource |
| 667 | * @return void |
| 668 | */ |
| 669 | function _close($conn_id) |
| 670 | { |
| 671 | @mssql_close($conn_id); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 672 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 673 | |
| 674 | } |
| 675 | |
| 676 | |
| 677 | |
| 678 | /* End of file mssql_driver.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 679 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |