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