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