Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 2.0.3 |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 39 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 40 | /** |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 41 | * SQLSRV Database Adapter Class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 42 | * |
| 43 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 44 | * creates dynamically based on whether the query builder |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 45 | * class is being used or not. |
| 46 | * |
| 47 | * @package CodeIgniter |
| 48 | * @subpackage Drivers |
| 49 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 50 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 51 | * @link https://codeigniter.com/user_guide/database/ |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 52 | */ |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 53 | class CI_DB_sqlsrv_driver extends CI_DB { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 54 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 55 | /** |
| 56 | * Database driver |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 60 | public $dbdriver = 'sqlsrv'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 61 | |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame] | 62 | /** |
| 63 | * Scrollable flag |
| 64 | * |
| 65 | * Determines what cursor type to use when executing queries. |
| 66 | * |
| 67 | * FALSE or SQLSRV_CURSOR_FORWARD would increase performance, |
| 68 | * but would disable num_rows() (and possibly insert_id()) |
| 69 | * |
| 70 | * @var mixed |
| 71 | */ |
| 72 | public $scrollable; |
| 73 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 74 | // -------------------------------------------------------------------- |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 75 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 76 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 77 | * ORDER BY random keyword |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 78 | * |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 79 | * @var array |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 80 | */ |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 81 | protected $_random_keyword = array('NEWID()', 'RAND(%d)'); |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Quoted identifier flag |
| 85 | * |
| 86 | * Whether to use SQL-92 standard quoted identifier |
| 87 | * (double quotes) or brackets for identifier escaping. |
| 88 | * |
| 89 | * @var bool |
| 90 | */ |
| 91 | protected $_quoted_identifier = TRUE; |
| 92 | |
| 93 | // -------------------------------------------------------------------- |
| 94 | |
| 95 | /** |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame] | 96 | * Class constructor |
| 97 | * |
| 98 | * @param array $params |
| 99 | * @return void |
| 100 | */ |
| 101 | public function __construct($params) |
| 102 | { |
| 103 | parent::__construct($params); |
| 104 | |
| 105 | // This is only supported as of SQLSRV 3.0 |
| 106 | if ($this->scrollable === NULL) |
| 107 | { |
| 108 | $this->scrollable = defined('SQLSRV_CURSOR_CLIENT_BUFFERED') |
| 109 | ? SQLSRV_CURSOR_CLIENT_BUFFERED |
| 110 | : FALSE; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // -------------------------------------------------------------------- |
| 115 | |
| 116 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 117 | * Database connection |
| 118 | * |
| 119 | * @param bool $pooling |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 120 | * @return resource |
| 121 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 122 | public function db_connect($pooling = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 123 | { |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 124 | $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE) |
| 125 | ? 'UTF-8' : SQLSRV_ENC_CHAR; |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 126 | |
| 127 | $connection = array( |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 128 | 'UID' => empty($this->username) ? '' : $this->username, |
| 129 | 'PWD' => empty($this->password) ? '' : $this->password, |
| 130 | 'Database' => $this->database, |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 131 | 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0, |
| 132 | 'CharacterSet' => $charset, |
| 133 | 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0, |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 134 | 'ReturnDatesAsStrings' => 1 |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 135 | ); |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 136 | |
| 137 | // If the username and password are both empty, assume this is a |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 138 | // 'Windows Authentication Mode' connection. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 139 | if (empty($connection['UID']) && empty($connection['PWD'])) |
| 140 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 141 | unset($connection['UID'], $connection['PWD']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Andrey Andreev | eb49258 | 2015-08-19 11:16:52 +0300 | [diff] [blame] | 144 | if (FALSE !== ($this->conn_id = sqlsrv_connect($this->hostname, $connection))) |
| 145 | { |
| 146 | // Determine how identifiers are escaped |
| 147 | $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi'); |
| 148 | $query = $query->row_array(); |
| 149 | $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi']; |
| 150 | $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']'); |
| 151 | } |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 152 | |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 153 | return $this->conn_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // -------------------------------------------------------------------- |
| 157 | |
| 158 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 159 | * Select the database |
| 160 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 161 | * @param string $database |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 162 | * @return bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 163 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 164 | public function db_select($database = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 165 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 166 | if ($database === '') |
| 167 | { |
| 168 | $database = $this->database; |
| 169 | } |
| 170 | |
Eco91 | d0822ce | 2012-11-19 20:49:38 +0100 | [diff] [blame] | 171 | if ($this->_execute('USE '.$this->escape_identifiers($database))) |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 172 | { |
| 173 | $this->database = $database; |
Andrey Andreev | ed6f010 | 2016-06-08 11:06:59 +0300 | [diff] [blame] | 174 | $this->data_cache = array(); |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 175 | return TRUE; |
| 176 | } |
| 177 | |
| 178 | return FALSE; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 184 | * Execute the query |
| 185 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 186 | * @param string $sql an SQL query |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 187 | * @return resource |
| 188 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 189 | protected function _execute($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 190 | { |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame] | 191 | return ($this->scrollable === FALSE OR $this->is_write_type($sql)) |
Andrey Andreev | 846acc7 | 2012-05-24 23:27:46 +0300 | [diff] [blame] | 192 | ? sqlsrv_query($this->conn_id, $sql) |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame] | 193 | : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => $this->scrollable)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // -------------------------------------------------------------------- |
| 197 | |
| 198 | /** |
| 199 | * Begin Transaction |
| 200 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 201 | * @return bool |
| 202 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 203 | protected function _trans_begin() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 204 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 205 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // -------------------------------------------------------------------- |
| 209 | |
| 210 | /** |
| 211 | * Commit Transaction |
| 212 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 213 | * @return bool |
| 214 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 215 | protected function _trans_commit() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 216 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 217 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Rollback Transaction |
| 224 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 225 | * @return bool |
| 226 | */ |
Andrey Andreev | a7d4aba | 2015-10-19 14:39:44 +0300 | [diff] [blame] | 227 | protected function _trans_rollback() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 228 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 229 | return sqlsrv_rollback($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 235 | * Affected Rows |
| 236 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 237 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 238 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 239 | public function affected_rows() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 240 | { |
Andrey Andreev | ede49ba | 2012-07-23 16:06:36 +0300 | [diff] [blame] | 241 | return sqlsrv_rows_affected($this->result_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 247 | * Insert ID |
| 248 | * |
| 249 | * Returns the last id created in the Identity column. |
| 250 | * |
| 251 | * @return string |
| 252 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 253 | public function insert_id() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 254 | { |
Andrey Andreev | 9d02cea | 2015-10-13 14:38:30 +0300 | [diff] [blame] | 255 | return $this->query('SELECT SCOPE_IDENTITY() AS insert_id')->row()->insert_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 261 | * Database version number |
| 262 | * |
| 263 | * @return string |
| 264 | */ |
| 265 | public function version() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 266 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 267 | if (isset($this->data_cache['version'])) |
| 268 | { |
| 269 | return $this->data_cache['version']; |
| 270 | } |
| 271 | |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 272 | if ( ! $this->conn_id OR ($info = sqlsrv_server_info($this->conn_id)) === FALSE) |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 273 | { |
| 274 | return FALSE; |
| 275 | } |
| 276 | |
| 277 | return $this->data_cache['version'] = $info['SQLServerVersion']; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 283 | * List table query |
| 284 | * |
| 285 | * Generates a platform-specific query string so that the table names can be fetched |
| 286 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 287 | * @param bool |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 288 | * @return string $prefix_limit |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 289 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 290 | protected function _list_tables($prefix_limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 291 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 292 | $sql = 'SELECT '.$this->escape_identifiers('name') |
| 293 | .' FROM '.$this->escape_identifiers('sysobjects') |
| 294 | .' WHERE '.$this->escape_identifiers('type')." = 'U'"; |
| 295 | |
| 296 | if ($prefix_limit === TRUE && $this->dbprefix !== '') |
| 297 | { |
| 298 | $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' " |
| 299 | .sprintf($this->_escape_like_str, $this->_escape_like_chr); |
| 300 | } |
| 301 | |
| 302 | return $sql.' ORDER BY '.$this->escape_identifiers('name'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * List column query |
| 309 | * |
| 310 | * Generates a platform-specific query string so that the column names can be fetched |
| 311 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 312 | * @param string $table |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 313 | * @return string |
| 314 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 315 | protected function _list_columns($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 316 | { |
Andrey Andreev | e158057 | 2012-11-16 16:17:54 +0200 | [diff] [blame] | 317 | return 'SELECT COLUMN_NAME |
| 318 | FROM INFORMATION_SCHEMA.Columns |
Andrey Andreev | 4658307 | 2012-11-16 16:44:31 +0200 | [diff] [blame] | 319 | WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 325 | * Returns an object with field data |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 326 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 327 | * @param string $table |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 328 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 329 | */ |
Andrey Andreev | 5350f05 | 2015-01-12 12:33:37 +0200 | [diff] [blame] | 330 | public function field_data($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 331 | { |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 332 | $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT |
| 333 | FROM INFORMATION_SCHEMA.Columns |
Andrey Andreev | 4658307 | 2012-11-16 16:44:31 +0200 | [diff] [blame] | 334 | WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table)); |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 335 | |
| 336 | if (($query = $this->query($sql)) === FALSE) |
| 337 | { |
| 338 | return FALSE; |
| 339 | } |
| 340 | $query = $query->result_object(); |
| 341 | |
| 342 | $retval = array(); |
| 343 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 344 | { |
| 345 | $retval[$i] = new stdClass(); |
| 346 | $retval[$i]->name = $query[$i]->COLUMN_NAME; |
| 347 | $retval[$i]->type = $query[$i]->DATA_TYPE; |
Andrey Andreev | e158057 | 2012-11-16 16:17:54 +0200 | [diff] [blame] | 348 | $retval[$i]->max_length = ($query[$i]->CHARACTER_MAXIMUM_LENGTH > 0) ? $query[$i]->CHARACTER_MAXIMUM_LENGTH : $query[$i]->NUMERIC_PRECISION; |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 349 | $retval[$i]->default = $query[$i]->COLUMN_DEFAULT; |
| 350 | } |
| 351 | |
| 352 | return $retval; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // -------------------------------------------------------------------- |
| 356 | |
| 357 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 358 | * Error |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 359 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 360 | * Returns an array containing code and message of the last |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 361 | * database error that has occurred. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 362 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 363 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 364 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 365 | public function error() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 366 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 367 | $error = array('code' => '00000', 'message' => ''); |
| 368 | $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
| 369 | |
| 370 | if ( ! is_array($sqlsrv_errors)) |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 371 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 372 | return $error; |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 375 | $sqlsrv_error = array_shift($sqlsrv_errors); |
| 376 | if (isset($sqlsrv_error['SQLSTATE'])) |
| 377 | { |
| 378 | $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE']; |
| 379 | } |
| 380 | elseif (isset($sqlsrv_error['code'])) |
| 381 | { |
| 382 | $error['code'] = $sqlsrv_error['code']; |
| 383 | } |
| 384 | |
| 385 | if (isset($sqlsrv_error['message'])) |
| 386 | { |
| 387 | $error['message'] = $sqlsrv_error['message']; |
| 388 | } |
| 389 | |
| 390 | return $error; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // -------------------------------------------------------------------- |
| 394 | |
| 395 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 396 | * Update statement |
| 397 | * |
| 398 | * Generates a platform-specific update string from the supplied data |
| 399 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 400 | * @param string $table |
| 401 | * @param array $values |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 402 | * @return string |
| 403 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 404 | protected function _update($table, $values) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 405 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 406 | $this->qb_limit = FALSE; |
| 407 | $this->qb_orderby = array(); |
| 408 | return parent::_update($table, $values); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 409 | } |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 410 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Truncate statement |
| 415 | * |
| 416 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 417 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 418 | * If the database does not support the TRUNCATE statement, |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 419 | * then this method maps to 'DELETE FROM table' |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 420 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 421 | * @param string $table |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 422 | * @return string |
| 423 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 424 | protected function _truncate($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 425 | { |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 426 | return 'TRUNCATE TABLE '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | /** |
| 432 | * Delete statement |
| 433 | * |
| 434 | * Generates a platform-specific delete string from the supplied data |
| 435 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 436 | * @param string $table |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 437 | * @return string |
| 438 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 439 | protected function _delete($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 440 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 441 | if ($this->qb_limit) |
| 442 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 443 | return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete'; |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 444 | } |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 445 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 446 | return parent::_delete($table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // -------------------------------------------------------------------- |
| 450 | |
| 451 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 452 | * LIMIT |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 453 | * |
| 454 | * Generates a platform-specific LIMIT clause |
| 455 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 456 | * @param string $sql SQL Query |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 457 | * @return string |
| 458 | */ |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 459 | protected function _limit($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 460 | { |
Andrey Andreev | d25c589 | 2012-06-08 16:23:01 +0300 | [diff] [blame] | 461 | // As of SQL Server 2012 (11.0.*) OFFSET is supported |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 462 | if (version_compare($this->version(), '11', '>=')) |
| 463 | { |
Andrey Andreev | ed3fc51 | 2014-11-18 11:12:35 +0200 | [diff] [blame] | 464 | // SQL Server OFFSET-FETCH can be used only with the ORDER BY clause |
| 465 | empty($this->qb_orderby) && $sql .= ' ORDER BY 1'; |
| 466 | |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 467 | return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY'; |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 468 | } |
| 469 | |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 470 | $limit = $this->qb_offset + $this->qb_limit; |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 471 | |
| 472 | // An ORDER BY clause is required for ROW_NUMBER() to work |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 473 | if ($this->qb_offset && ! empty($this->qb_orderby)) |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 474 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 475 | $orderby = $this->_compile_order_by(); |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 476 | |
| 477 | // We have to strip the ORDER BY clause |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 478 | $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 479 | |
Andrey Andreev | 4451454 | 2012-10-23 15:35:09 +0300 | [diff] [blame] | 480 | // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results |
Andrey Andreev | 613b898 | 2017-08-25 13:24:51 +0300 | [diff] [blame] | 481 | if (count($this->qb_select) === 0 OR strpos(implode(',', $this->qb_select), '*') !== FALSE) |
Andrey Andreev | 4451454 | 2012-10-23 15:35:09 +0300 | [diff] [blame] | 482 | { |
| 483 | $select = '*'; // Inevitable |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | // Use only field names and their aliases, everything else is out of our scope. |
| 488 | $select = array(); |
| 489 | $field_regexp = ($this->_quoted_identifier) |
| 490 | ? '("[^\"]+")' : '(\[[^\]]+\])'; |
| 491 | for ($i = 0, $c = count($this->qb_select); $i < $c; $i++) |
| 492 | { |
| 493 | $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m) |
| 494 | ? $m[1] : $this->qb_select[$i]; |
| 495 | } |
| 496 | $select = implode(', ', $select); |
| 497 | } |
| 498 | |
| 499 | return 'SELECT '.$select." FROM (\n\n" |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 500 | .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql) |
Andrey Andreev | 4451454 | 2012-10-23 15:35:09 +0300 | [diff] [blame] | 501 | ."\n\n) ".$this->escape_identifiers('CI_subquery') |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 502 | ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit; |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // -------------------------------------------------------------------- |
| 509 | |
| 510 | /** |
Andrey Andreev | 083e3c8 | 2012-11-06 12:48:32 +0200 | [diff] [blame] | 511 | * Insert batch statement |
| 512 | * |
| 513 | * Generates a platform-specific insert string from the supplied data. |
| 514 | * |
| 515 | * @param string $table Table name |
| 516 | * @param array $keys INSERT keys |
| 517 | * @param array $values INSERT values |
| 518 | * @return string|bool |
| 519 | */ |
| 520 | protected function _insert_batch($table, $keys, $values) |
| 521 | { |
| 522 | // Multiple-value inserts are only supported as of SQL Server 2008 |
| 523 | if (version_compare($this->version(), '10', '>=')) |
| 524 | { |
| 525 | return parent::_insert_batch($table, $keys, $values); |
| 526 | } |
| 527 | |
Andrey Andreev | ea07352 | 2017-03-14 18:42:12 +0200 | [diff] [blame] | 528 | return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | 083e3c8 | 2012-11-06 12:48:32 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // -------------------------------------------------------------------- |
| 532 | |
| 533 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 534 | * Close DB Connection |
| 535 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 536 | * @return void |
| 537 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 538 | protected function _close() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 539 | { |
Andrey Andreev | 2bbbd1a | 2014-05-09 10:24:14 +0300 | [diff] [blame] | 540 | sqlsrv_close($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | } |