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