Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 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 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +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) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 31 | * SQLSRV Database Adapter Class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 43 | class CI_DB_sqlsrv_driver extends CI_DB { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 44 | |
| 45 | var $dbdriver = 'sqlsrv'; |
| 46 | |
| 47 | // The character used for escaping |
| 48 | var $_escape_char = ''; |
| 49 | |
| 50 | // clause and character used for LIKE escape sequences |
| 51 | var $_like_escape_str = " ESCAPE '%s' "; |
| 52 | var $_like_escape_chr = '!'; |
| 53 | |
| 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 |
| 67 | */ |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 68 | function db_connect($pooling = false) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 69 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 70 | // Check for a UTF-8 charset being passed as CI's default 'utf8'. |
| 71 | $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set; |
| 72 | |
| 73 | $connection = array( |
| 74 | 'UID' => empty($this->username) ? '' : $this->username, |
| 75 | 'PWD' => empty($this->password) ? '' : $this->password, |
| 76 | 'Database' => $this->database, |
| 77 | 'ConnectionPooling' => $pooling ? 1 : 0, |
| 78 | 'CharacterSet' => $character_set, |
| 79 | 'ReturnDatesAsStrings' => 1 |
| 80 | ); |
| 81 | |
| 82 | // If the username and password are both empty, assume this is a |
| 83 | // 'Windows Authentication Mode' connection. |
| 84 | if(empty($connection['UID']) && empty($connection['PWD'])) { |
| 85 | unset($connection['UID'], $connection['PWD']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 88 | return sqlsrv_connect($this->hostname, $connection); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // -------------------------------------------------------------------- |
| 92 | |
| 93 | /** |
| 94 | * Persistent database connection |
| 95 | * |
| 96 | * @access private called by the base class |
| 97 | * @return resource |
| 98 | */ |
| 99 | function db_pconnect() |
| 100 | { |
Kyle Farris | 37e351f | 2011-09-07 11:14:46 -0300 | [diff] [blame] | 101 | return $this->db_connect(TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // -------------------------------------------------------------------- |
| 105 | |
| 106 | /** |
| 107 | * Reconnect |
| 108 | * |
| 109 | * Keep / reestablish the db connection if no queries have been |
| 110 | * sent for a length of time exceeding the server's idle timeout |
| 111 | * |
| 112 | * @access public |
| 113 | * @return void |
| 114 | */ |
| 115 | function reconnect() |
| 116 | { |
| 117 | // not implemented in MSSQL |
| 118 | } |
| 119 | |
| 120 | // -------------------------------------------------------------------- |
| 121 | |
| 122 | /** |
| 123 | * Select the database |
| 124 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 125 | * @param string database name |
| 126 | * @return bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 127 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 128 | public function db_select($database = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 129 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 130 | if ($database === '') |
| 131 | { |
| 132 | $database = $this->database; |
| 133 | } |
| 134 | |
| 135 | if ($this->_execute('USE '.$database)) |
| 136 | { |
| 137 | $this->database = $database; |
| 138 | return TRUE; |
| 139 | } |
| 140 | |
| 141 | return FALSE; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
| 145 | |
| 146 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 147 | * Execute the query |
| 148 | * |
| 149 | * @access private called by the base class |
| 150 | * @param string an SQL query |
| 151 | * @return resource |
| 152 | */ |
| 153 | function _execute($sql) |
| 154 | { |
| 155 | $sql = $this->_prep_query($sql); |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 156 | return sqlsrv_query($this->conn_id, $sql, null, array( |
| 157 | 'Scrollable' => SQLSRV_CURSOR_STATIC, |
| 158 | 'SendStreamParamsAtExec' => true |
| 159 | )); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Prep the query |
| 166 | * |
| 167 | * If needed, each database adapter can prep the query string |
| 168 | * |
| 169 | * @access private called by execute() |
| 170 | * @param string an SQL query |
| 171 | * @return string |
| 172 | */ |
| 173 | function _prep_query($sql) |
| 174 | { |
| 175 | return $sql; |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Begin Transaction |
| 182 | * |
| 183 | * @access public |
| 184 | * @return bool |
| 185 | */ |
| 186 | function trans_begin($test_mode = FALSE) |
| 187 | { |
| 188 | if ( ! $this->trans_enabled) |
| 189 | { |
| 190 | return TRUE; |
| 191 | } |
| 192 | |
| 193 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 194 | if ($this->_trans_depth > 0) |
| 195 | { |
| 196 | return TRUE; |
| 197 | } |
| 198 | |
| 199 | // Reset the transaction failure flag. |
| 200 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 201 | // even if the queries produce a successful result. |
| 202 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 203 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 204 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | /** |
| 210 | * Commit Transaction |
| 211 | * |
| 212 | * @access public |
| 213 | * @return bool |
| 214 | */ |
| 215 | function trans_commit() |
| 216 | { |
| 217 | if ( ! $this->trans_enabled) |
| 218 | { |
| 219 | return TRUE; |
| 220 | } |
| 221 | |
| 222 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 223 | if ($this->_trans_depth > 0) |
| 224 | { |
| 225 | return TRUE; |
| 226 | } |
| 227 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 228 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // -------------------------------------------------------------------- |
| 232 | |
| 233 | /** |
| 234 | * Rollback Transaction |
| 235 | * |
| 236 | * @access public |
| 237 | * @return bool |
| 238 | */ |
| 239 | function trans_rollback() |
| 240 | { |
| 241 | if ( ! $this->trans_enabled) |
| 242 | { |
| 243 | return TRUE; |
| 244 | } |
| 245 | |
| 246 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 247 | if ($this->_trans_depth > 0) |
| 248 | { |
| 249 | return TRUE; |
| 250 | } |
| 251 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 252 | return sqlsrv_rollback($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // -------------------------------------------------------------------- |
| 256 | |
| 257 | /** |
| 258 | * Escape String |
| 259 | * |
| 260 | * @access public |
| 261 | * @param string |
| 262 | * @param bool whether or not the string will be used in a LIKE condition |
| 263 | * @return string |
| 264 | */ |
| 265 | function escape_str($str, $like = FALSE) |
| 266 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 267 | // Escape single quotes |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 268 | return str_replace("'", "''", $str); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // -------------------------------------------------------------------- |
| 272 | |
| 273 | /** |
| 274 | * Affected Rows |
| 275 | * |
| 276 | * @access public |
| 277 | * @return integer |
| 278 | */ |
| 279 | function affected_rows() |
| 280 | { |
Alex Bilbie | 56e2040 | 2011-03-12 23:43:54 +0000 | [diff] [blame] | 281 | return @sqlrv_rows_affected($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // -------------------------------------------------------------------- |
| 285 | |
| 286 | /** |
| 287 | * Insert ID |
| 288 | * |
| 289 | * Returns the last id created in the Identity column. |
| 290 | * |
| 291 | * @access public |
| 292 | * @return integer |
| 293 | */ |
| 294 | function insert_id() |
| 295 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 296 | return $this->query('select @@IDENTITY as insert_id')->row('insert_id'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // -------------------------------------------------------------------- |
| 300 | |
| 301 | /** |
| 302 | * Parse major version |
| 303 | * |
| 304 | * Grabs the major version number from the |
| 305 | * database server version string passed in. |
| 306 | * |
| 307 | * @access private |
| 308 | * @param string $version |
| 309 | * @return int16 major version number |
| 310 | */ |
| 311 | function _parse_major_version($version) |
| 312 | { |
| 313 | preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info); |
| 314 | return $ver_info[1]; // return the major version b/c that's all we're interested in. |
| 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * Version number query string |
| 321 | * |
| 322 | * @access public |
| 323 | * @return string |
| 324 | */ |
| 325 | function _version() |
| 326 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 327 | $info = sqlsrv_server_info($this->conn_id); |
| 328 | return sprintf("select '%s' as ver", $info['SQLServerVersion']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * "Count All" query |
| 335 | * |
| 336 | * Generates a platform-specific query string that counts all records in |
| 337 | * the specified database |
| 338 | * |
| 339 | * @access public |
| 340 | * @param string |
| 341 | * @return string |
| 342 | */ |
| 343 | function count_all($table = '') |
| 344 | { |
| 345 | if ($table == '') |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 346 | return '0'; |
| 347 | |
| 348 | $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table); |
| 349 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 350 | if ($query->num_rows() == 0) |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 351 | return '0'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 352 | |
| 353 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 354 | $this->_reset_select(); |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 355 | return $row->numrows; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * List table query |
| 362 | * |
| 363 | * Generates a platform-specific query string so that the table names can be fetched |
| 364 | * |
| 365 | * @access private |
| 366 | * @param boolean |
| 367 | * @return string |
| 368 | */ |
| 369 | function _list_tables($prefix_limit = FALSE) |
| 370 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 371 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // -------------------------------------------------------------------- |
| 375 | |
| 376 | /** |
| 377 | * List column query |
| 378 | * |
| 379 | * Generates a platform-specific query string so that the column names can be fetched |
| 380 | * |
| 381 | * @access private |
| 382 | * @param string the table name |
| 383 | * @return string |
| 384 | */ |
| 385 | function _list_columns($table = '') |
| 386 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 387 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 393 | * Field data query |
| 394 | * |
| 395 | * Generates a platform-specific query so that the column data can be retrieved |
| 396 | * |
| 397 | * @access public |
| 398 | * @param string the table name |
| 399 | * @return object |
| 400 | */ |
| 401 | function _field_data($table) |
| 402 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 403 | return "SELECT TOP 1 * FROM " . $this->_escape_table($table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // -------------------------------------------------------------------- |
| 407 | |
| 408 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 409 | * Error |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 410 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 411 | * Returns an array containing code and message of the last |
| 412 | * database error that has occured. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 413 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 414 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 415 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 416 | public function error() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 417 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 418 | $error = array('code' => '00000', 'message' => ''); |
| 419 | $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
| 420 | |
| 421 | if ( ! is_array($sqlsrv_errors)) |
Andrey Andreev | 850f601 | 2012-03-01 15:58:25 +0200 | [diff] [blame] | 422 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 423 | return $error; |
Andrey Andreev | 850f601 | 2012-03-01 15:58:25 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 426 | $sqlsrv_error = array_shift($sqlsrv_errors); |
| 427 | if (isset($sqlsrv_error['SQLSTATE'])) |
| 428 | { |
| 429 | $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE']; |
| 430 | } |
| 431 | elseif (isset($sqlsrv_error['code'])) |
| 432 | { |
| 433 | $error['code'] = $sqlsrv_error['code']; |
| 434 | } |
| 435 | |
| 436 | if (isset($sqlsrv_error['message'])) |
| 437 | { |
| 438 | $error['message'] = $sqlsrv_error['message']; |
| 439 | } |
| 440 | |
| 441 | return $error; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // -------------------------------------------------------------------- |
| 445 | |
| 446 | /** |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 447 | * Escape Table Name |
| 448 | * |
| 449 | * This function adds backticks if the table name has a period |
| 450 | * in it. Some DBs will get cranky unless periods are escaped |
| 451 | * |
| 452 | * @access private |
| 453 | * @param string the table name |
| 454 | * @return string |
| 455 | */ |
| 456 | function _escape_table($table) |
| 457 | { |
| 458 | return $table; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 463 | * Escape the SQL Identifiers |
| 464 | * |
| 465 | * This function escapes column and table names |
| 466 | * |
| 467 | * @access private |
| 468 | * @param string |
| 469 | * @return string |
| 470 | */ |
| 471 | function _escape_identifiers($item) |
| 472 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 473 | return $item; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // -------------------------------------------------------------------- |
| 477 | |
| 478 | /** |
| 479 | * From Tables |
| 480 | * |
| 481 | * This function implicitly groups FROM tables so there is no confusion |
| 482 | * about operator precedence in harmony with SQL standards |
| 483 | * |
| 484 | * @access public |
| 485 | * @param type |
| 486 | * @return type |
| 487 | */ |
| 488 | function _from_tables($tables) |
| 489 | { |
| 490 | if ( ! is_array($tables)) |
| 491 | { |
| 492 | $tables = array($tables); |
| 493 | } |
| 494 | |
| 495 | return implode(', ', $tables); |
| 496 | } |
| 497 | |
| 498 | // -------------------------------------------------------------------- |
| 499 | |
| 500 | /** |
| 501 | * Insert statement |
| 502 | * |
| 503 | * Generates a platform-specific insert string from the supplied data |
| 504 | * |
| 505 | * @access public |
| 506 | * @param string the table name |
| 507 | * @param array the insert keys |
| 508 | * @param array the insert values |
| 509 | * @return string |
| 510 | */ |
| 511 | function _insert($table, $keys, $values) |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 512 | { |
| 513 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // -------------------------------------------------------------------- |
| 517 | |
| 518 | /** |
| 519 | * Update statement |
| 520 | * |
| 521 | * Generates a platform-specific update string from the supplied data |
| 522 | * |
| 523 | * @access public |
| 524 | * @param string the table name |
| 525 | * @param array the update data |
| 526 | * @param array the where clause |
| 527 | * @param array the orderby clause |
| 528 | * @param array the limit clause |
| 529 | * @return string |
| 530 | */ |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 531 | function _update($table, $values, $where) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 532 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 533 | foreach($values as $key => $val) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 534 | { |
| 535 | $valstr[] = $key." = ".$val; |
| 536 | } |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 537 | |
| 538 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 539 | } |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 540 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 541 | // -------------------------------------------------------------------- |
| 542 | |
| 543 | /** |
| 544 | * Truncate statement |
| 545 | * |
| 546 | * Generates a platform-specific truncate string from the supplied data |
| 547 | * If the database does not support the truncate() command |
| 548 | * This function maps to "DELETE FROM table" |
| 549 | * |
| 550 | * @access public |
| 551 | * @param string the table name |
| 552 | * @return string |
| 553 | */ |
| 554 | function _truncate($table) |
| 555 | { |
| 556 | return "TRUNCATE ".$table; |
| 557 | } |
| 558 | |
| 559 | // -------------------------------------------------------------------- |
| 560 | |
| 561 | /** |
| 562 | * Delete statement |
| 563 | * |
| 564 | * Generates a platform-specific delete string from the supplied data |
| 565 | * |
| 566 | * @access public |
| 567 | * @param string the table name |
| 568 | * @param array the where clause |
| 569 | * @param string the limit clause |
| 570 | * @return string |
| 571 | */ |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 572 | function _delete($table, $where) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 573 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 574 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // -------------------------------------------------------------------- |
| 578 | |
| 579 | /** |
| 580 | * Limit string |
| 581 | * |
| 582 | * Generates a platform-specific LIMIT clause |
| 583 | * |
| 584 | * @access public |
| 585 | * @param string the sql query string |
| 586 | * @param integer the number of rows to limit the query to |
| 587 | * @param integer the offset value |
| 588 | * @return string |
| 589 | */ |
| 590 | function _limit($sql, $limit, $offset) |
| 591 | { |
| 592 | $i = $limit + $offset; |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 593 | |
| 594 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // -------------------------------------------------------------------- |
| 598 | |
| 599 | /** |
| 600 | * Close DB Connection |
| 601 | * |
| 602 | * @access public |
| 603 | * @param resource |
| 604 | * @return void |
| 605 | */ |
| 606 | function _close($conn_id) |
| 607 | { |
| 608 | @sqlsrv_close($conn_id); |
| 609 | } |
| 610 | |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | /* End of file mssql_driver.php */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 616 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |