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 | * |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame^] | 290 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | */ |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame^] | 292 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame^] | 294 | 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] | 295 | } |
| 296 | |
| 297 | // -------------------------------------------------------------------- |
| 298 | |
| 299 | /** |
| 300 | * "Count All" query |
| 301 | * |
| 302 | * Generates a platform-specific query string that counts all records in |
| 303 | * the specified database |
| 304 | * |
| 305 | * @access public |
| 306 | * @param string |
| 307 | * @return string |
| 308 | */ |
| 309 | function count_all($table = '') |
| 310 | { |
| 311 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 312 | { |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 319 | { |
| 320 | return 0; |
| 321 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | |
| 323 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 324 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 325 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // -------------------------------------------------------------------- |
| 329 | |
| 330 | /** |
| 331 | * Show table query |
| 332 | * |
| 333 | * Generates a platform-specific query string so that the table names can be fetched |
| 334 | * |
| 335 | * @access private |
| 336 | * @param boolean |
| 337 | * @return string |
| 338 | */ |
| 339 | function _list_tables($prefix_limit = FALSE) |
| 340 | { |
| 341 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 342 | |
| 343 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 344 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 345 | //$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] | 346 | return FALSE; // not currently supported |
| 347 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 348 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | return $sql; |
| 350 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | // -------------------------------------------------------------------- |
| 353 | |
| 354 | /** |
| 355 | * Show column query |
| 356 | * |
| 357 | * Generates a platform-specific query string so that the column names can be fetched |
| 358 | * |
| 359 | * @access public |
| 360 | * @param string the table name |
| 361 | * @return string |
| 362 | */ |
| 363 | function _list_columns($table = '') |
| 364 | { |
| 365 | return "SHOW COLUMNS FROM ".$table; |
| 366 | } |
| 367 | |
| 368 | // -------------------------------------------------------------------- |
| 369 | |
| 370 | /** |
| 371 | * Field data query |
| 372 | * |
| 373 | * Generates a platform-specific query so that the column data can be retrieved |
| 374 | * |
| 375 | * @access public |
| 376 | * @param string the table name |
| 377 | * @return object |
| 378 | */ |
| 379 | function _field_data($table) |
| 380 | { |
| 381 | return "SELECT TOP 1 FROM ".$table; |
| 382 | } |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
| 385 | |
| 386 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 387 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 389 | * Returns an array containing code and message of the last |
| 390 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 392 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 394 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 396 | 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] | 397 | } |
| 398 | |
| 399 | // -------------------------------------------------------------------- |
| 400 | |
| 401 | /** |
| 402 | * Escape the SQL Identifiers |
| 403 | * |
| 404 | * This function escapes column and table names |
| 405 | * |
| 406 | * @access private |
| 407 | * @param string |
| 408 | * @return string |
| 409 | */ |
| 410 | function _escape_identifiers($item) |
| 411 | { |
| 412 | if ($this->_escape_char == '') |
| 413 | { |
| 414 | return $item; |
| 415 | } |
| 416 | |
| 417 | foreach ($this->_reserved_identifiers as $id) |
| 418 | { |
| 419 | if (strpos($item, '.'.$id) !== FALSE) |
| 420 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 422 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | // remove duplicates if the user already included the escape |
| 424 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 425 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | if (strpos($item, '.') !== FALSE) |
| 429 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | $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] | 431 | } |
| 432 | else |
| 433 | { |
| 434 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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); |
| 439 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | // -------------------------------------------------------------------- |
| 442 | |
| 443 | /** |
| 444 | * From Tables |
| 445 | * |
| 446 | * This function implicitly groups FROM tables so there is no confusion |
| 447 | * about operator precedence in harmony with SQL standards |
| 448 | * |
| 449 | * @access public |
| 450 | * @param type |
| 451 | * @return type |
| 452 | */ |
| 453 | function _from_tables($tables) |
| 454 | { |
| 455 | if ( ! is_array($tables)) |
| 456 | { |
| 457 | $tables = array($tables); |
| 458 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | return '('.implode(', ', $tables).')'; |
| 461 | } |
| 462 | |
| 463 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 464 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | /** |
| 466 | * Insert statement |
| 467 | * |
| 468 | * Generates a platform-specific insert string from the supplied data |
| 469 | * |
| 470 | * @access public |
| 471 | * @param string the table name |
| 472 | * @param array the insert keys |
| 473 | * @param array the insert values |
| 474 | * @return string |
| 475 | */ |
| 476 | function _insert($table, $keys, $values) |
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 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 479 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | // -------------------------------------------------------------------- |
| 482 | |
| 483 | /** |
| 484 | * Update statement |
| 485 | * |
| 486 | * Generates a platform-specific update string from the supplied data |
| 487 | * |
| 488 | * @access public |
| 489 | * @param string the table name |
| 490 | * @param array the update data |
| 491 | * @param array the where clause |
| 492 | * @param array the orderby clause |
| 493 | * @param array the limit clause |
| 494 | * @return string |
| 495 | */ |
| 496 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 497 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 498 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | { |
| 500 | $valstr[] = $key." = ".$val; |
| 501 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 502 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 504 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 508 | |
| 509 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 510 | |
| 511 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | return $sql; |
| 514 | } |
| 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 | // -------------------------------------------------------------------- |
| 518 | |
| 519 | /** |
| 520 | * Truncate statement |
| 521 | * |
| 522 | * Generates a platform-specific truncate string from the supplied data |
| 523 | * If the database does not support the truncate() command |
| 524 | * This function maps to "DELETE FROM table" |
| 525 | * |
| 526 | * @access public |
| 527 | * @param string the table name |
| 528 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 529 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 530 | function _truncate($table) |
| 531 | { |
| 532 | return $this->_delete($table); |
| 533 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 534 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | // -------------------------------------------------------------------- |
| 536 | |
| 537 | /** |
| 538 | * Delete statement |
| 539 | * |
| 540 | * Generates a platform-specific delete string from the supplied data |
| 541 | * |
| 542 | * @access public |
| 543 | * @param string the table name |
| 544 | * @param array the where clause |
| 545 | * @param string the limit clause |
| 546 | * @return string |
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 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 549 | { |
| 550 | $conditions = ''; |
| 551 | |
| 552 | if (count($where) > 0 OR count($like) > 0) |
| 553 | { |
| 554 | $conditions = "\nWHERE "; |
| 555 | $conditions .= implode("\n", $this->ar_where); |
| 556 | |
| 557 | if (count($where) > 0 && count($like) > 0) |
| 558 | { |
| 559 | $conditions .= " AND "; |
| 560 | } |
| 561 | $conditions .= implode("\n", $like); |
| 562 | } |
| 563 | |
| 564 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | return "DELETE FROM ".$table.$conditions.$limit; |
| 567 | } |
| 568 | |
| 569 | // -------------------------------------------------------------------- |
| 570 | |
| 571 | /** |
| 572 | * Limit string |
| 573 | * |
| 574 | * Generates a platform-specific LIMIT clause |
| 575 | * |
| 576 | * @access public |
| 577 | * @param string the sql query string |
| 578 | * @param integer the number of rows to limit the query to |
| 579 | * @param integer the offset value |
| 580 | * @return string |
| 581 | */ |
| 582 | function _limit($sql, $limit, $offset) |
| 583 | { |
| 584 | // Does ODBC doesn't use the LIMIT clause? |
| 585 | return $sql; |
| 586 | } |
| 587 | |
| 588 | // -------------------------------------------------------------------- |
| 589 | |
| 590 | /** |
| 591 | * Close DB Connection |
| 592 | * |
| 593 | * @access public |
| 594 | * @param resource |
| 595 | * @return void |
| 596 | */ |
| 597 | function _close($conn_id) |
| 598 | { |
| 599 | @odbc_close($conn_id); |
| 600 | } |
| 601 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 602 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
| 606 | |
| 607 | /* End of file odbc_driver.php */ |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 608 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |