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.45 2006/08/26 15:06:36 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.         $query 'SELECT column_name name, data_type "type", nullable, data_default "default"';
  82.         $query.= ', COALESCE(data_precision, data_length) "length", data_scale "scale"';
  83.         $query.= ' FROM user_tab_columns';
  84.         $query.= ' WHERE (table_name='.$db->quote($table'text').' OR table_name='.$db->quote(strtoupper($table)'text').')';
  85.         $query.= ' AND (column_name='.$db->quote($field_name'text').' OR column_name='.$db->quote(strtoupper($field_name)'text').')';
  86.         $query.= ' ORDER BY column_id';
  87.         $column $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  88.         if (PEAR::isError($column)) {
  89.             return $column;
  90.         }
  91.  
  92.         if (empty($column)) {
  93.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  94.                 'it was not specified an existing table column'__FUNCTION__);
  95.         }
  96.  
  97.         $column array_change_key_case($columnCASE_LOWER);
  98.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  99.             if ($db->options['field_case'== CASE_LOWER{
  100.                 $column['name'strtolower($column['name']);
  101.             else {
  102.                 $column['name'strtoupper($column['name']);
  103.             }
  104.         }
  105.         list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  106.         $notnull = false;
  107.         if (!empty($column['nullable']&& $column['nullable'== 'N'{
  108.             $notnull = true;
  109.         }
  110.         $default = false;
  111.         if (array_key_exists('default'$column)) {
  112.             $default $column['default'];
  113.             if ($default === 'NULL'{
  114.                 $default = null;
  115.             }
  116.             if (is_null($default&& $notnull{
  117.                 $default '';
  118.             }
  119.         }
  120.  
  121.         $definition[0= array('notnull' => $notnull'nativetype' => $column['type']);
  122.         if ($length > 0{
  123.             $definition[0]['length'$length;
  124.         }
  125.         if (!is_null($unsigned)) {
  126.             $definition[0]['unsigned'$unsigned;
  127.         }
  128.         if (!is_null($fixed)) {
  129.             $definition[0]['fixed'$fixed;
  130.         }
  131.         if ($default !== false{
  132.             $definition[0]['default'$default;
  133.         }
  134.         foreach ($types as $key => $type{
  135.             $definition[$key$definition[0];
  136.             if ($type == 'clob' || $type == 'blob'{
  137.                 unset($definition[$key]['default']);
  138.             }
  139.             $definition[$key]['type'$type;
  140.             $definition[$key]['mdb2type'$type;
  141.         }
  142.         return $definition;
  143.     }
  144.  
  145.     // }}}
  146.  
  147.     // {{{ getTableIndexDefinition()
  148.  
  149.     /**
  150.      * Get the stucture of an index into an array
  151.      *
  152.      * @param string    $table      name of table that should be used in method
  153.      * @param string    $index_name name of index that should be used in method
  154.      * @return mixed data array on success, a MDB2 error on failure
  155.      * @access public
  156.      */
  157.     function getTableIndexDefinition($table$index_name)
  158.     {
  159.         $db =$this->getDBInstance();
  160.         if (PEAR::isError($db)) {
  161.             return $db;
  162.         }
  163.  
  164.         $index_name $db->getIndexName($index_name);
  165.         $definition = array();
  166.  
  167.         $query 'SELECT * FROM user_ind_columns';
  168.         $query.= ' WHERE (table_name='.$db->quote($table'text').' OR table_name='.$db->quote(strtoupper($table)'text').')';
  169.         $query.= ' AND (index_name='.$db->quote($index_name'text').' OR index_name='.$db->quote(strtoupper($index_name)'text').')';
  170.         $result $db->query($query);
  171.         if (PEAR::isError($result)) {
  172.             return $result;
  173.         }
  174.         while ($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  175.             $row array_change_key_case($rowCASE_LOWER);
  176.             $column_name $row['column_name'];
  177.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  178.                 if ($db->options['field_case'== CASE_LOWER{
  179.                     $column_name strtolower($column_name);
  180.                 else {
  181.                     $column_name strtoupper($column_name);
  182.                 }
  183.             }
  184.             $definition['fields'][$column_name= array();
  185.             if (!empty($row['descend'])) {
  186.                 $definition['fields'][$column_name]['sorting'=
  187.                     ($row['descend'== 'ASC' 'ascending' 'descending');
  188.             }
  189.         }
  190.         $result->free();
  191.         if (empty($definition['fields'])) {
  192.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  193.                 'it was not specified an existing table index'__FUNCTION__);
  194.         }
  195.         return $definition;
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ getTableConstraintDefinition()
  200.  
  201.     /**
  202.      * Get the stucture of a constraint into an array
  203.      *
  204.      * @param string    $table      name of table that should be used in method
  205.      * @param string    $index_name name of index that should be used in method
  206.      * @return mixed data array on success, a MDB2 error on failure
  207.      * @access public
  208.      */
  209.     function getTableConstraintDefinition($table$index_name)
  210.     {
  211.         $db =$this->getDBInstance();
  212.         if (PEAR::isError($db)) {
  213.             return $db;
  214.         }
  215.  
  216.         if (strtolower($index_name!= 'primary'{
  217.             $index_name $db->getIndexName($index_name);
  218.         }
  219.  
  220.         $query 'SELECT all.constraint_type, cols.column_name';
  221.         $query.= ' FROM all_constraints AS all, all_cons_columns AS cols';
  222.         $query.= ' WHERE (all.table_name='.$db->quote($table'text').' OR all.table_name='.$db->quote(strtoupper($table)'text').')';
  223.         $query.= ' AND (all.index_name='.$db->quote($index_name'text').' OR all.index_name='.$db->quote(strtoupper($index_name)'text').')';
  224.         $query.= ' AND all.constraint_name = cols.constraint_name';
  225.         $query.= ' AND all.owner = '.$db->quote(strtoupper($db->dsn['username'])'text');
  226.         $result $db->query($query);
  227.         if (PEAR::isError($result)) {
  228.             return $result;
  229.         }
  230.         $definition = array();
  231.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  232.             $row array_change_key_case($rowCASE_LOWER);
  233.             $column_name $row['column_name'];
  234.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  235.                 if ($db->options['field_case'== CASE_LOWER{
  236.                     $column_name strtolower($column_name);
  237.                 else {
  238.                     $column_name strtoupper($column_name);
  239.                 }
  240.             }
  241.             $definition['fields'][$column_name= array();
  242.         }
  243.         $result->free();
  244.         if (empty($definition['fields'])) {
  245.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  246.                 'it was not specified an existing table constraint'__FUNCTION__);
  247.         }
  248.         if ($row['constraint_type'=== 'P'{
  249.             $definition['primary'= true;
  250.         elseif ($row['constraint_type'=== 'U'{
  251.             $definition['unique'= true;
  252.         }
  253.         return $definition;
  254.     }
  255.  
  256.     // }}}
  257.     // {{{ getSequenceDefinition()
  258.  
  259.     /**
  260.      * Get the stucture of a sequence into an array
  261.      *
  262.      * @param string    $sequence   name of sequence that should be used in method
  263.      * @return mixed data array on success, a MDB2 error on failure
  264.      * @access public
  265.      */
  266.     function getSequenceDefinition($sequence)
  267.     {
  268.         $db =$this->getDBInstance();
  269.         if (PEAR::isError($db)) {
  270.             return $db;
  271.         }
  272.  
  273.         $sequence_name $db->getSequenceName($sequence);
  274.         $query 'SELECT last_number FROM user_sequences';
  275.         $query.= ' WHERE sequence_name='.$db->quote($sequence_name'text');
  276.         $query.= ' OR sequence_name='.$db->quote(strtoupper($sequence_name)'text');
  277.         $start $db->queryOne($query);
  278.         if (PEAR::isError($start)) {
  279.             return $start;
  280.         }
  281.         $start ($db->currId($sequence)+1);
  282.         if (PEAR::isError($start)) {
  283.             return $start;
  284.         }
  285.         $definition = array();
  286.         if ($start != 1{
  287.             $definition = array('start' => $start);
  288.         }
  289.         return $definition;
  290.     }
  291.  
  292.     // }}}
  293.     // {{{ tableInfo()
  294.  
  295.     /**
  296.      * Returns information about a table or a result set
  297.      *
  298.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  299.      * is a table name.
  300.      *
  301.      * NOTE: flags won't contain index information.
  302.      *
  303.      * @param object|string $result  MDB2_result object from a query or a
  304.      *                                  string containing the name of a table.
  305.      *                                  While this also accepts a query result
  306.      *                                  resource identifier, this behavior is
  307.      *                                  deprecated.
  308.      * @param int            $mode    a valid tableInfo mode
  309.      *
  310.      * @return array  an associative array with the information requested.
  311.      *                  A MDB2_Error object on failure.
  312.      *
  313.      * @see MDB2_Driver_Common::tableInfo()
  314.      */
  315.     function tableInfo($result$mode = null)
  316.     {
  317.         if (is_string($result)) {
  318.            return parent::tableInfo($result$mode);
  319.         }
  320.  
  321.         $db =$this->getDBInstance();
  322.         if (PEAR::isError($db)) {
  323.             return $db;
  324.         }
  325.  
  326.         $id = MDB2::isResultCommon($result$result->getResource($result;
  327.         if (!is_resource($id)) {
  328.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  329.                 'Could not generate result ressource'__FUNCTION__);
  330.         }
  331.  
  332.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  333.             if ($db->options['field_case'== CASE_LOWER{
  334.                 $case_func 'strtolower';
  335.             else {
  336.                 $case_func 'strtoupper';
  337.             }
  338.         else {
  339.             $case_func 'strval';
  340.         }
  341.  
  342.         $count $result->numCols();
  343.         $res = array();
  344.  
  345.         if ($mode{
  346.             $res['num_fields'$count;
  347.         }
  348.  
  349.         $db->loadModule('Datatype'nulltrue);
  350.         for ($i = 0; $i $count$i++{
  351.             $column = array(
  352.                 'table'  => '',
  353.                 'name'   => $case_func(@OCIColumnName($resource$i+1)),
  354.                 'type'   => @OCIColumnType($resource$i+1),
  355.                 'length' => @OCIColumnSize($resource$i+1),
  356.                 'flags'  => '',
  357.             );
  358.             $res[$i$column;
  359.             $res[$i]['mdb2type'$db->datatype->mapNativeDatatype($res[$i]);
  360.             if ($mode MDB2_TABLEINFO_ORDER{
  361.                 $res['order'][$res[$i]['name']] $i;
  362.             }
  363.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  364.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  365.             }
  366.         }
  367.         return $res;
  368.     }
  369. }
  370. ?>

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