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 CI_DB_odbc_driver($params)
53 parent::CI_DB($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 return (int) $row->numrows;
345 // --------------------------------------------------------------------
350 * Generates a platform-specific query string so that the table names can be fetched
356 function _list_tables($prefix_limit = FALSE)
358 $sql = "SHOW TABLES FROM `".$this->database."`";
360 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
362 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
363 return FALSE; // not currently supported
369 // --------------------------------------------------------------------
374 * Generates a platform-specific query string so that the column names can be fetched
377 * @param string the table name
380 function _list_columns($table = '')
382 return "SHOW COLUMNS FROM ".$table;
385 // --------------------------------------------------------------------
390 * Generates a platform-specific query so that the column data can be retrieved
393 * @param string the table name
396 function _field_data($table)
398 return "SELECT TOP 1 FROM ".$table;
401 // --------------------------------------------------------------------
404 * The error message string
409 function _error_message()
411 return odbc_errormsg($this->conn_id);
414 // --------------------------------------------------------------------
417 * The error message number
422 function _error_number()
424 return odbc_error($this->conn_id);
427 // --------------------------------------------------------------------
430 * Escape the SQL Identifiers
432 * This function escapes column and table names
438 function _escape_identifiers($item)
440 if ($this->_escape_char == '')
445 foreach ($this->_reserved_identifiers as $id)
447 if (strpos($item, '.'.$id) !== FALSE)
449 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
451 // remove duplicates if the user already included the escape
452 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
456 if (strpos($item, '.') !== FALSE)
458 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
462 $str = $this->_escape_char.$item.$this->_escape_char;
465 // remove duplicates if the user already included the escape
466 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
469 // --------------------------------------------------------------------
474 * This function implicitly groups FROM tables so there is no confusion
475 * about operator precedence in harmony with SQL standards
481 function _from_tables($tables)
483 if ( ! is_array($tables))
485 $tables = array($tables);
488 return '('.implode(', ', $tables).')';
491 // --------------------------------------------------------------------
496 * Generates a platform-specific insert string from the supplied data
499 * @param string the table name
500 * @param array the insert keys
501 * @param array the insert values
504 function _insert($table, $keys, $values)
506 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
509 // --------------------------------------------------------------------
514 * Generates a platform-specific update string from the supplied data
517 * @param string the table name
518 * @param array the update data
519 * @param array the where clause
520 * @param array the orderby clause
521 * @param array the limit clause
524 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
526 foreach ($values as $key => $val)
528 $valstr[] = $key." = ".$val;
531 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
533 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
535 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
537 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
539 $sql .= $orderby.$limit;
545 // --------------------------------------------------------------------
550 * Generates a platform-specific truncate string from the supplied data
551 * If the database does not support the truncate() command
552 * This function maps to "DELETE FROM table"
555 * @param string the table name
558 function _truncate($table)
560 return $this->_delete($table);
563 // --------------------------------------------------------------------
568 * Generates a platform-specific delete string from the supplied data
571 * @param string the table name
572 * @param array the where clause
573 * @param string the limit clause
576 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
580 if (count($where) > 0 OR count($like) > 0)
582 $conditions = "\nWHERE ";
583 $conditions .= implode("\n", $this->ar_where);
585 if (count($where) > 0 && count($like) > 0)
587 $conditions .= " AND ";
589 $conditions .= implode("\n", $like);
592 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
594 return "DELETE FROM ".$table.$conditions.$limit;
597 // --------------------------------------------------------------------
602 * Generates a platform-specific LIMIT clause
605 * @param string the sql query string
606 * @param integer the number of rows to limit the query to
607 * @param integer the offset value
610 function _limit($sql, $limit, $offset)
612 // Does ODBC doesn't use the LIMIT clause?
616 // --------------------------------------------------------------------
619 * Close DB Connection
625 function _close($conn_id)
627 @odbc_close($conn_id);
635 /* End of file odbc_driver.php */
636 /* Location: ./system/database/drivers/odbc/odbc_driver.php */