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