1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
19
* ODBC Database Adapter Class
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.
25
* @package CodeIgniter
28
* @author ExpressionEngine Dev Team
29
* @link http://codeigniter.com/user_guide/database/
31
class CI_DB_odbc_driver extends CI_DB {
33
var $dbdriver = 'odbc';
35
// the character used to excape - not necessary for ODBC
36
var $_escape_char = '';
38
// clause and character used for LIKE escape sequences
39
var $_like_escape_str = " {escape '%s'} ";
40
var $_like_escape_chr = '!';
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.
47
var $_count_string = "SELECT COUNT(*) AS ";
51
function __construct($params)
53
parent::__construct($params);
55
$this->_random_keyword = ' RND('.time().')'; // database specific random keyword
59
* Non-persistent database connection
61
* @access private called by the base class
66
return @odbc_connect($this->hostname, $this->username, $this->password);
69
// --------------------------------------------------------------------
72
* Persistent database connection
74
* @access private called by the base class
77
function db_pconnect()
79
return @odbc_pconnect($this->hostname, $this->username, $this->password);
82
// --------------------------------------------------------------------
87
* Keep / reestablish the db connection if no queries have been
88
* sent for a length of time exceeding the server's idle timeout
95
// not implemented in odbc
98
// --------------------------------------------------------------------
101
* Select the database
103
* @access private called by the base class
108
// Not needed for ODBC
112
// --------------------------------------------------------------------
115
* Set client character set
122
function db_set_charset($charset, $collation)
124
// @todo - add support if needed
128
// --------------------------------------------------------------------
131
* Version number query string
138
return "SELECT version() AS ver";
141
// --------------------------------------------------------------------
146
* @access private called by the base class
147
* @param string an SQL query
150
function _execute($sql)
152
$sql = $this->_prep_query($sql);
153
return @odbc_exec($this->conn_id, $sql);
156
// --------------------------------------------------------------------
161
* If needed, each database adapter can prep the query string
163
* @access private called by execute()
164
* @param string an SQL query
167
function _prep_query($sql)
172
// --------------------------------------------------------------------
180
function trans_begin($test_mode = FALSE)
182
if ( ! $this->trans_enabled)
187
// When transactions are nested we only begin/commit/rollback the outermost ones
188
if ($this->_trans_depth > 0)
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;
198
return odbc_autocommit($this->conn_id, FALSE);
201
// --------------------------------------------------------------------
209
function trans_commit()
211
if ( ! $this->trans_enabled)
216
// When transactions are nested we only begin/commit/rollback the outermost ones
217
if ($this->_trans_depth > 0)
222
$ret = odbc_commit($this->conn_id);
223
odbc_autocommit($this->conn_id, TRUE);
227
// --------------------------------------------------------------------
230
* Rollback Transaction
235
function trans_rollback()
237
if ( ! $this->trans_enabled)
242
// When transactions are nested we only begin/commit/rollback the outermost ones
243
if ($this->_trans_depth > 0)
248
$ret = odbc_rollback($this->conn_id);
249
odbc_autocommit($this->conn_id, TRUE);
253
// --------------------------------------------------------------------
260
* @param bool whether or not the string will be used in a LIKE condition
263
function escape_str($str, $like = FALSE)
267
foreach ($str as $key => $val)
269
$str[$key] = $this->escape_str($val, $like);
275
// ODBC doesn't require escaping
276
$str = remove_invisible_characters($str);
278
// escape LIKE condition wildcards
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),
289
// --------------------------------------------------------------------
297
function affected_rows()
299
return @odbc_num_rows($this->conn_id);
302
// --------------------------------------------------------------------
312
return @odbc_insert_id($this->conn_id);
315
// --------------------------------------------------------------------
320
* Generates a platform-specific query string that counts all records in
321
* the specified database
327
function count_all($table = '')
334
$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
336
if ($query->num_rows() == 0)
341
$row = $query->row();
342
$this->_reset_select();
343
return (int) $row->numrows;
346
// --------------------------------------------------------------------
351
* Generates a platform-specific query string so that the table names can be fetched
357
function _list_tables($prefix_limit = FALSE)
359
$sql = "SHOW TABLES FROM `".$this->database."`";
361
if ($prefix_limit !== FALSE AND $this->dbprefix != '')
363
//$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
364
return FALSE; // not currently supported
370
// --------------------------------------------------------------------
375
* Generates a platform-specific query string so that the column names can be fetched
378
* @param string the table name
381
function _list_columns($table = '')
383
return "SHOW COLUMNS FROM ".$table;
386
// --------------------------------------------------------------------
391
* Generates a platform-specific query so that the column data can be retrieved
394
* @param string the table name
397
function _field_data($table)
399
return "SELECT TOP 1 FROM ".$table;
402
// --------------------------------------------------------------------
405
* The error message string
410
function _error_message()
412
return odbc_errormsg($this->conn_id);
415
// --------------------------------------------------------------------
418
* The error message number
423
function _error_number()
425
return odbc_error($this->conn_id);
428
// --------------------------------------------------------------------
431
* Escape the SQL Identifiers
433
* This function escapes column and table names
439
function _escape_identifiers($item)
441
if ($this->_escape_char == '')
446
foreach ($this->_reserved_identifiers as $id)
448
if (strpos($item, '.'.$id) !== FALSE)
450
$str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
452
// remove duplicates if the user already included the escape
453
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
457
if (strpos($item, '.') !== FALSE)
459
$str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
463
$str = $this->_escape_char.$item.$this->_escape_char;
466
// remove duplicates if the user already included the escape
467
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
470
// --------------------------------------------------------------------
475
* This function implicitly groups FROM tables so there is no confusion
476
* about operator precedence in harmony with SQL standards
482
function _from_tables($tables)
484
if ( ! is_array($tables))
486
$tables = array($tables);
489
return '('.implode(', ', $tables).')';
492
// --------------------------------------------------------------------
497
* Generates a platform-specific insert string from the supplied data
500
* @param string the table name
501
* @param array the insert keys
502
* @param array the insert values
505
function _insert($table, $keys, $values)
507
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
510
// --------------------------------------------------------------------
515
* Generates a platform-specific update string from the supplied data
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
525
function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
527
foreach ($values as $key => $val)
529
$valstr[] = $key." = ".$val;
532
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
534
$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
536
$sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
538
$sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
540
$sql .= $orderby.$limit;
546
// --------------------------------------------------------------------
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"
556
* @param string the table name
559
function _truncate($table)
561
return $this->_delete($table);
564
// --------------------------------------------------------------------
569
* Generates a platform-specific delete string from the supplied data
572
* @param string the table name
573
* @param array the where clause
574
* @param string the limit clause
577
function _delete($table, $where = array(), $like = array(), $limit = FALSE)
581
if (count($where) > 0 OR count($like) > 0)
583
$conditions = "\nWHERE ";
584
$conditions .= implode("\n", $this->ar_where);
586
if (count($where) > 0 && count($like) > 0)
588
$conditions .= " AND ";
590
$conditions .= implode("\n", $like);
593
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
595
return "DELETE FROM ".$table.$conditions.$limit;
598
// --------------------------------------------------------------------
603
* Generates a platform-specific LIMIT clause
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
611
function _limit($sql, $limit, $offset)
613
// Does ODBC doesn't use the LIMIT clause?
617
// --------------------------------------------------------------------
620
* Close DB Connection
626
function _close($conn_id)
628
@odbc_close($conn_id);
636
/* End of file odbc_driver.php */
637
/* Location: ./system/database/drivers/odbc/odbc_driver.php */
b'\\ No newline at end of file'