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 * 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 = '"';
52 * The syntax to count rows is slightly different across different
53 * database engines, so this string appears in each driver and is
54 * used for the count_all() and count_all_results() functions.
56 var $_count_string = "SELECT COUNT(1) AS ";
57 var $_random_keyword = ' ASC'; // not currently supported
59 // Set "auto commit" by default
60 var $_commit = OCI_COMMIT_ON_SUCCESS;
62 // need to track statement id and cursor id
66 // if we use a limit, we will add a field that will
67 // throw off num_fields later
71 * Non-persistent database connection
73 * @access private called by the base class
78 return @ocilogon($this->username, $this->password, $this->hostname);
81 // --------------------------------------------------------------------
84 * Persistent database connection
86 * @access private called by the base class
89 function db_pconnect()
91 return @ociplogon($this->username, $this->password, $this->hostname);
94 // --------------------------------------------------------------------
99 * @access private called by the base class
107 // --------------------------------------------------------------------
110 * Set client character set
117 function db_set_charset($charset, $collation)
119 // @todo - add support if needed
123 // --------------------------------------------------------------------
126 * Version number query string
133 return ociserverversion($this->conn_id);
136 // --------------------------------------------------------------------
141 * @access private called by the base class
142 * @param string an SQL query
145 function _execute($sql)
147 // oracle must parse the query before it is run. All of the actions with
148 // the query are based on the statement id returned by ociparse
149 $this->stmt_id = FALSE;
150 $this->_set_stmt_id($sql);
151 ocisetprefetch($this->stmt_id, 1000);
152 return @ociexecute($this->stmt_id, $this->_commit);
156 * Generate a statement ID
159 * @param string an SQL query
162 function _set_stmt_id($sql)
164 if ( ! is_resource($this->stmt_id))
166 $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));
170 // --------------------------------------------------------------------
175 * If needed, each database adapter can prep the query string
177 * @access private called by execute()
178 * @param string an SQL query
181 function _prep_query($sql)
186 // --------------------------------------------------------------------
189 * getCursor. Returns a cursor from the datbase
194 function get_cursor()
196 $this->curs_id = ocinewcursor($this->conn_id);
197 return $this->curs_id;
200 // --------------------------------------------------------------------
203 * Stored Procedure. Executes a stored procedure
206 * @param package package stored procedure is in
207 * @param procedure stored procedure to execute
208 * @param params array of parameters
214 * name no the name of the parameter should be in :<param_name> format
215 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
216 * this should be a reference to a variable
217 * type yes the type of the parameter
218 * length yes the max size of the parameter
220 function stored_procedure($package, $procedure, $params)
222 if ($package == '' OR $procedure == '' OR ! is_array($params))
226 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
227 return $this->display_error('db_invalid_query');
232 // build the query string
233 $sql = "begin $package.$procedure(";
235 $have_cursor = FALSE;
236 foreach($params as $param)
238 $sql .= $param['name'] . ",";
240 if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))
245 $sql = trim($sql, ",") . "); end;";
247 $this->stmt_id = FALSE;
248 $this->_set_stmt_id($sql);
249 $this->_bind_params($params);
250 $this->query($sql, FALSE, $have_cursor);
253 // --------------------------------------------------------------------
261 function _bind_params($params)
263 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
268 foreach ($params as $param)
270 foreach (array('name', 'value', 'type', 'length') as $val)
272 if ( ! isset($param[$val]))
278 ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
282 // --------------------------------------------------------------------
290 function trans_begin($test_mode = FALSE)
292 if ( ! $this->trans_enabled)
297 // When transactions are nested we only begin/commit/rollback the outermost ones
298 if ($this->_trans_depth > 0)
303 // Reset the transaction failure flag.
304 // If the $test_mode flag is set to TRUE transactions will be rolled back
305 // even if the queries produce a successful result.
306 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
308 $this->_commit = OCI_DEFAULT;
312 // --------------------------------------------------------------------
320 function trans_commit()
322 if ( ! $this->trans_enabled)
327 // When transactions are nested we only begin/commit/rollback the outermost ones
328 if ($this->_trans_depth > 0)
333 $ret = OCIcommit($this->conn_id);
334 $this->_commit = OCI_COMMIT_ON_SUCCESS;
338 // --------------------------------------------------------------------
341 * Rollback Transaction
346 function trans_rollback()
348 if ( ! $this->trans_enabled)
353 // When transactions are nested we only begin/commit/rollback the outermost ones
354 if ($this->_trans_depth > 0)
359 $ret = OCIrollback($this->conn_id);
360 $this->_commit = OCI_COMMIT_ON_SUCCESS;
364 // --------------------------------------------------------------------
373 function escape_str($str)
375 // Access the CI object
376 $CI =& get_instance();
378 return $CI->_remove_invisible_characters($str);
381 // --------------------------------------------------------------------
389 function affected_rows()
391 return @ocirowcount($this->stmt_id);
394 // --------------------------------------------------------------------
404 // not supported in oracle
405 return $this->display_error('db_unsupported_function');
408 // --------------------------------------------------------------------
413 * Generates a platform-specific query string that counts all records in
414 * the specified database
420 function count_all($table = '')
425 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
432 $row = $query->row();
433 return $row->NUMROWS;
436 // --------------------------------------------------------------------
441 * Generates a platform-specific query string so that the table names can be fetched
447 function _list_tables($prefix_limit = FALSE)
449 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
451 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
453 $sql .= " WHERE TABLE_NAME LIKE '".$this->dbprefix."%'";
459 // --------------------------------------------------------------------
464 * Generates a platform-specific query string so that the column names can be fetched
467 * @param string the table name
470 function _list_columns($table = '')
472 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
475 // --------------------------------------------------------------------
480 * Generates a platform-specific query so that the column data can be retrieved
483 * @param string the table name
486 function _field_data($table)
488 return "SELECT * FROM ".$table." where rownum = 1";
491 // --------------------------------------------------------------------
494 * The error message string
499 function _error_message()
501 $error = ocierror($this->conn_id);
502 return $error['message'];
505 // --------------------------------------------------------------------
508 * The error message number
513 function _error_number()
515 $error = ocierror($this->conn_id);
516 return $error['code'];
519 // --------------------------------------------------------------------
522 * Escape the SQL Identifiers
524 * This function escapes column and table names
530 function _escape_identifiers($item)
532 if ($this->_escape_char == '')
537 if (strpos($item, '.') !== FALSE)
539 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
543 $str = $this->_escape_char.$item.$this->_escape_char;
546 // remove duplicates if the user already included the escape
547 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
550 // --------------------------------------------------------------------
555 * This function implicitly groups FROM tables so there is no confusion
556 * about operator precedence in harmony with SQL standards
562 function _from_tables($tables)
564 if ( ! is_array($tables))
566 $tables = array($tables);
569 return implode(', ', $tables);
572 // --------------------------------------------------------------------
577 * Generates a platform-specific insert string from the supplied data
580 * @param string the table name
581 * @param array the insert keys
582 * @param array the insert values
585 function _insert($table, $keys, $values)
587 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
590 // --------------------------------------------------------------------
595 * Generates a platform-specific update string from the supplied data
598 * @param string the table name
599 * @param array the update data
600 * @param array the where clause
601 * @param array the orderby clause
602 * @param array the limit clause
605 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
607 foreach($values as $key => $val)
609 $valstr[] = $key." = ".$val;
612 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
614 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
616 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
618 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
620 $sql .= $orderby.$limit;
625 // --------------------------------------------------------------------
630 * Generates a platform-specific truncate string from the supplied data
631 * If the database does not support the truncate() command
632 * This function maps to "DELETE FROM table"
635 * @param string the table name
638 function _truncate($table)
640 return "TRUNCATE TABLE ".$table;
643 // --------------------------------------------------------------------
648 * Generates a platform-specific delete string from the supplied data
651 * @param string the table name
652 * @param array the where clause
653 * @param string the limit clause
656 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
660 if (count($where) > 0 OR count($like) > 0)
662 $conditions = "\nWHERE ";
663 $conditions .= implode("\n", $this->ar_where);
665 if (count($where) > 0 && count($like) > 0)
667 $conditions .= " AND ";
669 $conditions .= implode("\n", $like);
672 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
674 return "DELETE FROM ".$table.$conditions.$limit;
677 // --------------------------------------------------------------------
682 * Generates a platform-specific LIMIT clause
685 * @param string the sql query string
686 * @param integer the number of rows to limit the query to
687 * @param integer the offset value
690 function _limit($sql, $limit, $offset)
692 $limit = $offset + $limit;
693 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
697 $newsql .= " WHERE rnum >= $offset";
700 // remember that we used limits
701 $this->limit_used = TRUE;
706 // --------------------------------------------------------------------
709 * Close DB Connection
715 function _close($conn_id)
717 @ocilogoff($conn_id);
725 /* End of file oci8_driver.php */
726 /* Location: ./system/database/drivers/oci8/oci8_driver.php */