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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 | /** |
| 127 | * Set client character set |
| 128 | * |
| 129 | * @access public |
| 130 | * @param string |
| 131 | * @param string |
| 132 | * @return resource |
| 133 | */ |
| 134 | function db_set_charset($charset, $collation) |
| 135 | { |
| 136 | // @todo - add support if needed |
| 137 | return TRUE; |
| 138 | } |
| 139 | |
| 140 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | /** |
| 143 | * Version number query string |
| 144 | * |
| 145 | * @access public |
| 146 | * @return string |
| 147 | */ |
| 148 | function _version() |
| 149 | { |
| 150 | return "SELECT version() AS ver"; |
| 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | /** |
| 156 | * Execute the query |
| 157 | * |
| 158 | * @access private called by the base class |
| 159 | * @param string an SQL query |
| 160 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | function _execute($sql) |
| 163 | { |
| 164 | $sql = $this->_prep_query($sql); |
| 165 | return @odbc_exec($this->conn_id, $sql); |
| 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Prep the query |
| 172 | * |
| 173 | * If needed, each database adapter can prep the query string |
| 174 | * |
| 175 | * @access private called by execute() |
| 176 | * @param string an SQL query |
| 177 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | function _prep_query($sql) |
| 180 | { |
| 181 | return $sql; |
| 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Begin Transaction |
| 188 | * |
| 189 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | * @return bool |
| 191 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | function trans_begin($test_mode = FALSE) |
| 193 | { |
| 194 | if ( ! $this->trans_enabled) |
| 195 | { |
| 196 | return TRUE; |
| 197 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 200 | if ($this->_trans_depth > 0) |
| 201 | { |
| 202 | return TRUE; |
| 203 | } |
| 204 | |
| 205 | // Reset the transaction failure flag. |
| 206 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 207 | // even if the queries produce a successful result. |
| 208 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 209 | |
| 210 | return odbc_autocommit($this->conn_id, FALSE); |
| 211 | } |
| 212 | |
| 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Commit Transaction |
| 217 | * |
| 218 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 219 | * @return bool |
| 220 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | function trans_commit() |
| 222 | { |
| 223 | if ( ! $this->trans_enabled) |
| 224 | { |
| 225 | return TRUE; |
| 226 | } |
| 227 | |
| 228 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 229 | if ($this->_trans_depth > 0) |
| 230 | { |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
| 234 | $ret = odbc_commit($this->conn_id); |
| 235 | odbc_autocommit($this->conn_id, TRUE); |
| 236 | return $ret; |
| 237 | } |
| 238 | |
| 239 | // -------------------------------------------------------------------- |
| 240 | |
| 241 | /** |
| 242 | * Rollback Transaction |
| 243 | * |
| 244 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | * @return bool |
| 246 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | function trans_rollback() |
| 248 | { |
| 249 | if ( ! $this->trans_enabled) |
| 250 | { |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
| 254 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 255 | if ($this->_trans_depth > 0) |
| 256 | { |
| 257 | return TRUE; |
| 258 | } |
| 259 | |
| 260 | $ret = odbc_rollback($this->conn_id); |
| 261 | odbc_autocommit($this->conn_id, TRUE); |
| 262 | return $ret; |
| 263 | } |
| 264 | |
| 265 | // -------------------------------------------------------------------- |
| 266 | |
| 267 | /** |
| 268 | * Escape String |
| 269 | * |
| 270 | * @access public |
| 271 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 272 | * @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] | 273 | * @return string |
| 274 | */ |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 275 | function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 277 | if (is_array($str)) |
| 278 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 279 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 281 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return $str; |
| 285 | } |
| 286 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | // ODBC doesn't require escaping |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 288 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 290 | // escape LIKE condition wildcards |
| 291 | if ($like === TRUE) |
| 292 | { |
| 293 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 294 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 295 | $str); |
| 296 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 297 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 298 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 300 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | // -------------------------------------------------------------------- |
| 302 | |
| 303 | /** |
| 304 | * Affected Rows |
| 305 | * |
| 306 | * @access public |
| 307 | * @return integer |
| 308 | */ |
| 309 | function affected_rows() |
| 310 | { |
| 311 | return @odbc_num_rows($this->conn_id); |
| 312 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | // -------------------------------------------------------------------- |
| 315 | |
| 316 | /** |
| 317 | * Insert ID |
| 318 | * |
| 319 | * @access public |
| 320 | * @return integer |
| 321 | */ |
| 322 | function insert_id() |
| 323 | { |
| 324 | return @odbc_insert_id($this->conn_id); |
| 325 | } |
| 326 | |
| 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | /** |
| 330 | * "Count All" query |
| 331 | * |
| 332 | * Generates a platform-specific query string that counts all records in |
| 333 | * the specified database |
| 334 | * |
| 335 | * @access public |
| 336 | * @param string |
| 337 | * @return string |
| 338 | */ |
| 339 | function count_all($table = '') |
| 340 | { |
| 341 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 342 | { |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 349 | { |
| 350 | return 0; |
| 351 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | |
| 353 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 354 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 355 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Show table query |
| 362 | * |
| 363 | * Generates a platform-specific query string so that the table names can be fetched |
| 364 | * |
| 365 | * @access private |
| 366 | * @param boolean |
| 367 | * @return string |
| 368 | */ |
| 369 | function _list_tables($prefix_limit = FALSE) |
| 370 | { |
| 371 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 372 | |
| 373 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 374 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 375 | //$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] | 376 | return FALSE; // not currently supported |
| 377 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | return $sql; |
| 380 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | // -------------------------------------------------------------------- |
| 383 | |
| 384 | /** |
| 385 | * Show column query |
| 386 | * |
| 387 | * Generates a platform-specific query string so that the column names can be fetched |
| 388 | * |
| 389 | * @access public |
| 390 | * @param string the table name |
| 391 | * @return string |
| 392 | */ |
| 393 | function _list_columns($table = '') |
| 394 | { |
| 395 | return "SHOW COLUMNS FROM ".$table; |
| 396 | } |
| 397 | |
| 398 | // -------------------------------------------------------------------- |
| 399 | |
| 400 | /** |
| 401 | * Field data query |
| 402 | * |
| 403 | * Generates a platform-specific query so that the column data can be retrieved |
| 404 | * |
| 405 | * @access public |
| 406 | * @param string the table name |
| 407 | * @return object |
| 408 | */ |
| 409 | function _field_data($table) |
| 410 | { |
| 411 | return "SELECT TOP 1 FROM ".$table; |
| 412 | } |
| 413 | |
| 414 | // -------------------------------------------------------------------- |
| 415 | |
| 416 | /** |
| 417 | * The error message string |
| 418 | * |
| 419 | * @access private |
| 420 | * @return string |
| 421 | */ |
| 422 | function _error_message() |
| 423 | { |
| 424 | return odbc_errormsg($this->conn_id); |
| 425 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
| 430 | * The error message number |
| 431 | * |
| 432 | * @access private |
| 433 | * @return integer |
| 434 | */ |
| 435 | function _error_number() |
| 436 | { |
| 437 | return odbc_error($this->conn_id); |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
| 441 | |
| 442 | /** |
| 443 | * Escape the SQL Identifiers |
| 444 | * |
| 445 | * This function escapes column and table names |
| 446 | * |
| 447 | * @access private |
| 448 | * @param string |
| 449 | * @return string |
| 450 | */ |
| 451 | function _escape_identifiers($item) |
| 452 | { |
| 453 | if ($this->_escape_char == '') |
| 454 | { |
| 455 | return $item; |
| 456 | } |
| 457 | |
| 458 | foreach ($this->_reserved_identifiers as $id) |
| 459 | { |
| 460 | if (strpos($item, '.'.$id) !== FALSE) |
| 461 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 463 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | // remove duplicates if the user already included the escape |
| 465 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 468 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | if (strpos($item, '.') !== FALSE) |
| 470 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | $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] | 472 | } |
| 473 | else |
| 474 | { |
| 475 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 476 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | // remove duplicates if the user already included the escape |
| 479 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 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 | // -------------------------------------------------------------------- |
| 483 | |
| 484 | /** |
| 485 | * From Tables |
| 486 | * |
| 487 | * This function implicitly groups FROM tables so there is no confusion |
| 488 | * about operator precedence in harmony with SQL standards |
| 489 | * |
| 490 | * @access public |
| 491 | * @param type |
| 492 | * @return type |
| 493 | */ |
| 494 | function _from_tables($tables) |
| 495 | { |
| 496 | if ( ! is_array($tables)) |
| 497 | { |
| 498 | $tables = array($tables); |
| 499 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | return '('.implode(', ', $tables).')'; |
| 502 | } |
| 503 | |
| 504 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 505 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | /** |
| 507 | * Insert statement |
| 508 | * |
| 509 | * Generates a platform-specific insert string from the supplied data |
| 510 | * |
| 511 | * @access public |
| 512 | * @param string the table name |
| 513 | * @param array the insert keys |
| 514 | * @param array the insert values |
| 515 | * @return string |
| 516 | */ |
| 517 | function _insert($table, $keys, $values) |
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 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 520 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | // -------------------------------------------------------------------- |
| 523 | |
| 524 | /** |
| 525 | * Update statement |
| 526 | * |
| 527 | * Generates a platform-specific update string from the supplied data |
| 528 | * |
| 529 | * @access public |
| 530 | * @param string the table name |
| 531 | * @param array the update data |
| 532 | * @param array the where clause |
| 533 | * @param array the orderby clause |
| 534 | * @param array the limit clause |
| 535 | * @return string |
| 536 | */ |
| 537 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 538 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 539 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | { |
| 541 | $valstr[] = $key." = ".$val; |
| 542 | } |
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 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 545 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 547 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 549 | |
| 550 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 551 | |
| 552 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | return $sql; |
| 555 | } |
| 556 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 557 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | // -------------------------------------------------------------------- |
| 559 | |
| 560 | /** |
| 561 | * Truncate statement |
| 562 | * |
| 563 | * Generates a platform-specific truncate string from the supplied data |
| 564 | * If the database does not support the truncate() command |
| 565 | * This function maps to "DELETE FROM table" |
| 566 | * |
| 567 | * @access public |
| 568 | * @param string the table name |
| 569 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 570 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | function _truncate($table) |
| 572 | { |
| 573 | return $this->_delete($table); |
| 574 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 575 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | // -------------------------------------------------------------------- |
| 577 | |
| 578 | /** |
| 579 | * Delete statement |
| 580 | * |
| 581 | * Generates a platform-specific delete string from the supplied data |
| 582 | * |
| 583 | * @access public |
| 584 | * @param string the table name |
| 585 | * @param array the where clause |
| 586 | * @param string the limit clause |
| 587 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 588 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 590 | { |
| 591 | $conditions = ''; |
| 592 | |
| 593 | if (count($where) > 0 OR count($like) > 0) |
| 594 | { |
| 595 | $conditions = "\nWHERE "; |
| 596 | $conditions .= implode("\n", $this->ar_where); |
| 597 | |
| 598 | if (count($where) > 0 && count($like) > 0) |
| 599 | { |
| 600 | $conditions .= " AND "; |
| 601 | } |
| 602 | $conditions .= implode("\n", $like); |
| 603 | } |
| 604 | |
| 605 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 606 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | return "DELETE FROM ".$table.$conditions.$limit; |
| 608 | } |
| 609 | |
| 610 | // -------------------------------------------------------------------- |
| 611 | |
| 612 | /** |
| 613 | * Limit string |
| 614 | * |
| 615 | * Generates a platform-specific LIMIT clause |
| 616 | * |
| 617 | * @access public |
| 618 | * @param string the sql query string |
| 619 | * @param integer the number of rows to limit the query to |
| 620 | * @param integer the offset value |
| 621 | * @return string |
| 622 | */ |
| 623 | function _limit($sql, $limit, $offset) |
| 624 | { |
| 625 | // Does ODBC doesn't use the LIMIT clause? |
| 626 | return $sql; |
| 627 | } |
| 628 | |
| 629 | // -------------------------------------------------------------------- |
| 630 | |
| 631 | /** |
| 632 | * Close DB Connection |
| 633 | * |
| 634 | * @access public |
| 635 | * @param resource |
| 636 | * @return void |
| 637 | */ |
| 638 | function _close($conn_id) |
| 639 | { |
| 640 | @odbc_close($conn_id); |
| 641 | } |
| 642 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 643 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
| 647 | |
| 648 | /* End of file odbc_driver.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 649 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |