summaryrefslogtreecommitdiffstats
path: root/system/database/drivers/oci8/oci8_driver.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/database/drivers/oci8/oci8_driver.php')
-rw-r--r--system/database/drivers/oci8/oci8_driver.php470
1 files changed, 176 insertions, 294 deletions
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php
index 81eff70c8..33a89df94 100644
--- a/system/database/drivers/oci8/oci8_driver.php
+++ b/system/database/drivers/oci8/oci8_driver.php
@@ -1,13 +1,13 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
- *
+ *
* Licensed under the Open Software License version 3.0
- *
+ *
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
@@ -25,8 +25,6 @@
* @filesource
*/
-// ------------------------------------------------------------------------
-
/**
* oci8 Database Adapter Class
*
@@ -48,92 +46,140 @@
* permit access to oracle databases
*
* @author Kelly McArdle
- *
*/
-
class CI_DB_oci8_driver extends CI_DB {
- var $dbdriver = 'oci8';
+ public $dbdriver = 'oci8';
// The character used for excaping
- var $_escape_char = '"';
+ protected $_escape_char = '"';
// clause and character used for LIKE escape sequences
- var $_like_escape_str = " escape '%s' ";
- var $_like_escape_chr = '!';
+ protected $_like_escape_str = " ESCAPE '%s' ";
+ protected $_like_escape_chr = '!';
/**
* The syntax to count rows is slightly different across different
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $_count_string = "SELECT COUNT(1) AS ";
- var $_random_keyword = ' ASC'; // not currently supported
+ protected $_count_string = 'SELECT COUNT(1) AS ';
+ protected $_random_keyword = ' ASC'; // not currently supported
// Set "auto commit" by default
- var $_commit = OCI_COMMIT_ON_SUCCESS;
+ public $commit_mode = OCI_COMMIT_ON_SUCCESS;
// need to track statement id and cursor id
- var $stmt_id;
- var $curs_id;
+ public $stmt_id;
+ public $curs_id;
// if we use a limit, we will add a field that will
// throw off num_fields later
- var $limit_used;
+ public $limit_used;
- /**
- * Non-persistent database connection
- *
- * @access private called by the base class
- * @return resource
- */
- public function db_connect()
+ public function __construct($params)
{
- return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
- }
+ parent::__construct($params);
- // --------------------------------------------------------------------
+ $valid_dsns = array(
+ 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
+ // Easy Connect string (Oracle 10g+)
+ 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
+ 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora)
+ );
- /**
- * Persistent database connection
- *
- * @access private called by the base class
- * @return resource
- */
- public function db_pconnect()
- {
- return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
- }
+ /* Space characters don't have any effect when actually
+ * connecting, but can be a hassle while validating the DSN.
+ */
+ $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn);
- // --------------------------------------------------------------------
+ if ($this->dsn !== '')
+ {
+ foreach ($valid_dsns as $regexp)
+ {
+ if (preg_match($regexp, $this->dsn))
+ {
+ return;
+ }
+ }
+ }
+
+ // Legacy support for TNS in the hostname configuration field
+ $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname);
+ if (preg_match($valid_dsns['tns'], $this->hostname))
+ {
+ $this->dsn = $this->hostname;
+ return;
+ }
+ elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE
+ && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== ''))
+ {
+ /* If the hostname field isn't empty, doesn't contain
+ * ':' and/or '/' and if port and/or database aren't
+ * empty, then the hostname field is most likely indeed
+ * just a hostname. Therefore we'll try and build an
+ * Easy Connect string from these 3 settings, assuming
+ * that the database field is a service name.
+ */
+ $this->dsn = $this->hostname
+ .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '')
+ .($this->database !== '' ? '/'.ltrim($this->database, '/') : '');
+
+ if (preg_match($valid_dsns['ec'], $this->dsn))
+ {
+ return;
+ }
+ }
+
+ /* At this point, we can only try and validate the hostname and
+ * database fields separately as DSNs.
+ */
+ if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname))
+ {
+ $this->dsn = $this->hostname;
+ return;
+ }
+
+ $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database);
+ foreach ($valid_dsns as $regexp)
+ {
+ if (preg_match($regexp, $this->database))
+ {
+ return;
+ }
+ }
+
+ /* Well - OK, an empty string should work as well.
+ * PHP will try to use environment variables to
+ * determine which Oracle instance to connect to.
+ */
+ $this->dsn = '';
+ }
/**
- * Reconnect
- *
- * Keep / reestablish the db connection if no queries have been
- * sent for a length of time exceeding the server's idle timeout
+ * Non-persistent database connection
*
- * @access public
- * @return void
+ * @return resource
*/
- public function reconnect()
+ public function db_connect()
{
- // not implemented in oracle
- return;
+ return ( ! empty($this->char_set))
+ ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set)
+ : @oci_connect($this->username, $this->password, $this->dsn);
}
// --------------------------------------------------------------------
/**
- * Select the database
+ * Persistent database connection
*
- * @access private called by the base class
- * @return resource
+ * @return resource
*/
- public function db_select()
+ public function db_pconnect()
{
- // Not in Oracle - schemas are actually usernames
- return TRUE;
+ return ( ! empty($this->char_set))
+ ? @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set)
+ : @oci_pconnect($this->username, $this->password, $this->dsn);
}
// --------------------------------------------------------------------
@@ -155,63 +201,44 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Execute the query
*
- * @access protected called by the base class
- * @param string an SQL query
- * @return resource
+ * @param string an SQL query
+ * @return resource
*/
protected function _execute($sql)
{
- // oracle must parse the query before it is run. All of the actions with
- // the query are based on the statement id returned by ociparse
+ /* Oracle must parse the query before it is run. All of the actions with
+ * the query are based on the statement id returned by oci_parse().
+ */
$this->stmt_id = FALSE;
$this->_set_stmt_id($sql);
oci_set_prefetch($this->stmt_id, 1000);
- return @oci_execute($this->stmt_id, $this->_commit);
+ return @oci_execute($this->stmt_id, $this->commit_mode);
}
/**
* Generate a statement ID
*
- * @access private
- * @param string an SQL query
- * @return none
+ * @param string an SQL query
+ * @return void
*/
- private function _set_stmt_id($sql)
+ protected function _set_stmt_id($sql)
{
if ( ! is_resource($this->stmt_id))
{
- $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
+ $this->stmt_id = oci_parse($this->conn_id, $sql);
}
}
// --------------------------------------------------------------------
/**
- * Prep the query
- *
- * If needed, each database adapter can prep the query string
- *
- * @access private called by execute()
- * @param string an SQL query
- * @return string
- */
- private function _prep_query($sql)
- {
- return $sql;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * getCursor. Returns a cursor from the datbase
+ * Get cursor. Returns a cursor from the database
*
- * @access public
- * @return cursor id
+ * @return cursor id
*/
public function get_cursor()
{
- $this->curs_id = oci_new_cursor($this->conn_id);
- return $this->curs_id;
+ return $this->curs_id = oci_new_cursor($this->conn_id);
}
// --------------------------------------------------------------------
@@ -219,20 +246,19 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Stored Procedure. Executes a stored procedure
*
- * @access public
- * @param package package stored procedure is in
- * @param procedure stored procedure to execute
- * @param params array of parameters
- * @return array
+ * @param string package name in which the stored procedure is in
+ * @param string stored procedure name to execute
+ * @param array parameters
+ * @return mixed
*
* params array keys
*
* KEY OPTIONAL NOTES
- * name no the name of the parameter should be in :<param_name> format
- * value no the value of the parameter. If this is an OUT or IN OUT parameter,
- * this should be a reference to a variable
- * type yes the type of the parameter
- * length yes the max size of the parameter
+ * name no the name of the parameter should be in :<param_name> format
+ * value no the value of the parameter. If this is an OUT or IN OUT parameter,
+ * this should be a reference to a variable
+ * type yes the type of the parameter
+ * length yes the max size of the parameter
*/
public function stored_procedure($package, $procedure, $params)
{
@@ -247,24 +273,24 @@ class CI_DB_oci8_driver extends CI_DB {
}
// build the query string
- $sql = "begin $package.$procedure(";
+ $sql = 'BEGIN '.$package.'.'.$procedure.'(';
$have_cursor = FALSE;
foreach ($params as $param)
{
- $sql .= $param['name'] . ",";
+ $sql .= $param['name'].',';
- if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
+ if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
{
$have_cursor = TRUE;
}
}
- $sql = trim($sql, ",") . "); end;";
+ $sql = trim($sql, ',') . '); END;';
$this->stmt_id = FALSE;
$this->_set_stmt_id($sql);
$this->_bind_params($params);
- $this->query($sql, FALSE, $have_cursor);
+ return $this->query($sql, FALSE, $have_cursor);
}
// --------------------------------------------------------------------
@@ -272,10 +298,9 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Bind parameters
*
- * @access private
- * @return none
+ * @return void
*/
- private function _bind_params($params)
+ protected function _bind_params($params)
{
if ( ! is_array($params) OR ! is_resource($this->stmt_id))
{
@@ -301,7 +326,6 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Begin Transaction
*
- * @access public
* @return bool
*/
public function trans_begin($test_mode = FALSE)
@@ -322,7 +346,7 @@ class CI_DB_oci8_driver extends CI_DB {
// even if the queries produce a successful result.
$this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
- $this->_commit = OCI_DEFAULT;
+ $this->commit_mode = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
return TRUE;
}
@@ -331,7 +355,6 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Commit Transaction
*
- * @access public
* @return bool
*/
public function trans_commit()
@@ -347,9 +370,8 @@ class CI_DB_oci8_driver extends CI_DB {
return TRUE;
}
- $ret = oci_commit($this->conn_id);
- $this->_commit = OCI_COMMIT_ON_SUCCESS;
- return $ret;
+ $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
+ return oci_commit($this->conn_id);
}
// --------------------------------------------------------------------
@@ -357,25 +379,18 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Rollback Transaction
*
- * @access public
* @return bool
*/
public function trans_rollback()
{
- if ( ! $this->trans_enabled)
- {
- return TRUE;
- }
-
// When transactions are nested we only begin/commit/rollback the outermost ones
- if ($this->_trans_depth > 0)
+ if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
{
return TRUE;
}
- $ret = oci_rollback($this->conn_id);
- $this->_commit = OCI_COMMIT_ON_SUCCESS;
- return $ret;
+ $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
+ return oci_rollback($this->conn_id);
}
// --------------------------------------------------------------------
@@ -417,8 +432,7 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Affected Rows
*
- * @access public
- * @return integer
+ * @return int
*/
public function affected_rows()
{
@@ -430,8 +444,7 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Insert ID
*
- * @access public
- * @return integer
+ * @return int
*/
public function insert_id()
{
@@ -447,9 +460,8 @@ class CI_DB_oci8_driver extends CI_DB {
* Generates a platform-specific query string that counts all records in
* the specified database
*
- * @access public
- * @param string
- * @return string
+ * @param string
+ * @return int
*/
public function count_all($table = '')
{
@@ -476,17 +488,16 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific query string so that the table names can be fetched
*
- * @access protected
- * @param boolean
+ * @param bool
* @return string
*/
protected function _list_tables($prefix_limit = FALSE)
{
- $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
+ $sql = 'SELECT TABLE_NAME FROM ALL_TABLES';
- if ($prefix_limit !== FALSE AND $this->dbprefix != '')
+ if ($prefix_limit !== FALSE && $this->dbprefix != '')
{
- $sql .= " WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
+ return $sql." WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
}
return $sql;
@@ -499,13 +510,12 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific query string so that the column names can be fetched
*
- * @access protected
- * @param string the table name
- * @return string
+ * @param string the table name
+ * @return string
*/
protected function _list_columns($table = '')
{
- return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
+ return 'SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = \''.$table.'\'';
}
// --------------------------------------------------------------------
@@ -515,13 +525,12 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific query so that the column data can be retrieved
*
- * @access public
- * @param string the table name
- * @return object
+ * @param string the table name
+ * @return string
*/
protected function _field_data($table)
{
- return "SELECT * FROM ".$table." where rownum = 1";
+ return 'SELECT * FROM '.$table.' WHERE rownum = 1';
}
// --------------------------------------------------------------------
@@ -558,83 +567,17 @@ class CI_DB_oci8_driver extends CI_DB {
// --------------------------------------------------------------------
/**
- * Escape the SQL Identifiers
- *
- * This function escapes column and table names
- *
- * @access protected
- * @param string
- * @return string
- */
- protected function _escape_identifiers($item)
- {
- if ($this->_escape_char == '')
- {
- return $item;
- }
-
- foreach ($this->_reserved_identifiers as $id)
- {
- if (strpos($item, '.'.$id) !== FALSE)
- {
- $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
-
- // remove duplicates if the user already included the escape
- return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
- }
- }
-
- if (strpos($item, '.') !== FALSE)
- {
- $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
- }
- else
- {
- $str = $this->_escape_char.$item.$this->_escape_char;
- }
-
- // remove duplicates if the user already included the escape
- return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
- }
-
- // --------------------------------------------------------------------
-
- /**
* From Tables
*
* This function implicitly groups FROM tables so there is no confusion
* about operator precedence in harmony with SQL standards
*
- * @access protected
- * @param type
- * @return type
+ * @param array
+ * @return string
*/
protected function _from_tables($tables)
{
- if ( ! is_array($tables))
- {
- $tables = array($tables);
- }
-
- return implode(', ', $tables);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Insert statement
- *
- * Generates a platform-specific insert string from the supplied data
- *
- * @access public
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
- */
- protected function _insert($table, $keys, $values)
- {
- return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
+ return is_array($tables) ? implode(', ', $tables) : $tables;
}
// --------------------------------------------------------------------
@@ -644,10 +587,10 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific insert string from the supplied data
*
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
+ * @param string the table name
+ * @param array the insert keys
+ * @param array the insert values
+ * @return string
*/
protected function _insert_batch($table, $keys, $values)
{
@@ -656,47 +599,10 @@ class CI_DB_oci8_driver extends CI_DB {
for ($i = 0, $c = count($values); $i < $c; $i++)
{
- $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
+ $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i]."\n";
}
- $sql .= 'SELECT * FROM dual';
-
- return $sql;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Update statement
- *
- * Generates a platform-specific update string from the supplied data
- *
- * @access protected
- * @param string the table name
- * @param array the update data
- * @param array the where clause
- * @param array the orderby clause
- * @param array the limit clause
- * @return string
- */
- protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
- {
- foreach ($values as $key => $val)
- {
- $valstr[] = $key." = ".$val;
- }
-
- $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
-
- $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
-
- $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
-
- $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
-
- $sql .= $orderby.$limit;
-
- return $sql;
+ return $sql.'SELECT * FROM dual';
}
// --------------------------------------------------------------------
@@ -705,16 +611,16 @@ class CI_DB_oci8_driver extends CI_DB {
* Truncate statement
*
* Generates a platform-specific truncate string from the supplied data
- * If the database does not support the truncate() command
- * This function maps to "DELETE FROM table"
*
- * @access protected
+ * If the database does not support the truncate() command,
+ * then this method maps to 'DELETE FROM table'
+ *
* @param string the table name
* @return string
*/
protected function _truncate($table)
{
- return "TRUNCATE TABLE ".$table;
+ return 'TRUNCATE TABLE '.$table;
}
// --------------------------------------------------------------------
@@ -724,31 +630,21 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific delete string from the supplied data
*
- * @access protected
* @param string the table name
* @param array the where clause
+ * @param array the like clause
* @param string the limit clause
* @return string
*/
protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
- $conditions = '';
-
- if (count($where) > 0 OR count($like) > 0)
- {
- $conditions = "\nWHERE ";
- $conditions .= implode("\n", $this->qb_where);
-
- if (count($where) > 0 && count($like) > 0)
- {
- $conditions .= " AND ";
- }
- $conditions .= implode("\n", $like);
- }
+ $conditions = array();
- $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
+ empty($where) OR $conditions[] = implode(' ', $where);
+ empty($like) OR $conditions[] = implode(' ', $like);
+ empty($limit) OR $conditions[] = 'rownum <= '.$limit;
- return "DELETE FROM ".$table.$conditions.$limit;
+ return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '');
}
// --------------------------------------------------------------------
@@ -758,26 +654,16 @@ class CI_DB_oci8_driver extends CI_DB {
*
* Generates a platform-specific LIMIT clause
*
- * @access protected
- * @param string the sql query string
- * @param integer the number of rows to limit the query to
- * @param integer the offset value
- * @return string
+ * @param string the sql query string
+ * @param int the number of rows to limit the query to
+ * @param int the offset value
+ * @return string
*/
protected function _limit($sql, $limit, $offset)
{
- $limit = $offset + $limit;
- $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
-
- if ($offset != 0)
- {
- $newsql .= " WHERE rnum >= $offset";
- }
-
- // remember that we used limits
$this->limit_used = TRUE;
-
- return $newsql;
+ return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit).')'
+ .($offset != 0 ? ' WHERE rnum >= '.$offset : '');
}
// --------------------------------------------------------------------
@@ -785,19 +671,15 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Close DB Connection
*
- * @access protected
- * @param resource
- * @return void
+ * @param resource
+ * @return void
*/
protected function _close($conn_id)
{
@oci_close($conn_id);
}
-
}
-
-
/* End of file oci8_driver.php */
/* Location: ./system/database/drivers/oci8/oci8_driver.php */