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.php824
1 files changed, 352 insertions, 472 deletions
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php
index 6255b330a..fb2f6b31b 100644
--- a/system/database/drivers/oci8/oci8_driver.php
+++ b/system/database/drivers/oci8/oci8_driver.php
@@ -1,32 +1,54 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP
*
- * @package CodeIgniter
- * @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
- * @license http://codeigniter.com/user_guide/license.html
- * @link http://codeigniter.com
- * @since Version 1.0
+ * This content is released under the MIT License (MIT)
+ *
+ * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @package CodeIgniter
+ * @author EllisLab Dev Team
+ * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
+ * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
+ * @license http://opensource.org/licenses/MIT MIT License
+ * @link https://codeigniter.com
+ * @since Version 1.4.1
* @filesource
*/
-
-// ------------------------------------------------------------------------
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* oci8 Database Adapter Class
*
* Note: _DB is an extender class that the app controller
- * creates dynamically based on whether the active record
+ * creates dynamically based on whether the query builder
* class is being used or not.
*
* @package CodeIgniter
* @subpackage Drivers
* @category Database
- * @author ExpressionEngine Dev Team
- * @link http://codeigniter.com/user_guide/database/
+ * @author EllisLab Dev Team
+ * @link https://codeigniter.com/user_guide/database/
*/
/**
@@ -36,185 +58,244 @@
* permit access to oracle databases
*
* @author Kelly McArdle
- *
*/
-
class CI_DB_oci8_driver extends CI_DB {
- var $dbdriver = 'oci8';
-
- // The character used for excaping
- var $_escape_char = '"';
-
- // clause and character used for LIKE escape sequences
- var $_like_escape_str = " escape '%s' ";
- var $_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.
+ * Database driver
+ *
+ * @var string
*/
- var $_count_string = "SELECT COUNT(1) AS ";
- var $_random_keyword = ' ASC'; // not currently supported
+ public $dbdriver = 'oci8';
- // Set "auto commit" by default
- var $_commit = OCI_COMMIT_ON_SUCCESS;
+ /**
+ * Statement ID
+ *
+ * @var resource
+ */
+ public $stmt_id;
- // need to track statement id and cursor id
- var $stmt_id;
- var $curs_id;
+ /**
+ * Cursor ID
+ *
+ * @var resource
+ */
+ public $curs_id;
- // if we use a limit, we will add a field that will
- // throw off num_fields later
- var $limit_used;
+ /**
+ * Commit mode flag
+ *
+ * @var int
+ */
+ public $commit_mode = OCI_COMMIT_ON_SUCCESS;
/**
- * Non-persistent database connection
+ * Limit used flag
+ *
+ * If we use LIMIT, we'll add a field that will
+ * throw off num_fields later.
*
- * @access private called by the base class
- * @return resource
+ * @var bool
*/
- public function db_connect()
- {
- return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
- }
+ public $limit_used;
// --------------------------------------------------------------------
/**
- * Persistent database connection
+ * Reset $stmt_id flag
*
- * @access private called by the base class
- * @return resource
+ * Used by stored_procedure() to prevent _execute() from
+ * re-setting the statement ID.
*/
- public function db_pconnect()
- {
- return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
- }
-
- // --------------------------------------------------------------------
+ protected $_reset_stmt_id = TRUE;
/**
- * Reconnect
+ * List of reserved identifiers
*
- * Keep / reestablish the db connection if no queries have been
- * sent for a length of time exceeding the server's idle timeout
+ * Identifiers that must NOT be escaped.
*
- * @access public
- * @return void
+ * @var string[]
*/
- public function reconnect()
- {
- // not implemented in oracle
- return;
- }
-
- // --------------------------------------------------------------------
+ protected $_reserved_identifiers = array('*', 'rownum');
/**
- * Select the database
+ * ORDER BY random keyword
*
- * @access private called by the base class
- * @return resource
+ * @var array
*/
- public function db_select()
- {
- // Not in Oracle - schemas are actually usernames
- return TRUE;
- }
-
- // --------------------------------------------------------------------
+ protected $_random_keyword = array('ASC', 'ASC'); // not currently supported
/**
- * Set client character set
+ * COUNT string
*
- * @access public
- * @param string
- * @param string
- * @return resource
+ * @used-by CI_DB_driver::count_all()
+ * @used-by CI_DB_query_builder::count_all_results()
+ *
+ * @var string
*/
- public function db_set_charset($charset, $collation)
- {
- // @todo - add support if needed
- return TRUE;
- }
+ protected $_count_string = 'SELECT COUNT(1) AS ';
// --------------------------------------------------------------------
/**
- * Version number query string
+ * Class constructor
*
- * @access protected
- * @return string
+ * @param array $params
+ * @return void
*/
- protected function _version()
+ public function __construct($params)
{
- return oci_server_version($this->conn_id);
+ 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)
+ );
+
+ /* 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 = '';
}
// --------------------------------------------------------------------
/**
- * Execute the query
+ * Non-persistent database connection
*
- * @access protected called by the base class
- * @param string an SQL query
- * @return resource
+ * @param bool $persistent
+ * @return resource
*/
- protected function _execute($sql)
+ public function db_connect($persistent = FALSE)
{
- // 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
- $this->stmt_id = FALSE;
- $this->_set_stmt_id($sql);
- oci_set_prefetch($this->stmt_id, 1000);
- return @oci_execute($this->stmt_id, $this->_commit);
+ $func = ($persistent === TRUE) ? 'oci_pconnect' : 'oci_connect';
+ return empty($this->char_set)
+ ? $func($this->username, $this->password, $this->dsn)
+ : $func($this->username, $this->password, $this->dsn, $this->char_set);
}
+ // --------------------------------------------------------------------
+
/**
- * Generate a statement ID
+ * Database version number
*
- * @access private
- * @param string an SQL query
- * @return none
+ * @return string
*/
- private function _set_stmt_id($sql)
+ public function version()
{
- if ( ! is_resource($this->stmt_id))
+ if (isset($this->data_cache['version']))
+ {
+ return $this->data_cache['version'];
+ }
+
+ if ( ! $this->conn_id OR ($version_string = oci_server_version($this->conn_id)) === FALSE)
{
- $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
+ return FALSE;
+ }
+ elseif (preg_match('#Release\s(\d+(?:\.\d+)+)#', $version_string, $match))
+ {
+ return $this->data_cache['version'] = $match[1];
}
+
+ return FALSE;
}
// --------------------------------------------------------------------
/**
- * Prep the query
- *
- * If needed, each database adapter can prep the query string
+ * Execute the query
*
- * @access private called by execute()
- * @param string an SQL query
- * @return string
+ * @param string $sql an SQL query
+ * @return resource
*/
- private function _prep_query($sql)
+ protected function _execute($sql)
{
- return $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 oci_parse().
+ */
+ if ($this->_reset_stmt_id === TRUE)
+ {
+ $this->stmt_id = oci_parse($this->conn_id, $sql);
+ }
+
+ oci_set_prefetch($this->stmt_id, 1000);
+ return oci_execute($this->stmt_id, $this->commit_mode);
}
// --------------------------------------------------------------------
/**
- * getCursor. Returns a cursor from the datbase
+ * Get cursor. Returns a cursor from the database
*
- * @access public
- * @return cursor id
+ * @return resource
*/
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);
}
// --------------------------------------------------------------------
@@ -222,52 +303,49 @@ 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
+ * 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
*/
- public function stored_procedure($package, $procedure, $params)
+ public function stored_procedure($package, $procedure, array $params)
{
- if ($package == '' OR $procedure == '' OR ! is_array($params))
+ if ($package === '' OR $procedure === '')
{
- if ($this->db_debug)
- {
- log_message('error', 'Invalid query: '.$package.'.'.$procedure);
- return $this->display_error('db_invalid_query');
- }
- return FALSE;
+ log_message('error', 'Invalid query: '.$package.'.'.$procedure);
+ return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
}
- // build the query string
- $sql = "begin $package.$procedure(";
+ // Build the query string
+ $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->_reset_stmt_id = FALSE;
+ $this->stmt_id = oci_parse($this->conn_id, $sql);
$this->_bind_params($params);
- $this->query($sql, FALSE, $have_cursor);
+ $result = $this->query($sql, FALSE, $have_cursor);
+ $this->_reset_stmt_id = TRUE;
+ return $result;
}
// --------------------------------------------------------------------
@@ -275,10 +353,10 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Bind parameters
*
- * @access private
- * @return none
+ * @param array $params
+ * @return void
*/
- private function _bind_params($params)
+ protected function _bind_params($params)
{
if ( ! is_array($params) OR ! is_resource($this->stmt_id))
{
@@ -304,28 +382,11 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Begin Transaction
*
- * @access public
* @return bool
*/
- public function trans_begin($test_mode = FALSE)
+ protected function _trans_begin()
{
- if ( ! $this->trans_enabled)
- {
- return TRUE;
- }
-
- // When transactions are nested we only begin/commit/rollback the outermost ones
- if ($this->_trans_depth > 0)
- {
- return TRUE;
- }
-
- // Reset the transaction failure flag.
- // If the $test_mode flag is set to TRUE transactions will be rolled back
- // even if the queries produce a successful result.
- $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
-
- $this->_commit = OCI_DEFAULT;
+ $this->commit_mode = OCI_NO_AUTO_COMMIT;
return TRUE;
}
@@ -334,25 +395,13 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Commit Transaction
*
- * @access public
* @return bool
*/
- public function trans_commit()
+ protected function _trans_commit()
{
- if ( ! $this->trans_enabled)
- {
- return TRUE;
- }
+ $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
- // When transactions are nested we only begin/commit/rollback the outermost ones
- if ($this->_trans_depth > 0)
- {
- return TRUE;
- }
-
- $ret = oci_commit($this->conn_id);
- $this->_commit = OCI_COMMIT_ON_SUCCESS;
- return $ret;
+ return oci_commit($this->conn_id);
}
// --------------------------------------------------------------------
@@ -360,60 +409,12 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Rollback Transaction
*
- * @access public
* @return bool
*/
- public function trans_rollback()
+ protected 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)
- {
- return TRUE;
- }
-
- $ret = oci_rollback($this->conn_id);
- $this->_commit = OCI_COMMIT_ON_SUCCESS;
- return $ret;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Escape String
- *
- * @access public
- * @param string
- * @param bool whether or not the string will be used in a LIKE condition
- * @return string
- */
- public function escape_str($str, $like = FALSE)
- {
- if (is_array($str))
- {
- foreach ($str as $key => $val)
- {
- $str[$key] = $this->escape_str($val, $like);
- }
-
- return $str;
- }
-
- $str = remove_invisible_characters($str);
-
- // escape LIKE condition wildcards
- if ($like === TRUE)
- {
- $str = str_replace( array('%', '_', $this->_like_escape_chr),
- array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
- $str);
- }
-
- return $str;
+ $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
+ return oci_rollback($this->conn_id);
}
// --------------------------------------------------------------------
@@ -421,12 +422,11 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Affected Rows
*
- * @access public
- * @return integer
+ * @return int
*/
public function affected_rows()
{
- return @oci_num_rows($this->stmt_id);
+ return oci_num_rows($this->stmt_id);
}
// --------------------------------------------------------------------
@@ -434,8 +434,7 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Insert ID
*
- * @access public
- * @return integer
+ * @return int
*/
public function insert_id()
{
@@ -446,52 +445,21 @@ class CI_DB_oci8_driver extends CI_DB {
// --------------------------------------------------------------------
/**
- * "Count All" query
- *
- * Generates a platform-specific query string that counts all records in
- * the specified database
- *
- * @access public
- * @param string
- * @return string
- */
- public function count_all($table = '')
- {
- if ($table == '')
- {
- return 0;
- }
-
- $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
-
- if ($query == FALSE)
- {
- return 0;
- }
-
- $row = $query->row();
- $this->_reset_select();
- return (int) $row->numrows;
- }
-
- // --------------------------------------------------------------------
-
- /**
* Show table query
*
* Generates a platform-specific query string so that the table names can be fetched
*
- * @access protected
- * @param boolean
+ * @param bool $prefix_limit
* @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;
@@ -504,155 +472,129 @@ 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 $table
+ * @return string
*/
protected function _list_columns($table = '')
{
- return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Field data query
- *
- * Generates a platform-specific query so that the column data can be retrieved
- *
- * @access public
- * @param string the table name
- * @return object
- */
- protected function _field_data($table)
- {
- return "SELECT * FROM ".$table." where rownum = 1";
- }
-
- // --------------------------------------------------------------------
+ if (strpos($table, '.') !== FALSE)
+ {
+ sscanf($table, '%[^.].%s', $owner, $table);
+ }
+ else
+ {
+ $owner = $this->username;
+ }
- /**
- * The error message string
- *
- * @access protected
- * @return string
- */
- protected function _error_message()
- {
- // If the error was during connection, no conn_id should be passed
- $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
- return $error['message'];
+ return 'SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
+ WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
+ AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
}
// --------------------------------------------------------------------
/**
- * The error message number
+ * Returns an object with field data
*
- * @access protected
- * @return integer
+ * @param string $table
+ * @return array
*/
- protected function _error_number()
+ public function field_data($table)
{
- // Same as _error_message()
- $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
- return $error['code'];
- }
+ if (strpos($table, '.') !== FALSE)
+ {
+ sscanf($table, '%[^.].%s', $owner, $table);
+ }
+ else
+ {
+ $owner = $this->username;
+ }
- // --------------------------------------------------------------------
+ $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_LENGTH, DATA_DEFAULT, NULLABLE
+ FROM ALL_TAB_COLUMNS
+ WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
+ AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
- /**
- * 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 == '')
+ if (($query = $this->query($sql)) === FALSE)
{
- return $item;
+ return FALSE;
}
+ $query = $query->result_object();
- foreach ($this->_reserved_identifiers as $id)
+ $retval = array();
+ for ($i = 0, $c = count($query); $i < $c; $i++)
{
- if (strpos($item, '.'.$id) !== FALSE)
- {
- $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
+ $retval[$i] = new stdClass();
+ $retval[$i]->name = $query[$i]->COLUMN_NAME;
+ $retval[$i]->type = $query[$i]->DATA_TYPE;
- // remove duplicates if the user already included the escape
- return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
+ $length = ($query[$i]->CHAR_LENGTH > 0)
+ ? $query[$i]->CHAR_LENGTH : $query[$i]->DATA_PRECISION;
+ if ($length === NULL)
+ {
+ $length = $query[$i]->DATA_LENGTH;
}
- }
+ $retval[$i]->max_length = $length;
- 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;
+ $default = $query[$i]->DATA_DEFAULT;
+ if ($default === NULL && $query[$i]->NULLABLE === 'N')
+ {
+ $default = '';
+ }
+ $retval[$i]->default = $default;
}
- // remove duplicates if the user already included the escape
- return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
+ return $retval;
}
// --------------------------------------------------------------------
/**
- * From Tables
+ * Error
*
- * This function implicitly groups FROM tables so there is no confusion
- * about operator precedence in harmony with SQL standards
+ * Returns an array containing code and message of the last
+ * database error that has occurred.
*
- * @access protected
- * @param type
- * @return type
+ * @return array
*/
- protected function _from_tables($tables)
+ public function error()
{
- if ( ! is_array($tables))
+ // oci_error() returns an array that already contains
+ // 'code' and 'message' keys, but it can return false
+ // if there was no error ....
+ if (is_resource($this->curs_id))
{
- $tables = array($tables);
+ $error = oci_error($this->curs_id);
+ }
+ elseif (is_resource($this->stmt_id))
+ {
+ $error = oci_error($this->stmt_id);
+ }
+ elseif (is_resource($this->conn_id))
+ {
+ $error = oci_error($this->conn_id);
+ }
+ else
+ {
+ $error = oci_error();
}
- 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($error)
+ ? $error
+ : array('code' => '', 'message' => '');
}
// --------------------------------------------------------------------
/**
- * Insert_batch statement
+ * Insert batch statement
*
* Generates a platform-specific insert string from the supplied data
*
- * @access protected
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
+ * @param string $table Table name
+ * @param array $keys INSERT keys
+ * @param array $values INSERT values
+ * @return string
*/
protected function _insert_batch($table, $keys, $values)
{
@@ -661,47 +603,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';
}
// --------------------------------------------------------------------
@@ -710,16 +615,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
- * @param string the table name
+ * If the database does not support the TRUNCATE statement,
+ * then this method maps to 'DELETE FROM table'
+ *
+ * @param string $table
* @return string
*/
protected function _truncate($table)
{
- return "TRUNCATE TABLE ".$table;
+ return 'TRUNCATE TABLE '.$table;
}
// --------------------------------------------------------------------
@@ -729,60 +634,43 @@ 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 string the limit clause
+ * @param string $table
* @return string
*/
- protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
+ protected function _delete($table)
{
- $conditions = '';
-
- if (count($where) > 0 OR count($like) > 0)
+ if ($this->qb_limit)
{
- $conditions = "\nWHERE ";
- $conditions .= implode("\n", $this->ar_where);
-
- if (count($where) > 0 && count($like) > 0)
- {
- $conditions .= " AND ";
- }
- $conditions .= implode("\n", $like);
+ $this->where('rownum <= ',$this->qb_limit, FALSE);
+ $this->qb_limit = FALSE;
}
- $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
-
- return "DELETE FROM ".$table.$conditions.$limit;
+ return parent::_delete($table);
}
// --------------------------------------------------------------------
/**
- * Limit string
+ * LIMIT
*
* 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 $sql SQL Query
+ * @return string
*/
- protected function _limit($sql, $limit, $offset)
+ protected function _limit($sql)
{
- $limit = $offset + $limit;
- $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
-
- if ($offset != 0)
+ if (version_compare($this->version(), '12.1', '>='))
{
- $newsql .= " WHERE rnum >= $offset";
+ // OFFSET-FETCH can be used only with the ORDER BY clause
+ empty($this->qb_orderby) && $sql .= ' ORDER BY 1';
+
+ return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
}
- // 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 < '.($this->qb_offset + $this->qb_limit + 1).')'
+ .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1) : '');
}
// --------------------------------------------------------------------
@@ -790,19 +678,11 @@ class CI_DB_oci8_driver extends CI_DB {
/**
* Close DB Connection
*
- * @access protected
- * @param resource
- * @return void
+ * @return void
*/
- protected function _close($conn_id)
+ protected function _close()
{
- @oci_close($conn_id);
+ oci_close($this->conn_id);
}
-
}
-
-
-
-/* End of file oci8_driver.php */
-/* Location: ./system/database/drivers/oci8/oci8_driver.php */