Andrey Andreev | e629734 | 2012-03-20 16:25:07 +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 | e629734 | 2012-03-20 16:25:07 +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 | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 28 | /** |
Alex Bilbie | 01ab004 | 2011-03-18 19:24:30 +0000 | [diff] [blame] | 29 | * SQLSRV Database Adapter Class |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
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 | e629734 | 2012-03-20 16:25:07 +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 | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 46 | protected $_escape_char = ''; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 47 | |
| 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 50 | protected $_like_escape_chr = '!'; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
| 55 | * used for the count_all() and count_all_results() functions. |
| 56 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
Andrey Andreev | 3b2587e | 2012-03-28 13:39:34 +0300 | [diff] [blame] | 58 | protected $_random_keyword = ' NEWID()'; // not currently supported |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Non-persistent database connection |
| 62 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 63 | * @return resource |
| 64 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 65 | public function db_connect($pooling = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 66 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 67 | // Check for a UTF-8 charset being passed as CI's default 'utf8'. |
| 68 | $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set; |
| 69 | |
| 70 | $connection = array( |
Andrey Andreev | 3b2587e | 2012-03-28 13:39:34 +0300 | [diff] [blame] | 71 | 'UID' => empty($this->username) ? '' : $this->username, |
| 72 | 'PWD' => empty($this->password) ? '' : $this->password, |
| 73 | 'Database' => $this->database, |
| 74 | 'ConnectionPooling' => $pooling ? 1 : 0, |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 75 | 'CharacterSet' => $character_set, |
Andrey Andreev | 3b2587e | 2012-03-28 13:39:34 +0300 | [diff] [blame] | 76 | 'ReturnDatesAsStrings' => 1 |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 77 | ); |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 78 | |
| 79 | // If the username and password are both empty, assume this is a |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 80 | // 'Windows Authentication Mode' connection. |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 81 | if (empty($connection['UID']) && empty($connection['PWD'])) |
| 82 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 83 | unset($connection['UID'], $connection['PWD']); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 86 | return sqlsrv_connect($this->hostname, $connection); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | /** |
| 92 | * Persistent database connection |
| 93 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 94 | * @return resource |
| 95 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 96 | public function db_pconnect() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 97 | { |
Kyle Farris | 37e351f | 2011-09-07 11:14:46 -0300 | [diff] [blame] | 98 | return $this->db_connect(TRUE); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // -------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 104 | * Select the database |
| 105 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 106 | * @param string database name |
| 107 | * @return bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 108 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 109 | public function db_select($database = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 110 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 111 | if ($database === '') |
| 112 | { |
| 113 | $database = $this->database; |
| 114 | } |
| 115 | |
| 116 | if ($this->_execute('USE '.$database)) |
| 117 | { |
| 118 | $this->database = $database; |
| 119 | return TRUE; |
| 120 | } |
| 121 | |
| 122 | return FALSE; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // -------------------------------------------------------------------- |
| 126 | |
| 127 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 128 | * Execute the query |
| 129 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 130 | * @param string an SQL query |
| 131 | * @return resource |
| 132 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 133 | protected function _execute($sql) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 134 | { |
Andrey Andreev | 846acc7 | 2012-05-24 23:27:46 +0300 | [diff] [blame^] | 135 | return (is_write_type($sql) && stripos($sql, 'INSERT') === FALSE) |
| 136 | ? sqlsrv_query($this->conn_id, $sql) |
| 137 | : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC)); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // -------------------------------------------------------------------- |
| 141 | |
| 142 | /** |
| 143 | * Begin Transaction |
| 144 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 145 | * @return bool |
| 146 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 147 | public function trans_begin($test_mode = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 148 | { |
| 149 | if ( ! $this->trans_enabled) |
| 150 | { |
| 151 | return TRUE; |
| 152 | } |
| 153 | |
| 154 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 155 | if ($this->_trans_depth > 0) |
| 156 | { |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
| 160 | // Reset the transaction failure flag. |
| 161 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 162 | // even if the queries produce a successful result. |
| 163 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 164 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 165 | return sqlsrv_begin_transaction($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Commit Transaction |
| 172 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 173 | * @return bool |
| 174 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 175 | public function trans_commit() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 176 | { |
| 177 | if ( ! $this->trans_enabled) |
| 178 | { |
| 179 | return TRUE; |
| 180 | } |
| 181 | |
| 182 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 183 | if ($this->_trans_depth > 0) |
| 184 | { |
| 185 | return TRUE; |
| 186 | } |
| 187 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 188 | return sqlsrv_commit($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // -------------------------------------------------------------------- |
| 192 | |
| 193 | /** |
| 194 | * Rollback Transaction |
| 195 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 196 | * @return bool |
| 197 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 198 | public function trans_rollback() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 199 | { |
| 200 | if ( ! $this->trans_enabled) |
| 201 | { |
| 202 | return TRUE; |
| 203 | } |
| 204 | |
| 205 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 206 | if ($this->_trans_depth > 0) |
| 207 | { |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 211 | return sqlsrv_rollback($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // -------------------------------------------------------------------- |
| 215 | |
| 216 | /** |
| 217 | * Escape String |
| 218 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 219 | * @param string |
| 220 | * @param bool whether or not the string will be used in a LIKE condition |
| 221 | * @return string |
| 222 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 223 | public function escape_str($str, $like = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 224 | { |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 225 | // Escape single quotes |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 226 | return str_replace("'", "''", $str); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // -------------------------------------------------------------------- |
| 230 | |
| 231 | /** |
| 232 | * Affected Rows |
| 233 | * |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 234 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 235 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 236 | public function affected_rows() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 237 | { |
Andrey Andreev | 846acc7 | 2012-05-24 23:27:46 +0300 | [diff] [blame^] | 238 | return sqlrv_rows_affected($this->result_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 244 | * Insert ID |
| 245 | * |
| 246 | * Returns the last id created in the Identity column. |
| 247 | * |
| 248 | * @return string |
| 249 | */ |
| 250 | public function insert_id() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 251 | { |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 252 | $query = $this->query('SELECT @@IDENTITY AS insert_id'); |
| 253 | $query = $query->row(); |
| 254 | return $query->insert_id; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 260 | * Database version number |
| 261 | * |
| 262 | * @return string |
| 263 | */ |
| 264 | public function version() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 265 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 266 | if (isset($this->data_cache['version'])) |
| 267 | { |
| 268 | return $this->data_cache['version']; |
| 269 | } |
| 270 | |
| 271 | if (($info = sqlsrv_server_info($this->conn_id)) === FALSE) |
| 272 | { |
| 273 | return FALSE; |
| 274 | } |
| 275 | |
| 276 | return $this->data_cache['version'] = $info['SQLServerVersion']; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // -------------------------------------------------------------------- |
| 280 | |
| 281 | /** |
| 282 | * "Count All" query |
| 283 | * |
| 284 | * Generates a platform-specific query string that counts all records in |
| 285 | * the specified database |
| 286 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 287 | * @param string |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 288 | * @return int |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 289 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 290 | public function count_all($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 291 | { |
| 292 | if ($table == '') |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 293 | { |
| 294 | return 0; |
| 295 | } |
| 296 | |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 297 | $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 298 | if ($query->num_rows() == 0) |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 299 | { |
| 300 | return 0; |
| 301 | } |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 302 | |
| 303 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 304 | $this->_reset_select(); |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 305 | return (int) $row->numrows; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // -------------------------------------------------------------------- |
| 309 | |
| 310 | /** |
| 311 | * List table query |
| 312 | * |
| 313 | * Generates a platform-specific query string so that the table names can be fetched |
| 314 | * |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 315 | * @param bool |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 316 | * @return string |
| 317 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 318 | protected function _list_tables($prefix_limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 319 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 320 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
| 326 | * List column query |
| 327 | * |
| 328 | * Generates a platform-specific query string so that the column names can be fetched |
| 329 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 330 | * @param string the table name |
| 331 | * @return string |
| 332 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 333 | protected function _list_columns($table = '') |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 334 | { |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 335 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // -------------------------------------------------------------------- |
| 339 | |
| 340 | /** |
| 341 | * Field data query |
| 342 | * |
| 343 | * Generates a platform-specific query so that the column data can be retrieved |
| 344 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 345 | * @param string the table name |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 346 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 347 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 348 | protected function _field_data($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 349 | { |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 350 | return 'SELECT TOP 1 * FROM '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // -------------------------------------------------------------------- |
| 354 | |
| 355 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 356 | * Error |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 357 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 358 | * Returns an array containing code and message of the last |
| 359 | * database error that has occured. |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 360 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 361 | * @return array |
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 | public function error() |
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 | $error = array('code' => '00000', 'message' => ''); |
| 366 | $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
| 367 | |
| 368 | if ( ! is_array($sqlsrv_errors)) |
Andrey Andreev | 850f601 | 2012-03-01 15:58:25 +0200 | [diff] [blame] | 369 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 370 | return $error; |
Andrey Andreev | 850f601 | 2012-03-01 15:58:25 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 373 | $sqlsrv_error = array_shift($sqlsrv_errors); |
| 374 | if (isset($sqlsrv_error['SQLSTATE'])) |
| 375 | { |
| 376 | $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE']; |
| 377 | } |
| 378 | elseif (isset($sqlsrv_error['code'])) |
| 379 | { |
| 380 | $error['code'] = $sqlsrv_error['code']; |
| 381 | } |
| 382 | |
| 383 | if (isset($sqlsrv_error['message'])) |
| 384 | { |
| 385 | $error['message'] = $sqlsrv_error['message']; |
| 386 | } |
| 387 | |
| 388 | return $error; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // -------------------------------------------------------------------- |
| 392 | |
| 393 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 394 | * From Tables |
| 395 | * |
| 396 | * This function implicitly groups FROM tables so there is no confusion |
| 397 | * about operator precedence in harmony with SQL standards |
| 398 | * |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 399 | * @param array |
| 400 | * @return string |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 401 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 402 | protected function _from_tables($tables) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 403 | { |
| 404 | if ( ! is_array($tables)) |
| 405 | { |
| 406 | $tables = array($tables); |
| 407 | } |
| 408 | |
| 409 | return implode(', ', $tables); |
| 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 415 | * Update statement |
| 416 | * |
| 417 | * Generates a platform-specific update string from the supplied data |
| 418 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 419 | * @param string the table name |
| 420 | * @param array the update data |
| 421 | * @param array the where clause |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 422 | * @param array the orderby clause (ignored) |
| 423 | * @param array the limit clause (ignored) |
| 424 | * @param array the like clause |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 425 | * @return string |
| 426 | */ |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 427 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 428 | { |
Alex Bilbie | 3a43c7a | 2011-03-18 19:48:04 +0000 | [diff] [blame] | 429 | foreach($values as $key => $val) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 430 | { |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 431 | $valstr[] = $key.' = '.$val; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 432 | } |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 433 | |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 434 | $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); |
| 435 | |
| 436 | if ( ! empty($like)) |
| 437 | { |
| 438 | $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); |
| 439 | } |
| 440 | |
st2 | 4b47e98 | 2012-04-24 17:45:44 +0800 | [diff] [blame] | 441 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 442 | } |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 443 | |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 444 | // -------------------------------------------------------------------- |
| 445 | |
| 446 | /** |
| 447 | * Truncate statement |
| 448 | * |
| 449 | * Generates a platform-specific truncate string from the supplied data |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 450 | * |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 451 | * If the database does not support the truncate() command, |
| 452 | * then this method maps to 'DELETE FROM table' |
| 453 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 454 | * @param string the table name |
| 455 | * @return string |
| 456 | */ |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 457 | protected function _truncate($table) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 458 | { |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 459 | return 'TRUNCATE TABLE '.$table; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // -------------------------------------------------------------------- |
| 463 | |
| 464 | /** |
| 465 | * Delete statement |
| 466 | * |
| 467 | * Generates a platform-specific delete string from the supplied data |
| 468 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 469 | * @param string the table name |
| 470 | * @param array the where clause |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 471 | * @param array the like clause |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 472 | * @param string the limit clause |
| 473 | * @return string |
| 474 | */ |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 475 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 476 | { |
Andrey Andreev | 5c0e9fe | 2012-04-09 12:28:11 +0300 | [diff] [blame] | 477 | $conditions = array(); |
| 478 | |
| 479 | empty($where) OR $conditions[] = implode(' ', $where); |
| 480 | empty($like) OR $conditions[] = implode(' ', $like); |
| 481 | |
| 482 | $conditions = (count($conditions) > 0) ? ' WHERE '.implode(' AND ', $conditions) : ''; |
| 483 | |
| 484 | return ($limit) |
| 485 | ? 'WITH ci_delete AS (SELECT TOP '.$limit.' * FROM '.$table.$conditions.') DELETE FROM ci_delete' |
| 486 | : 'DELETE FROM '.$table.$conditions; |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // -------------------------------------------------------------------- |
| 490 | |
| 491 | /** |
| 492 | * Limit string |
| 493 | * |
| 494 | * Generates a platform-specific LIMIT clause |
| 495 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 496 | * @param string the sql query string |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 497 | * @param int the number of rows to limit the query to |
| 498 | * @param int the offset value |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 499 | * @return string |
| 500 | */ |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 501 | protected function _limit($sql, $limit, $offset) |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 502 | { |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 503 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // -------------------------------------------------------------------- |
| 507 | |
| 508 | /** |
| 509 | * Close DB Connection |
| 510 | * |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 511 | * @return void |
| 512 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 513 | protected function _close() |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 514 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 515 | @sqlsrv_close($this->conn_id); |
Alex Bilbie | 84445d0 | 2011-03-10 16:43:39 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | } |
| 519 | |
Andrey Andreev | e629734 | 2012-03-20 16:25:07 +0200 | [diff] [blame] | 520 | /* End of file sqlsrv_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 521 | /* Location: ./system/database/drivers/sqlsrv/sqlsrv_driver.php */ |