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.27 2006/04/15 11:36:07 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_ERRORnullnull,
  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$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 ($unsigned{
  132.                 $definition[$key]['unsigned'= true;
  133.             }
  134.             if ($default !== false{
  135.                 $definition[$key]['default'$default;
  136.             }
  137.         }
  138.         return $definition;
  139.     }
  140.  
  141.     // }}}
  142.  
  143.     // {{{ getTableIndexDefinition()
  144.  
  145.     /**
  146.      * get the stucture of an index into an array
  147.      *
  148.      * @param string    $table      name of table that should be used in method
  149.      * @param string    $index_name name of index that should be used in method
  150.      * @return mixed data array on success, a MDB2 error on failure
  151.      * @access public
  152.      */
  153.     function getTableIndexDefinition($table$index_name)
  154.     {
  155.         $db =$this->getDBInstance();
  156.         if (PEAR::isError($db)) {
  157.             return $db;
  158.         }
  159.  
  160.         $table $db->quote($table'text');
  161.         $index_name $db->quote($db->getIndexName($index_name)'text');
  162.         $query 'SELECT * FROM user_indexes';
  163.         $query.= ' WHERE (table_name='.$table.' OR table_name='.strtoupper($table).')';
  164.         $query.= ' AND (index_name='.$index_name.' OR index_name='.strtoupper($index_name).')';
  165.         $row $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  166.         if (PEAR::isError($row)) {
  167.             return $row;
  168.         }
  169.         $definition = array();
  170.         if (!empty($row)) {
  171.             $row array_change_key_case($rowCASE_LOWER);
  172.             $key_name $row['index_name'];
  173.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  174.                 if ($db->options['field_case'== CASE_LOWER{
  175.                     $key_name strtolower($key_name);
  176.                 else {
  177.                     $key_name strtoupper($key_name);
  178.                 }
  179.             }
  180.             $query 'SELECT * FROM user_ind_columns';
  181.             $query.= ' WHERE (table_name='.$table.' OR table_name='.strtoupper($table).')';
  182.             $query.= ' AND (index_name='.$index_name.' OR index_name='.strtoupper($index_name).')';
  183.             $result $db->query($query);
  184.             if (PEAR::isError($result)) {
  185.                 return $result;
  186.             }
  187.             while ($colrow $result->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  188.                 $colrow array_change_key_case($colrowCASE_LOWER);
  189.                 $column_name $colrow['column_name'];
  190.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  191.                     if ($db->options['field_case'== CASE_LOWER{
  192.                         $column_name strtolower($column_name);
  193.                     else {
  194.                         $column_name strtoupper($column_name);
  195.                     }
  196.                 }
  197.                 $definition['fields'][$column_name= array();
  198.                 if (array_key_exists('descend'$colrow)) {
  199.                     $definition['fields'][$column_name]['sorting'=
  200.                         ($colrow['descend'== 'ASC' 'ascending' 'descending');
  201.                 }
  202.             }
  203.             $result->free();
  204.         }
  205.         if (!array_key_exists('fields'$definition)) {
  206.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  207.                 'getTableIndexDefinition: it was not specified an existing table index');
  208.         }
  209.         return $definition;
  210.     }
  211.  
  212.     // }}}
  213.     // {{{ getTableConstraintDefinition()
  214.  
  215.     /**
  216.      * get the stucture of a constraint into an array
  217.      *
  218.      * @param string    $table      name of table that should be used in method
  219.      * @param string    $index_name name of index that should be used in method
  220.      * @return mixed data array on success, a MDB2 error on failure
  221.      * @access public
  222.      */
  223.     function getTableConstraintDefinition($table$index_name)
  224.     {
  225.         $db =$this->getDBInstance();
  226.         if (PEAR::isError($db)) {
  227.             return $db;
  228.         }
  229.  
  230.         if (strtolower($index_name!= 'primary'{
  231.             $index_name $db->getIndexName($index_name);
  232.         }
  233.  
  234.         $database_name $db->quote(strtoupper($db->dsn['username'])'text');
  235.         $index_name $db->quote($index_name'text');
  236.         $table $db->quote($table'text');
  237.         $query = "SELECT * FROM all_constraints WHERE owner = $database_name";
  238.         $query.= ' AND (table_name='.$table.' OR table_name='.strtoupper($table).')';
  239.         $query.= ' AND (index_name='.$index_name.' OR index_name='.strtoupper($index_name).')';
  240.         $result $db->query($query);
  241.         if (PEAR::isError($result)) {
  242.             return $result;
  243.         }
  244.         $definition = array();
  245.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  246.             $row array_change_key_case($rowCASE_LOWER);
  247.             $key_name $row['constraint_name'];
  248.             if ($row{
  249.                 $definition['primary'$row['constraint_type'== 'P';
  250.                 $definition['unique'$row['constraint_type'== 'U';
  251.  
  252.                 $query 'SELECT * FROM all_cons_columns WHERE constraint_name='.$db->quote($key_name'text');
  253.                 $query.= ' AND (table_name='.$table.' OR table_name='.strtoupper($table).')';
  254.                 $colres $db->query($query);
  255.                 if (PEAR::isError($colres)) {
  256.                     return $colres;
  257.                 }
  258.                 while ($colrow $colres->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  259.                     $colrow array_change_key_case($colrowCASE_LOWER);
  260.                     $column_name $colrow['column_name'];
  261.                     if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  262.                         if ($db->options['field_case'== CASE_LOWER{
  263.                             $column_name strtolower($column_name);
  264.                         else {
  265.                             $column_name strtoupper($column_name);
  266.                         }
  267.                     }
  268.                     $definition['fields'][$column_name= array();
  269.                 }
  270.             }
  271.         }
  272.         $result->free();
  273.         if (!array_key_exists('fields'$definition)) {
  274.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  275.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  276.         }
  277.         return $definition;
  278.     }
  279.  
  280.     // }}}
  281.     // {{{ getSequenceDefinition()
  282.  
  283.     /**
  284.      * get the stucture of a sequence into an array
  285.      *
  286.      * @param string    $sequence   name of sequence that should be used in method
  287.      * @return mixed data array on success, a MDB2 error on failure
  288.      * @access public
  289.      */
  290.     function getSequenceDefinition($sequence)
  291.     {
  292.         $db =$this->getDBInstance();
  293.         if (PEAR::isError($db)) {
  294.             return $db;
  295.         }
  296.  
  297.         $sequence_name $db->quote($db->getSequenceName($sequence)'text');
  298.         $query 'SELECT last_number FROM user_sequences';
  299.         $query.= ' WHERE sequence_name='.$sequence_name.' OR sequence_name='.strtoupper($sequence_name);
  300.         $start $db->queryOne($query);
  301.         if (PEAR::isError($start)) {
  302.             return $start;
  303.         }
  304.         $start ($db->currId($sequence)+1);
  305.         if (PEAR::isError($start)) {
  306.             return $start;
  307.         }
  308.         $definition = array();
  309.         if ($start != 1{
  310.             $definition = array('start' => $start);
  311.         }
  312.         return $definition;
  313.     }
  314.  
  315.     // }}}
  316.     // {{{ tableInfo()
  317.  
  318.     /**
  319.      * Returns information about a table or a result set
  320.      *
  321.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  322.      * is a table name.
  323.      *
  324.      * NOTE: flags won't contain index information.
  325.      *
  326.      * @param object|string $result  MDB2_result object from a query or a
  327.      *                                  string containing the name of a table.
  328.      *                                  While this also accepts a query result
  329.      *                                  resource identifier, this behavior is
  330.      *                                  deprecated.
  331.      * @param int            $mode    a valid tableInfo mode
  332.      *
  333.      * @return array  an associative array with the information requested.
  334.      *                  A MDB2_Error object on failure.
  335.      *
  336.      * @see MDB2_Driver_Common::tableInfo()
  337.      */
  338.     function tableInfo($result$mode = null)
  339.     {
  340.         $db =$this->getDBInstance();
  341.         if (PEAR::isError($db)) {
  342.             return $db;
  343.         }
  344.  
  345.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  346.             if ($db->options['field_case'== CASE_LOWER{
  347.                 $case_func 'strtolower';
  348.             else {
  349.                 $case_func 'strtoupper';
  350.             }
  351.         else {
  352.             $case_func 'strval';
  353.         }
  354.  
  355.         $res = array();
  356.  
  357.         if (is_string($result)) {
  358.             /*
  359.              * Probably received a table name.
  360.              * Create a result resource identifier.
  361.              */
  362.             $query 'SELECT column_name, data_type, data_length, nullable';
  363.             $query.= ' FROM user_tab_columns';
  364.             $query.= ' WHERE table_name='.$db->quote(strtoupper($result)'text');
  365.             $query.= ' OR table_name='.$db->quote($result'text');
  366.             $query.= ' ORDER BY column_id';
  367.  
  368.             $stmt $db->_doQuery($queryfalse);
  369.             if (PEAR::isError($stmt)) {
  370.                 return $stmt;
  371.             }
  372.  
  373.             $i = 0;
  374.             while (@OCIFetch($stmt)) {
  375.                 $res[$i= array(
  376.                     'table'  => $case_func($result),
  377.                     'name'   => $case_func(@OCIResult($stmt1)),
  378.                     'type'   => @OCIResult($stmt2),
  379.                     'length' => @OCIResult($stmt3),
  380.                     'flags'  => (@OCIResult($stmt4== 'N''not_null' '',
  381.                 );
  382.                 $res[$i]['mdb2type'$db->datatype->mapNativeDatatype($res[$i]);
  383.                 if ($mode MDB2_TABLEINFO_ORDER{
  384.                     $res['order'][$res[$i]['name']] $i;
  385.                 }
  386.                 if ($mode MDB2_TABLEINFO_ORDERTABLE{
  387.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  388.                 }
  389.                 $i++;
  390.             }
  391.  
  392.             if ($mode{
  393.                 $res['num_fields'$i;
  394.             }
  395.             @OCIFreeStatement($stmt);
  396.  
  397.         else {
  398.             if (MDB2::isResultCommon($result)) {
  399.                 /*
  400.                  * Probably received a result object.
  401.                  * Extract the result resource identifier.
  402.                  */
  403.                 $result $result->getResource();
  404.             }
  405.  
  406.             $res = array();
  407.  
  408.             $count @OCINumCols($result);
  409.             if ($mode{
  410.                 $res['num_fields'$count;
  411.             }
  412.             for ($i = 0; $i $count$i++{
  413.                 $res[$i= array(
  414.                     'table'  => '',
  415.                     'name'   => $case_func(@OCIColumnName($result$i+1)),
  416.                     'type'   => @OCIColumnType($result$i+1),
  417.                     'length' => @OCIColumnSize($result$i+1),
  418.                     'flags'  => '',
  419.                 );
  420.                 $res[$i]['mdb2type'$db->datatype->mapNativeDatatype($res[$i]);
  421.                 if ($mode MDB2_TABLEINFO_ORDER{
  422.                     $res['order'][$res[$i]['name']] $i;
  423.                 }
  424.                 if ($mode MDB2_TABLEINFO_ORDERTABLE{
  425.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  426.                 }
  427.             }
  428.         }
  429.         return $res;
  430.     }
  431. }
  432. ?>

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