Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 | b76029d | 2012-01-26 15:13:19 +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 | b76029d | 2012-01-26 15:13:19 +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 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * ODBC Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
| 32 | * creates dynamically based on whether the active record |
| 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_odbc_driver extends CI_DB { |
| 42 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 43 | public $dbdriver = 'odbc'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | // the character used to excape - not necessary for ODBC |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 46 | protected $_escape_char = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 47 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = ' {escape \'%s\'} '; |
| 50 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
| 55 | * used for the count_all() and count_all_results() functions. |
| 56 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | |
Timothy Warren | a2097a0 | 2011-10-10 10:10:46 -0400 | [diff] [blame] | 60 | function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | { |
Timothy Warren | a2097a0 | 2011-10-10 10:10:46 -0400 | [diff] [blame] | 62 | parent::__construct($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
Andrey Andreev | 46f6dbc | 2012-02-13 01:39:53 +0200 | [diff] [blame] | 65 | |
| 66 | // Legacy support for DSN in the hostname field |
| 67 | if ($this->dsn == '') |
| 68 | { |
| 69 | $this->dsn = $this->hostname; |
| 70 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Non-persistent database connection |
| 75 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 78 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | { |
Andrey Andreev | 46f6dbc | 2012-02-13 01:39:53 +0200 | [diff] [blame] | 80 | return @odbc_connect($this->dsn, $this->username, $this->password); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | // -------------------------------------------------------------------- |
| 84 | |
| 85 | /** |
| 86 | * Persistent database connection |
| 87 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 90 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
Andrey Andreev | 46f6dbc | 2012-02-13 01:39:53 +0200 | [diff] [blame] | 92 | return @odbc_pconnect($this->dsn, $this->username, $this->password); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | // -------------------------------------------------------------------- |
| 96 | |
| 97 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 98 | * Reconnect |
| 99 | * |
| 100 | * Keep / reestablish the db connection if no queries have been |
| 101 | * sent for a length of time exceeding the server's idle timeout |
| 102 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 103 | * @return void |
| 104 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 105 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 106 | { |
| 107 | // not implemented in odbc |
| 108 | } |
| 109 | |
| 110 | // -------------------------------------------------------------------- |
| 111 | |
| 112 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | * Select the database |
| 114 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 117 | public function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | { |
| 119 | // Not needed for ODBC |
| 120 | return TRUE; |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * Set client character set |
| 127 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | * @param string |
| 129 | * @param string |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 130 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 132 | public function db_set_charset($charset, $collation) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
| 134 | // @todo - add support if needed |
| 135 | return TRUE; |
| 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 | /** |
| 141 | * Version number query string |
| 142 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | * @return string |
| 144 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 145 | protected function _version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 147 | return 'SELECT version() AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // -------------------------------------------------------------------- |
| 151 | |
| 152 | /** |
| 153 | * Execute the query |
| 154 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | * @param string an SQL query |
| 156 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 158 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 160 | return @odbc_exec($this->conn_id, $this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | // -------------------------------------------------------------------- |
| 164 | |
| 165 | /** |
| 166 | * Prep the query |
| 167 | * |
| 168 | * If needed, each database adapter can prep the query string |
| 169 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | * @param string an SQL query |
| 171 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 173 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | { |
| 175 | return $sql; |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Begin Transaction |
| 182 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | * @return bool |
| 184 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 185 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 188 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | { |
| 190 | return TRUE; |
| 191 | } |
| 192 | |
| 193 | // Reset the transaction failure flag. |
| 194 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 195 | // even if the queries produce a successful result. |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 196 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | |
| 198 | return odbc_autocommit($this->conn_id, FALSE); |
| 199 | } |
| 200 | |
| 201 | // -------------------------------------------------------------------- |
| 202 | |
| 203 | /** |
| 204 | * Commit Transaction |
| 205 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | * @return bool |
| 207 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 208 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 211 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | { |
| 213 | return TRUE; |
| 214 | } |
| 215 | |
| 216 | $ret = odbc_commit($this->conn_id); |
| 217 | odbc_autocommit($this->conn_id, TRUE); |
| 218 | return $ret; |
| 219 | } |
| 220 | |
| 221 | // -------------------------------------------------------------------- |
| 222 | |
| 223 | /** |
| 224 | * Rollback Transaction |
| 225 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | * @return bool |
| 227 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 228 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 231 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | { |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | $ret = odbc_rollback($this->conn_id); |
| 237 | odbc_autocommit($this->conn_id, TRUE); |
| 238 | return $ret; |
| 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * Escape String |
| 245 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 247 | * @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] | 248 | * @return string |
| 249 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 250 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 252 | if (is_array($str)) |
| 253 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 254 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 256 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | return $str; |
| 260 | } |
| 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | // ODBC doesn't require escaping |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 263 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 265 | // escape LIKE condition wildcards |
| 266 | if ($like === TRUE) |
| 267 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 268 | return str_replace(array('%', '_', $this->_like_escape_chr), |
| 269 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 270 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 271 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 273 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | // -------------------------------------------------------------------- |
| 277 | |
| 278 | /** |
| 279 | * Affected Rows |
| 280 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 281 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 283 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | { |
| 285 | return @odbc_num_rows($this->conn_id); |
| 286 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | // -------------------------------------------------------------------- |
| 289 | |
| 290 | /** |
| 291 | * Insert ID |
| 292 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 293 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 295 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
| 297 | return @odbc_insert_id($this->conn_id); |
| 298 | } |
| 299 | |
| 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
| 303 | * "Count All" query |
| 304 | * |
| 305 | * Generates a platform-specific query string that counts all records in |
| 306 | * the specified database |
| 307 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | * @param string |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 309 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 311 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | { |
| 313 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 314 | { |
| 315 | return 0; |
| 316 | } |
| 317 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 318 | $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 319 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 321 | { |
| 322 | return 0; |
| 323 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 325 | $query = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 326 | $this->_reset_select(); |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 327 | return (int) $query->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * Show table query |
| 334 | * |
| 335 | * Generates a platform-specific query string so that the table names can be fetched |
| 336 | * |
| 337 | * @access private |
| 338 | * @param boolean |
| 339 | * @return string |
| 340 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 341 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 343 | $sql = 'SHOW TABLES FROM `'.$this->database.'`'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 345 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 347 | //$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] | 348 | return FALSE; // not currently supported |
| 349 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | return $sql; |
| 352 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | // -------------------------------------------------------------------- |
| 355 | |
| 356 | /** |
| 357 | * Show column query |
| 358 | * |
| 359 | * Generates a platform-specific query string so that the column names can be fetched |
| 360 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | * @param string the table name |
| 362 | * @return string |
| 363 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 364 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 366 | return 'SHOW COLUMNS FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // -------------------------------------------------------------------- |
| 370 | |
| 371 | /** |
| 372 | * Field data query |
| 373 | * |
| 374 | * Generates a platform-specific query so that the column data can be retrieved |
| 375 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | * @param string the table name |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 377 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 379 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 381 | return 'SELECT TOP 1 FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
| 385 | |
| 386 | /** |
| 387 | * The error message string |
| 388 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | * @return string |
| 390 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 391 | protected function _error_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | { |
| 393 | return odbc_errormsg($this->conn_id); |
| 394 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * The error message number |
| 400 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 401 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 403 | protected function _error_number() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | { |
| 405 | return odbc_error($this->conn_id); |
| 406 | } |
| 407 | |
| 408 | // -------------------------------------------------------------------- |
| 409 | |
| 410 | /** |
| 411 | * Escape the SQL Identifiers |
| 412 | * |
| 413 | * This function escapes column and table names |
| 414 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | * @param string |
| 416 | * @return string |
| 417 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 418 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
| 420 | if ($this->_escape_char == '') |
| 421 | { |
| 422 | return $item; |
| 423 | } |
| 424 | |
| 425 | foreach ($this->_reserved_identifiers as $id) |
| 426 | { |
| 427 | if (strpos($item, '.'.$id) !== FALSE) |
| 428 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 429 | $item = str_replace('.', $this->_escape_char.'.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | // remove duplicates if the user already included the escape |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 432 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | if (strpos($item, '.') !== FALSE) |
| 437 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 438 | $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | // remove duplicates if the user already included the escape |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 442 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | // -------------------------------------------------------------------- |
| 446 | |
| 447 | /** |
| 448 | * From Tables |
| 449 | * |
| 450 | * This function implicitly groups FROM tables so there is no confusion |
| 451 | * about operator precedence in harmony with SQL standards |
| 452 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 453 | * @param string the table name |
| 454 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 456 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | { |
| 458 | if ( ! is_array($tables)) |
| 459 | { |
| 460 | $tables = array($tables); |
| 461 | } |
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 | return '('.implode(', ', $tables).')'; |
| 464 | } |
| 465 | |
| 466 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | /** |
| 469 | * Insert statement |
| 470 | * |
| 471 | * Generates a platform-specific insert string from the supplied data |
| 472 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | * @param string the table name |
| 474 | * @param array the insert keys |
| 475 | * @param array the insert values |
| 476 | * @return string |
| 477 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 478 | protected function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 480 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | // -------------------------------------------------------------------- |
| 484 | |
| 485 | /** |
| 486 | * Update statement |
| 487 | * |
| 488 | * Generates a platform-specific update string from the supplied data |
| 489 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | * @param string the table name |
| 491 | * @param array the update data |
| 492 | * @param array the where clause |
| 493 | * @param array the orderby clause |
| 494 | * @param array the limit clause |
| 495 | * @return string |
| 496 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 497 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 499 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 501 | $valstr[] = $key.' = '.$val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 504 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
| 505 | .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') |
| 506 | .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') |
| 507 | .( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | // -------------------------------------------------------------------- |
| 512 | |
| 513 | /** |
| 514 | * Truncate statement |
| 515 | * |
| 516 | * Generates a platform-specific truncate string from the supplied data |
| 517 | * If the database does not support the truncate() command |
| 518 | * This function maps to "DELETE FROM table" |
| 519 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | * @param string the table name |
| 521 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 522 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 523 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | { |
| 525 | return $this->_delete($table); |
| 526 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 527 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | // -------------------------------------------------------------------- |
| 529 | |
| 530 | /** |
| 531 | * Delete statement |
| 532 | * |
| 533 | * Generates a platform-specific delete string from the supplied data |
| 534 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | * @param string the table name |
| 536 | * @param array the where clause |
| 537 | * @param string the limit clause |
| 538 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 539 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 540 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | { |
| 542 | $conditions = ''; |
| 543 | |
| 544 | if (count($where) > 0 OR count($like) > 0) |
| 545 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 546 | $conditions = "\nWHERE ".implode("\n", $this->ar_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | |
| 548 | if (count($where) > 0 && count($like) > 0) |
| 549 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 550 | $conditions .= ' AND '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | } |
| 552 | $conditions .= implode("\n", $like); |
| 553 | } |
| 554 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 555 | return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // -------------------------------------------------------------------- |
| 559 | |
| 560 | /** |
| 561 | * Limit string |
| 562 | * |
| 563 | * Generates a platform-specific LIMIT clause |
| 564 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | * @param string the sql query string |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 566 | * @param int the number of rows to limit the query to |
| 567 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | * @return string |
| 569 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 570 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 572 | return $sql.($offset == 0 ? '' : $offset.', ').$limit; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // -------------------------------------------------------------------- |
| 576 | |
| 577 | /** |
| 578 | * Close DB Connection |
| 579 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | * @param resource |
| 581 | * @return void |
| 582 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 583 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | { |
| 585 | @odbc_close($conn_id); |
| 586 | } |
| 587 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | /* End of file odbc_driver.php */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 591 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |