Source for file oci8.php
Documentation is available at oci8.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: James L. Pine <jlp@valinux.com> |
// | Maintainer: Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
// $Id: oci8.php,v 1.64 2004/03/25 17:12:03 danielc Exp $
// be aware... OCIError() only appears to return anything when given a
// statement, so functions return the generic DB_ERROR instead of more
// useful errors that have to do with feedback from the database.
require_once 'DB/common.php';
* Database independent query interface definition for PHP's Oracle 8
* call-interface extension.
* Definitely works with versions 8 and 9 of Oracle.
* @version $Id: oci8.php,v 1.64 2004/03/25 17:12:03 danielc Exp $
* @author James L. Pine <jlp@valinux.com>
* stores the $data passed to execute() in the oci8 driver
* Gets reset to array() when simpleQuery() is run.
* Needed in case user wants to call numRows() after prepare/execute
* Connect to a database and log in as the specified user.
* @param $dsn the data source name (see DB::parseDSN for syntax)
* @param $persistent (optional) whether the connection should
* @return int DB_OK on success, a DB error code on failure
function connect($dsninfo, $persistent = false )
$connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
if ($dsninfo['hostspec']) {
$conn = @$connect_function($dsninfo['username'],
} elseif ($dsninfo['username'] || $dsninfo['password']) {
$conn = @$connect_function($dsninfo['username'],
$error = (is_array($error)) ? $error['message'] : null;
* Log out and disconnect from the database.
* @return bool true on success, false if not connected.
* Send a query to oracle and return the results as an oci8 resource
* @param $query the SQL query
* @return int returns a valid oci8 result for successful SELECT
* queries, DB_OK for other successful queries. A DB error code
* is returned on failure.
$success = @OCIExecute ($result,OCI_COMMIT_ON_SUCCESS );
$success = @OCIExecute ($result,OCI_DEFAULT );
// Determine which queries that should return data, and which
// should return an error code only.
* Move the internal oracle result pointer to the next available result
* @param a valid oci8 result resource
* @return true if a result is available otherwise return false
* Fetch a row and insert the data into an existing array.
* Formating of the array and the data therein are configurable.
* See DB_result::fetchInto() for more information.
* @param resource $result query result identifier
* @param array $arr (reference) array where data from the row
* @param int $fetchmode how the resulting array should be indexed
* @param int $rownum the row number to fetch
* @return mixed DB_OK on success, null when end of result set is
* @see DB_result::fetchInto()
function fetchInto ($result, &$arr, $fetchmode, $rownum=null )
$moredata = @OCIFetchInto ($result,$arr,OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS );
$moredata = OCIFetchInto ($result,$arr,OCI_RETURN_NULLS+OCI_RETURN_LOBS );
$this->_rtrimArrayValues ($arr);
$this->_convertNullArrayValuesToEmpty ($arr);
* Free the internal resources associated with $result.
* @param $result oci8 result identifier
* @return bool true on success, false if $result is invalid
return @OCIFreeStatement ($result);
* Free the internal resources associated with a prepared query.
* @param $stmt oci8 statement identifier
* @return bool true on success, false if $result is invalid
// emulate numRows for Oracle. yuck.
$countquery = 'SELECT COUNT(*) FROM ('. $this->last_query. ')';
if (count($this->_data)) {
$count = $this->execute($smt, $this->_data);
$count = & $this->query($countquery);
* Get the number of columns in a result set.
* @param $result oci8 result identifier
* @return int the number of columns per row in $result
$cols = @OCINumCols ($result);
* Get the native error code of the last error (if any) that occured
* on the current connection. This does not work, as OCIError does
* not work unless given a statement. If OCIError does return
* something, so will this.
* @return int native oci8 error code
* Prepares a query for multiple execution with execute().
* With oci8, this is emulated.
* prepare() requires a generic query as string like <code>
* INSERT INTO numbers VALUES (?, ?, ?)
* </code>. The <kbd>?</kbd> characters are placeholders.
* Three types of placeholders can be used:
* + <kbd>?</kbd> a quoted scalar value, i.e. strings, integers
* + <kbd>!</kbd> value is inserted 'as is'
* + <kbd>&</kbd> requires a file name. The file's contents get
* inserted into the query (i.e. saving binary
* Use backslashes to escape placeholder characters if you don't want
* them to be interpreted as placeholders. Example: <code>
* "UPDATE foo SET col=? WHERE col='over \& under'"
* @param string $query query to be prepared
* @return mixed DB statement resource on success. DB_Error on failure.
$tokens = preg_split('/((?<!\\\)[&?!])/', $query, -1 ,
PREG_SPLIT_DELIM_CAPTURE );
$binds = count($tokens) - 1;
foreach ($tokens as $key => $val) {
$newquery .= $tokens[$key] . ':bind' . $token;
$newquery .= $tokens[$key];
if (!$stmt = @OCIParse ($this->connection, $newquery)) {
* Executes a DB statement prepared with prepare().
* @param resource $stmt a DB statement resource returned from prepare()
* @param mixed $data array, string or numeric data to be used in
* execution of the statement. Quantity of items
* passed must match quantity of placeholders in
* query: meaning 1 for non-array items or the
* quantity of elements in the array.
* @return int returns an oci8 result resource for successful
* SELECT queries, DB_OK for other successful queries. A DB error
* code is returned on failure.
function &execute($stmt, $data = array ())
foreach ($data as $key => $value) {
* Oracle doesn't seem to have the ability to pass a
* parameter along unchanged, so strip off quotes from start
* and end, plus turn two single quotes to one single quote,
* in order to avoid the quotes getting escaped by
* Oracle and ending up in the database.
$data[$key] = preg_replace("/^'(.*)'$/", "\\1", $data[$key]);
$fp = @fopen($data[$key], 'rb');
if (!@OCIBindByName ($stmt, ':bind' . $i, $data[$key], -1 )) {
$success = @OCIExecute ($stmt, OCI_COMMIT_ON_SUCCESS );
$success = @OCIExecute ($stmt, OCI_DEFAULT );
* Enable/disable automatic commits
* @param $onoff true/false whether to autocommit
* Commit transactions on the current connection
* @return DB_ERROR or DB_OK
* Roll back all uncommitted transactions on the current connection.
* @return DB_ERROR or DB_OK
* Gets the number of rows affected by the last query.
* if the last query was a select, returns 0.
* @return number of rows affected by the last query or DB_ERROR
// "SELECT 2+2" must be "SELECT 2+2 FROM dual" in Oracle
// {{{ modifyLimitQuery()
* Emulate the row limit support altering the query
* @param string $query The query to treat
* @param int $from The row to start to fetch from
* @param int $count The offset
* @return string The modified query
* @author Tomas V.V.Cox <cox@idecnet.com>
// Let Oracle return the name of the columns instead of
// coding a "home" SQL parser
$result = $this->prepare(" SELECT * FROM ($query) "
$tmp = & $this->execute($result, $params);
$q_fields = " SELECT * FROM ($query) WHERE NULL = NULL";
if (!$result = @OCIParse ($this->connection, $q_fields)) {
if (!@OCIExecute ($result, OCI_DEFAULT )) {
$ncols = OCINumCols ($result);
for ( $i = 1; $i <= $ncols; $i++ ) {
$cols[] = '"' . OCIColumnName ($result, $i) . '"';
// XXX Test that (tip by John Lim)
//if (preg_match('/^\s*SELECT\s+/is', $query, $match)) {
// // Introduce the FIRST_ROWS Oracle query optimizer
// $query = substr($query, strlen($match[0]), strlen($query));
// $query = "SELECT /* +FIRST_ROWS */ " . $query;
// more at: http://marc.theaimsgroup.com/?l=php-db&m=99831958101212&w=2
// Perhaps this could be optimized with the use of Unions
$query = " SELECT $fields FROM".
" (SELECT rownum as linenum, $fields FROM".
' WHERE rownum <= '. ($from + $count) .
') WHERE linenum >= ' . ++ $from;
* Returns the next free id in a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true, the seqence is automatically
* created if it does not exist
* @return int the next id number in the sequence. DB_Error if problem.
* @see DB_common::nextID()
function nextId($seq_name, $ondemand = true )
$seqname = $this->getSequenceName ($seq_name);
$result = & $this->query(" SELECT ${seqname}.nextval FROM dual" );
* @param string $seq_name name of the new sequence
* @return int DB_OK on success. A DB_Error object is returned if
* @see DB_common::createSequence()
$seqname = $this->getSequenceName ($seq_name);
return $this->query(" CREATE SEQUENCE ${seqname}" );
* @param string $seq_name name of the sequence to be deleted
* @return int DB_OK on success. DB_Error if problems.
* @see DB_common::dropSequence()
$seqname = $this->getSequenceName ($seq_name);
return $this->query(" DROP SEQUENCE ${seqname}" );
* Gather information about an error, then use that info to create a
* DB error object and finally return that object.
* @param integer $errno PEAR error number (usually a DB constant) if
* manually raising an error
* @return object DB error object
* @see DB_common::errorCode()
* @see DB_common::raiseError()
null , null , null , $error['message']);
$error = @OCIError ($errno);
null , null , null , $error['message']);
* Returns the query needed to get some backend info
* @param string $type What kind of info you want to retrieve
* @return string The SQL query string
return 'SELECT table_name FROM user_tables';
* Returns information about a table or a result set.
* NOTE: only supports 'table' and 'flags' if <var>$result</var>
* NOTE: flags won't contain index information.
* @param object|string $result DB_result object from a query or a
* string containing the name of a table
* @param int $mode a valid tableInfo mode
* @return array an associative array with the information requested
* or an error object if something is wrong
* @see DB_common::tableInfo()
$case_func = 'strtolower';
* Probably received a table name.
* Create a result resource identifier.
$q_fields = 'SELECT column_name, data_type, data_length, '
. 'FROM user_tab_columns '
. " WHERE table_name='$result' ORDER BY column_id";
if (!$stmt = @OCIParse ($this->connection, $q_fields)) {
if (!@OCIExecute ($stmt, OCI_DEFAULT )) {
while (@OCIFetch ($stmt)) {
$res[$i]['table'] = $case_func($result);
$res[$i]['name'] = $case_func(@OCIResult ($stmt, 1 ));
$res[$i]['type'] = @OCIResult ($stmt, 2 );
$res[$i]['len'] = @OCIResult ($stmt, 3 );
$res[$i]['flags'] = (@OCIResult ($stmt, 4 ) == 'N') ? 'not_null' : '';
$res['order'][$res[$i]['name']] = $i;
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
@OCIFreeStatement ($stmt);
if (isset ($result->result )) {
* Probably received a result object.
* Extract the result resource identifier.
$result = $result->result;
* ELSE, probably received a result resource identifier.
* Depricated. Here for compatibility only.
$count = @OCINumCols ($result);
for ($i=0; $i< $count; $i++ ) {
$res[$i]['name'] = $case_func(@OCIColumnName ($result, $i+1 ));
$res[$i]['type'] = @OCIColumnType ($result, $i+1 );
$res[$i]['len'] = @OCIColumnSize ($result, $i+1 );
$res['order'][$res[$i]['name']] = $i;
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
$res['num_fields'] = $count;
Documentation generated on Mon, 11 Mar 2019 10:14:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|