Source for file oci8.php
Documentation is available at oci8.php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann, Lorenzo Alberton |
// | 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. |
// +----------------------------------------------------------------------+
// | Authors: Lukas Smith <smith@pooteeweet.org> |
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
// $Id: oci8.php 327310 2012-08-27 15:16:18Z danielc $
require_once 'MDB2/Driver/Reverse/Common.php';
* MDB2 Oracle driver for the schema reverse engineering module
* @author Lukas Smith <smith@dybnet.de>
// {{{ getTableFieldDefinition()
* Get the structure of a field into an array
* @param string $table_name name of table that should be used in method
* @param string $field_name name of field that should be used in method
* @return mixed data array on success, a MDB2 error on failure
$db = $this->getDBInstance ();
if (MDB2 ::isError ($db)) {
$result = $db->loadModule ('Datatype', null , true );
if (MDB2 ::isError ($result)) {
list ($owner, $table) = $this->splitTableSchema ($table_name);
$owner = $db->dsn ['username'];
$query = 'SELECT column_name name,
COALESCE(data_precision, data_length) "length",
WHERE (table_name=? OR table_name=?)
AND (column_name=? OR column_name=?)
$stmt = $db->prepare ($query);
if (MDB2 ::isError ($stmt)) {
$result = $stmt->execute ($args);
if (MDB2 ::isError ($result)) {
$column = $result->fetchRow (MDB2_FETCHMODE_ASSOC );
if (MDB2 ::isError ($column)) {
return $db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
$field_name . ' is not a column in table ' . $table_name, __FUNCTION__ );
if ($db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE ) {
if ($db->options ['field_case'] == CASE_LOWER ) {
$mapped_datatype = $db->datatype ->mapNativeDatatype ($column);
if (MDB2 ::isError ($mapped_datatype)) {
list ($types, $length, $unsigned, $fixed) = $mapped_datatype;
if (!empty ($column['nullable']) && $column['nullable'] == 'N') {
$default = $column['default'];
if ($default === 'NULL') {
if ((null === $default) && $notnull) {
$definition[0 ] = array ('notnull' => $notnull, 'nativetype' => $column['type']);
$definition[0 ]['length'] = $length;
if (null !== $unsigned) {
$definition[0 ]['unsigned'] = $unsigned;
$definition[0 ]['fixed'] = $fixed;
if ($default !== false ) {
$definition[0 ]['default'] = $default;
foreach ($types as $key => $type) {
$definition[$key] = $definition[0 ];
if ($type == 'clob' || $type == 'blob') {
unset ($definition[$key]['default']);
$definition[$key]['type'] = $type;
$definition[$key]['mdb2type'] = $type;
if ($type == 'integer') {
$query= "SELECT trigger_body
AND triggering_event='INSERT'
AND trigger_type='BEFORE EACH ROW'";
// ^^ pretty reasonable mimic for "auto_increment" in oracle?
$stmt = $db->prepare ($query);
if (MDB2 ::isError ($stmt)) {
if (MDB2 ::isError ($result)) {
while ($triggerstr = $result->fetchOne ()) {
if (preg_match('/.*SELECT\W+(.+)\.nextval +into +\:NEW\.'. $field_name. ' +FROM +dual/im', $triggerstr, $matches)) {
$definition[0 ]['autoincrement'] = $matches[1 ];
// {{{ getTableIndexDefinition()
* Get the structure of an index into an array
* @param string $table_name name of table that should be used in method
* @param string $index_name name of index that should be used in method
* @return mixed data array on success, a MDB2 error on failure
$db = $this->getDBInstance ();
if (MDB2 ::isError ($db)) {
list ($owner, $table) = $this->splitTableSchema ($table_name);
$owner = $db->dsn ['username'];
$query = "SELECT aic.column_name,
LEFT JOIN all_constraints alc
ON aic.index_name = alc.constraint_name
AND aic.table_name = alc.table_name
AND aic.table_owner = alc.owner
WHERE (aic.table_name=? OR aic.table_name=?)
AND (aic.index_name=? OR aic.index_name=?)
AND (aic.table_owner=? OR aic.table_owner=?)
ORDER BY column_position";
$stmt = $db->prepare ($query);
if (MDB2 ::isError ($stmt)) {
$indexnames = array_unique(array ($db->getIndexName ($index_name), $index_name));
$result = $stmt->execute ($args);
if (MDB2 ::isError ($result)) {
$row = $result->fetchRow (MDB2_FETCHMODE_ASSOC );
if (MDB2 ::isError ($row)) {
return $db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
$index_name. ' is not an index on table '. $table_name, __FUNCTION__ );
if ($row['constraint_type'] == 'U' || $row['constraint_type'] == 'P') {
return $db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
$index_name. ' is a constraint, not an index on table '. $table_name, __FUNCTION__ );
$column_name = $row['column_name'];
if ($db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE ) {
if ($db->options ['field_case'] == CASE_LOWER ) {
$definition['fields'][$column_name] = array (
'position' => (int) $row['column_position'],
if (!empty ($row['descend'])) {
$definition['fields'][$column_name]['sorting'] =
($row['descend'] == 'ASC' ? 'ascending' : 'descending');
$row = $result->fetchRow (MDB2_FETCHMODE_ASSOC );
if (empty ($definition['fields'])) {
return $db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
$index_name. ' is not an index on table '. $table_name, __FUNCTION__ );
// {{{ getTableConstraintDefinition()
* Get the structure of a constraint into an array
* @param string $table_name name of table that should be used in method
* @param string $constraint_name name of constraint that should be used in method
* @return mixed data array on success, a MDB2 error on failure
$db = $this->getDBInstance ();
if (MDB2 ::isError ($db)) {
list ($owner, $table) = $this->splitTableSchema ($table_name);
$owner = $db->dsn ['username'];
$query = 'SELECT alc.constraint_name,
CASE alc.constraint_type WHEN \'P\' THEN 1 ELSE 0 END "primary",
CASE alc.constraint_type WHEN \'R\' THEN 1 ELSE 0 END "foreign",
CASE alc.constraint_type WHEN \'U\' THEN 1 ELSE 0 END "unique",
CASE alc.constraint_type WHEN \'C\' THEN 1 ELSE 0 END "check",
alc.DELETE_RULE "ondelete",
\'NO ACTION\' "onupdate",
CASE alc.deferrable WHEN \'NOT DEFERRABLE\' THEN 0 ELSE 1 END "deferrable",
CASE alc.deferred WHEN \'IMMEDIATE\' THEN 0 ELSE 1 END "initiallydeferred",
r_alc.table_name "references_table",
r_cols.column_name "references_field",
r_cols.position "references_field_position"
FROM all_cons_columns cols
LEFT JOIN all_constraints alc
ON alc.constraint_name = cols.constraint_name
AND alc.owner = cols.owner
LEFT JOIN all_constraints r_alc
ON alc.r_constraint_name = r_alc.constraint_name
AND alc.r_owner = r_alc.owner
LEFT JOIN all_cons_columns r_cols
ON r_alc.constraint_name = r_cols.constraint_name
AND r_alc.owner = r_cols.owner
AND cols.position = r_cols.position
WHERE (alc.constraint_name=? OR alc.constraint_name=?)
AND alc.constraint_name = cols.constraint_name
AND (alc.owner=? OR alc.owner=?)';
$query.= ' AND (alc.table_name=? OR alc.table_name=?)';
$stmt = $db->prepare ($query);
if (MDB2 ::isError ($stmt)) {
$constraintnames = array_unique(array ($db->getIndexName ($constraint_name), $constraint_name));
$result = $stmt->execute ($args);
if (MDB2 ::isError ($result)) {
$row = $result->fetchRow (MDB2_FETCHMODE_ASSOC );
if (MDB2 ::isError ($row)) {
'primary' => (boolean) $row['primary'],
'unique' => (boolean) $row['unique'],
'foreign' => (boolean) $row['foreign'],
'check' => (boolean) $row['check'],
'deferrable' => (boolean) $row['deferrable'],
'initiallydeferred' => (boolean) $row['initiallydeferred'],
'ondelete' => $row['ondelete'],
'onupdate' => $row['onupdate'],
'match' => $row['match'],
if ($definition['check']) {
// pattern match constraint for check constraint values into enum-style output:
$enumregex = '/'. $row['column_name']. ' in \((.+?)\)/i';
if (preg_match($enumregex, $row['search_condition'], $rangestr)) {
$definition['fields'][$column_name] = array ();
$allowed = explode(',', $rangestr[1 ]);
foreach ($allowed as $val) {
array_push($definition['fields'][$column_name], $val);
$column_name = $row['column_name'];
if ($db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE ) {
if ($db->options ['field_case'] == CASE_LOWER ) {
$definition['fields'][$column_name] = array (
'position' => (int) $row['position']
$ref_column_name = $row['references_field'];
$ref_table_name = $row['references_table'];
if ($db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE ) {
if ($db->options ['field_case'] == CASE_LOWER ) {
$definition['references']['table'] = $ref_table_name;
$definition['references']['fields'][$ref_column_name] = array (
'position' => (int) $row['references_field_position']
$row = $result->fetchRow (MDB2_FETCHMODE_ASSOC );
if (empty ($definition['fields'])) {
return $db->raiseError (MDB2_ERROR_NOT_FOUND , null , null ,
$constraint_name . ' is not a constraint on table '. $table_name, __FUNCTION__ );
// {{{ getSequenceDefinition()
* Get the structure of a sequence into an array
* @param string $sequence name of sequence that should be used in method
* @return mixed data array on success, a MDB2 error on failure
$db = $this->getDBInstance ();
if (MDB2 ::isError ($db)) {
$sequence_name = $db->getSequenceName ($sequence);
$query = 'SELECT last_number FROM user_sequences';
$query.= ' WHERE sequence_name='. $db->quote ($sequence_name, 'text');
$query.= ' OR sequence_name='. $db->quote (strtoupper($sequence_name), 'text');
$start = $db->queryOne ($query, 'integer');
if (MDB2 ::isError ($start)) {
$definition = array ('start' => $start);
// {{{ getTriggerDefinition()
* Get the structure of a trigger into an array
* WARNING: this function is experimental and may change the returned value
* at any time until labelled as non-experimental
* @param string $trigger name of trigger that should be used in method
* @return mixed data array on success, a MDB2 error on failure
$db = $this->getDBInstance ();
if (MDB2 ::isError ($db)) {
$query = 'SELECT trigger_name,
triggering_event trigger_event,
description trigger_comment,
WHERE trigger_name = \''. strtoupper($trigger). '\'';
'trigger_name' => 'text',
'trigger_body' => 'text',
'trigger_type' => 'text',
'trigger_event' => 'text',
'trigger_comment' => 'text',
'trigger_enabled' => 'boolean',
$result = $db->queryRow ($query, $types, MDB2_FETCHMODE_ASSOC );
if (MDB2 ::isError ($result)) {
if (!empty ($result['trigger_type'])) {
//$result['trigger_type'] = array_shift(explode(' ', $result['trigger_type']));
$result['trigger_type'] = preg_replace('/(\S+).*/', '\\1', $result['trigger_type']);
* 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 MDB2_result object from a query or a
* string containing the name of a table.
* While this also accepts a query result
* resource identifier, this behavior is
* @param int $mode a valid tableInfo mode
* @return array an associative array with the information requested.
* A MDB2_Error object on failure.
* @see MDB2_Driver_Common::tableInfo()
return parent ::tableInfo ($result, $mode);
$db = $this->getDBInstance ();
if (MDB2 ::isError ($db)) {
$resource = MDB2 ::isResultCommon ($result) ? $result->getResource () : $result;
return $db->raiseError (MDB2_ERROR_NEED_MORE_DATA , null , null ,
'Could not generate result resource', __FUNCTION__ );
if ($db->options ['portability'] & MDB2_PORTABILITY_FIX_CASE ) {
if ($db->options ['field_case'] == CASE_LOWER ) {
$case_func = 'strtolower';
$case_func = 'strtoupper';
$count = @OCINumCols ($resource);
$res['num_fields'] = $count;
$db->loadModule ('Datatype', null , true );
for ($i = 0; $i < $count; $i++ ) {
'name' => $case_func(@OCIColumnName ($resource, $i+1 )),
'type' => @OCIColumnType ($resource, $i+1 ),
'length' => @OCIColumnSize ($resource, $i+1 ),
$res[$i]['mdb2type'] = $db->datatype ->mapNativeDatatype ($res[$i]);
if ($mode & MDB2_TABLEINFO_ORDER ) {
$res['order'][$res[$i]['name']] = $i;
if ($mode & MDB2_TABLEINFO_ORDERTABLE ) {
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
Documentation generated on Mon, 11 Mar 2019 15:51:20 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|