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