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 | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 45 | var $dbdriver = 'sqlsrv'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 46 | |
| 47 | // The character used for escaping |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 48 | var $_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 | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 51 | var $_like_escape_str = " ESCAPE '%s' "; |
| 52 | var $_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 | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 57 | * used for the count_all() and count_all_results() functions. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 58 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 59 | var $_count_string = "SELECT COUNT(*) AS "; |
| 60 | var $_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 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 65 | * @access private called by the base class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 66 | * @return resource |
| 67 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [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 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 96 | * @access private called by the base class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 97 | * @return resource |
| 98 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 99 | function db_pconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 112 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 113 | * @return void |
| 114 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 115 | function reconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 149 | * @access private called by the base class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 150 | * @param string an SQL query |
| 151 | * @return resource |
| 152 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 153 | function _execute($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 154 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 155 | return sqlsrv_query($this->conn_id, $sql, null, array( |
| 156 | 'Scrollable' => SQLSRV_CURSOR_STATIC, |
| 157 | 'SendStreamParamsAtExec' => true |
| 158 | )); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // -------------------------------------------------------------------- |
| 162 | |
| 163 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 164 | * Begin Transaction |
| 165 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 166 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 167 | * @return bool |
| 168 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 169 | function trans_begin($test_mode = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 170 | { |
| 171 | if ( ! $this->trans_enabled) |
| 172 | { |
| 173 | return TRUE; |
| 174 | } |
| 175 | |
| 176 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 177 | if ($this->_trans_depth > 0) |
| 178 | { |
| 179 | return TRUE; |
| 180 | } |
| 181 | |
| 182 | // Reset the transaction failure flag. |
| 183 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 184 | // even if the queries produce a successful result. |
| 185 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 186 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 187 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Commit Transaction |
| 194 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 195 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 196 | * @return bool |
| 197 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 198 | function trans_commit() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 199 | { |
| 200 | if ( ! $this->trans_enabled) |
| 201 | { |
| 202 | return TRUE; |
| 203 | } |
| 204 | |
| 205 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 206 | if ($this->_trans_depth > 0) |
| 207 | { |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 211 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // -------------------------------------------------------------------- |
| 215 | |
| 216 | /** |
| 217 | * Rollback Transaction |
| 218 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 219 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 220 | * @return bool |
| 221 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 222 | function trans_rollback() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 223 | { |
| 224 | if ( ! $this->trans_enabled) |
| 225 | { |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
| 229 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 230 | if ($this->_trans_depth > 0) |
| 231 | { |
| 232 | return TRUE; |
| 233 | } |
| 234 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 235 | return sqlsrv_rollback($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
| 239 | |
| 240 | /** |
| 241 | * Escape String |
| 242 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 243 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 244 | * @param string |
| 245 | * @param bool whether or not the string will be used in a LIKE condition |
| 246 | * @return string |
| 247 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 248 | function escape_str($str, $like = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 249 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 250 | // Escape single quotes |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 251 | return str_replace("'", "''", $str); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // -------------------------------------------------------------------- |
| 255 | |
| 256 | /** |
| 257 | * Affected Rows |
| 258 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 259 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 260 | * @return integer |
| 261 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 262 | function affected_rows() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 263 | { |
Alex Bilbie | 56e2040 | 2011-03-12 23:43:54 +0000 | [diff] [blame] | 264 | return @sqlrv_rows_affected($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
| 270 | * Insert ID |
| 271 | * |
| 272 | * Returns the last id created in the Identity column. |
| 273 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 274 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 275 | * @return integer |
| 276 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 277 | function insert_id() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 278 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 279 | return $this->query('select @@IDENTITY as insert_id')->row('insert_id'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // -------------------------------------------------------------------- |
| 283 | |
| 284 | /** |
| 285 | * Parse major version |
| 286 | * |
| 287 | * Grabs the major version number from the |
| 288 | * database server version string passed in. |
| 289 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 290 | * @access private |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 291 | * @param string $version |
| 292 | * @return int16 major version number |
| 293 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 294 | function _parse_major_version($version) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 295 | { |
| 296 | preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info); |
| 297 | return $ver_info[1]; // return the major version b/c that's all we're interested in. |
| 298 | } |
| 299 | |
| 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 303 | * Database version number |
| 304 | * |
| 305 | * @return string |
| 306 | */ |
| 307 | public function version() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 308 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 309 | if (isset($this->data_cache['version'])) |
| 310 | { |
| 311 | return $this->data_cache['version']; |
| 312 | } |
| 313 | |
| 314 | if (($info = sqlsrv_server_info($this->conn_id)) === FALSE) |
| 315 | { |
| 316 | return FALSE; |
| 317 | } |
| 318 | |
| 319 | return $this->data_cache['version'] = $info['SQLServerVersion']; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
| 325 | * "Count All" query |
| 326 | * |
| 327 | * Generates a platform-specific query string that counts all records in |
| 328 | * the specified database |
| 329 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 330 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 331 | * @param string |
| 332 | * @return string |
| 333 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 334 | function count_all($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 335 | { |
| 336 | if ($table == '') |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 337 | return '0'; |
| 338 | |
| 339 | $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table); |
| 340 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 341 | if ($query->num_rows() == 0) |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 342 | return '0'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 343 | |
| 344 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 345 | $this->_reset_select(); |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 346 | return $row->numrows; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // -------------------------------------------------------------------- |
| 350 | |
| 351 | /** |
| 352 | * List table query |
| 353 | * |
| 354 | * Generates a platform-specific query string so that the table names can be fetched |
| 355 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 356 | * @access private |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 357 | * @param boolean |
| 358 | * @return string |
| 359 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 360 | function _list_tables($prefix_limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 361 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 362 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // -------------------------------------------------------------------- |
| 366 | |
| 367 | /** |
| 368 | * List column query |
| 369 | * |
| 370 | * Generates a platform-specific query string so that the column names can be fetched |
| 371 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 372 | * @access private |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 373 | * @param string the table name |
| 374 | * @return string |
| 375 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 376 | function _list_columns($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 377 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 378 | 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] | 379 | } |
| 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
| 384 | * Field data query |
| 385 | * |
| 386 | * Generates a platform-specific query so that the column data can be retrieved |
| 387 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 388 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 389 | * @param string the table name |
| 390 | * @return object |
| 391 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 392 | function _field_data($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 393 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 394 | return "SELECT TOP 1 * FROM " . $this->_escape_table($table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 400 | * Error |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 401 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 402 | * Returns an array containing code and message of the last |
| 403 | * database error that has occured. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 404 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 405 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 406 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 407 | public function error() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 408 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 409 | $error = array('code' => '00000', 'message' => ''); |
| 410 | $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
| 411 | |
| 412 | if ( ! is_array($sqlsrv_errors)) |
Andrey Andreev | 850f601 | 2012-03-01 15:58:25 +0200 | [diff] [blame] | 413 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 414 | return $error; |
Andrey Andreev | 850f601 | 2012-03-01 15:58:25 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 417 | $sqlsrv_error = array_shift($sqlsrv_errors); |
| 418 | if (isset($sqlsrv_error['SQLSTATE'])) |
| 419 | { |
| 420 | $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE']; |
| 421 | } |
| 422 | elseif (isset($sqlsrv_error['code'])) |
| 423 | { |
| 424 | $error['code'] = $sqlsrv_error['code']; |
| 425 | } |
| 426 | |
| 427 | if (isset($sqlsrv_error['message'])) |
| 428 | { |
| 429 | $error['message'] = $sqlsrv_error['message']; |
| 430 | } |
| 431 | |
| 432 | return $error; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // -------------------------------------------------------------------- |
| 436 | |
| 437 | /** |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 438 | * Escape Table Name |
| 439 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 440 | * This function adds backticks if the table name has a period |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 441 | * in it. Some DBs will get cranky unless periods are escaped |
| 442 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 443 | * @access private |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 444 | * @param string the table name |
| 445 | * @return string |
| 446 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 447 | function _escape_table($table) |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 448 | { |
| 449 | return $table; |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 452 | |
| 453 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 454 | * Escape the SQL Identifiers |
| 455 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 456 | * This function escapes column and table names |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 457 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 458 | * @access private |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 459 | * @param string |
| 460 | * @return string |
| 461 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 462 | function _escape_identifiers($item) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 463 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 464 | return $item; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 467 | // -------------------------------------------------------------------- |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 468 | |
| 469 | /** |
| 470 | * From Tables |
| 471 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 472 | * This function implicitly groups FROM tables so there is no confusion |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 473 | * about operator precedence in harmony with SQL standards |
| 474 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 475 | * @access public |
| 476 | * @param type |
| 477 | * @return type |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 478 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 479 | function _from_tables($tables) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 480 | { |
| 481 | if ( ! is_array($tables)) |
| 482 | { |
| 483 | $tables = array($tables); |
| 484 | } |
| 485 | |
| 486 | return implode(', ', $tables); |
| 487 | } |
| 488 | |
| 489 | // -------------------------------------------------------------------- |
| 490 | |
| 491 | /** |
| 492 | * Insert statement |
| 493 | * |
| 494 | * Generates a platform-specific insert string from the supplied data |
| 495 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 496 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 497 | * @param string the table name |
| 498 | * @param array the insert keys |
| 499 | * @param array the insert values |
| 500 | * @return string |
| 501 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 502 | function _insert($table, $keys, $values) |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 503 | { |
| 504 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // -------------------------------------------------------------------- |
| 508 | |
| 509 | /** |
| 510 | * Update statement |
| 511 | * |
| 512 | * Generates a platform-specific update string from the supplied data |
| 513 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 514 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 515 | * @param string the table name |
| 516 | * @param array the update data |
| 517 | * @param array the where clause |
| 518 | * @param array the orderby clause |
| 519 | * @param array the limit clause |
| 520 | * @return string |
| 521 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 522 | function _update($table, $values, $where) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 523 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 524 | foreach($values as $key => $val) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 525 | { |
| 526 | $valstr[] = $key." = ".$val; |
| 527 | } |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 528 | |
| 529 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 530 | } |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 531 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 532 | // -------------------------------------------------------------------- |
| 533 | |
| 534 | /** |
| 535 | * Truncate statement |
| 536 | * |
| 537 | * Generates a platform-specific truncate string from the supplied data |
| 538 | * If the database does not support the truncate() command |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 539 | * This function maps to "DELETE FROM table" |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 540 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 541 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 542 | * @param string the table name |
| 543 | * @return string |
| 544 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 545 | function _truncate($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 546 | { |
| 547 | return "TRUNCATE ".$table; |
| 548 | } |
| 549 | |
| 550 | // -------------------------------------------------------------------- |
| 551 | |
| 552 | /** |
| 553 | * Delete statement |
| 554 | * |
| 555 | * Generates a platform-specific delete string from the supplied data |
| 556 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 557 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 558 | * @param string the table name |
| 559 | * @param array the where clause |
| 560 | * @param string the limit clause |
| 561 | * @return string |
| 562 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 563 | function _delete($table, $where) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 564 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 565 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // -------------------------------------------------------------------- |
| 569 | |
| 570 | /** |
| 571 | * Limit string |
| 572 | * |
| 573 | * Generates a platform-specific LIMIT clause |
| 574 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 575 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 576 | * @param string the sql query string |
| 577 | * @param integer the number of rows to limit the query to |
| 578 | * @param integer the offset value |
| 579 | * @return string |
| 580 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 581 | function _limit($sql, $limit, $offset) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 582 | { |
| 583 | $i = $limit + $offset; |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 584 | |
| 585 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // -------------------------------------------------------------------- |
| 589 | |
| 590 | /** |
| 591 | * Close DB Connection |
| 592 | * |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 593 | * @access public |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 594 | * @param resource |
| 595 | * @return void |
| 596 | */ |
Timothy Warren | 0e20da1 | 2012-03-19 19:05:46 -0400 | [diff] [blame] | 597 | function _close($conn_id) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 598 | { |
| 599 | @sqlsrv_close($conn_id); |
| 600 | } |
| 601 | |
| 602 | } |
| 603 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 604 | /* End of file mssql_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame^] | 605 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |