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