1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, 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 $str = remove_invisible_characters($str);
408 // escape LIKE condition wildcards
411 $str = str_replace( array('%', '_', $this->_like_escape_chr),
412 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
419 // --------------------------------------------------------------------
427 function affected_rows()
429 return @ocirowcount($this->stmt_id);
432 // --------------------------------------------------------------------
442 // not supported in oracle
443 return $this->display_error('db_unsupported_function');
446 // --------------------------------------------------------------------
451 * Generates a platform-specific query string that counts all records in
452 * the specified database
458 function count_all($table = '')
465 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
472 $row = $query->row();
473 return (int) $row->numrows;
476 // --------------------------------------------------------------------
481 * Generates a platform-specific query string so that the table names can be fetched
487 function _list_tables($prefix_limit = FALSE)
489 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
491 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
493 $sql .= " WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
499 // --------------------------------------------------------------------
504 * Generates a platform-specific query string so that the column names can be fetched
507 * @param string the table name
510 function _list_columns($table = '')
512 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
515 // --------------------------------------------------------------------
520 * Generates a platform-specific query so that the column data can be retrieved
523 * @param string the table name
526 function _field_data($table)
528 return "SELECT * FROM ".$table." where rownum = 1";
531 // --------------------------------------------------------------------
534 * The error message string
539 function _error_message()
541 $error = ocierror($this->conn_id);
542 return $error['message'];
545 // --------------------------------------------------------------------
548 * The error message number
553 function _error_number()
555 $error = ocierror($this->conn_id);
556 return $error['code'];
559 // --------------------------------------------------------------------
562 * Escape the SQL Identifiers
564 * This function escapes column and table names
570 function _escape_identifiers($item)
572 if ($this->_escape_char == '')
577 foreach ($this->_reserved_identifiers as $id)
579 if (strpos($item, '.'.$id) !== FALSE)
581 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
583 // remove duplicates if the user already included the escape
584 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
588 if (strpos($item, '.') !== FALSE)
590 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
594 $str = $this->_escape_char.$item.$this->_escape_char;
597 // remove duplicates if the user already included the escape
598 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
601 // --------------------------------------------------------------------
606 * This function implicitly groups FROM tables so there is no confusion
607 * about operator precedence in harmony with SQL standards
613 function _from_tables($tables)
615 if ( ! is_array($tables))
617 $tables = array($tables);
620 return implode(', ', $tables);
623 // --------------------------------------------------------------------
628 * Generates a platform-specific insert string from the supplied data
631 * @param string the table name
632 * @param array the insert keys
633 * @param array the insert values
636 function _insert($table, $keys, $values)
638 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
641 // --------------------------------------------------------------------
646 * Generates a platform-specific update string from the supplied data
649 * @param string the table name
650 * @param array the update data
651 * @param array the where clause
652 * @param array the orderby clause
653 * @param array the limit clause
656 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
658 foreach ($values as $key => $val)
660 $valstr[] = $key." = ".$val;
663 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
665 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
667 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
669 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
671 $sql .= $orderby.$limit;
676 // --------------------------------------------------------------------
681 * Generates a platform-specific truncate string from the supplied data
682 * If the database does not support the truncate() command
683 * This function maps to "DELETE FROM table"
686 * @param string the table name
689 function _truncate($table)
691 return "TRUNCATE TABLE ".$table;
694 // --------------------------------------------------------------------
699 * Generates a platform-specific delete string from the supplied data
702 * @param string the table name
703 * @param array the where clause
704 * @param string the limit clause
707 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
711 if (count($where) > 0 OR count($like) > 0)
713 $conditions = "\nWHERE ";
714 $conditions .= implode("\n", $this->ar_where);
716 if (count($where) > 0 && count($like) > 0)
718 $conditions .= " AND ";
720 $conditions .= implode("\n", $like);
723 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
725 return "DELETE FROM ".$table.$conditions.$limit;
728 // --------------------------------------------------------------------
733 * Generates a platform-specific LIMIT clause
736 * @param string the sql query string
737 * @param integer the number of rows to limit the query to
738 * @param integer the offset value
741 function _limit($sql, $limit, $offset)
743 $limit = $offset + $limit;
744 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
748 $newsql .= " WHERE rnum >= $offset";
751 // remember that we used limits
752 $this->limit_used = TRUE;
757 // --------------------------------------------------------------------
760 * Close DB Connection
766 function _close($conn_id)
768 @ocilogoff($conn_id);
776 /* End of file oci8_driver.php */
777 /* Location: ./system/database/drivers/oci8/oci8_driver.php */