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 |
Timothy Warren | 9e560a4 | 2012-03-19 19:06:46 -0400 | [diff] [blame] | 55 | * used for the count_all() and count_all_results() functions. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Timothy Warren | 9e560a4 | 2012-03-19 19:06:46 -0400 | [diff] [blame] | 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 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | return @odbc_exec($this->conn_id, $sql); |
| 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | * Begin Transaction |
| 135 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | * @return bool |
| 137 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 138 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
| 140 | if ( ! $this->trans_enabled) |
| 141 | { |
| 142 | return TRUE; |
| 143 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 146 | if ($this->_trans_depth > 0) |
| 147 | { |
| 148 | return TRUE; |
| 149 | } |
| 150 | |
| 151 | // Reset the transaction failure flag. |
| 152 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 153 | // even if the queries produce a successful result. |
| 154 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 155 | |
| 156 | return odbc_autocommit($this->conn_id, FALSE); |
| 157 | } |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
| 160 | |
| 161 | /** |
| 162 | * Commit Transaction |
| 163 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | * @return bool |
| 165 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 166 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | { |
| 168 | if ( ! $this->trans_enabled) |
| 169 | { |
| 170 | return TRUE; |
| 171 | } |
| 172 | |
| 173 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 174 | if ($this->_trans_depth > 0) |
| 175 | { |
| 176 | return TRUE; |
| 177 | } |
| 178 | |
| 179 | $ret = odbc_commit($this->conn_id); |
| 180 | odbc_autocommit($this->conn_id, TRUE); |
| 181 | return $ret; |
| 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Rollback Transaction |
| 188 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | * @return bool |
| 190 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 191 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | { |
| 193 | if ( ! $this->trans_enabled) |
| 194 | { |
| 195 | return TRUE; |
| 196 | } |
| 197 | |
| 198 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 199 | if ($this->_trans_depth > 0) |
| 200 | { |
| 201 | return TRUE; |
| 202 | } |
| 203 | |
| 204 | $ret = odbc_rollback($this->conn_id); |
| 205 | odbc_autocommit($this->conn_id, TRUE); |
| 206 | return $ret; |
| 207 | } |
| 208 | |
| 209 | // -------------------------------------------------------------------- |
| 210 | |
| 211 | /** |
| 212 | * Escape String |
| 213 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 215 | * @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] | 216 | * @return string |
| 217 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 218 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 220 | if (is_array($str)) |
| 221 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 222 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 224 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return $str; |
| 228 | } |
| 229 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | // ODBC doesn't require escaping |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 231 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 233 | // escape LIKE condition wildcards |
| 234 | if ($like === TRUE) |
| 235 | { |
Andrey Andreev | 6ea625e | 2012-03-20 14:46:27 +0200 | [diff] [blame] | 236 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
Andrey Andreev | f1bda68 | 2012-03-20 14:35:06 +0200 | [diff] [blame] | 237 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 238 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 239 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 241 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 243 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Affected Rows |
| 248 | * |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 249 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 251 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | { |
| 253 | return @odbc_num_rows($this->conn_id); |
| 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Insert ID |
| 260 | * |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 261 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | */ |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 263 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 265 | 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] | 266 | } |
| 267 | |
| 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * "Count All" query |
| 272 | * |
| 273 | * Generates a platform-specific query string that counts all records in |
| 274 | * the specified database |
| 275 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | * @param string |
| 277 | * @return string |
| 278 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 279 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | { |
| 281 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 282 | { |
| 283 | return 0; |
| 284 | } |
| 285 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 286 | $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] | 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 289 | { |
| 290 | return 0; |
| 291 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | |
| 293 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 294 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 295 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // -------------------------------------------------------------------- |
| 299 | |
| 300 | /** |
| 301 | * Show table query |
| 302 | * |
| 303 | * Generates a platform-specific query string so that the table names can be fetched |
| 304 | * |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 305 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | * @return string |
| 307 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 308 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | { |
| 310 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 311 | |
| 312 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 313 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 314 | //$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] | 315 | return FALSE; // not currently supported |
| 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | return $sql; |
| 319 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | // -------------------------------------------------------------------- |
| 322 | |
| 323 | /** |
| 324 | * Show column query |
| 325 | * |
| 326 | * Generates a platform-specific query string so that the column names can be fetched |
| 327 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | * @param string the table name |
| 329 | * @return string |
| 330 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 331 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | { |
| 333 | return "SHOW COLUMNS FROM ".$table; |
| 334 | } |
| 335 | |
| 336 | // -------------------------------------------------------------------- |
| 337 | |
| 338 | /** |
| 339 | * Field data query |
| 340 | * |
| 341 | * Generates a platform-specific query so that the column data can be retrieved |
| 342 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | * @param string the table name |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 344 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 346 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | { |
| 348 | return "SELECT TOP 1 FROM ".$table; |
| 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 354 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 356 | * Returns an array containing code and message of the last |
| 357 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 359 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 361 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 363 | 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] | 364 | } |
| 365 | |
| 366 | // -------------------------------------------------------------------- |
| 367 | |
| 368 | /** |
| 369 | * Escape the SQL Identifiers |
| 370 | * |
Timothy Warren | 9e560a4 | 2012-03-19 19:06:46 -0400 | [diff] [blame] | 371 | * This function escapes column and table names |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | * @param string |
| 374 | * @return string |
| 375 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 376 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | { |
| 378 | if ($this->_escape_char == '') |
| 379 | { |
| 380 | return $item; |
| 381 | } |
| 382 | |
| 383 | foreach ($this->_reserved_identifiers as $id) |
| 384 | { |
| 385 | if (strpos($item, '.'.$id) !== FALSE) |
| 386 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 388 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | // remove duplicates if the user already included the escape |
| 390 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | if (strpos($item, '.') !== FALSE) |
| 395 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 396 | $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] | 397 | } |
| 398 | else |
| 399 | { |
| 400 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 401 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | // remove duplicates if the user already included the escape |
| 404 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 405 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | // -------------------------------------------------------------------- |
| 408 | |
| 409 | /** |
| 410 | * From Tables |
| 411 | * |
Timothy Warren | 9e560a4 | 2012-03-19 19:06:46 -0400 | [diff] [blame] | 412 | * This function implicitly groups FROM tables so there is no confusion |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | * about operator precedence in harmony with SQL standards |
| 414 | * |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 415 | * @param array |
| 416 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 418 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
| 420 | if ( ! is_array($tables)) |
| 421 | { |
| 422 | $tables = array($tables); |
| 423 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | return '('.implode(', ', $tables).')'; |
| 426 | } |
| 427 | |
| 428 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | /** |
| 431 | * Insert statement |
| 432 | * |
| 433 | * Generates a platform-specific insert string from the supplied data |
| 434 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | * @param string the table name |
| 436 | * @param array the insert keys |
| 437 | * @param array the insert values |
| 438 | * @return string |
| 439 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 440 | protected function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 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 | * Update statement |
| 449 | * |
| 450 | * Generates a platform-specific update string from the supplied data |
| 451 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | * @param string the table name |
| 453 | * @param array the update data |
| 454 | * @param array the where clause |
| 455 | * @param array the orderby clause |
| 456 | * @param array the limit clause |
| 457 | * @return string |
| 458 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 459 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 461 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
| 463 | $valstr[] = $key." = ".$val; |
| 464 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
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 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 471 | |
| 472 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 473 | |
| 474 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 475 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | return $sql; |
| 477 | } |
| 478 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | // -------------------------------------------------------------------- |
| 481 | |
| 482 | /** |
| 483 | * Truncate statement |
| 484 | * |
| 485 | * Generates a platform-specific truncate string from the supplied data |
| 486 | * If the database does not support the truncate() command |
Timothy Warren | 9e560a4 | 2012-03-19 19:06:46 -0400 | [diff] [blame] | 487 | * This function maps to "DELETE FROM table" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | * @param string the table name |
| 490 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 492 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | { |
| 494 | return $this->_delete($table); |
| 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 | // -------------------------------------------------------------------- |
| 498 | |
| 499 | /** |
| 500 | * Delete statement |
| 501 | * |
| 502 | * Generates a platform-specific delete string from the supplied data |
| 503 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | * @param string the table name |
| 505 | * @param array the where clause |
| 506 | * @param string the limit clause |
| 507 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 509 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | { |
| 511 | $conditions = ''; |
| 512 | |
| 513 | if (count($where) > 0 OR count($like) > 0) |
| 514 | { |
| 515 | $conditions = "\nWHERE "; |
| 516 | $conditions .= implode("\n", $this->ar_where); |
| 517 | |
| 518 | if (count($where) > 0 && count($like) > 0) |
| 519 | { |
| 520 | $conditions .= " AND "; |
| 521 | } |
| 522 | $conditions .= implode("\n", $like); |
| 523 | } |
| 524 | |
| 525 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 526 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | return "DELETE FROM ".$table.$conditions.$limit; |
| 528 | } |
| 529 | |
| 530 | // -------------------------------------------------------------------- |
| 531 | |
| 532 | /** |
| 533 | * Limit string |
| 534 | * |
| 535 | * Generates a platform-specific LIMIT clause |
| 536 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | * @param string the sql query string |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 538 | * @param int the number of rows to limit the query to |
| 539 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | * @return string |
| 541 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 542 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | { |
| 544 | // Does ODBC doesn't use the LIMIT clause? |
| 545 | return $sql; |
| 546 | } |
| 547 | |
| 548 | // -------------------------------------------------------------------- |
| 549 | |
| 550 | /** |
| 551 | * Close DB Connection |
| 552 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | * @param resource |
| 554 | * @return void |
| 555 | */ |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 556 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | { |
| 558 | @odbc_close($conn_id); |
| 559 | } |
Timothy Warren | 9e560a4 | 2012-03-19 19:06:46 -0400 | [diff] [blame] | 560 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | /* End of file odbc_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame^] | 564 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |