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