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 | * |
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 |
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 |
Andrey Andreev | bf94058 | 2012-06-10 07:05:05 +0300 | [diff] [blame] | 24 | * @since Version 2.0.3 |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 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 | 082ee2b | 2012-06-08 15:26:34 +0300 | [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 | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = " ESCAPE '%s' "; |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 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 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 60 | // SQLSRV-specific properties |
| 61 | protected $_quoted_identifier = TRUE; |
| 62 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Non-persistent database connection |
| 65 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 66 | * @return resource |
| 67 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 68 | public 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( |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 74 | 'UID' => empty($this->username) ? '' : $this->username, |
| 75 | 'PWD' => empty($this->password) ? '' : $this->password, |
| 76 | 'Database' => $this->database, |
| 77 | 'ConnectionPooling' => $pooling ? 1 : 0, |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 78 | 'CharacterSet' => $character_set, |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 79 | 'ReturnDatesAsStrings' => 1 |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 80 | ); |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 81 | |
| 82 | // If the username and password are both empty, assume this is a |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 83 | // 'Windows Authentication Mode' connection. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 84 | if (empty($connection['UID']) && empty($connection['PWD'])) |
| 85 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 86 | unset($connection['UID'], $connection['PWD']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 89 | $conn_id = sqlsrv_connect($this->hostname, $connection); |
| 90 | |
| 91 | // Determine how identifiers are escaped |
| 92 | $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi'); |
| 93 | $query = $query->row_array(); |
| 94 | $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query->qi; |
| 95 | $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']'); |
| 96 | |
| 97 | return $conn_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // -------------------------------------------------------------------- |
| 101 | |
| 102 | /** |
| 103 | * Persistent database connection |
| 104 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 105 | * @return resource |
| 106 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 107 | public function db_pconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 108 | { |
Kyle Farris | 37e351f | 2011-09-07 11:14:46 -0300 | [diff] [blame] | 109 | return $this->db_connect(TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 115 | * Select the database |
| 116 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 117 | * @param string database name |
| 118 | * @return bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 119 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 120 | public function db_select($database = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 122 | if ($database === '') |
| 123 | { |
| 124 | $database = $this->database; |
| 125 | } |
| 126 | |
| 127 | if ($this->_execute('USE '.$database)) |
| 128 | { |
| 129 | $this->database = $database; |
| 130 | return TRUE; |
| 131 | } |
| 132 | |
| 133 | return FALSE; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // -------------------------------------------------------------------- |
| 137 | |
| 138 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 139 | * Execute the query |
| 140 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 141 | * @param string an SQL query |
| 142 | * @return resource |
| 143 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 144 | protected function _execute($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 145 | { |
Andrey Andreev | 846acc7 | 2012-05-24 23:27:46 +0300 | [diff] [blame] | 146 | return (is_write_type($sql) && stripos($sql, 'INSERT') === FALSE) |
| 147 | ? sqlsrv_query($this->conn_id, $sql) |
| 148 | : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Begin Transaction |
| 155 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 156 | * @return bool |
| 157 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 158 | public function trans_begin($test_mode = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 159 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 160 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 161 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 162 | { |
| 163 | return TRUE; |
| 164 | } |
| 165 | |
| 166 | // Reset the transaction failure flag. |
| 167 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 168 | // even if the queries produce a successful result. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 169 | $this->_trans_failure = ($test_mode === TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 170 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 171 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Commit Transaction |
| 178 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 179 | * @return bool |
| 180 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 181 | public function trans_commit() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 182 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 183 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 184 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 185 | { |
| 186 | return TRUE; |
| 187 | } |
| 188 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 189 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // -------------------------------------------------------------------- |
| 193 | |
| 194 | /** |
| 195 | * Rollback Transaction |
| 196 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 197 | * @return bool |
| 198 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 199 | public function trans_rollback() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 200 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 201 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 202 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 203 | { |
| 204 | return TRUE; |
| 205 | } |
| 206 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 207 | return sqlsrv_rollback($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * Escape String |
| 214 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 215 | * @param string |
| 216 | * @param bool whether or not the string will be used in a LIKE condition |
| 217 | * @return string |
| 218 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 219 | public function escape_str($str, $like = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 220 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 221 | // Escape single quotes |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 222 | return str_replace("'", "''", $str); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // -------------------------------------------------------------------- |
| 226 | |
| 227 | /** |
| 228 | * Affected Rows |
| 229 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 230 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 231 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 232 | public function affected_rows() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 233 | { |
Andrey Andreev | 846acc7 | 2012-05-24 23:27:46 +0300 | [diff] [blame] | 234 | return sqlrv_rows_affected($this->result_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 240 | * Insert ID |
| 241 | * |
| 242 | * Returns the last id created in the Identity column. |
| 243 | * |
| 244 | * @return string |
| 245 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 246 | public function insert_id() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 247 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 248 | $query = $this->query('SELECT @@IDENTITY AS insert_id'); |
| 249 | $query = $query->row(); |
| 250 | return $query->insert_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // -------------------------------------------------------------------- |
| 254 | |
| 255 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 256 | * Database version number |
| 257 | * |
| 258 | * @return string |
| 259 | */ |
| 260 | public function version() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 261 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 262 | if (isset($this->data_cache['version'])) |
| 263 | { |
| 264 | return $this->data_cache['version']; |
| 265 | } |
| 266 | |
| 267 | if (($info = sqlsrv_server_info($this->conn_id)) === FALSE) |
| 268 | { |
| 269 | return FALSE; |
| 270 | } |
| 271 | |
| 272 | return $this->data_cache['version'] = $info['SQLServerVersion']; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // -------------------------------------------------------------------- |
| 276 | |
| 277 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 278 | * List table query |
| 279 | * |
| 280 | * Generates a platform-specific query string so that the table names can be fetched |
| 281 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 282 | * @param bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 283 | * @return string |
| 284 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 285 | protected function _list_tables($prefix_limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 286 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 287 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * List column query |
| 294 | * |
| 295 | * Generates a platform-specific query string so that the column names can be fetched |
| 296 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 297 | * @param string the table name |
| 298 | * @return string |
| 299 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 300 | protected function _list_columns($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 301 | { |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 302 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * Field data query |
| 309 | * |
| 310 | * Generates a platform-specific query so that the column data can be retrieved |
| 311 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 312 | * @param string the table name |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 313 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 314 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 315 | protected function _field_data($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 316 | { |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 317 | return 'SELECT TOP 1 * FROM '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // -------------------------------------------------------------------- |
| 321 | |
| 322 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 323 | * Error |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 324 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 325 | * Returns an array containing code and message of the last |
| 326 | * database error that has occured. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 327 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 328 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 329 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 330 | public function error() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 331 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 332 | $error = array('code' => '00000', 'message' => ''); |
| 333 | $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
| 334 | |
| 335 | if ( ! is_array($sqlsrv_errors)) |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 336 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 337 | return $error; |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 340 | $sqlsrv_error = array_shift($sqlsrv_errors); |
| 341 | if (isset($sqlsrv_error['SQLSTATE'])) |
| 342 | { |
| 343 | $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE']; |
| 344 | } |
| 345 | elseif (isset($sqlsrv_error['code'])) |
| 346 | { |
| 347 | $error['code'] = $sqlsrv_error['code']; |
| 348 | } |
| 349 | |
| 350 | if (isset($sqlsrv_error['message'])) |
| 351 | { |
| 352 | $error['message'] = $sqlsrv_error['message']; |
| 353 | } |
| 354 | |
| 355 | return $error; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 361 | * From Tables |
| 362 | * |
| 363 | * This function implicitly groups FROM tables so there is no confusion |
| 364 | * about operator precedence in harmony with SQL standards |
| 365 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 366 | * @param array |
| 367 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 368 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 369 | protected function _from_tables($tables) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 370 | { |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 371 | return is_array($tables) ? implode(', ', $tables) : $tables; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // -------------------------------------------------------------------- |
| 375 | |
| 376 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 377 | * Update statement |
| 378 | * |
| 379 | * Generates a platform-specific update string from the supplied data |
| 380 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 381 | * @param string the table name |
| 382 | * @param array the update data |
| 383 | * @param array the where clause |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 384 | * @param array the orderby clause (ignored) |
| 385 | * @param array the limit clause (ignored) |
| 386 | * @param array the like clause |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 387 | * @return string |
| 388 | */ |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 389 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 390 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 391 | foreach ($values as $key => $val) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 392 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 393 | $valstr[] = $key.' = '.$val; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 394 | } |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 395 | |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 396 | $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); |
| 397 | |
| 398 | if ( ! empty($like)) |
| 399 | { |
| 400 | $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); |
| 401 | } |
| 402 | |
st2 | 4b47e98 | 2012-04-24 17:45:44 +0800 | [diff] [blame] | 403 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 404 | } |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 405 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 406 | // -------------------------------------------------------------------- |
| 407 | |
| 408 | /** |
| 409 | * Truncate statement |
| 410 | * |
| 411 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 412 | * |
| 413 | * If the database does not support the truncate() command, |
| 414 | * then this method maps to 'DELETE FROM table' |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 415 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 416 | * @param string the table name |
| 417 | * @return string |
| 418 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 419 | protected function _truncate($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 420 | { |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 421 | return 'TRUNCATE TABLE '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // -------------------------------------------------------------------- |
| 425 | |
| 426 | /** |
| 427 | * Delete statement |
| 428 | * |
| 429 | * Generates a platform-specific delete string from the supplied data |
| 430 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 431 | * @param string the table name |
| 432 | * @param array the where clause |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 433 | * @param array the like clause |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 434 | * @param string the limit clause |
| 435 | * @return string |
| 436 | */ |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 437 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 438 | { |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 439 | $conditions = array(); |
| 440 | |
| 441 | empty($where) OR $conditions[] = implode(' ', $where); |
| 442 | empty($like) OR $conditions[] = implode(' ', $like); |
| 443 | |
| 444 | $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; |
| 445 | |
| 446 | return ($limit) |
| 447 | ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' |
| 448 | : 'DELETE FROM '.$table.$conditions; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // -------------------------------------------------------------------- |
| 452 | |
| 453 | /** |
| 454 | * Limit string |
| 455 | * |
| 456 | * Generates a platform-specific LIMIT clause |
| 457 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 458 | * @param string the sql query string |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 459 | * @param int the number of rows to limit the query to |
| 460 | * @param int the offset value |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 461 | * @return string |
| 462 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 463 | protected function _limit($sql, $limit, $offset) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 464 | { |
Andrey Andreev | d25c589 | 2012-06-08 16:23:01 +0300 | [diff] [blame] | 465 | // As of SQL Server 2012 (11.0.*) OFFSET is supported |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 466 | if (version_compare($this->version(), '11', '>=')) |
| 467 | { |
| 468 | return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY'; |
| 469 | } |
| 470 | |
| 471 | $limit = $offset + $limit; |
| 472 | |
| 473 | // An ORDER BY clause is required for ROW_NUMBER() to work |
| 474 | if ($offset && ! empty($this->qb_orderby)) |
| 475 | { |
| 476 | $orderby = 'ORDER BY '.implode(', ', $this->qb_orderby); |
| 477 | |
| 478 | // We have to strip the ORDER BY clause |
| 479 | $sql = trim(substr($sql, 0, strrpos($sql, 'ORDER BY '.$orderby))); |
| 480 | |
| 481 | return 'SELECT '.(count($this->qb_select) === 0 ? '*' : implode(', ', $this->qb_select))." FROM (\n" |
| 482 | .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.$orderby.') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) |
| 483 | ."\n) ".$this->escape_identifiers('CI_subquery') |
| 484 | ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.((int) $offset + 1).' AND '.$limit; |
| 485 | } |
| 486 | |
| 487 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // -------------------------------------------------------------------- |
| 491 | |
| 492 | /** |
| 493 | * Close DB Connection |
| 494 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 495 | * @return void |
| 496 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 497 | protected function _close() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 498 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 499 | @sqlsrv_close($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | } |
| 503 | |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 504 | /* End of file sqlsrv_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 505 | /* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */ |