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