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