Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 28 | /** |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 29 | * SQLSRV Database Adapter Class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
| 32 | * creates dynamically based on whether the active record |
| 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 41 | class CI_DB_sqlsrv_driver extends CI_DB { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 42 | |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 43 | public $dbdriver = 'sqlsrv'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 44 | |
| 45 | // The character used for escaping |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 46 | protected $_escape_char = ''; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 47 | |
| 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 49 | protected $_like_escape_str = ' ESCAPE \'%s\' '; |
| 50 | protected $_like_escape_chr = '!'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
| 55 | * used for the count_all() and count_all_results() functions. |
| 56 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword = ' NEWID()'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Non-persistent database connection |
| 62 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 63 | * @return resource |
| 64 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 65 | public function db_connect($pooling = TRUE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 66 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 67 | // Check for a UTF-8 charset being passed as CI's default 'utf8'. |
| 68 | $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set; |
| 69 | |
| 70 | $connection = array( |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 71 | 'UID' => empty($this->username) ? '' : $this->username, |
| 72 | 'PWD' => empty($this->password) ? '' : $this->password, |
| 73 | 'Database' => $this->database, |
| 74 | 'ConnectionPooling' => $pooling ? 1 : 0, |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 75 | 'CharacterSet' => $character_set, |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 76 | 'ReturnDatesAsStrings' => 1 |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 77 | ); |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 78 | |
| 79 | // If the username and password are both empty, assume this is a |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 80 | // 'Windows Authentication Mode' connection. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 81 | if (empty($connection['UID']) && empty($connection['PWD'])) |
| 82 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 83 | unset($connection['UID'], $connection['PWD']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 86 | return sqlsrv_connect($this->hostname, $connection); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | /** |
| 92 | * Persistent database connection |
| 93 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 94 | * @return resource |
| 95 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 96 | public function db_pconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 97 | { |
Kyle Farris | 37e351f | 2011-09-07 11:14:46 -0300 | [diff] [blame] | 98 | return $this->db_connect(TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // -------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
| 104 | * Reconnect |
| 105 | * |
| 106 | * Keep / reestablish the db connection if no queries have been |
| 107 | * sent for a length of time exceeding the server's idle timeout |
| 108 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 109 | * @return void |
| 110 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 111 | public function reconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 112 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 113 | // Not supported in MSSQL |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * Select the database |
| 120 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 121 | * @return resource |
| 122 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 123 | public function db_select() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 124 | { |
Alex Bilbie | 5336ee2 | 2011-03-12 23:35:29 +0000 | [diff] [blame] | 125 | return $this->_execute('USE ' . $this->database); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------- |
| 129 | |
| 130 | /** |
| 131 | * Set client character set |
| 132 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 133 | * @param string |
| 134 | * @param string |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 135 | * @return bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 136 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 137 | public function db_set_charset($charset, $collation) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 138 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 139 | // This is done upon connect |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 140 | return TRUE; |
| 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
| 146 | * Execute the query |
| 147 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 148 | * @param string an SQL query |
| 149 | * @return resource |
| 150 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 151 | protected function _execute($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 152 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 153 | return sqlsrv_query($this->conn_id, $this->_prep_query($sql), NULL, |
| 154 | array('Scrollable' => SQLSRV_CURSOR_STATIC, 'SendStreamParamsAtExec' => TRUE)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
| 158 | |
| 159 | /** |
| 160 | * Prep the query |
| 161 | * |
| 162 | * If needed, each database adapter can prep the query string |
| 163 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 164 | * @param string an SQL query |
| 165 | * @return string |
| 166 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 167 | protected function _prep_query($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 168 | { |
| 169 | return $sql; |
| 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Begin Transaction |
| 176 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 177 | * @return bool |
| 178 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 179 | public function trans_begin($test_mode = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 180 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 181 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 182 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 183 | { |
| 184 | return TRUE; |
| 185 | } |
| 186 | |
| 187 | // Reset the transaction failure flag. |
| 188 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 189 | // even if the queries produce a successful result. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 190 | $this->_trans_failure = ($test_mode === TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 191 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 192 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // -------------------------------------------------------------------- |
| 196 | |
| 197 | /** |
| 198 | * Commit Transaction |
| 199 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 200 | * @return bool |
| 201 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 202 | public function trans_commit() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 203 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 204 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 205 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 206 | { |
| 207 | return TRUE; |
| 208 | } |
| 209 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 210 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Rollback Transaction |
| 217 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 218 | * @return bool |
| 219 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 220 | public function trans_rollback() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 221 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 222 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 223 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [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 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 251 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 252 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [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 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 265 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 266 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 267 | public function insert_id() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 268 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 269 | $query = $this->query('SELECT @@IDENTITY AS insert_id'); |
| 270 | $query = $query->row(); |
| 271 | return $query->insert_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // -------------------------------------------------------------------- |
| 275 | |
| 276 | /** |
| 277 | * Version number query string |
| 278 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 279 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 280 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 281 | protected function _version() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 282 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 283 | $info = sqlsrv_server_info($this->conn_id); |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 284 | return $info['SQLServerVersion']; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * "Count All" query |
| 291 | * |
| 292 | * Generates a platform-specific query string that counts all records in |
| 293 | * the specified database |
| 294 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 295 | * @param string |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 296 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 297 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 298 | public function count_all($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 299 | { |
| 300 | if ($table == '') |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 301 | { |
| 302 | return 0; |
| 303 | } |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 304 | |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 305 | $query = $this->query('SELECT COUNT(*) AS numrows FROM '.$this->dbprefix.$table); |
| 306 | if ($query->num_rows() === 0) |
| 307 | { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | $query = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 312 | $this->_reset_select(); |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 313 | return (int) $query->numrows; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // -------------------------------------------------------------------- |
| 317 | |
| 318 | /** |
| 319 | * List table query |
| 320 | * |
| 321 | * Generates a platform-specific query string so that the table names can be fetched |
| 322 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 323 | * @param bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 324 | * @return string |
| 325 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 326 | protected function _list_tables($prefix_limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 327 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 328 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * List column query |
| 335 | * |
| 336 | * Generates a platform-specific query string so that the column names can be fetched |
| 337 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 338 | * @param string the table name |
| 339 | * @return string |
| 340 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 341 | protected function _list_columns($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 342 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 343 | 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] | 344 | } |
| 345 | |
| 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | /** |
| 349 | * Field data query |
| 350 | * |
| 351 | * Generates a platform-specific query so that the column data can be retrieved |
| 352 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 353 | * @param string the table name |
| 354 | * @return object |
| 355 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 356 | protected function _field_data($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 357 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 358 | return 'SELECT TOP 1 * FROM '.$this->_escape_table($table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
| 364 | * The error message string |
| 365 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 366 | * @return string |
| 367 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 368 | protected function _error_message() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 369 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 370 | $error = sqlsrv_errors(); |
| 371 | if ( ! is_array($error)) |
| 372 | { |
| 373 | return ''; |
| 374 | } |
| 375 | |
| 376 | $error = array_shift($error); |
| 377 | return isset($error['message']) ? $error['message'] : ''; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // -------------------------------------------------------------------- |
| 381 | |
| 382 | /** |
| 383 | * The error message number |
| 384 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 385 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 386 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 387 | protected function _error_number() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 388 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 389 | $error = sqlsrv_errors(); |
| 390 | if ( ! is_array($error)) |
| 391 | { |
| 392 | return ''; |
| 393 | } |
| 394 | elseif (isset($error['SQLSTATE'], $error['code'])) |
| 395 | { |
| 396 | return $error['SQLSTATE'].'/'.$error['code']; |
| 397 | } |
| 398 | elseif (isset($error['SQLSTATE'])) |
| 399 | { |
| 400 | return $error['SQLSTATE']; |
| 401 | } |
| 402 | elseif (isset($error['code'])) |
| 403 | { |
| 404 | return $error['code']; |
| 405 | } |
| 406 | |
| 407 | return ''; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // -------------------------------------------------------------------- |
| 411 | |
| 412 | /** |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 413 | * Escape Table Name |
| 414 | * |
| 415 | * This function adds backticks if the table name has a period |
| 416 | * in it. Some DBs will get cranky unless periods are escaped |
| 417 | * |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 418 | * @param string the table name |
| 419 | * @return string |
| 420 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 421 | protected function _escape_table($table) |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 422 | { |
| 423 | return $table; |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 424 | } |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 425 | |
| 426 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 427 | * Escape the SQL Identifiers |
| 428 | * |
| 429 | * This function escapes column and table names |
| 430 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 431 | * @param string |
| 432 | * @return string |
| 433 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 434 | public function _escape_identifiers($item) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 435 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 436 | return $item; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // -------------------------------------------------------------------- |
| 440 | |
| 441 | /** |
| 442 | * From Tables |
| 443 | * |
| 444 | * This function implicitly groups FROM tables so there is no confusion |
| 445 | * about operator precedence in harmony with SQL standards |
| 446 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 447 | * @param array |
| 448 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 449 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 450 | protected function _from_tables($tables) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 451 | { |
| 452 | if ( ! is_array($tables)) |
| 453 | { |
| 454 | $tables = array($tables); |
| 455 | } |
| 456 | |
| 457 | return implode(', ', $tables); |
| 458 | } |
| 459 | |
| 460 | // -------------------------------------------------------------------- |
| 461 | |
| 462 | /** |
| 463 | * Insert statement |
| 464 | * |
| 465 | * Generates a platform-specific insert string from the supplied data |
| 466 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 467 | * @param string the table name |
| 468 | * @param array the insert keys |
| 469 | * @param array the insert values |
| 470 | * @return string |
| 471 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 472 | protected function _insert($table, $keys, $values) |
| 473 | { |
| 474 | return 'INSERT INTO '.$this->_escape_table($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // -------------------------------------------------------------------- |
| 478 | |
| 479 | /** |
| 480 | * Update statement |
| 481 | * |
| 482 | * Generates a platform-specific update string from the supplied data |
| 483 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 484 | * @param string the table name |
| 485 | * @param array the update data |
| 486 | * @param array the where clause |
| 487 | * @param array the orderby clause |
| 488 | * @param array the limit clause |
| 489 | * @return string |
| 490 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 491 | protected function _update($table, $values, $where) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 492 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 493 | foreach ($values as $key => $val) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 494 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 495 | $valstr[] = $key.' = '.$val; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 496 | } |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 497 | |
| 498 | return 'UPDATE '.$this->_escape_table($table).' SET '.implode(', ', $valstr).' WHERE '.implode(' ', $where); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 499 | } |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 500 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 501 | // -------------------------------------------------------------------- |
| 502 | |
| 503 | /** |
| 504 | * Truncate statement |
| 505 | * |
| 506 | * Generates a platform-specific truncate string from the supplied data |
| 507 | * If the database does not support the truncate() command |
| 508 | * This function maps to "DELETE FROM table" |
| 509 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 510 | * @param string the table name |
| 511 | * @return string |
| 512 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 513 | protected function _truncate($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 514 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 515 | return 'TRUNCATE '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // -------------------------------------------------------------------- |
| 519 | |
| 520 | /** |
| 521 | * Delete statement |
| 522 | * |
| 523 | * Generates a platform-specific delete string from the supplied data |
| 524 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 525 | * @param string the table name |
| 526 | * @param array the where clause |
| 527 | * @param string the limit clause |
| 528 | * @return string |
| 529 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 530 | protected function _delete($table, $where) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 531 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 532 | return 'DELETE FROM '.$this->_escape_table($table).' WHERE '.implode(' ', $where); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // -------------------------------------------------------------------- |
| 536 | |
| 537 | /** |
| 538 | * Limit string |
| 539 | * |
| 540 | * Generates a platform-specific LIMIT clause |
| 541 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 542 | * @param string the sql query string |
| 543 | * @param integer the number of rows to limit the query to |
| 544 | * @param integer the offset value |
| 545 | * @return string |
| 546 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 547 | protected function _limit($sql, $limit, $offset) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 548 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 549 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // -------------------------------------------------------------------- |
| 553 | |
| 554 | /** |
| 555 | * Close DB Connection |
| 556 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 557 | * @param resource |
| 558 | * @return void |
| 559 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 560 | protected function _close($conn_id) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 561 | { |
| 562 | @sqlsrv_close($conn_id); |
| 563 | } |
| 564 | |
| 565 | } |
| 566 | |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame^] | 567 | /* End of file sqlsrv_driver.php */ |
| 568 | /* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */ |