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, 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 = '';
39 * The syntax to count rows is slightly different across different
40 * database engines, so this string appears in each driver and is
41 * used for the count_all() and count_all_results() functions.
43 var $_count_string = "SELECT COUNT(*) AS ";
47 function CI_DB_odbc_driver($params)
49 parent::CI_DB($params);
51 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
55 * Non-persistent database connection
57 * @access private called by the base class
62 return @odbc_connect($this->hostname, $this->username, $this->password);
65 // --------------------------------------------------------------------
68 * Persistent database connection
70 * @access private called by the base class
73 function db_pconnect()
75 return @odbc_pconnect($this->hostname, $this->username, $this->password);
78 // --------------------------------------------------------------------
83 * @access private called by the base class
88 // Not needed for ODBC
92 // --------------------------------------------------------------------
95 * Set client character set
102 function db_set_charset($charset, $collation)
104 // @todo - add support if needed
108 // --------------------------------------------------------------------
111 * Version number query string
118 return "SELECT version() AS ver";
121 // --------------------------------------------------------------------
126 * @access private called by the base class
127 * @param string an SQL query
130 function _execute($sql)
132 $sql = $this->_prep_query($sql);
133 return @odbc_exec($this->conn_id, $sql);
136 // --------------------------------------------------------------------
141 * If needed, each database adapter can prep the query string
143 * @access private called by execute()
144 * @param string an SQL query
147 function _prep_query($sql)
152 // --------------------------------------------------------------------
160 function trans_begin($test_mode = FALSE)
162 if ( ! $this->trans_enabled)
167 // When transactions are nested we only begin/commit/rollback the outermost ones
168 if ($this->_trans_depth > 0)
173 // Reset the transaction failure flag.
174 // If the $test_mode flag is set to TRUE transactions will be rolled back
175 // even if the queries produce a successful result.
176 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
178 return odbc_autocommit($this->conn_id, FALSE);
181 // --------------------------------------------------------------------
189 function trans_commit()
191 if ( ! $this->trans_enabled)
196 // When transactions are nested we only begin/commit/rollback the outermost ones
197 if ($this->_trans_depth > 0)
202 $ret = odbc_commit($this->conn_id);
203 odbc_autocommit($this->conn_id, TRUE);
207 // --------------------------------------------------------------------
210 * Rollback Transaction
215 function trans_rollback()
217 if ( ! $this->trans_enabled)
222 // When transactions are nested we only begin/commit/rollback the outermost ones
223 if ($this->_trans_depth > 0)
228 $ret = odbc_rollback($this->conn_id);
229 odbc_autocommit($this->conn_id, TRUE);
233 // --------------------------------------------------------------------
242 function escape_str($str)
244 // Access the CI object
245 $CI =& get_instance();
247 // ODBC doesn't require escaping
248 return $CI->_remove_invisible_characters($str);
251 // --------------------------------------------------------------------
259 function affected_rows()
261 return @odbc_num_rows($this->conn_id);
264 // --------------------------------------------------------------------
274 return @odbc_insert_id($this->conn_id);
277 // --------------------------------------------------------------------
282 * Generates a platform-specific query string that counts all records in
283 * the specified database
289 function count_all($table = '')
294 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
296 if ($query->num_rows() == 0)
299 $row = $query->row();
300 return $row->numrows;
303 // --------------------------------------------------------------------
308 * Generates a platform-specific query string so that the table names can be fetched
314 function _list_tables($prefix_limit = FALSE)
316 $sql = "SHOW TABLES FROM `".$this->database."`";
318 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
320 //$sql .= " LIKE '".$this->dbprefix."%'";
321 return FALSE; // not currently supported
327 // --------------------------------------------------------------------
332 * Generates a platform-specific query string so that the column names can be fetched
335 * @param string the table name
338 function _list_columns($table = '')
340 return "SHOW COLUMNS FROM ".$table;
343 // --------------------------------------------------------------------
348 * Generates a platform-specific query so that the column data can be retrieved
351 * @param string the table name
354 function _field_data($table)
356 return "SELECT TOP 1 FROM ".$table;
359 // --------------------------------------------------------------------
362 * The error message string
367 function _error_message()
369 return odbc_errormsg($this->conn_id);
372 // --------------------------------------------------------------------
375 * The error message number
380 function _error_number()
382 return odbc_error($this->conn_id);
385 // --------------------------------------------------------------------
388 * Escape the SQL Identifiers
390 * This function escapes column and table names
396 function _escape_identifiers($item)
398 if ($this->_escape_char == '')
403 if (strpos($item, '.') !== FALSE)
405 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
409 $str = $this->_escape_char.$item.$this->_escape_char;
412 // remove duplicates if the user already included the escape
413 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
416 // --------------------------------------------------------------------
421 * This function implicitly groups FROM tables so there is no confusion
422 * about operator precedence in harmony with SQL standards
428 function _from_tables($tables)
430 if ( ! is_array($tables))
432 $tables = array($tables);
435 return '('.implode(', ', $tables).')';
438 // --------------------------------------------------------------------
443 * Generates a platform-specific insert string from the supplied data
446 * @param string the table name
447 * @param array the insert keys
448 * @param array the insert values
451 function _insert($table, $keys, $values)
453 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
456 // --------------------------------------------------------------------
461 * Generates a platform-specific update string from the supplied data
464 * @param string the table name
465 * @param array the update data
466 * @param array the where clause
467 * @param array the orderby clause
468 * @param array the limit clause
471 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
473 foreach($values as $key => $val)
475 $valstr[] = $key." = ".$val;
478 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
480 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
482 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
484 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
486 $sql .= $orderby.$limit;
492 // --------------------------------------------------------------------
497 * Generates a platform-specific truncate string from the supplied data
498 * If the database does not support the truncate() command
499 * This function maps to "DELETE FROM table"
502 * @param string the table name
505 function _truncate($table)
507 return $this->_delete($table);
510 // --------------------------------------------------------------------
515 * Generates a platform-specific delete string from the supplied data
518 * @param string the table name
519 * @param array the where clause
520 * @param string the limit clause
523 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
527 if (count($where) > 0 OR count($like) > 0)
529 $conditions = "\nWHERE ";
530 $conditions .= implode("\n", $this->ar_where);
532 if (count($where) > 0 && count($like) > 0)
534 $conditions .= " AND ";
536 $conditions .= implode("\n", $like);
539 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
541 return "DELETE FROM ".$table.$conditions.$limit;
544 // --------------------------------------------------------------------
549 * Generates a platform-specific LIMIT clause
552 * @param string the sql query string
553 * @param integer the number of rows to limit the query to
554 * @param integer the offset value
557 function _limit($sql, $limit, $offset)
559 // Does ODBC doesn't use the LIMIT clause?
563 // --------------------------------------------------------------------
566 * Close DB Connection
572 function _close($conn_id)
574 @odbc_close($conn_id);
582 /* End of file odbc_driver.php */
583 /* Location: ./system/database/drivers/odbc/odbc_driver.php */