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