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 * oci8 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/
33 * oci8 Database Adapter Class
35 * This is a modification of the DB_driver class to
36 * permit access to oracle databases
38 * NOTE: this uses the PHP 4 oci methods
40 * @author Kelly McArdle
44 class CI_DB_oci8_driver extends CI_DB {
46 var $dbdriver = 'oci8';
48 // The character used for excaping
49 var $_escape_char = '"';
51 // clause and character used for LIKE escape sequences
52 var $_like_escape_str = " escape '%s' ";
53 var $_like_escape_chr = '!';
56 * The syntax to count rows is slightly different across different
57 * database engines, so this string appears in each driver and is
58 * used for the count_all() and count_all_results() functions.
60 var $_count_string = "SELECT COUNT(1) AS ";
61 var $_random_keyword = ' ASC'; // not currently supported
63 // Set "auto commit" by default
64 var $_commit = OCI_COMMIT_ON_SUCCESS;
66 // need to track statement id and cursor id
70 // if we use a limit, we will add a field that will
71 // throw off num_fields later
75 * Non-persistent database connection
77 * @access private called by the base class
82 return @ocilogon($this->username, $this->password, $this->hostname);
85 // --------------------------------------------------------------------
88 * Persistent database connection
90 * @access private called by the base class
93 function db_pconnect()
95 return @ociplogon($this->username, $this->password, $this->hostname);
98 // --------------------------------------------------------------------
103 * Keep / reestablish the db connection if no queries have been
104 * sent for a length of time exceeding the server's idle timeout
111 // not implemented in oracle
114 // --------------------------------------------------------------------
117 * Select the database
119 * @access private called by the base class
127 // --------------------------------------------------------------------
130 * Set client character set
137 function db_set_charset($charset, $collation)
139 // @todo - add support if needed
143 // --------------------------------------------------------------------
146 * Version number query string
153 return ociserverversion($this->conn_id);
156 // --------------------------------------------------------------------
161 * @access private called by the base class
162 * @param string an SQL query
165 function _execute($sql)
167 // oracle must parse the query before it is run. All of the actions with
168 // the query are based on the statement id returned by ociparse
169 $this->stmt_id = FALSE;
170 $this->_set_stmt_id($sql);
171 ocisetprefetch($this->stmt_id, 1000);
172 return @ociexecute($this->stmt_id, $this->_commit);
176 * Generate a statement ID
179 * @param string an SQL query
182 function _set_stmt_id($sql)
184 if ( ! is_resource($this->stmt_id))
186 $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));
190 // --------------------------------------------------------------------
195 * If needed, each database adapter can prep the query string
197 * @access private called by execute()
198 * @param string an SQL query
201 function _prep_query($sql)
206 // --------------------------------------------------------------------
209 * getCursor. Returns a cursor from the datbase
214 function get_cursor()
216 $this->curs_id = ocinewcursor($this->conn_id);
217 return $this->curs_id;
220 // --------------------------------------------------------------------
223 * Stored Procedure. Executes a stored procedure
226 * @param package package stored procedure is in
227 * @param procedure stored procedure to execute
228 * @param params array of parameters
234 * name no the name of the parameter should be in :<param_name> format
235 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
236 * this should be a reference to a variable
237 * type yes the type of the parameter
238 * length yes the max size of the parameter
240 function stored_procedure($package, $procedure, $params)
242 if ($package == '' OR $procedure == '' OR ! is_array($params))
246 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
247 return $this->display_error('db_invalid_query');
252 // build the query string
253 $sql = "begin $package.$procedure(";
255 $have_cursor = FALSE;
256 foreach($params as $param)
258 $sql .= $param['name'] . ",";
260 if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))
265 $sql = trim($sql, ",") . "); end;";
267 $this->stmt_id = FALSE;
268 $this->_set_stmt_id($sql);
269 $this->_bind_params($params);
270 $this->query($sql, FALSE, $have_cursor);
273 // --------------------------------------------------------------------
281 function _bind_params($params)
283 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
288 foreach ($params as $param)
290 foreach (array('name', 'value', 'type', 'length') as $val)
292 if ( ! isset($param[$val]))
298 ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
302 // --------------------------------------------------------------------
310 function trans_begin($test_mode = FALSE)
312 if ( ! $this->trans_enabled)
317 // When transactions are nested we only begin/commit/rollback the outermost ones
318 if ($this->_trans_depth > 0)
323 // Reset the transaction failure flag.
324 // If the $test_mode flag is set to TRUE transactions will be rolled back
325 // even if the queries produce a successful result.
326 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
328 $this->_commit = OCI_DEFAULT;
332 // --------------------------------------------------------------------
340 function trans_commit()
342 if ( ! $this->trans_enabled)
347 // When transactions are nested we only begin/commit/rollback the outermost ones
348 if ($this->_trans_depth > 0)
353 $ret = OCIcommit($this->conn_id);
354 $this->_commit = OCI_COMMIT_ON_SUCCESS;
358 // --------------------------------------------------------------------
361 * Rollback Transaction
366 function trans_rollback()
368 if ( ! $this->trans_enabled)
373 // When transactions are nested we only begin/commit/rollback the outermost ones
374 if ($this->_trans_depth > 0)
379 $ret = OCIrollback($this->conn_id);
380 $this->_commit = OCI_COMMIT_ON_SUCCESS;
384 // --------------------------------------------------------------------
391 * @param bool whether or not the string will be used in a LIKE condition
394 function escape_str($str, $like = FALSE)
398 foreach($str as $key => $val)
400 $str[$key] = $this->escape_str($val, $like);
406 // Access the CI object
407 $CI =& get_instance();
409 $str = $CI->input->_remove_invisible_characters($str);
411 // escape LIKE condition wildcards
414 $str = str_replace( array('%', '_', $this->_like_escape_chr),
415 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
422 // --------------------------------------------------------------------
430 function affected_rows()
432 return @ocirowcount($this->stmt_id);
435 // --------------------------------------------------------------------
445 // not supported in oracle
446 return $this->display_error('db_unsupported_function');
449 // --------------------------------------------------------------------
454 * Generates a platform-specific query string that counts all records in
455 * the specified database
461 function count_all($table = '')
468 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
475 $row = $query->row();
476 return (int) $row->numrows;
479 // --------------------------------------------------------------------
484 * Generates a platform-specific query string so that the table names can be fetched
490 function _list_tables($prefix_limit = FALSE)
492 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
494 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
496 $sql .= " WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_char);
502 // --------------------------------------------------------------------
507 * Generates a platform-specific query string so that the column names can be fetched
510 * @param string the table name
513 function _list_columns($table = '')
515 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
518 // --------------------------------------------------------------------
523 * Generates a platform-specific query so that the column data can be retrieved
526 * @param string the table name
529 function _field_data($table)
531 return "SELECT * FROM ".$table." where rownum = 1";
534 // --------------------------------------------------------------------
537 * The error message string
542 function _error_message()
544 $error = ocierror($this->conn_id);
545 return $error['message'];
548 // --------------------------------------------------------------------
551 * The error message number
556 function _error_number()
558 $error = ocierror($this->conn_id);
559 return $error['code'];
562 // --------------------------------------------------------------------
565 * Escape the SQL Identifiers
567 * This function escapes column and table names
573 function _escape_identifiers($item)
575 if ($this->_escape_char == '')
580 foreach ($this->_reserved_identifiers as $id)
582 if (strpos($item, '.'.$id) !== FALSE)
584 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
586 // remove duplicates if the user already included the escape
587 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
591 if (strpos($item, '.') !== FALSE)
593 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
597 $str = $this->_escape_char.$item.$this->_escape_char;
600 // remove duplicates if the user already included the escape
601 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
604 // --------------------------------------------------------------------
609 * This function implicitly groups FROM tables so there is no confusion
610 * about operator precedence in harmony with SQL standards
616 function _from_tables($tables)
618 if ( ! is_array($tables))
620 $tables = array($tables);
623 return implode(', ', $tables);
626 // --------------------------------------------------------------------
631 * Generates a platform-specific insert string from the supplied data
634 * @param string the table name
635 * @param array the insert keys
636 * @param array the insert values
639 function _insert($table, $keys, $values)
641 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
644 // --------------------------------------------------------------------
649 * Generates a platform-specific update string from the supplied data
652 * @param string the table name
653 * @param array the update data
654 * @param array the where clause
655 * @param array the orderby clause
656 * @param array the limit clause
659 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
661 foreach($values as $key => $val)
663 $valstr[] = $key." = ".$val;
666 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
668 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
670 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
672 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
674 $sql .= $orderby.$limit;
679 // --------------------------------------------------------------------
684 * Generates a platform-specific truncate string from the supplied data
685 * If the database does not support the truncate() command
686 * This function maps to "DELETE FROM table"
689 * @param string the table name
692 function _truncate($table)
694 return "TRUNCATE TABLE ".$table;
697 // --------------------------------------------------------------------
702 * Generates a platform-specific delete string from the supplied data
705 * @param string the table name
706 * @param array the where clause
707 * @param string the limit clause
710 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
714 if (count($where) > 0 OR count($like) > 0)
716 $conditions = "\nWHERE ";
717 $conditions .= implode("\n", $this->ar_where);
719 if (count($where) > 0 && count($like) > 0)
721 $conditions .= " AND ";
723 $conditions .= implode("\n", $like);
726 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
728 return "DELETE FROM ".$table.$conditions.$limit;
731 // --------------------------------------------------------------------
736 * Generates a platform-specific LIMIT clause
739 * @param string the sql query string
740 * @param integer the number of rows to limit the query to
741 * @param integer the offset value
744 function _limit($sql, $limit, $offset)
746 $limit = $offset + $limit;
747 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
751 $newsql .= " WHERE rnum >= $offset";
754 // remember that we used limits
755 $this->limit_used = TRUE;
760 // --------------------------------------------------------------------
763 * Close DB Connection
769 function _close($conn_id)
771 @ocilogoff($conn_id);
779 /* End of file oci8_driver.php */
780 /* Location: ./system/database/drivers/oci8/oci8_driver.php */