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 |
| 34 | * creates dynamically based on whether the active record |
| 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 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 117 | * @param string database name |
| 118 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 120 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame^] | 122 | if ($database === '') |
| 123 | { |
| 124 | $database = $this->database; |
| 125 | } |
| 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | // Note: The brackets are required in the event that the DB name |
| 128 | // contains reserved characters |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame^] | 129 | if (@mssql_select_db('['.$database.']', $this->conn_id)) |
| 130 | { |
| 131 | $this->database = $database; |
| 132 | return TRUE; |
| 133 | } |
| 134 | |
| 135 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | /** |
| 141 | * Set client character set |
| 142 | * |
| 143 | * @access public |
| 144 | * @param string |
| 145 | * @param string |
| 146 | * @return resource |
| 147 | */ |
| 148 | function db_set_charset($charset, $collation) |
| 149 | { |
| 150 | // @todo - add support if needed |
| 151 | return TRUE; |
| 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | /** |
| 157 | * Execute the query |
| 158 | * |
| 159 | * @access private called by the base class |
| 160 | * @param string an SQL query |
| 161 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | function _execute($sql) |
| 164 | { |
| 165 | $sql = $this->_prep_query($sql); |
| 166 | return @mssql_query($sql, $this->conn_id); |
| 167 | } |
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 | // -------------------------------------------------------------------- |
| 170 | |
| 171 | /** |
| 172 | * Prep the query |
| 173 | * |
| 174 | * If needed, each database adapter can prep the query string |
| 175 | * |
| 176 | * @access private called by execute() |
| 177 | * @param string an SQL query |
| 178 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | function _prep_query($sql) |
| 181 | { |
| 182 | return $sql; |
| 183 | } |
| 184 | |
| 185 | // -------------------------------------------------------------------- |
| 186 | |
| 187 | /** |
| 188 | * Begin Transaction |
| 189 | * |
| 190 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | * @return bool |
| 192 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | function trans_begin($test_mode = FALSE) |
| 194 | { |
| 195 | if ( ! $this->trans_enabled) |
| 196 | { |
| 197 | return TRUE; |
| 198 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 201 | if ($this->_trans_depth > 0) |
| 202 | { |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | // Reset the transaction failure flag. |
| 207 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 208 | // even if the queries produce a successful result. |
| 209 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 210 | |
| 211 | $this->simple_query('BEGIN TRAN'); |
| 212 | return TRUE; |
| 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Commit Transaction |
| 219 | * |
| 220 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | * @return bool |
| 222 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | function trans_commit() |
| 224 | { |
| 225 | if ( ! $this->trans_enabled) |
| 226 | { |
| 227 | return TRUE; |
| 228 | } |
| 229 | |
| 230 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 231 | if ($this->_trans_depth > 0) |
| 232 | { |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | $this->simple_query('COMMIT TRAN'); |
| 237 | return TRUE; |
| 238 | } |
| 239 | |
| 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
| 243 | * Rollback Transaction |
| 244 | * |
| 245 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 246 | * @return bool |
| 247 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | function trans_rollback() |
| 249 | { |
| 250 | if ( ! $this->trans_enabled) |
| 251 | { |
| 252 | return TRUE; |
| 253 | } |
| 254 | |
| 255 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 256 | if ($this->_trans_depth > 0) |
| 257 | { |
| 258 | return TRUE; |
| 259 | } |
| 260 | |
| 261 | $this->simple_query('ROLLBACK TRAN'); |
| 262 | return TRUE; |
| 263 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | // -------------------------------------------------------------------- |
| 266 | |
| 267 | /** |
| 268 | * Escape String |
| 269 | * |
| 270 | * @access public |
| 271 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 272 | * @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] | 273 | * @return string |
| 274 | */ |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 275 | function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 277 | if (is_array($str)) |
| 278 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 279 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 281 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return $str; |
| 285 | } |
| 286 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | // Escape single quotes |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 288 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 290 | // escape LIKE condition wildcards |
| 291 | if ($like === TRUE) |
| 292 | { |
Phil Sturgeon | 36b0c94 | 2011-04-02 12:16:41 +0100 | [diff] [blame] | 293 | $str = str_replace( |
| 294 | array($this->_like_escape_chr, '%', '_'), |
| 295 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 296 | $str |
| 297 | ); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 298 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 300 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Affected Rows |
| 307 | * |
| 308 | * @access public |
| 309 | * @return integer |
| 310 | */ |
| 311 | function affected_rows() |
| 312 | { |
| 313 | return @mssql_rows_affected($this->conn_id); |
| 314 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | // -------------------------------------------------------------------- |
| 317 | |
| 318 | /** |
| 319 | * Insert ID |
| 320 | * |
| 321 | * Returns the last id created in the Identity column. |
| 322 | * |
| 323 | * @access public |
| 324 | * @return integer |
| 325 | */ |
| 326 | function insert_id() |
| 327 | { |
| 328 | $ver = self::_parse_major_version($this->version()); |
| 329 | $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id"); |
| 330 | $query = $this->query($sql); |
| 331 | $row = $query->row(); |
| 332 | return $row->last_id; |
| 333 | } |
| 334 | |
| 335 | // -------------------------------------------------------------------- |
| 336 | |
| 337 | /** |
| 338 | * Parse major version |
| 339 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | * Grabs the major version number from the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | * database server version string passed in. |
| 342 | * |
| 343 | * @access private |
| 344 | * @param string $version |
| 345 | * @return int16 major version number |
| 346 | */ |
| 347 | function _parse_major_version($version) |
| 348 | { |
| 349 | preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info); |
| 350 | return $ver_info[1]; // return the major version b/c that's all we're interested in. |
| 351 | } |
| 352 | |
| 353 | // -------------------------------------------------------------------- |
| 354 | |
| 355 | /** |
| 356 | * Version number query string |
| 357 | * |
| 358 | * @access public |
| 359 | * @return string |
| 360 | */ |
| 361 | function _version() |
| 362 | { |
| 363 | return "SELECT @@VERSION AS ver"; |
| 364 | } |
| 365 | |
| 366 | // -------------------------------------------------------------------- |
| 367 | |
| 368 | /** |
| 369 | * "Count All" query |
| 370 | * |
| 371 | * Generates a platform-specific query string that counts all records in |
| 372 | * the specified database |
| 373 | * |
| 374 | * @access public |
| 375 | * @param string |
| 376 | * @return string |
| 377 | */ |
| 378 | function count_all($table = '') |
| 379 | { |
| 380 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 381 | { |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | $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] | 386 | |
| 387 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 388 | { |
| 389 | return 0; |
| 390 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | |
| 392 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 393 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 394 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
| 400 | * List table query |
| 401 | * |
| 402 | * Generates a platform-specific query string so that the table names can be fetched |
| 403 | * |
| 404 | * @access private |
| 405 | * @param boolean |
| 406 | * @return string |
| 407 | */ |
| 408 | function _list_tables($prefix_limit = FALSE) |
| 409 | { |
| 410 | $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | // for future compatibility |
| 413 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 414 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 415 | //$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] | 416 | return FALSE; // not currently supported |
| 417 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | return $sql; |
| 420 | } |
| 421 | |
| 422 | // -------------------------------------------------------------------- |
| 423 | |
| 424 | /** |
| 425 | * List column query |
| 426 | * |
| 427 | * Generates a platform-specific query string so that the column names can be fetched |
| 428 | * |
| 429 | * @access private |
| 430 | * @param string the table name |
| 431 | * @return string |
| 432 | */ |
| 433 | function _list_columns($table = '') |
| 434 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // -------------------------------------------------------------------- |
| 439 | |
| 440 | /** |
| 441 | * Field data query |
| 442 | * |
| 443 | * Generates a platform-specific query so that the column data can be retrieved |
| 444 | * |
| 445 | * @access public |
| 446 | * @param string the table name |
| 447 | * @return object |
| 448 | */ |
| 449 | function _field_data($table) |
| 450 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | return "SELECT TOP 1 * FROM ".$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // -------------------------------------------------------------------- |
| 455 | |
| 456 | /** |
| 457 | * The error message string |
| 458 | * |
| 459 | * @access private |
| 460 | * @return string |
| 461 | */ |
| 462 | function _error_message() |
| 463 | { |
Derek Allard | 64c398b | 2009-08-17 15:13:45 +0000 | [diff] [blame] | 464 | return mssql_get_last_message(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | // -------------------------------------------------------------------- |
| 468 | |
| 469 | /** |
| 470 | * The error message number |
| 471 | * |
| 472 | * @access private |
| 473 | * @return integer |
| 474 | */ |
| 475 | function _error_number() |
| 476 | { |
| 477 | // Are error numbers supported? |
| 478 | return ''; |
| 479 | } |
| 480 | |
| 481 | // -------------------------------------------------------------------- |
| 482 | |
| 483 | /** |
| 484 | * Escape the SQL Identifiers |
| 485 | * |
| 486 | * This function escapes column and table names |
| 487 | * |
| 488 | * @access private |
| 489 | * @param string |
| 490 | * @return string |
| 491 | */ |
| 492 | function _escape_identifiers($item) |
| 493 | { |
| 494 | if ($this->_escape_char == '') |
| 495 | { |
| 496 | return $item; |
| 497 | } |
| 498 | |
| 499 | foreach ($this->_reserved_identifiers as $id) |
| 500 | { |
| 501 | if (strpos($item, '.'.$id) !== FALSE) |
| 502 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 504 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | // remove duplicates if the user already included the escape |
| 506 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
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 | } |
| 509 | |
| 510 | if (strpos($item, '.') !== FALSE) |
| 511 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | $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] | 513 | } |
| 514 | else |
| 515 | { |
| 516 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 517 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 518 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | // remove duplicates if the user already included the escape |
| 520 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 521 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 522 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | // -------------------------------------------------------------------- |
| 524 | |
| 525 | /** |
| 526 | * From Tables |
| 527 | * |
| 528 | * This function implicitly groups FROM tables so there is no confusion |
| 529 | * about operator precedence in harmony with SQL standards |
| 530 | * |
| 531 | * @access public |
| 532 | * @param type |
| 533 | * @return type |
| 534 | */ |
| 535 | function _from_tables($tables) |
| 536 | { |
| 537 | if ( ! is_array($tables)) |
| 538 | { |
| 539 | $tables = array($tables); |
| 540 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | return implode(', ', $tables); |
| 543 | } |
| 544 | |
| 545 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 546 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | /** |
| 548 | * Insert statement |
| 549 | * |
| 550 | * Generates a platform-specific insert string from the supplied data |
| 551 | * |
| 552 | * @access public |
| 553 | * @param string the table name |
| 554 | * @param array the insert keys |
| 555 | * @param array the insert values |
| 556 | * @return string |
| 557 | */ |
| 558 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 559 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 561 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 562 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | // -------------------------------------------------------------------- |
| 564 | |
| 565 | /** |
| 566 | * Update statement |
| 567 | * |
| 568 | * Generates a platform-specific update string from the supplied data |
| 569 | * |
| 570 | * @access public |
| 571 | * @param string the table name |
| 572 | * @param array the update data |
| 573 | * @param array the where clause |
| 574 | * @param array the orderby clause |
| 575 | * @param array the limit clause |
| 576 | * @return string |
| 577 | */ |
| 578 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 579 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 580 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | { |
| 582 | $valstr[] = $key." = ".$val; |
| 583 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 584 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 585 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 586 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 588 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 590 | |
| 591 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 592 | |
| 593 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 594 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | return $sql; |
| 596 | } |
| 597 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 598 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | // -------------------------------------------------------------------- |
| 600 | |
| 601 | /** |
| 602 | * Truncate statement |
| 603 | * |
| 604 | * Generates a platform-specific truncate string from the supplied data |
| 605 | * If the database does not support the truncate() command |
| 606 | * This function maps to "DELETE FROM table" |
| 607 | * |
| 608 | * @access public |
| 609 | * @param string the table name |
| 610 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 611 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | function _truncate($table) |
| 613 | { |
| 614 | return "TRUNCATE ".$table; |
| 615 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | // -------------------------------------------------------------------- |
| 618 | |
| 619 | /** |
| 620 | * Delete statement |
| 621 | * |
| 622 | * Generates a platform-specific delete string from the supplied data |
| 623 | * |
| 624 | * @access public |
| 625 | * @param string the table name |
| 626 | * @param array the where clause |
| 627 | * @param string the limit clause |
| 628 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 629 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 631 | { |
| 632 | $conditions = ''; |
| 633 | |
| 634 | if (count($where) > 0 OR count($like) > 0) |
| 635 | { |
| 636 | $conditions = "\nWHERE "; |
| 637 | $conditions .= implode("\n", $this->ar_where); |
| 638 | |
| 639 | if (count($where) > 0 && count($like) > 0) |
| 640 | { |
| 641 | $conditions .= " AND "; |
| 642 | } |
| 643 | $conditions .= implode("\n", $like); |
| 644 | } |
| 645 | |
| 646 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 647 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | return "DELETE FROM ".$table.$conditions.$limit; |
| 649 | } |
| 650 | |
| 651 | // -------------------------------------------------------------------- |
| 652 | |
| 653 | /** |
| 654 | * Limit string |
| 655 | * |
| 656 | * Generates a platform-specific LIMIT clause |
| 657 | * |
| 658 | * @access public |
| 659 | * @param string the sql query string |
| 660 | * @param integer the number of rows to limit the query to |
| 661 | * @param integer the offset value |
| 662 | * @return string |
| 663 | */ |
| 664 | function _limit($sql, $limit, $offset) |
| 665 | { |
| 666 | $i = $limit + $offset; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 667 | |
| 668 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | // -------------------------------------------------------------------- |
| 672 | |
| 673 | /** |
| 674 | * Close DB Connection |
| 675 | * |
| 676 | * @access public |
| 677 | * @param resource |
| 678 | * @return void |
| 679 | */ |
| 680 | function _close($conn_id) |
| 681 | { |
| 682 | @mssql_close($conn_id); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 683 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 684 | |
| 685 | } |
| 686 | |
| 687 | |
| 688 | |
| 689 | /* End of file mssql_driver.php */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 690 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |