Source for file oci8.php
Documentation is available at oci8.php
// vim: set et ts=4 sw=4 fdm=marker:
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Lukas Smith <smith@backendmedia.com> |
// +----------------------------------------------------------------------+
// $Id: oci8.php,v 1.11 2004/04/23 17:06:46 lsmith Exp $
* @author Lukas Smith <smith@backendmedia.com>
$this->dbsyntax = 'oci8';
$this->supported['summary_functions'] = true;
$this->options['DBA_username'] = false;
$this->options['DBA_password'] = false;
$this->options['database_name_prefix'] = false;
$this->options['default_tablespace'] = false;
$this->options['default_text_field_length'] = 4000;
* This method is used to collect information about an error
$error_data = @OCIError ($error);
$error_data = @OCIError ($this->connection);
$native_code = $error_data['code'];
$native_msg = $error_data['message'];
if (isset ($ecode_map[$native_code])) {
$error = $ecode_map[$native_code];
return array ($error, $native_code, $native_msg);
* Define whether database changes done on the database be automatically
* committed. This function may also implicitly start or end a transaction.
* @param boolean $auto_commit flag that indicates whether the database
* changes should be committed right after
* executing every query statement. If this
* argument is 0 a transaction implicitly
* started. Otherwise, if a transaction is
* in progress it is ended by committing any
* database changes that were pending.
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug(($auto_commit ? 'On' : 'Off'), 'autoCommit');
$this->in_transaction = !$auto_commit;
* Commit the database changes done during a transaction that is in
* progress. This function may only be called when auto-committing is
* disabled, otherwise it will fail. Therefore, a new transaction is
* implicitly started after committing the pending changes.
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug('commit transaction', 'commit');
if (!isset ($this->supported['transactions'])) {
'commit: transactions are not in use');
'commit: transaction changes are being auto commited');
if (!@OCICommit ($this->connection)) {
* Cancel any database changes done during a transaction that is in
* progress. This function may only be called when auto-committing is
* disabled, otherwise it will fail. Therefore, a new transaction is
* implicitly started after canceling the pending changes.
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug('rolling back transaction', 'rollback');
'rollback: transactions can not be rolled back when changes are auto commited');
if (!@OCIRollback ($this->connection)) {
* do the grunt work of the connect
* @return connection on success or MDB2 Error Object on failure
function _doConnect ($username, $password, $persistent = false )
if (!PEAR ::loadExtension ($this->phptype)) {
'extension '. $this->phptype. ' is not compiled into PHP');
if (isset ($this->dsn['hostspec'])) {
$sid = $this->dsn['hostspec'];
'it was not specified a valid Oracle Service Identifier (SID)');
$function = ($persistent ? 'OCIPLogon' : 'OCINLogon');
$connection = @$function($username, $password, $sid);
* Connect to the database
* @return MDB2_OK on success, MDB2 Error Object on failure
if ($this->connection != 0 ) {
&& $this->opened_persistent == $this->options['persistent'])
if ($this->database_name) {
$database_name = $this->options['database_name_prefix']. $this->database_name;
$connection = $this->_doConnect ($database_name, $this->dsn['password'], $this->options['persistent']);
$this->connection = $connection;
$this->connected_dsn = $this->dsn;
$this->opened_persistent = $this->options['persistent'];
$query = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
$doquery = $this->_doQuery ($query);
$query = "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
$doquery = $this->_doQuery ($query);
* Close the database connection
if ($this->connection != 0 ) {
@OCILogOff ($this->connection);
$this->affected_rows = -1;
unset ($GLOBALS['_MDB2_databases'][$this->db_index]);
* This method is used by backends to alter queries for various
* @param string $query query to modify
* @return the new (modified) query
function _modifyQuery ($query)
// "SELECT 2+2" must be "SELECT 2+2 FROM dual" in Oracle
* @param string $query the SQL query
* @return mixed result identifier if query executed, else MDB2_error
function _doQuery ($query, $ismanip = null , $prepared_query = 0 )
for (reset($this->clobs[$prepared_query]), $clob = 0;
$clob < count($this->clobs[$prepared_query]);
$clob++ , next($this->clobs[$prepared_query])
$clob_stream = key($this->clobs[$prepared_query]);
$descriptors[$clob_stream] = @OCINewDescriptor ($this->connection, OCI_D_LOB );
if (!is_object($descriptors[$clob_stream])) {
'Could not create descriptor for clob parameter');
$parameter = $GLOBALS['_MDB2_LOBs'][$clob_stream]->parameter;
$columns.= ($lobs == 0 ? ' RETURNING ' : ',').
$this->prepared_queries[$prepared_query-1 ]['fields'][$parameter-1 ];
$variables.= ($lobs == 0 ? ' INTO ' : ','). ':clob'. $parameter;
for (reset($this->blobs[$prepared_query]), $blob = 0;
$blob < count($this->blobs[$prepared_query]);
$blob++ , next($this->blobs[$prepared_query])
$blob_stream = key($this->blobs[$prepared_query]);
$descriptors[$blob_stream] = @OCINewDescriptor ($this->connection, OCI_D_LOB );
if (!is_object($descriptors[$blob_stream])) {
'Could not create descriptor for blob parameter');
$parameter = $GLOBALS['_MDB2_LOBs'][$blob_stream]->parameter;
$columns.= ($lobs == 0 ? ' RETURNING ' : ',').
$this->prepared_queries[$prepared_query-1 ]['fields'][$parameter-1 ];
$variables.= ($lobs == 0 ? ' INTO ' : ','). ':blob'. $parameter;
$query.= $columns. $variables;
if (($statement = @OCIParse ($this->connection, $query))) {
for (reset($this->clobs[$prepared_query]), $clob = 0;
$clob < count($this->clobs[$prepared_query]);
$clob++ , next($this->clobs[$prepared_query])
$clob_stream = key($this->clobs[$prepared_query]);
$parameter = $GLOBALS['_MDB2_LOBs'][$clob_stream]->parameter;
if (!OCIBindByName ($statement, ':clob'. $parameter, $descriptors[$clob_stream], -1 , OCI_B_CLOB )) {
for (reset($this->blobs[$prepared_query]), $blob = 0;
$blob < count($this->blobs[$prepared_query]);
$blob++ , next($this->blobs[$prepared_query])
$blob_stream = key($this->blobs[$prepared_query]);
$parameter = $GLOBALS['_MDB2_LOBs'][$blob_stream]->parameter;
if (!OCIBindByName ($statement, ':blob'. $parameter, $descriptors[$blob_stream], -1 , OCI_B_BLOB )) {
$mode = ($lobs == 0 && $this->auto_commit) ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT;
$result = @OCIExecute ($statement, $mode);
for (reset($this->clobs[$prepared_query]), $clob = 0;
$clob < count($this->clobs[$prepared_query]);
$clob++ , next($this->clobs[$prepared_query])
$clob_stream = key($this->clobs[$prepared_query]);
for ($value = ''; !$this->datatype->endOfLOB ($clob_stream); ) {
if ($this->datatype->readLOB ($clob_stream, $data, $this->options['lob_buffer_length']) < 0 ) {
if (!MDB2::isError($success) && !$descriptors[$clob_stream]->save ($value)) {
for (reset($this->blobs[$prepared_query]), $blob = 0;
$blob < count($this->blobs[$prepared_query]);
$blob++ , next($this->blobs[$prepared_query])
$blob_stream = key($this->blobs[$prepared_query]);
for ($value = ''; !$this->datatype->endOfLOB ($blob_stream); ) {
if ($this->datatype->readLOB ($blob_stream, $data, $this->options['lob_buffer_length']) < 0 ) {
if (!MDB2::isError($success) && !$descriptors[$blob_stream]->save ($value)) {
if (!OCIRollback ($this->connection)) {
if (!OCICommit ($this->connection)) {
$this->affected_rows = @OCIRowCount ($statement);
@OCIFreeCursor ($statement);
for (reset($descriptors), $descriptor = 0;
$descriptor < count($descriptors);
$descriptor++ , next($descriptors)
@$descriptors[key($descriptors)]->free ();
* @param string $query the SQL query
* @return mixed MDB2_OK on success, a MDB2 error on failure
$connection = $this->_doConnect ($this->options['DBA_username'], $this->options['DBA_password'], $this->options['persistent']);
$result = @OCIParse ($connection, $query);
$success = @OCIExecute ($result, OCI_COMMIT_ON_SUCCESS );
$success = @OCIExecute ($result, OCI_DEFAULT );
// {{{ _executePrepared()
* Execute a prepared query statement.
* @param int $prepared_query argument is a handle that was returned by
* @param string $query query to be executed
* @param array $types array that contains the types of the columns in the result set
* @param mixed $result_class string which specifies which result class to use
* @param mixed $result_wrap_class string which specifies which class to wrap results in
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
function &_executePrepared ($prepared_query, $query, $types = null ,
$result_class = false , $result_wrap_class = false )
$offset = $this->row_offset;
$limit = $this->row_limit;
$this->row_offset = $this->row_limit = 0;
$query = $this->_modifyQuery ($query);
$this->debug($query, 'query');
$result = $this->_doQuery ($query, $ismanip, $prepared_query);
$result_class = $this->options['result_buffering']
? $this->options['buffered_result_class'] : $this->options['result_class'];
$class_name = sprintf($result_class, $this->phptype);
$result = & new $class_name($this, $result, $offset, $limit);
$err = $result->setResultTypes ($types);
if (!$result_wrap_class) {
$result_wrap_class = $this->options['result_wrap_class'];
if ($result_wrap_class) {
$result = & new $result_wrap_class($result);
* Send a query to the database and return any results
* @param string $query the SQL query
* @param array $types array that contains the types of the columns in the result set
* @param mixed $result_class string which specifies which result class to use
* @param mixed $result_wrap_class string which specifies which class to wrap results in
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
function &query($query, $types = null , $result_class = false , $result_wrap_class = false )
$result = & $this->_executePrepared (false , $query, $types, $result_class, $result_wrap_class);
* returns the next free id of a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true the seqence is
* automatic created, if it
* @return mixed MDB2 Error Object or id
function nextID($seq_name, $ondemand = true )
$result = $this->queryOne(" SELECT $sequence_name.nextval FROM DUAL" );
$result = $this->manager->createSequence ($seq_name, 1 );
return $this->nextId ($seq_name, false );
* returns the current id of a sequence
* @param string $seq_name name of the sequence
* @return mixed MDB2_Error or id
return $this->queryOne(" SELECT $sequence_name.currval FROM DUAL" );
class MDB2_Result_oci8 extends MDB2_Result_Common
function MDB2_Result_oci8 (&$mdb, &$result, $offset, $limit)
parent ::MDB2_Result_Common ($mdb, $result);
// {{{ _skipLimitOffset()
* Skip the first row of a result set.
* @param resource $result
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
function _skipLimitOffset ()
if (isset ($this->limits) && is_array($this->limits)) {
if ($this->rownum > $this->limits['limit']) {
while ($this->limits['count'] < $this->limits['offset']) {
++ $this->limits['count'];
if (!@OCIFetchInto ($this->result, $row, OCI_RETURN_NULLS )) {
* fetch value from a result set
* @param int $rownum number of the row where the data can be found
* @param int $colnum field number where the data can be found
* @return mixed string on success, a MDB2 error on failure
function fetch ($rownum = 0 , $colnum = 0 )
$seek = $this->seek ($rownum);
$row = $this->fetchRow ($fetchmode);
* Fetch a row and insert the data into an existing array.
* @param int $fetchmode how the array data should be indexed
* @return int data array on success, a MDB2 error on failure
function fetchRow ($fetchmode = MDB2_FETCHMODE_DEFAULT )
$fetchmode = $this->mdb->fetchmode;
if (!$this->_skipLimitOffset ()) {
@OCIFetchInto ($this->result, $row, OCI_ASSOC+OCI_RETURN_NULLS );
@OCIFetchInto ($this->result, $row, OCI_RETURN_NULLS );
'fetchRow: resultset has already been freed');
if (isset ($this->types)) {
$row = $this->mdb->datatype ->convertResultRow ($this->types, $row);
$this->mdb->_rtrimArrayValues ($row);
* Retrieve the names of columns returned by the DBMS in a query result.
* @return mixed associative array variable
* that holds the names of columns. The indexes of the array are
* the column names mapped to lower case and the values are the
* respective numbers of the columns starting from 0. Some DBMS may
* not return any columns when the result set does not contain any
function getColumnNames ()
$numcols = $this->numCols ();
for ($column = 0; $column < $numcols; $column++ ) {
$column_name = @OCIColumnName ($this->result, $column + 1 );
$columns[$column_name] = $column;
* Count the number of columns returned by the DBMS in a query result.
* @return mixed integer value with the number of columns, a MDB2 error
$cols = @OCINumCols ($this->result);
'numCols: resultset has already been freed');
return $this->mdb->raiseError ();
* Free the internal resources associated with $result.
* @return boolean true on success, false if $result is invalid
$free = @OCIFreeCursor ($this->result);
return $this->mdb->raiseError ();
class MDB2_BufferedResult_oci8 extends MDB2_Result_oci8
var $buffer_rownum = - 1;
function MDB2_BufferedResult_oci8 (&$mdb, &$result, $offset, $limit)
parent ::MDB2_Result_oci8 ($mdb, $result, $offset, $limit);
* @param int $rownum row number upto which the buffer should be filled
if the row number is null all rows are ready into the buffer
* @return boolean true on success, false on failure
function _fillBuffer ($rownum = null )
if (isset ($this->buffer) && is_array($this->buffer)) {
if (!end($this->buffer)) {
} else if (isset ($this->buffer[$rownum])) {
return (bool) $this->buffer[$rownum];
if (!$this->_skipLimitOffset ()) {
while ((is_null($rownum) || $this->buffer_rownum < $rownum)
&& (!isset ($this->limits) || $this->buffer_rownum < $this->limits['limit'])
&& ($row = @OCIFetchInto ($this->result, $buffer, OCI_RETURN_NULLS ))
$this->buffer[$this->buffer_rownum] = $buffer;
$this->buffer[$this->buffer_rownum] = false;
} elseif (isset ($this->limits) && $this->buffer_rownum >= $this->limits['limit']) {
$this->buffer[$this->buffer_rownum] = false;
* Fetch a row and insert the data into an existing array.
* @param int $fetchmode how the array data should be indexed
* @return int data array on success, a MDB2 error on failure
function fetchRow ($fetchmode = MDB2_FETCHMODE_DEFAULT )
'fetchRow: resultset has already been freed');
$target_rownum = $this->rownum + 1;
$fetchmode = $this->mdb->fetchmode;
if (!$this->_fillBuffer ($target_rownum)) {
$row = $this->buffer[$target_rownum];
$column_names = $this->getColumnNames ();
foreach ($column_names as $name => $i) {
$column_names[$name] = $row[$i];
if (isset ($this->types)) {
$row = $this->mdb->datatype ->convertResultRow ($this->types, $row);
$this->mdb->_rtrimArrayValues ($row);
* seek to a specific row in a result set
* @param int $rownum number of the row where the data can be found
* @return mixed MDB2_OK on success, a MDB2 error on failure
function seek ($rownum = 0 )
'seek: resultset has already been freed');
$this->rownum = $rownum - 1;
* check if the end of the result set has been reached
* @return mixed true or false on sucess, a MDB2 error on failure
'valid: resultset has already been freed');
if ($this->_fillBuffer ($this->rownum + 1 )) {
* returns the number of rows in a result object
* @return mixed MDB2 Error Object or the number of rows
'seek: resultset has already been freed');
return $this->buffer_rownum;
* Free the internal resources associated with $result.
* @return boolean true on success, false if $result is invalid
$this->buffer_rownum = null;
Documentation generated on Mon, 11 Mar 2019 10:15:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|