Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * MS SQL Database Adapter Class |
| 32 | * |
| 33 | * Note: _DB is an extender class that the app controller |
| 34 | * creates dynamically based on whether the active record |
| 35 | * class is being used or not. |
| 36 | * |
| 37 | * @package CodeIgniter |
| 38 | * @subpackage Drivers |
| 39 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
| 43 | class CI_DB_mssql_driver extends CI_DB { |
| 44 | |
| 45 | var $dbdriver = 'mssql'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | // The character used for escaping |
| 48 | var $_escape_char = ''; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 49 | |
| 50 | // clause and character used for LIKE escape sequences |
| 51 | var $_like_escape_str = " ESCAPE '%s' "; |
| 52 | var $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | /** |
| 55 | * The syntax to count rows is slightly different across different |
| 56 | * database engines, so this string appears in each driver and is |
| 57 | * used for the count_all() and count_all_results() functions. |
| 58 | */ |
| 59 | var $_count_string = "SELECT COUNT(*) AS "; |
| 60 | var $_random_keyword = ' ASC'; // not currently supported |
| 61 | |
| 62 | /** |
| 63 | * Non-persistent database connection |
| 64 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 65 | * @access private called by the base class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 68 | function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | { |
| 70 | if ($this->port != '') |
| 71 | { |
| 72 | $this->hostname .= ','.$this->port; |
| 73 | } |
| 74 | |
| 75 | return @mssql_connect($this->hostname, $this->username, $this->password); |
| 76 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | // -------------------------------------------------------------------- |
| 79 | |
| 80 | /** |
| 81 | * Persistent database connection |
| 82 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 83 | * @access private called by the base class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 86 | function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | { |
| 88 | if ($this->port != '') |
| 89 | { |
| 90 | $this->hostname .= ','.$this->port; |
| 91 | } |
| 92 | |
| 93 | return @mssql_pconnect($this->hostname, $this->username, $this->password); |
| 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Reconnect |
| 100 | * |
| 101 | * Keep / reestablish the db connection if no queries have been |
| 102 | * sent for a length of time exceeding the server's idle timeout |
| 103 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 104 | * @access public |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 105 | * @return void |
| 106 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 107 | function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 108 | { |
| 109 | // not implemented in MSSQL |
| 110 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 112 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Select the database |
| 116 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 117 | * @param string database name |
| 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 | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 129 | if (@mssql_select_db('['.$database.']', $this->conn_id)) |
| 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 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 143 | * @access private called by the base class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | * @param string an SQL query |
| 145 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 147 | function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | return @mssql_query($sql, $this->conn_id); |
| 150 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | // -------------------------------------------------------------------- |
| 153 | |
| 154 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | * Begin Transaction |
| 156 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 157 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | * @return bool |
| 159 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 160 | function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | { |
| 162 | if ( ! $this->trans_enabled) |
| 163 | { |
| 164 | return TRUE; |
| 165 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 168 | if ($this->_trans_depth > 0) |
| 169 | { |
| 170 | return TRUE; |
| 171 | } |
| 172 | |
| 173 | // Reset the transaction failure flag. |
| 174 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 175 | // even if the queries produce a successful result. |
| 176 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 177 | |
| 178 | $this->simple_query('BEGIN TRAN'); |
| 179 | return TRUE; |
| 180 | } |
| 181 | |
| 182 | // -------------------------------------------------------------------- |
| 183 | |
| 184 | /** |
| 185 | * Commit Transaction |
| 186 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 187 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | * @return bool |
| 189 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 190 | function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
| 192 | if ( ! $this->trans_enabled) |
| 193 | { |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 198 | if ($this->_trans_depth > 0) |
| 199 | { |
| 200 | return TRUE; |
| 201 | } |
| 202 | |
| 203 | $this->simple_query('COMMIT TRAN'); |
| 204 | return TRUE; |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | /** |
| 210 | * Rollback Transaction |
| 211 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 212 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | * @return bool |
| 214 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 215 | function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | { |
| 217 | if ( ! $this->trans_enabled) |
| 218 | { |
| 219 | return TRUE; |
| 220 | } |
| 221 | |
| 222 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 223 | if ($this->_trans_depth > 0) |
| 224 | { |
| 225 | return TRUE; |
| 226 | } |
| 227 | |
| 228 | $this->simple_query('ROLLBACK TRAN'); |
| 229 | return TRUE; |
| 230 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
| 235 | * Escape String |
| 236 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 237 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 239 | * @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] | 240 | * @return string |
| 241 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 242 | function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 244 | if (is_array($str)) |
| 245 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 246 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 248 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return $str; |
| 252 | } |
| 253 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | // Escape single quotes |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 255 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 256 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 257 | // escape LIKE condition wildcards |
| 258 | if ($like === TRUE) |
| 259 | { |
Phil Sturgeon | 36b0c94 | 2011-04-02 12:16:41 +0100 | [diff] [blame] | 260 | $str = str_replace( |
| 261 | array($this->_like_escape_chr, '%', '_'), |
| 262 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 263 | $str |
| 264 | ); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 265 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 267 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | // -------------------------------------------------------------------- |
| 271 | |
| 272 | /** |
| 273 | * Affected Rows |
| 274 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 275 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | * @return integer |
| 277 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 278 | function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | { |
| 280 | return @mssql_rows_affected($this->conn_id); |
| 281 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | // -------------------------------------------------------------------- |
| 284 | |
| 285 | /** |
| 286 | * Insert ID |
| 287 | * |
| 288 | * Returns the last id created in the Identity column. |
| 289 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 290 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | * @return integer |
| 292 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 293 | function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | { |
| 295 | $ver = self::_parse_major_version($this->version()); |
| 296 | $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id"); |
| 297 | $query = $this->query($sql); |
| 298 | $row = $query->row(); |
| 299 | return $row->last_id; |
| 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | /** |
| 305 | * Parse major version |
| 306 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | * Grabs the major version number from the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | * database server version string passed in. |
| 309 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 310 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | * @param string $version |
| 312 | * @return int16 major version number |
| 313 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 314 | function _parse_major_version($version) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | { |
| 316 | preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info); |
| 317 | return $ver_info[1]; // return the major version b/c that's all we're interested in. |
| 318 | } |
| 319 | |
| 320 | // -------------------------------------------------------------------- |
| 321 | |
| 322 | /** |
| 323 | * Version number query string |
| 324 | * |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 325 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 327 | protected function _version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 329 | return 'SELECT @@VERSION AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // -------------------------------------------------------------------- |
| 333 | |
| 334 | /** |
| 335 | * "Count All" query |
| 336 | * |
| 337 | * Generates a platform-specific query string that counts all records in |
| 338 | * the specified database |
| 339 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 340 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | * @param string |
| 342 | * @return string |
| 343 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 344 | function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | { |
| 346 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 347 | { |
| 348 | return 0; |
| 349 | } |
| 350 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 351 | $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 353 | { |
| 354 | return 0; |
| 355 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | |
| 357 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 358 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 359 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // -------------------------------------------------------------------- |
| 363 | |
| 364 | /** |
| 365 | * List table query |
| 366 | * |
| 367 | * Generates a platform-specific query string so that the table names can be fetched |
| 368 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 369 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | * @param boolean |
| 371 | * @return string |
| 372 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 373 | function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | { |
| 375 | $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | // for future compatibility |
| 378 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 379 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 380 | //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | return FALSE; // not currently supported |
| 382 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 383 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | return $sql; |
| 385 | } |
| 386 | |
| 387 | // -------------------------------------------------------------------- |
| 388 | |
| 389 | /** |
| 390 | * List column query |
| 391 | * |
| 392 | * Generates a platform-specific query string so that the column names can be fetched |
| 393 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 394 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | * @param string the table name |
| 396 | * @return string |
| 397 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 398 | function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // -------------------------------------------------------------------- |
| 404 | |
| 405 | /** |
| 406 | * Field data query |
| 407 | * |
| 408 | * Generates a platform-specific query so that the column data can be retrieved |
| 409 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 410 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | * @param string the table name |
| 412 | * @return object |
| 413 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 414 | function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | return "SELECT TOP 1 * FROM ".$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // -------------------------------------------------------------------- |
| 420 | |
| 421 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 422 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 424 | * Returns an array containing code and message of the last |
| 425 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 427 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 429 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 431 | $query = $this->query('SELECT @@ERROR AS code'); |
| 432 | $query = $query->row(); |
| 433 | return array('code' => $query->code, 'message' => mssql_get_last_message()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // -------------------------------------------------------------------- |
| 437 | |
| 438 | /** |
| 439 | * Escape the SQL Identifiers |
| 440 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 441 | * This function escapes column and table names |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 443 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | * @param string |
| 445 | * @return string |
| 446 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 447 | function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
| 449 | if ($this->_escape_char == '') |
| 450 | { |
| 451 | return $item; |
| 452 | } |
| 453 | |
| 454 | foreach ($this->_reserved_identifiers as $id) |
| 455 | { |
| 456 | if (strpos($item, '.'.$id) !== FALSE) |
| 457 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 459 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | // remove duplicates if the user already included the escape |
| 461 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (strpos($item, '.') !== FALSE) |
| 466 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | } |
| 469 | else |
| 470 | { |
| 471 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 472 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | // remove duplicates if the user already included the escape |
| 475 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 476 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | // -------------------------------------------------------------------- |
| 479 | |
| 480 | /** |
| 481 | * From Tables |
| 482 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 483 | * This function implicitly groups FROM tables so there is no confusion |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | * about operator precedence in harmony with SQL standards |
| 485 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 486 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | * @param type |
| 488 | * @return type |
| 489 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 490 | function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | { |
| 492 | if ( ! is_array($tables)) |
| 493 | { |
| 494 | $tables = array($tables); |
| 495 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 496 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | return implode(', ', $tables); |
| 498 | } |
| 499 | |
| 500 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 501 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | /** |
| 503 | * Insert statement |
| 504 | * |
| 505 | * Generates a platform-specific insert string from the supplied data |
| 506 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 507 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | * @param string the table name |
| 509 | * @param array the insert keys |
| 510 | * @param array the insert values |
| 511 | * @return string |
| 512 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 513 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 514 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 516 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 517 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | // -------------------------------------------------------------------- |
| 519 | |
| 520 | /** |
| 521 | * Update statement |
| 522 | * |
| 523 | * Generates a platform-specific update string from the supplied data |
| 524 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 525 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | * @param string the table name |
| 527 | * @param array the update data |
| 528 | * @param array the where clause |
| 529 | * @param array the orderby clause |
| 530 | * @param array the limit clause |
| 531 | * @return string |
| 532 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 533 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 534 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 535 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | { |
| 537 | $valstr[] = $key." = ".$val; |
| 538 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 539 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 543 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 545 | |
| 546 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 547 | |
| 548 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 549 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | return $sql; |
| 551 | } |
| 552 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | // -------------------------------------------------------------------- |
| 555 | |
| 556 | /** |
| 557 | * Truncate statement |
| 558 | * |
| 559 | * Generates a platform-specific truncate string from the supplied data |
| 560 | * If the database does not support the truncate() command |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 561 | * This function maps to "DELETE FROM table" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 563 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | * @param string the table name |
| 565 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 566 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 567 | function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | { |
| 569 | return "TRUNCATE ".$table; |
| 570 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 571 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | // -------------------------------------------------------------------- |
| 573 | |
| 574 | /** |
| 575 | * Delete statement |
| 576 | * |
| 577 | * Generates a platform-specific delete string from the supplied data |
| 578 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 579 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | * @param string the table name |
| 581 | * @param array the where clause |
| 582 | * @param string the limit clause |
| 583 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 584 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 585 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | { |
| 587 | $conditions = ''; |
| 588 | |
| 589 | if (count($where) > 0 OR count($like) > 0) |
| 590 | { |
| 591 | $conditions = "\nWHERE "; |
| 592 | $conditions .= implode("\n", $this->ar_where); |
| 593 | |
| 594 | if (count($where) > 0 && count($like) > 0) |
| 595 | { |
| 596 | $conditions .= " AND "; |
| 597 | } |
| 598 | $conditions .= implode("\n", $like); |
| 599 | } |
| 600 | |
| 601 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 602 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | return "DELETE FROM ".$table.$conditions.$limit; |
| 604 | } |
| 605 | |
| 606 | // -------------------------------------------------------------------- |
| 607 | |
| 608 | /** |
| 609 | * Limit string |
| 610 | * |
| 611 | * Generates a platform-specific LIMIT clause |
| 612 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 613 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | * @param string the sql query string |
| 615 | * @param integer the number of rows to limit the query to |
| 616 | * @param integer the offset value |
| 617 | * @return string |
| 618 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 619 | function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | { |
| 621 | $i = $limit + $offset; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 622 | |
| 623 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // -------------------------------------------------------------------- |
| 627 | |
| 628 | /** |
| 629 | * Close DB Connection |
| 630 | * |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 631 | * @access public |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | * @param resource |
| 633 | * @return void |
| 634 | */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 635 | function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 636 | { |
| 637 | @mssql_close($conn_id); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 638 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 639 | |
| 640 | } |
| 641 | |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 642 | |
| 643 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | /* End of file mssql_driver.php */ |
Timothy Warren | 8c1b2dc | 2012-03-19 19:07:18 -0400 | [diff] [blame] | 645 | /* Location: ./system/database/drivers/mssql/mssql_driver.php */ |