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 | * |
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 |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 28 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 29 | /** |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 30 | * SQLSRV Database Adapter Class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 39 | * @author EllisLab Dev Team |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 42 | class CI_DB_sqlsrv_driver extends CI_DB { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 43 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Database driver |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 49 | public $dbdriver = 'sqlsrv'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 50 | |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame^] | 51 | /** |
| 52 | * Scrollable flag |
| 53 | * |
| 54 | * Determines what cursor type to use when executing queries. |
| 55 | * |
| 56 | * FALSE or SQLSRV_CURSOR_FORWARD would increase performance, |
| 57 | * but would disable num_rows() (and possibly insert_id()) |
| 58 | * |
| 59 | * @var mixed |
| 60 | */ |
| 61 | public $scrollable; |
| 62 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 63 | // -------------------------------------------------------------------- |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 64 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 65 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 66 | * ORDER BY random keyword |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 67 | * |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 68 | * @var array |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 69 | */ |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 70 | protected $_random_keyword = array('NEWID()', 'RAND(%d)'); |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Quoted identifier flag |
| 74 | * |
| 75 | * Whether to use SQL-92 standard quoted identifier |
| 76 | * (double quotes) or brackets for identifier escaping. |
| 77 | * |
| 78 | * @var bool |
| 79 | */ |
| 80 | protected $_quoted_identifier = TRUE; |
| 81 | |
| 82 | // -------------------------------------------------------------------- |
| 83 | |
| 84 | /** |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame^] | 85 | * Class constructor |
| 86 | * |
| 87 | * @param array $params |
| 88 | * @return void |
| 89 | */ |
| 90 | public function __construct($params) |
| 91 | { |
| 92 | parent::__construct($params); |
| 93 | |
| 94 | // This is only supported as of SQLSRV 3.0 |
| 95 | if ($this->scrollable === NULL) |
| 96 | { |
| 97 | $this->scrollable = defined('SQLSRV_CURSOR_CLIENT_BUFFERED') |
| 98 | ? SQLSRV_CURSOR_CLIENT_BUFFERED |
| 99 | : FALSE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // -------------------------------------------------------------------- |
| 104 | |
| 105 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 106 | * Database connection |
| 107 | * |
| 108 | * @param bool $pooling |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 109 | * @return resource |
| 110 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 111 | public function db_connect($pooling = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 112 | { |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 113 | $charset = in_array(strtolower($this->char_set), array('utf-8', 'utf8'), TRUE) |
| 114 | ? 'UTF-8' : SQLSRV_ENC_CHAR; |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 115 | |
| 116 | $connection = array( |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 117 | 'UID' => empty($this->username) ? '' : $this->username, |
| 118 | 'PWD' => empty($this->password) ? '' : $this->password, |
| 119 | 'Database' => $this->database, |
Andrey Andreev | 2f8bf9b | 2012-10-12 20:37:52 +0300 | [diff] [blame] | 120 | 'ConnectionPooling' => ($pooling === TRUE) ? 1 : 0, |
| 121 | 'CharacterSet' => $charset, |
| 122 | 'Encrypt' => ($this->encrypt === TRUE) ? 1 : 0, |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 123 | 'ReturnDatesAsStrings' => 1 |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 124 | ); |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 125 | |
| 126 | // If the username and password are both empty, assume this is a |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 127 | // 'Windows Authentication Mode' connection. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 128 | if (empty($connection['UID']) && empty($connection['PWD'])) |
| 129 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 130 | unset($connection['UID'], $connection['PWD']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 133 | $this->conn_id = sqlsrv_connect($this->hostname, $connection); |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 134 | |
| 135 | // Determine how identifiers are escaped |
| 136 | $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi'); |
| 137 | $query = $query->row_array(); |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 138 | $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi']; |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 139 | $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']'); |
| 140 | |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 141 | return $this->conn_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
| 145 | |
| 146 | /** |
| 147 | * Persistent database connection |
| 148 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 149 | * @return resource |
| 150 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 151 | public function db_pconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 152 | { |
Kyle Farris | 37e351f | 2011-09-07 11:14:46 -0300 | [diff] [blame] | 153 | return $this->db_connect(TRUE); |
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; |
| 174 | return TRUE; |
| 175 | } |
| 176 | |
| 177 | return FALSE; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 183 | * Execute the query |
| 184 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 185 | * @param string $sql an SQL query |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 186 | * @return resource |
| 187 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 188 | protected function _execute($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 189 | { |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame^] | 190 | return ($this->scrollable === FALSE OR $this->is_write_type($sql)) |
Andrey Andreev | 846acc7 | 2012-05-24 23:27:46 +0300 | [diff] [blame] | 191 | ? sqlsrv_query($this->conn_id, $sql) |
Andrey Andreev | ba8bf56 | 2014-01-21 19:04:18 +0200 | [diff] [blame^] | 192 | : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => $this->scrollable)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // -------------------------------------------------------------------- |
| 196 | |
| 197 | /** |
| 198 | * Begin Transaction |
| 199 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 200 | * @param bool $test_mode |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 201 | * @return bool |
| 202 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 203 | public function trans_begin($test_mode = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 204 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 205 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 206 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 207 | { |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
| 211 | // Reset the transaction failure flag. |
| 212 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 213 | // even if the queries produce a successful result. |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 214 | $this->_trans_failure = ($test_mode === TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 215 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 216 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
| 220 | |
| 221 | /** |
| 222 | * Commit Transaction |
| 223 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 224 | * @return bool |
| 225 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 226 | public function trans_commit() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 227 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 228 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 229 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 230 | { |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 234 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
| 240 | * Rollback Transaction |
| 241 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 242 | * @return bool |
| 243 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 244 | public function trans_rollback() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 245 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 246 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 247 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 248 | { |
| 249 | return TRUE; |
| 250 | } |
| 251 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 252 | return sqlsrv_rollback($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // -------------------------------------------------------------------- |
| 256 | |
| 257 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 258 | * Affected Rows |
| 259 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 260 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 261 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 262 | public function affected_rows() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 263 | { |
Andrey Andreev | ede49ba | 2012-07-23 16:06:36 +0300 | [diff] [blame] | 264 | return sqlsrv_rows_affected($this->result_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 270 | * Insert ID |
| 271 | * |
| 272 | * Returns the last id created in the Identity column. |
| 273 | * |
| 274 | * @return string |
| 275 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 276 | public function insert_id() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 277 | { |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 278 | $query = $this->query('SELECT @@IDENTITY AS insert_id'); |
| 279 | $query = $query->row(); |
| 280 | return $query->insert_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // -------------------------------------------------------------------- |
| 284 | |
| 285 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 286 | * Database version number |
| 287 | * |
| 288 | * @return string |
| 289 | */ |
| 290 | public function version() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 291 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 292 | if (isset($this->data_cache['version'])) |
| 293 | { |
| 294 | return $this->data_cache['version']; |
| 295 | } |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 296 | elseif ( ! $this->conn_id) |
| 297 | { |
| 298 | $this->initialize(); |
| 299 | } |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 300 | |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame] | 301 | 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] | 302 | { |
| 303 | return FALSE; |
| 304 | } |
| 305 | |
| 306 | return $this->data_cache['version'] = $info['SQLServerVersion']; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 312 | * List table query |
| 313 | * |
| 314 | * Generates a platform-specific query string so that the table names can be fetched |
| 315 | * |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 316 | * @param bool |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 317 | * @return string $prefix_limit |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 318 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 319 | protected function _list_tables($prefix_limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 320 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 321 | $sql = 'SELECT '.$this->escape_identifiers('name') |
| 322 | .' FROM '.$this->escape_identifiers('sysobjects') |
| 323 | .' WHERE '.$this->escape_identifiers('type')." = 'U'"; |
| 324 | |
| 325 | if ($prefix_limit === TRUE && $this->dbprefix !== '') |
| 326 | { |
| 327 | $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' " |
| 328 | .sprintf($this->_escape_like_str, $this->_escape_like_chr); |
| 329 | } |
| 330 | |
| 331 | return $sql.' ORDER BY '.$this->escape_identifiers('name'); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
| 337 | * List column query |
| 338 | * |
| 339 | * Generates a platform-specific query string so that the column names can be fetched |
| 340 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 341 | * @param string $table |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 342 | * @return string |
| 343 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 344 | protected function _list_columns($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 345 | { |
Andrey Andreev | e158057 | 2012-11-16 16:17:54 +0200 | [diff] [blame] | 346 | return 'SELECT COLUMN_NAME |
| 347 | FROM INFORMATION_SCHEMA.Columns |
Andrey Andreev | 4658307 | 2012-11-16 16:44:31 +0200 | [diff] [blame] | 348 | WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 354 | * Returns an object with field data |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 355 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 356 | * @param string $table |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 357 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 358 | */ |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 359 | public function field_data($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 360 | { |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 361 | if ($table === '') |
| 362 | { |
| 363 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
| 364 | } |
| 365 | |
| 366 | $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT |
| 367 | FROM INFORMATION_SCHEMA.Columns |
Andrey Andreev | 4658307 | 2012-11-16 16:44:31 +0200 | [diff] [blame] | 368 | WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table)); |
Andrey Andreev | f1e1b77 | 2012-11-16 01:27:00 +0200 | [diff] [blame] | 369 | |
| 370 | if (($query = $this->query($sql)) === FALSE) |
| 371 | { |
| 372 | return FALSE; |
| 373 | } |
| 374 | $query = $query->result_object(); |
| 375 | |
| 376 | $retval = array(); |
| 377 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 378 | { |
| 379 | $retval[$i] = new stdClass(); |
| 380 | $retval[$i]->name = $query[$i]->COLUMN_NAME; |
| 381 | $retval[$i]->type = $query[$i]->DATA_TYPE; |
Andrey Andreev | e158057 | 2012-11-16 16:17:54 +0200 | [diff] [blame] | 382 | $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] | 383 | $retval[$i]->default = $query[$i]->COLUMN_DEFAULT; |
| 384 | } |
| 385 | |
| 386 | return $retval; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // -------------------------------------------------------------------- |
| 390 | |
| 391 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 392 | * Error |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 393 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 394 | * Returns an array containing code and message of the last |
| 395 | * database error that has occured. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 396 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 397 | * @return array |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 398 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 399 | public function error() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 400 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 401 | $error = array('code' => '00000', 'message' => ''); |
| 402 | $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
| 403 | |
| 404 | if ( ! is_array($sqlsrv_errors)) |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 405 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 406 | return $error; |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 409 | $sqlsrv_error = array_shift($sqlsrv_errors); |
| 410 | if (isset($sqlsrv_error['SQLSTATE'])) |
| 411 | { |
| 412 | $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE']; |
| 413 | } |
| 414 | elseif (isset($sqlsrv_error['code'])) |
| 415 | { |
| 416 | $error['code'] = $sqlsrv_error['code']; |
| 417 | } |
| 418 | |
| 419 | if (isset($sqlsrv_error['message'])) |
| 420 | { |
| 421 | $error['message'] = $sqlsrv_error['message']; |
| 422 | } |
| 423 | |
| 424 | return $error; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 430 | * Update statement |
| 431 | * |
| 432 | * Generates a platform-specific update string from the supplied data |
| 433 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 434 | * @param string $table |
| 435 | * @param array $values |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 436 | * @return string |
| 437 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 438 | protected function _update($table, $values) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 439 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 440 | $this->qb_limit = FALSE; |
| 441 | $this->qb_orderby = array(); |
| 442 | return parent::_update($table, $values); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 443 | } |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 444 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 445 | // -------------------------------------------------------------------- |
| 446 | |
| 447 | /** |
| 448 | * Truncate statement |
| 449 | * |
| 450 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 451 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 452 | * If the database does not support the TRUNCATE statement, |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 453 | * then this method maps to 'DELETE FROM table' |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 454 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 455 | * @param string $table |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 456 | * @return string |
| 457 | */ |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 458 | protected function _truncate($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 459 | { |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 460 | return 'TRUNCATE TABLE '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // -------------------------------------------------------------------- |
| 464 | |
| 465 | /** |
| 466 | * Delete statement |
| 467 | * |
| 468 | * Generates a platform-specific delete string from the supplied data |
| 469 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 470 | * @param string $table |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 471 | * @return string |
| 472 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 473 | protected function _delete($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 474 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 475 | if ($this->qb_limit) |
| 476 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 477 | 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] | 478 | } |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 479 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 480 | return parent::_delete($table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // -------------------------------------------------------------------- |
| 484 | |
| 485 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 486 | * LIMIT |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 487 | * |
| 488 | * Generates a platform-specific LIMIT clause |
| 489 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 490 | * @param string $sql SQL Query |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 491 | * @return string |
| 492 | */ |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 493 | protected function _limit($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 494 | { |
Andrey Andreev | d25c589 | 2012-06-08 16:23:01 +0300 | [diff] [blame] | 495 | // As of SQL Server 2012 (11.0.*) OFFSET is supported |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 496 | if (version_compare($this->version(), '11', '>=')) |
| 497 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 498 | 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] | 499 | } |
| 500 | |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 501 | $limit = $this->qb_offset + $this->qb_limit; |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 502 | |
| 503 | // An ORDER BY clause is required for ROW_NUMBER() to work |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 504 | if ($this->qb_offset && ! empty($this->qb_orderby)) |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 505 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 506 | $orderby = $this->_compile_order_by(); |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 507 | |
| 508 | // We have to strip the ORDER BY clause |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 509 | $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 510 | |
Andrey Andreev | 4451454 | 2012-10-23 15:35:09 +0300 | [diff] [blame] | 511 | // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results |
| 512 | if (count($this->qb_select) === 0) |
| 513 | { |
| 514 | $select = '*'; // Inevitable |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | // Use only field names and their aliases, everything else is out of our scope. |
| 519 | $select = array(); |
| 520 | $field_regexp = ($this->_quoted_identifier) |
| 521 | ? '("[^\"]+")' : '(\[[^\]]+\])'; |
| 522 | for ($i = 0, $c = count($this->qb_select); $i < $c; $i++) |
| 523 | { |
| 524 | $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m) |
| 525 | ? $m[1] : $this->qb_select[$i]; |
| 526 | } |
| 527 | $select = implode(', ', $select); |
| 528 | } |
| 529 | |
| 530 | return 'SELECT '.$select." FROM (\n\n" |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 531 | .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] | 532 | ."\n\n) ".$this->escape_identifiers('CI_subquery') |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 533 | ."\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] | 534 | } |
| 535 | |
| 536 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // -------------------------------------------------------------------- |
| 540 | |
| 541 | /** |
Andrey Andreev | 083e3c8 | 2012-11-06 12:48:32 +0200 | [diff] [blame] | 542 | * Insert batch statement |
| 543 | * |
| 544 | * Generates a platform-specific insert string from the supplied data. |
| 545 | * |
| 546 | * @param string $table Table name |
| 547 | * @param array $keys INSERT keys |
| 548 | * @param array $values INSERT values |
| 549 | * @return string|bool |
| 550 | */ |
| 551 | protected function _insert_batch($table, $keys, $values) |
| 552 | { |
| 553 | // Multiple-value inserts are only supported as of SQL Server 2008 |
| 554 | if (version_compare($this->version(), '10', '>=')) |
| 555 | { |
| 556 | return parent::_insert_batch($table, $keys, $values); |
| 557 | } |
| 558 | |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 559 | return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; |
Andrey Andreev | 083e3c8 | 2012-11-06 12:48:32 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // -------------------------------------------------------------------- |
| 563 | |
| 564 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 565 | * Close DB Connection |
| 566 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 567 | * @return void |
| 568 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 569 | protected function _close() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 570 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 571 | @sqlsrv_close($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | } |
| 575 | |
Andrey Andreev | 0e8968a | 2012-01-26 02:04:37 +0200 | [diff] [blame] | 576 | /* End of file sqlsrv_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 577 | /* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */ |