MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file oci8.php

Documentation is available at oci8.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith, Frank M. Kromann                       |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: oci8.php,v 1.30 2006/05/14 05:51:45 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 Oracle driver for the schema reverse engineering module
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@dybnet.de>
  56.  */
  57. class MDB2_Driver_Reverse_oci8 extends MDB2_Driver_Reverse_Common
  58. {
  59.     // {{{ getTableFieldDefinition()
  60.  
  61.     /**
  62.      * Get the stucture of a field into an array
  63.      *
  64.      * @param string    $table         name of table that should be used in method
  65.      * @param string    $field_name     name of field that should be used in method
  66.      * @return mixed data array on success, a MDB2 error on failure
  67.      * @access public
  68.      */
  69.     function getTableFieldDefinition($table$field_name)
  70.     {
  71.         $db =$this->getDBInstance();
  72.         if (PEAR::isError($db)) {
  73.             return $db;
  74.         }
  75.  
  76.         $result $db->loadModule('Datatype'nulltrue);
  77.         if (PEAR::isError($result)) {
  78.             return $result;
  79.         }
  80.  
  81.         $table $db->quote($table'text');
  82.         $field_name $db->quote($field_name'text');
  83.         $query 'SELECT column_name name, data_type type';
  84.         $query.= ', data_length length, nullable, data_default "default"';
  85.         $query.= ' FROM user_tab_columns';
  86.         $query.= ' WHERE (table_name='.$table.' OR table_name='.strtoupper($table).')';
  87.         $query.= ' AND (column_name='.$field_name.' OR column_name='.strtoupper($field_name).')';
  88.         $query.= ' ORDER BY column_id';
  89.         $column $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  90.         if (PEAR::isError($column)) {
  91.             return $column;
  92.         }
  93.  
  94.         if (empty($column)) {
  95.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  96.                 'getTableFieldDefinition: it was not specified an existing table column');
  97.         }
  98.  
  99.         $column array_change_key_case($columnCASE_LOWER);
  100.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  101.             if ($db->options['field_case'== CASE_LOWER{
  102.                 $column['name'strtolower($column['name']);
  103.             else {
  104.                 $column['name'strtoupper($column['name']);
  105.             }
  106.         }
  107.         list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  108.         $notnull = false;
  109.         if (array_key_exists('nullable'$column&& $column['nullable'== 'N'{
  110.             $notnull = true;
  111.         }
  112.         $default = false;
  113.         if (array_key_exists('default'$column)) {
  114.             $default $column['default'];
  115.             if ($default === 'NULL'{
  116.                 $default = null;
  117.             }
  118.             if (is_null($default&& $notnull{
  119.                 $default '';
  120.             }
  121.         }
  122.         $definition = array();
  123.         foreach ($types as $key => $type{
  124.             $definition[$key= array(
  125.                 'type' => $type,
  126.                 'notnull' => $notnull,
  127.             );
  128.             if ($length > 0{
  129.                 $definition[$key]['length'$length;
  130.             }
  131.             if (!is_null($unsigned)) {
  132.                 $definition[$key]['unsigned'$unsigned;
  133.             }
  134.             if (!is_null($fixed)) {
  135.                 $definition[$key]['fixed'$fixed;
  136.             }
  137.             if ($default !== false{
  138.                 $definition[$key]['default'$default;
  139.             }
  140.         }
  141.         return $definition;
  142.     }
  143.  
  144.     // }}}
  145.  
  146.     // {{{ getTableIndexDefinition()
  147.  
  148.     /**
  149.      * Get the stucture of an index into an array
  150.      *
  151.      * @param string    $table      name of table that should be used in method
  152.      * @param string    $index_name name of index that should be used in method
  153.      * @return mixed data array on success, a MDB2 error on failure
  154.      * @access public
  155.      */
  156.     function getTableIndexDefinition($table$index_name)
  157.     {
  158.         $db =$this->getDBInstance();
  159.         if (PEAR::isError($db)) {
  160.             return $db;
  161.         }
  162.  
  163.         $table $db->quote($table'text');
  164.         $index_name $db->quote($db->getIndexName($index_name)'text');
  165.         $query 'SELECT * FROM user_indexes';
  166.         $query.= ' WHERE (table_name='.$table.' OR table_name='.strtoupper($table).')';
  167.         $query.= ' AND (index_name='.$index_name.' OR index_name='.strtoupper($index_name).')';
  168.         $row $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  169.         if (PEAR::isError($row)) {
  170.             return $row;
  171.         }
  172.         $definition = array();
  173.         if (!empty($row)) {
  174.             $row array_change_key_case($rowCASE_LOWER);
  175.             $key_name $row['index_name'];
  176.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  177.                 if ($db->options['field_case'== CASE_LOWER{
  178.                     $key_name strtolower($key_name);
  179.                 else {
  180.                     $key_name strtoupper($key_name);
  181.                 }
  182.             }
  183.             $query 'SELECT * FROM user_ind_columns';
  184.             $query.= ' WHERE (table_name='.$table.' OR table_name='.strtoupper($table).')';
  185.             $query.= ' AND (index_name='.$index_name.' OR index_name='.strtoupper($index_name).')';
  186.             $result $db->query($query);
  187.             if (PEAR::isError($result)) {
  188.                 return $result;
  189.             }
  190.             while ($colrow $result->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  191.                 $colrow array_change_key_case($colrowCASE_LOWER);
  192.                 $column_name $colrow['column_name'];
  193.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  194.                     if ($db->options['field_case'== CASE_LOWER{
  195.                         $column_name strtolower($column_name);
  196.                     else {
  197.                         $column_name strtoupper($column_name);
  198.                     }
  199.                 }
  200.                 $definition['fields'][$column_name= array();
  201.                 if (array_key_exists('descend'$colrow)) {
  202.                     $definition['fields'][$column_name]['sorting'=
  203.                         ($colrow['descend'== 'ASC' 'ascending' 'descending');
  204.                 }
  205.             }
  206.             $result->free();
  207.         }
  208.         if (!array_key_exists('fields'$definition)) {
  209.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  210.                 'getTableIndexDefinition: it was not specified an existing table index');
  211.         }
  212.         return $definition;
  213.     }
  214.  
  215.     // }}}
  216.     // {{{ getTableConstraintDefinition()
  217.  
  218.     /**
  219.      * Get the stucture of a constraint into an array
  220.      *
  221.      * @param string    $table      name of table that should be used in method
  222.      * @param string    $index_name name of index that should be used in method
  223.      * @return mixed data array on success, a MDB2 error on failure
  224.      * @access public
  225.      */
  226.     function getTableConstraintDefinition($table$index_name)
  227.     {
  228.         $db =$this->getDBInstance();
  229.         if (PEAR::isError($db)) {
  230.             return $db;
  231.         }
  232.  
  233.         if (strtolower($index_name!= 'primary'{
  234.             $index_name $db->getIndexName($index_name);
  235.         }
  236.  
  237.         $database_name $db->quote(strtoupper($db->dsn['username'])'text');
  238.         $index_name $db->quote($index_name'text');
  239.         $table $db->quote($table'text');
  240.         $query = "SELECT * FROM all_constraints WHERE owner = $database_name";
  241.         $query.= ' AND (table_name='.$table.' OR table_name='.strtoupper($table).')';
  242.         $query.= ' AND (index_name='.$index_name.' OR index_name='.strtoupper($index_name).')';
  243.         $result $db->query($query);
  244.         if (PEAR::isError($result)) {
  245.             return $result;
  246.         }
  247.         $definition = array();
  248.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  249.             $row array_change_key_case($rowCASE_LOWER);
  250.             $key_name $row['constraint_name'];
  251.             if ($row{
  252.                 $definition['primary'$row['constraint_type'== 'P';
  253.                 $definition['unique'$row['constraint_type'== 'U';
  254.  
  255.                 $query 'SELECT * FROM all_cons_columns WHERE constraint_name='.$db->quote($key_name'text');
  256.                 $query.= ' AND (table_name='.$table.' OR table_name='.strtoupper($table).')';
  257.                 $colres $db->query($query);
  258.                 if (PEAR::isError($colres)) {
  259.                     return $colres;
  260.                 }
  261.                 while ($colrow $colres->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  262.                     $colrow array_change_key_case($colrowCASE_LOWER);
  263.                     $column_name $colrow['column_name'];
  264.                     if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  265.                         if ($db->options['field_case'== CASE_LOWER{
  266.                             $column_name strtolower($column_name);
  267.                         else {
  268.                             $column_name strtoupper($column_name);
  269.                         }
  270.                     }
  271.                     $definition['fields'][$column_name= array();
  272.                 }
  273.             }
  274.         }
  275.         $result->free();
  276.         if (!array_key_exists('fields'$definition)) {
  277.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  278.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  279.         }
  280.         return $definition;
  281.     }
  282.  
  283.     // }}}
  284.     // {{{ getSequenceDefinition()
  285.  
  286.     /**
  287.      * Get the stucture of a sequence into an array
  288.      *
  289.      * @param string    $sequence   name of sequence that should be used in method
  290.      * @return mixed data array on success, a MDB2 error on failure
  291.      * @access public
  292.      */
  293.     function getSequenceDefinition($sequence)
  294.     {
  295.         $db =$this->getDBInstance();
  296.         if (PEAR::isError($db)) {
  297.             return $db;
  298.         }
  299.  
  300.         $sequence_name $db->quote($db->getSequenceName($sequence)'text');
  301.         $query 'SELECT last_number FROM user_sequences';
  302.         $query.= ' WHERE sequence_name='.$sequence_name.' OR sequence_name='.strtoupper($sequence_name);
  303.         $start $db->queryOne($query);
  304.         if (PEAR::isError($start)) {
  305.             return $start;
  306.         }
  307.         $start ($db->currId($sequence)+1);
  308.         if (PEAR::isError($start)) {
  309.             return $start;
  310.         }
  311.         $definition = array();
  312.         if ($start != 1{
  313.             $definition = array('start' => $start);
  314.         }
  315.         return $definition;
  316.     }
  317.  
  318.     // }}}
  319.     // {{{ tableInfo()
  320.  
  321.     /**
  322.      * Returns information about a table or a result set
  323.      *
  324.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  325.      * is a table name.
  326.      *
  327.      * NOTE: flags won't contain index information.
  328.      *
  329.      * @param object|string $result  MDB2_result object from a query or a
  330.      *                                  string containing the name of a table.
  331.      *                                  While this also accepts a query result
  332.      *                                  resource identifier, this behavior is
  333.      *                                  deprecated.
  334.      * @param int            $mode    a valid tableInfo mode
  335.      *
  336.      * @return array  an associative array with the information requested.
  337.      *                  A MDB2_Error object on failure.
  338.      *
  339.      * @see MDB2_Driver_Common::tableInfo()
  340.      */
  341.     function tableInfo($result$mode = null)
  342.     {
  343.         $db =$this->getDBInstance();
  344.         if (PEAR::isError($db)) {
  345.             return $db;
  346.         }
  347.  
  348.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  349.             if ($db->options['field_case'== CASE_LOWER{
  350.                 $case_func 'strtolower';
  351.             else {
  352.                 $case_func 'strtoupper';
  353.             }
  354.         else {
  355.             $case_func 'strval';
  356.         }
  357.  
  358.         $res = array();
  359.  
  360.         if (is_string($result)) {
  361.             /*
  362.              * Probably received a table name.
  363.              * Create a result resource identifier.
  364.              */
  365.             $query 'SELECT column_name, data_type, data_length, nullable';
  366.             $query.= ' FROM user_tab_columns';
  367.             $query.= ' WHERE table_name='.$db->quote(strtoupper($result)'text');
  368.             $query.= ' OR table_name='.$db->quote($result'text');
  369.             $query.= ' ORDER BY column_id';
  370.  
  371.             $stmt =$db->_doQuery($queryfalse);
  372.             if (PEAR::isError($stmt)) {
  373.                 return $stmt;
  374.             }
  375.  
  376.             $i = 0;
  377.             while (@OCIFetch($stmt)) {
  378.                 $res[$i= array(
  379.                     'table'  => $case_func($result),
  380.                     'name'   => $case_func(@OCIResult($stmt1)),
  381.                     'type'   => @OCIResult($stmt2),
  382.                     'length' => @OCIResult($stmt3),
  383.                     'flags'  => (@OCIResult($stmt4== 'N''not_null' '',
  384.                 );
  385.                 $res[$i]['mdb2type'$db->datatype->mapNativeDatatype($res[$i]);
  386.                 if ($mode MDB2_TABLEINFO_ORDER{
  387.                     $res['order'][$res[$i]['name']] $i;
  388.                 }
  389.                 if ($mode MDB2_TABLEINFO_ORDERTABLE{
  390.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  391.                 }
  392.                 $i++;
  393.             }
  394.  
  395.             if ($mode{
  396.                 $res['num_fields'$i;
  397.             }
  398.             @OCIFreeStatement($stmt);
  399.  
  400.         else {
  401.             if (MDB2::isResultCommon($result)) {
  402.                 /*
  403.                  * Probably received a result object.
  404.                  * Extract the result resource identifier.
  405.                  */
  406.                 $result $result->getResource();
  407.             }
  408.  
  409.             $res = array();
  410.  
  411.             $count @OCINumCols($result);
  412.             if ($mode{
  413.                 $res['num_fields'$count;
  414.             }
  415.             for ($i = 0; $i $count$i++{
  416.                 $res[$i= array(
  417.                     'table'  => '',
  418.                     'name'   => $case_func(@OCIColumnName($result$i+1)),
  419.                     'type'   => @OCIColumnType($result$i+1),
  420.                     'length' => @OCIColumnSize($result$i+1),
  421.                     'flags'  => '',
  422.                 );
  423.                 $res[$i]['mdb2type'$db->datatype->mapNativeDatatype($res[$i]);
  424.                 if ($mode MDB2_TABLEINFO_ORDER{
  425.                     $res['order'][$res[$i]['name']] $i;
  426.                 }
  427.                 if ($mode MDB2_TABLEINFO_ORDERTABLE{
  428.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  429.                 }
  430.             }
  431.         }
  432.         return $res;
  433.     }
  434. }
  435. ?>

Documentation generated on Mon, 11 Mar 2019 14:39:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.