Source for file oci8.php
Documentation is available at oci8.php
// vim: set et ts=4 sw=4 fdm=marker:
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | 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.74 2005/04/27 14:13:40 lsmith Exp $
* @author Lukas Smith <smith@backendmedia.com>
* The result or statement handle from the most recently executed query
$this->supported['summary_functions'] = true;
$this->options['DBA_username'] = false;
$this->options['DBA_password'] = false;
$this->options['database_name_prefix'] = false;
$this->options['emulate_database'] = true;
$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 ();
$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);
// {{{ beginTransaction()
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug('starting transaction', 'beginTransaction');
* Commit the database changes done during a transaction that is in
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug('commit transaction', 'commit');
'commit: transaction changes are being auto committed');
* Cancel any database changes done during a transaction that is in
* @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 committed');
* 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)');
if (isset ($this->dsn['new_link'])
&& ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true )
$connect_function = 'oci_new_connect';
$connect_function = $persistent ? 'oci_pconnect' : 'oci_connect';
$charset = empty ($this->dsn['charset']) ? null : $this->dsn['charset'];
$connection = @$connect_function($username, $password, $sid, $charset);
if (isset ($error['code']) && $error['code'] == 12541 ) {
// Couldn't find TNS listener. Try direct connection.
$connection = @$connect_function($username, $password, null , $charset);
$connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
$connection = @$connect_function($username, $password, $sid);
* Connect to the database
* @return MDB2_OK on success, MDB2 Error Object on failure
if (PEAR ::isError ($connection)) {
$query = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
if (PEAR ::isError ($error)) {
$query = "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
if (PEAR ::isError ($error)) {
* Log out and disconnect from the database.
* @return mixed true on success, false if not connected and error
* @param string $query the SQL query
* @param mixed $types array that contains the types of the columns in
* @return mixed MDB2_OK on success, a MDB2 error on failure
if (PEAR ::isError ($connection)) {
$query = $this->_modifyQuery($query, $isManip, $limit, $offset);
$result = $this->_doQuery($query, $isManip, $connection, false );
if (PEAR ::isError ($result)) {
return $this->_wrapResult($result, $types, true , false , $limit, $offset);
* Changes a query string for various DBMS specific reasons
* @param string $query query to modify
* @return the new (modified) query
// "SELECT 2+2" must be "SELECT 2+2 FROM dual" in Oracle
* @param string $query query
* @param boolean $isManip if the query is a manipulation query
* @param resource $connection
* @param string $database_name
* @return result or error object
function _doQuery($query, $isManip = false , $connection = null , $database_name = null )
$this->debug($query, 'query');
if (PEAR ::isError ($error)) {
$result = @OCIParse ($connection, $query);
'Could not create statement');
$return = @OCIExecute ($result, $mode);
return @OCIRowCount ($result);
@ocisetprefetch ($result, $this->options['result_buffering']);
* Prepares a query for multiple execution with execute().
* With some database backends, this is emulated.
* prepare() requires a generic query as string like
* 'INSERT INTO numbers VALUES(?,?)' or
* 'INSERT INTO numbers VALUES(:foo,:bar)'.
* The ? and :[a-zA-Z] and are placeholders which can be set using
* bindParam() and the query can be send off using the execute() method.
* @param string $query the query to prepare
* @param mixed $types array that contains the types of the placeholders
* @param mixed $result_types array that contains the types of the columns in
* @return mixed resource handle for the prepared query on success, a MDB2
* @see bindParam, execute
function &prepare($query, $types = null , $result_types = null )
$this->debug($query, 'prepare');
$columns = $variables = '';
foreach ($types as $parameter => $type) {
if ($type == 'clob' || $type == 'blob') {
$columns.= ($columns ? ', ' : ' RETURNING '). $parameter;
$variables.= ($variables ? ', ' : ' INTO '). ':'. $parameter;
$placeholder_type_guess = $placeholder_type = null;
while ($position < strlen($query)) {
$q_position = strpos($query, $question, $position);
$c_position = strpos($query, $colon, $position);
if ($q_position && $c_position) {
$p_position = min($q_position, $c_position);
$p_position = $q_position;
$p_position = $c_position;
$placeholder_type_guess = $query[$p_position];
if (is_int($quote = strpos($query, "'", $position)) && $quote < $p_position) {
if (!is_int($end_quote = strpos($query, "'", $quote + 1 ))) {
'prepare: query with an unterminated text string specified');
$position = $end_quote + 1;
if ($end_quote == $quote + 1 ) {
$position = $end_quote + 1;
$position = $end_quote + 1;
} elseif ($query[$position] == $placeholder_type_guess) {
if ($placeholder_type_guess == ':' && !$contains_lobs) {
$placeholder_type = $query[$p_position];
$question = $colon = $placeholder_type;
if ($placeholder_type == ':') {
$parameter = preg_replace('/^.{'. ($position+1 ). '}([a-z0-9_]+).*$/i', '\\1', $query);
'prepare: named parameter with an empty name');
$length = strlen($parameter)+1;
if (isset ($types[$parameter])
&& ($types[$parameter] == 'clob' || $types[$parameter] == 'blob')
$value = $this->quote(true , $types[$parameter]);
$position = $p_position + strlen($value) - 1;
} elseif ($placeholder_type == '?') {
$position = $p_position + strlen($parameter);
$position = $p_position + strlen($parameter);
$query.= $columns. $variables;
$statement = @OCIParse ($this->connection, $query);
'Could not create statement');
$class_name = 'MDB2_Statement_'. $this->phptype;
$obj = & new $class_name($this, $statement, $query, $types, $result_types);
* 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 )
$query = " SELECT $sequence_name.nextval FROM DUAL";
$result = $this->queryOne($query, 'integer');
if (PEAR ::isError ($result)) {
$result = $this->manager->createSequence ($seq_name, 1 );
if (PEAR ::isError ($result)) {
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
// {{{ _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 ($this->rownum > $this->limit) {
while ($this->offset_count < $this->offset) {
if (!@OCIFetchInto ($this->result, $row, OCI_RETURN_NULLS )) {
$this->offset_count = $this->offset;
* Fetch a row and insert the data into an existing array.
* @param int $fetchmode how the array data should be indexed
* @param int $rownum number of the row where the data can be found
* @return int data array on success, a MDB2 error on failure
function &fetchRow ($fetchmode = MDB2_FETCHMODE_DEFAULT , $rownum = null )
if (!$this->_skipLimitOffset ()) {
$seek = $this->seek ($rownum);
if (PEAR ::isError ($seek)) {
$fetchmode = $this->db->fetchmode;
@OCIFetchInto ($this->result, $row, OCI_ASSOC+OCI_RETURN_NULLS );
@OCIFetchInto ($this->result, $row, OCI_RETURN_NULLS );
'fetchRow: resultset has already been freed');
$this->db->_rtrimArrayValues ($row);
if (isset ($this->values)) {
$this->_assignBindColumns ($row);
if (isset ($this->types)) {
$row = $this->db->datatype ->convertResultRow ($this->types, $row);
$object_class = $this->db->options ['fetch_class'];
if ($object_class == 'stdClass') {
$row = &new $object_class($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 ();
if (PEAR ::isError ($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->db->raiseError ();
* Free the internal resources associated with $result.
* @return boolean true on success, false if $result is invalid
$free = @OCIFreeCursor ($this->result);
return $this->db->raiseError ();
class MDB2_BufferedResult_oci8 extends MDB2_Result_oci8
var $buffer_rownum = - 1;
* @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)) {
} elseif (isset ($this->buffer[$rownum])) {
return (bool) $this->buffer[$rownum];
if (!$this->_skipLimitOffset ()) {
while ((is_null($rownum) || $this->buffer_rownum < $rownum)
&& (!$this->limit || $this->buffer_rownum < $this->limit)
&& ($row = @OCIFetchInto ($this->result, $buffer, OCI_RETURN_NULLS ))
$this->buffer[$this->buffer_rownum] = $buffer;
$this->buffer[$this->buffer_rownum] = false;
} elseif ($this->limit && $this->buffer_rownum >= $this->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
* @param int $rownum number of the row where the data can be found
* @return int data array on success, a MDB2 error on failure
function &fetchRow ($fetchmode = MDB2_FETCHMODE_DEFAULT , $rownum = null )
'fetchRow: resultset has already been freed');
$seek = $this->seek ($rownum);
if (PEAR ::isError ($seek)) {
$target_rownum = $this->rownum + 1;
$fetchmode = $this->db->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->values)) {
$this->_assignBindColumns ($row);
if (isset ($this->types)) {
$row = $this->db->datatype ->convertResultRow ($this->types, $row);
$this->db->_rtrimArrayValues ($row);
$object_class = $this->db->options ['fetch_class'];
if ($object_class == 'stdClass') {
$row = &new $object_class($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;
class MDB2_Statement_oci8 extends MDB2_Statement_Common
* Execute a prepared query statement.
* @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 &execute ($result_class = true , $result_wrap_class = false )
$this->db->last_query = $this->query;
$this->db->debug ($this->query, 'query');
if ($this->db->getOption ('disable_query')) {
$connected = $this->db->connect ();
if (PEAR ::isError ($connected)) {
$lobs = $descriptors = array ();
foreach ($this->values as $parameter => $value) {
$type = isset ($this->types[$parameter]) ? $this->types[$parameter] : null;
if ($type == 'clob' || $type == 'blob') {
$lobs[$parameter]['file'] = true;
$value.= fread($fp, 8192 );
$lobs[$parameter]['file'] = false;
} elseif (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
if ($match[1 ] == 'file://') {
$lobs[$parameter]['file'] = false;
$lobs[$parameter]['value'] = $value;
$descriptors[$parameter] = @OCINewDescriptor ($this->db->connection , OCI_D_LOB );
$result = $this->db->raiseError ();
$descriptors[$parameter] = $this->db->quote ($value, $type);
if (PEAR ::isError ($descriptors[$parameter])) {
return $descriptors[$parameter];
if ($type == 'clob' || $type == 'blob') {
$lob_type = ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB );
if (!@OCIBindByName ($this->statement, ':'. $parameter, $descriptors[$parameter], -1 , $lob_type)) {
$result = $this->db->raiseError ($this->statement);
if ($descriptors[$parameter] === 'NULL') {
$descriptors[$parameter] = null;
} elseif ($descriptors[$parameter][0 ] === "'") {
$descriptors[$parameter] = substr($descriptors[$parameter], 1 , -1 );
if (!@OCIBindByName ($this->statement, ':'. $parameter, $descriptors[$parameter], -1 )) {
$result = $this->db->raiseError ($this->statement);
$mode = (!empty ($lobs) || $this->db->in_transaction ) ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
$return = @OCIExecute ($this->statement, $mode);
return $this->db->raiseError ($this->statement);
foreach ($lobs as $parameter => $stream) {
if (!is_null($stream['value']) && $stream['value'] !== '') {
$result = @$descriptors[$parameter]->savefile ($stream['value']);
$result = @$descriptors[$parameter]->save ($stream['value']);
$result = $this->db->raiseError ();
if (!PEAR ::isError ($result)) {
if (!$this->db->in_transaction ) {
if (!@OCICommit ($this->db->connection )) {
$result = $this->db->raiseError ();
++ $this->db->uncommitedqueries;
foreach ($descriptors as $parameter => $foo) {
@$descriptors[$parameter]->close ();
@$descriptors[$parameter]->free ();
if (PEAR ::isError ($result)) {
return @OCIRowCount ($this->statement);
return $this->db->_wrapResult ($this->statement, $isManip, $this->types,
$result_class, $result_wrap_class, $this->row_offset, $this->row_limit);
* Release resources allocated for the specified prepared query.
* @return mixed MDB2_OK on success, a MDB2 error on failure
@OCIFreeStatement ($this->statement);
Documentation generated on Mon, 11 Mar 2019 14:20:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|