1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, 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 // Access the CI object
276 $CI =& get_instance();
278 // ODBC doesn't require escaping
279 $str = $CI->input->_remove_invisible_characters($str);
281 // escape LIKE condition wildcards
284 $str = str_replace( array('%', '_', $this->_like_escape_chr),
285 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
292 // --------------------------------------------------------------------
300 function affected_rows()
302 return @odbc_num_rows($this->conn_id);
305 // --------------------------------------------------------------------
315 return @odbc_insert_id($this->conn_id);
318 // --------------------------------------------------------------------
323 * Generates a platform-specific query string that counts all records in
324 * the specified database
330 function count_all($table = '')
337 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
339 if ($query->num_rows() == 0)
344 $row = $query->row();
345 return (int) $row->numrows;
348 // --------------------------------------------------------------------
353 * Generates a platform-specific query string so that the table names can be fetched
359 function _list_tables($prefix_limit = FALSE)
361 $sql = "SHOW TABLES FROM `".$this->database."`";
363 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
365 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_char);
366 return FALSE; // not currently supported
372 // --------------------------------------------------------------------
377 * Generates a platform-specific query string so that the column names can be fetched
380 * @param string the table name
383 function _list_columns($table = '')
385 return "SHOW COLUMNS FROM ".$table;
388 // --------------------------------------------------------------------
393 * Generates a platform-specific query so that the column data can be retrieved
396 * @param string the table name
399 function _field_data($table)
401 return "SELECT TOP 1 FROM ".$table;
404 // --------------------------------------------------------------------
407 * The error message string
412 function _error_message()
414 return odbc_errormsg($this->conn_id);
417 // --------------------------------------------------------------------
420 * The error message number
425 function _error_number()
427 return odbc_error($this->conn_id);
430 // --------------------------------------------------------------------
433 * Escape the SQL Identifiers
435 * This function escapes column and table names
441 function _escape_identifiers($item)
443 if ($this->_escape_char == '')
448 foreach ($this->_reserved_identifiers as $id)
450 if (strpos($item, '.'.$id) !== FALSE)
452 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
454 // remove duplicates if the user already included the escape
455 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
459 if (strpos($item, '.') !== FALSE)
461 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
465 $str = $this->_escape_char.$item.$this->_escape_char;
468 // remove duplicates if the user already included the escape
469 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
472 // --------------------------------------------------------------------
477 * This function implicitly groups FROM tables so there is no confusion
478 * about operator precedence in harmony with SQL standards
484 function _from_tables($tables)
486 if ( ! is_array($tables))
488 $tables = array($tables);
491 return '('.implode(', ', $tables).')';
494 // --------------------------------------------------------------------
499 * Generates a platform-specific insert string from the supplied data
502 * @param string the table name
503 * @param array the insert keys
504 * @param array the insert values
507 function _insert($table, $keys, $values)
509 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
512 // --------------------------------------------------------------------
517 * Generates a platform-specific update string from the supplied data
520 * @param string the table name
521 * @param array the update data
522 * @param array the where clause
523 * @param array the orderby clause
524 * @param array the limit clause
527 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
529 foreach($values as $key => $val)
531 $valstr[] = $key." = ".$val;
534 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
536 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
538 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
540 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
542 $sql .= $orderby.$limit;
548 // --------------------------------------------------------------------
553 * Generates a platform-specific truncate string from the supplied data
554 * If the database does not support the truncate() command
555 * This function maps to "DELETE FROM table"
558 * @param string the table name
561 function _truncate($table)
563 return $this->_delete($table);
566 // --------------------------------------------------------------------
571 * Generates a platform-specific delete string from the supplied data
574 * @param string the table name
575 * @param array the where clause
576 * @param string the limit clause
579 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
583 if (count($where) > 0 OR count($like) > 0)
585 $conditions = "\nWHERE ";
586 $conditions .= implode("\n", $this->ar_where);
588 if (count($where) > 0 && count($like) > 0)
590 $conditions .= " AND ";
592 $conditions .= implode("\n", $like);
595 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
597 return "DELETE FROM ".$table.$conditions.$limit;
600 // --------------------------------------------------------------------
605 * Generates a platform-specific LIMIT clause
608 * @param string the sql query string
609 * @param integer the number of rows to limit the query to
610 * @param integer the offset value
613 function _limit($sql, $limit, $offset)
615 // Does ODBC doesn't use the LIMIT clause?
619 // --------------------------------------------------------------------
622 * Close DB Connection
628 function _close($conn_id)
630 @odbc_close($conn_id);
638 /* End of file odbc_driver.php */
639 /* Location: ./system/database/drivers/odbc/odbc_driver.php */