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 | * |
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 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +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 | 4da24f8 | 2012-01-25 21:54:23 +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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * MS SQL Database Adapter Class |
| 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 39 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
| 42 | class CI_DB_mssql_driver extends CI_DB { |
| 43 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 44 | public $dbdriver = 'mssql'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 45 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | // The character used for escaping |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 47 | protected $_escape_char = '"'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 48 | |
Andrey Andreev | dd7242b | 2012-01-26 00:43:38 +0200 | [diff] [blame] | 49 | protected $_random_keyword = ' NEWID()'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 51 | // MSSQL-specific properties |
| 52 | protected $_quoted_identifier = TRUE; |
| 53 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 54 | /** |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 55 | * Constructor |
| 56 | * |
| 57 | * Appends the port number to the hostname, if needed. |
| 58 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 59 | * @param array $params |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 60 | * @return void |
| 61 | */ |
Andrey Andreev | bd601d3 | 2012-02-12 21:16:51 +0200 | [diff] [blame] | 62 | public function __construct($params) |
| 63 | { |
| 64 | parent::__construct($params); |
| 65 | |
Andrey Andreev | 2cb262f | 2012-03-28 13:45:04 +0300 | [diff] [blame] | 66 | if ( ! empty($this->port)) |
Andrey Andreev | bd601d3 | 2012-02-12 21:16:51 +0200 | [diff] [blame] | 67 | { |
| 68 | $this->hostname .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port; |
| 69 | } |
| 70 | } |
| 71 | |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 72 | // -------------------------------------------------------------------- |
| 73 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | /** |
| 75 | * Non-persistent database connection |
| 76 | * |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 77 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | */ |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 80 | public function db_connect($persistent = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | { |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 82 | $this->conn_id = ($persistent) |
| 83 | ? @mssql_pconnect($this->hostname, $this->username, $this->password) |
| 84 | : @mssql_connect($this->hostname, $this->username, $this->password); |
| 85 | |
| 86 | if ( ! $this->conn_id) |
| 87 | { |
| 88 | return FALSE; |
| 89 | } |
| 90 | |
| 91 | // Determine how identifiers are escaped |
| 92 | $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi'); |
| 93 | $query = $query->row_array(); |
| 94 | $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi']; |
| 95 | $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']'); |
| 96 | |
| 97 | return $this->conn_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | // -------------------------------------------------------------------- |
| 101 | |
| 102 | /** |
| 103 | * Persistent database connection |
| 104 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 107 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
Andrey Andreev | fac3761 | 2012-07-02 14:56:20 +0300 | [diff] [blame] | 109 | return $this->db_connect(TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 114 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | * Select the database |
| 116 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 117 | * @param string database name |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 118 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 120 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 122 | if ($database === '') |
| 123 | { |
| 124 | $database = $this->database; |
| 125 | } |
| 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | // Note: The brackets are required in the event that the DB name |
| 128 | // contains reserved characters |
Andrey Andreev | 082ee2b | 2012-06-08 15:26:34 +0300 | [diff] [blame] | 129 | if (@mssql_select_db($this->escape_identifiers($database), $this->conn_id)) |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 130 | { |
| 131 | $this->database = $database; |
| 132 | return TRUE; |
| 133 | } |
| 134 | |
| 135 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | * Execute the query |
| 142 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | * @param string an SQL query |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 144 | * @return mixed resource if rows are returned, bool otherwise |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 146 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | return @mssql_query($sql, $this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Begin Transaction |
| 155 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 156 | * @param bool $test_mode = FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | * @return bool |
| 158 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 159 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 162 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | { |
| 164 | return TRUE; |
| 165 | } |
| 166 | |
| 167 | // Reset the transaction failure flag. |
| 168 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 169 | // even if the queries produce a successful result. |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 170 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 172 | return $this->simple_query('BEGIN TRAN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // -------------------------------------------------------------------- |
| 176 | |
| 177 | /** |
| 178 | * Commit Transaction |
| 179 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | * @return bool |
| 181 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 182 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 185 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | { |
| 187 | return TRUE; |
| 188 | } |
| 189 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 190 | return $this->simple_query('COMMIT TRAN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // -------------------------------------------------------------------- |
| 194 | |
| 195 | /** |
| 196 | * Rollback Transaction |
| 197 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | * @return bool |
| 199 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 200 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 203 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | { |
| 205 | return TRUE; |
| 206 | } |
| 207 | |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 208 | return $this->simple_query('ROLLBACK TRAN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | // -------------------------------------------------------------------- |
| 212 | |
| 213 | /** |
| 214 | * Escape String |
| 215 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 217 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | * @return string |
| 219 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 220 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 222 | if (is_array($str)) |
| 223 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 224 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 226 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return $str; |
| 230 | } |
| 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | // Escape single quotes |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 233 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 235 | // escape LIKE condition wildcards |
| 236 | if ($like === TRUE) |
| 237 | { |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 238 | return str_replace( |
Phil Sturgeon | 36b0c94 | 2011-04-02 12:16:41 +0100 | [diff] [blame] | 239 | array($this->_like_escape_chr, '%', '_'), |
| 240 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 241 | $str |
| 242 | ); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 245 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | // -------------------------------------------------------------------- |
| 249 | |
| 250 | /** |
| 251 | * Affected Rows |
| 252 | * |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 253 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 255 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | { |
| 257 | return @mssql_rows_affected($this->conn_id); |
| 258 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | // -------------------------------------------------------------------- |
| 261 | |
| 262 | /** |
Andrey Andreev | 1f619a8 | 2012-03-20 16:03:04 +0200 | [diff] [blame] | 263 | * Insert ID |
| 264 | * |
| 265 | * Returns the last id created in the Identity column. |
| 266 | * |
| 267 | * @return string |
| 268 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 269 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 271 | $query = version_compare($this->version(), '8', '>=') |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 272 | ? 'SELECT SCOPE_IDENTITY() AS last_id' |
| 273 | : 'SELECT @@IDENTITY AS last_id'; |
| 274 | |
Andrey Andreev | b7a47a7 | 2012-01-26 02:13:33 +0200 | [diff] [blame] | 275 | $query = $this->query($query); |
| 276 | $query = $query->row(); |
| 277 | return $query->last_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
Andrey Andreev | 1f619a8 | 2012-03-20 16:03:04 +0200 | [diff] [blame] | 283 | * Version number query string |
| 284 | * |
| 285 | * @return string |
| 286 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 287 | protected function _version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 289 | return 'SELECT @@VERSION AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // -------------------------------------------------------------------- |
| 293 | |
| 294 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | * List table query |
| 296 | * |
| 297 | * Generates a platform-specific query string so that the table names can be fetched |
| 298 | * |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 299 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | * @return string |
| 301 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 302 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 304 | $sql = 'SELECT '.$this->escape_identifiers('name') |
| 305 | .' FROM '.$this->escape_identifiers('sysobjects') |
| 306 | .' WHERE '.$this->escape_identifiers('type')." = 'U'"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 308 | if ($prefix_limit !== FALSE AND $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 310 | $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' " |
| 311 | .sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 314 | return $sql.' ORDER BY '.$this->escape_identifiers('name'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * List column query |
| 321 | * |
| 322 | * Generates a platform-specific query string so that the column names can be fetched |
| 323 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | * @param string the table name |
| 325 | * @return string |
| 326 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 327 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // -------------------------------------------------------------------- |
| 333 | |
| 334 | /** |
| 335 | * Field data query |
| 336 | * |
| 337 | * Generates a platform-specific query so that the column data can be retrieved |
| 338 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | * @param string the table name |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 340 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 342 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | { |
Andrey Andreev | 70c72c9 | 2012-06-25 00:04:51 +0300 | [diff] [blame] | 344 | return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 350 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 352 | * Returns an array containing code and message of the last |
| 353 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 355 | * @return array |
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 | public function error() |
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 | $query = $this->query('SELECT @@ERROR AS code'); |
| 360 | $query = $query->row(); |
| 361 | return array('code' => $query->code, 'message' => mssql_get_last_message()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // -------------------------------------------------------------------- |
| 365 | |
| 366 | /** |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 367 | * Update statement |
| 368 | * |
| 369 | * Generates a platform-specific update string from the supplied data |
| 370 | * |
| 371 | * @param string the table name |
| 372 | * @param array the update data |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 373 | * @return string |
| 374 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 375 | protected function _update($table, $values) |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 376 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 377 | $this->qb_limit = FALSE; |
| 378 | $this->qb_orderby = array(); |
| 379 | return parent::_update($table, $values); |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // -------------------------------------------------------------------- |
| 383 | |
| 384 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | * Truncate statement |
| 386 | * |
| 387 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 388 | * |
| 389 | * If the database does not support the truncate() command, |
| 390 | * then this method maps to 'DELETE FROM table' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | * @param string the table name |
| 393 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 394 | */ |
Andrey Andreev | 4da24f8 | 2012-01-25 21:54:23 +0200 | [diff] [blame] | 395 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
Andrey Andreev | 6d83cde | 2012-04-05 16:20:50 +0300 | [diff] [blame] | 397 | return 'TRUNCATE TABLE '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | // -------------------------------------------------------------------- |
| 401 | |
| 402 | /** |
| 403 | * Delete statement |
| 404 | * |
| 405 | * Generates a platform-specific delete string from the supplied data |
| 406 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | * @param string the table name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 409 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 410 | protected function _delete($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 412 | if ($this->qb_limit) |
| 413 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 414 | 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] | 415 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 417 | return parent::_delete($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // -------------------------------------------------------------------- |
| 421 | |
| 422 | /** |
| 423 | * Limit string |
| 424 | * |
| 425 | * Generates a platform-specific LIMIT clause |
| 426 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | * @param string the sql query string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | * @return string |
| 429 | */ |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 430 | protected function _limit($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 432 | $limit = $this->qb_offset + $this->qb_limit; |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 433 | |
| 434 | // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported, |
| 435 | // however an ORDER BY clause is required for it to work |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 436 | 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] | 437 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 438 | $orderby = $this->_compile_order_by(); |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 439 | |
| 440 | // We have to strip the ORDER BY clause |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 441 | $sql = trim(substr($sql, 0, strrpos($sql, $orderby))); |
Andrey Andreev | 71379ca | 2012-06-11 16:12:43 +0300 | [diff] [blame] | 442 | |
Andrey Andreev | 4451454 | 2012-10-23 15:35:09 +0300 | [diff] [blame] | 443 | // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results |
| 444 | if (count($this->qb_select) === 0) |
| 445 | { |
| 446 | $select = '*'; // Inevitable |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | // Use only field names and their aliases, everything else is out of our scope. |
| 451 | $select = array(); |
| 452 | $field_regexp = ($this->_quoted_identifier) |
| 453 | ? '("[^\"]+")' : '(\[[^\]]+\])'; |
| 454 | for ($i = 0, $c = count($this->qb_select); $i < $c; $i++) |
| 455 | { |
| 456 | $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m) |
| 457 | ? $m[1] : $this->qb_select[$i]; |
| 458 | } |
| 459 | $select = implode(', ', $select); |
| 460 | } |
| 461 | |
| 462 | return 'SELECT '.$select." FROM (\n\n" |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 463 | .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] | 464 | ."\n\n) ".$this->escape_identifiers('CI_subquery') |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 465 | ."\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] | 466 | } |
| 467 | |
| 468 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // -------------------------------------------------------------------- |
| 472 | |
| 473 | /** |
| 474 | * Close DB Connection |
| 475 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | * @return void |
| 477 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 478 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 480 | @mssql_close($this->conn_id); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 481 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | |
| 483 | } |
| 484 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | /* End of file mssql_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 486 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |