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.57 2007/03/04 23:40:51 quipo 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 structure 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.         $mapped_datatype $db->datatype->mapNativeDatatype($column);
  106.         if (PEAR::IsError($mapped_datatype)) {
  107.             return $mapped_datatype;
  108.         }
  109.         list($types$length$unsigned$fixed$mapped_datatype;
  110.         $notnull = false;
  111.         if (!empty($column['nullable']&& $column['nullable'== 'N'{
  112.             $notnull = true;
  113.         }
  114.         $default = false;
  115.         if (array_key_exists('default'$column)) {
  116.             $default $column['default'];
  117.             if ($default === 'NULL'{
  118.                 $default = null;
  119.             }
  120.             if (is_null($default&& $notnull{
  121.                 $default '';
  122.             }
  123.         }
  124.  
  125.         $definition[0= array('notnull' => $notnull'nativetype' => $column['type']);
  126.         if (!is_null($length)) {
  127.             $definition[0]['length'$length;
  128.         }
  129.         if (!is_null($unsigned)) {
  130.             $definition[0]['unsigned'$unsigned;
  131.         }
  132.         if (!is_null($fixed)) {
  133.             $definition[0]['fixed'$fixed;
  134.         }
  135.         if ($default !== false{
  136.             $definition[0]['default'$default;
  137.         }
  138.         foreach ($types as $key => $type{
  139.             $definition[$key$definition[0];
  140.             if ($type == 'clob' || $type == 'blob'{
  141.                 unset($definition[$key]['default']);
  142.             }
  143.             $definition[$key]['type'$type;
  144.             $definition[$key]['mdb2type'$type;
  145.         }
  146.         return $definition;
  147.     }
  148.  
  149.     // }}}
  150.  
  151.     // {{{ getTableIndexDefinition()
  152.  
  153.     /**
  154.      * Get the structure of an index into an array
  155.      *
  156.      * @param string    $table      name of table that should be used in method
  157.      * @param string    $index_name name of index that should be used in method
  158.      * @return mixed data array on success, a MDB2 error on failure
  159.      * @access public
  160.      */
  161.     function getTableIndexDefinition($table$index_name)
  162.     {
  163.         $db =$this->getDBInstance();
  164.         if (PEAR::isError($db)) {
  165.             return $db;
  166.         }
  167.         
  168.         $query 'SELECT * FROM user_ind_columns';
  169.         $query.= ' WHERE (table_name='.$db->quote($table'text').' OR table_name='.$db->quote(strtoupper($table)'text').')';
  170.         $query.= ' AND (index_name=%s OR index_name=%s)';
  171.         $index_name_mdb2 $db->getIndexName($index_name);
  172.         $sql sprintf($query,
  173.             $db->quote($index_name_mdb2'text'),
  174.             $db->quote(strtoupper($index_name_mdb2)'text')
  175.         );
  176.         $result $db->queryRow($sql);
  177.         if (!PEAR::isError($result&& !is_null($result)) {
  178.             // apply 'idxname_format' only if the query succeeded, otherwise
  179.             // fallback to the given $index_name, without transformation
  180.             $index_name $index_name_mdb2;
  181.         }
  182.         $sql sprintf($query,
  183.             $db->quote($index_name'text'),
  184.             $db->quote(strtoupper($index_name)'text')
  185.         );
  186.         $result $db->query($sql);
  187.         if (PEAR::isError($result)) {
  188.             return $result;
  189.         }
  190.  
  191.         $definition = array();
  192.         while ($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  193.             $row array_change_key_case($rowCASE_LOWER);
  194.             $column_name $row['column_name'];
  195.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  196.                 if ($db->options['field_case'== CASE_LOWER{
  197.                     $column_name strtolower($column_name);
  198.                 else {
  199.                     $column_name strtoupper($column_name);
  200.                 }
  201.             }
  202.             $definition['fields'][$column_name= array();
  203.             if (!empty($row['descend'])) {
  204.                 $definition['fields'][$column_name]['sorting'=
  205.                     ($row['descend'== 'ASC' 'ascending' 'descending');
  206.             }
  207.         }
  208.         $result->free();
  209.         if (empty($definition['fields'])) {
  210.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  211.                 'it was not specified an existing table index'__FUNCTION__);
  212.         }
  213.         return $definition;
  214.     }
  215.  
  216.     // }}}
  217.     // {{{ getTableConstraintDefinition()
  218.  
  219.     /**
  220.      * Get the structure of a constraint into an array
  221.      *
  222.      * @param string    $table      name of table that should be used in method
  223.      * @param string    $index_name name of index that should be used in method
  224.      * @return mixed data array on success, a MDB2 error on failure
  225.      * @access public
  226.      */
  227.     function getTableConstraintDefinition($table$index_name)
  228.     {
  229.         $db =$this->getDBInstance();
  230.         if (PEAR::isError($db)) {
  231.             return $db;
  232.         }
  233.         
  234.         $query 'SELECT "all".constraint_type, cols.column_name';
  235.         $query.= ' FROM all_constraints "all", all_cons_columns cols';
  236.         $query.= ' WHERE ("all".table_name='.$db->quote($table'text').' OR "all".table_name='.$db->quote(strtoupper($table)'text').')';
  237.         $query.= ' AND ("all".index_name=%s OR "all".index_name=%s)';
  238.         $query.= ' AND "all".constraint_name = cols.constraint_name';
  239.         $query.= ' AND "all".owner = '.$db->quote(strtoupper($db->dsn['username'])'text');
  240.         if (strtolower($index_name!= 'primary'{
  241.             $index_name_mdb2 $db->getIndexName($index_name);
  242.             $sql sprintf($query,
  243.                 $db->quote($index_name_mdb2'text'),
  244.                 $db->quote(strtoupper($index_name_mdb2)'text')
  245.             );
  246.             $result $db->queryRow($sql);
  247.             if (!PEAR::isError($result&& !is_null($result)) {
  248.                 // apply 'idxname_format' only if the query succeeded, otherwise
  249.                 // fallback to the given $index_name, without transformation
  250.                 $index_name $index_name_mdb2;
  251.             }
  252.         }
  253.         $sql sprintf($query,
  254.             $db->quote($index_name'text'),
  255.             $db->quote(strtoupper($index_name)'text')
  256.         );
  257.         $result $db->query($sql);
  258.         if (PEAR::isError($result)) {
  259.             return $result;
  260.         }
  261.         $definition = array();
  262.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  263.             $row array_change_key_case($rowCASE_LOWER);
  264.             $column_name $row['column_name'];
  265.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  266.                 if ($db->options['field_case'== CASE_LOWER{
  267.                     $column_name strtolower($column_name);
  268.                 else {
  269.                     $column_name strtoupper($column_name);
  270.                 }
  271.             }
  272.             $definition['fields'][$column_name= array();
  273.         }
  274.         $result->free();
  275.         if (empty($definition['fields'])) {
  276.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  277.                 'it was not specified an existing table constraint'__FUNCTION__);
  278.         }
  279.         if ($row['constraint_type'=== 'P'{
  280.             $definition['primary'= true;
  281.         elseif ($row['constraint_type'=== 'U'{
  282.             $definition['unique'= true;
  283.         }
  284.         return $definition;
  285.     }
  286.  
  287.     // }}}
  288.     // {{{ getSequenceDefinition()
  289.  
  290.     /**
  291.      * Get the structure of a sequence into an array
  292.      *
  293.      * @param string    $sequence   name of sequence that should be used in method
  294.      * @return mixed data array on success, a MDB2 error on failure
  295.      * @access public
  296.      */
  297.     function getSequenceDefinition($sequence)
  298.     {
  299.         $db =$this->getDBInstance();
  300.         if (PEAR::isError($db)) {
  301.             return $db;
  302.         }
  303.  
  304.         $sequence_name $db->getSequenceName($sequence);
  305.         $query 'SELECT last_number FROM user_sequences';
  306.         $query.= ' WHERE sequence_name='.$db->quote($sequence_name'text');
  307.         $query.= ' OR sequence_name='.$db->quote(strtoupper($sequence_name)'text');
  308.         $start $db->queryOne($query'integer');
  309.         if (PEAR::isError($start)) {
  310.             return $start;
  311.         }
  312.         $definition = array();
  313.         if ($start != 1{
  314.             $definition = array('start' => $start);
  315.         }
  316.         return $definition;
  317.     }
  318.  
  319.     // }}}
  320.     // {{{ getTriggerDefinition()
  321.  
  322.     /**
  323.      * Get the structure of a trigger into an array
  324.      *
  325.      * EXPERIMENTAL
  326.      *
  327.      * WARNING: this function is experimental and may change the returned value
  328.      * at any time until labelled as non-experimental
  329.      *
  330.      * @param string    $trigger    name of trigger that should be used in method
  331.      * @return mixed data array on success, a MDB2 error on failure
  332.      * @access public
  333.      */
  334.     function getTriggerDefinition($trigger)
  335.     {
  336.         $db =$this->getDBInstance();
  337.         if (PEAR::isError($db)) {
  338.             return $db;
  339.         }
  340.  
  341.         $query 'SELECT trigger_name,
  342.                          table_name,
  343.                          trigger_body,
  344.                          trigger_type,
  345.                          triggering_event trigger_event,
  346.                          description trigger_comment,
  347.                          1 trigger_enabled,
  348.                          when_clause
  349.                     FROM user_triggers
  350.                    WHERE trigger_name = \''strtoupper($trigger).'\'';
  351.         $types = array(
  352.             'trigger_name'    => 'text',
  353.             'table_name'      => 'text',
  354.             'trigger_body'    => 'text',
  355.             'trigger_type'    => 'text',
  356.             'trigger_event'   => 'text',
  357.             'trigger_comment' => 'text',
  358.             'trigger_enabled' => 'boolean',
  359.             'when_clause'     => 'text',
  360.         );
  361.         $result $db->queryRow($query$typesMDB2_FETCHMODE_ASSOC);
  362.         if (PEAR::isError($result)) {
  363.             return $result;
  364.         }
  365.         if (!empty($result['trigger_type'])) {
  366.             //$result['trigger_type'] = array_shift(explode(' ', $result['trigger_type']));
  367.             $result['trigger_type'preg_replace('/(\S+).*/''\\1'$result['trigger_type']);
  368.         }
  369.         return $result;
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ tableInfo()
  374.  
  375.     /**
  376.      * Returns information about a table or a result set
  377.      *
  378.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  379.      * is a table name.
  380.      *
  381.      * NOTE: flags won't contain index information.
  382.      *
  383.      * @param object|string $result  MDB2_result object from a query or a
  384.      *                                  string containing the name of a table.
  385.      *                                  While this also accepts a query result
  386.      *                                  resource identifier, this behavior is
  387.      *                                  deprecated.
  388.      * @param int            $mode    a valid tableInfo mode
  389.      *
  390.      * @return array  an associative array with the information requested.
  391.      *                  A MDB2_Error object on failure.
  392.      *
  393.      * @see MDB2_Driver_Common::tableInfo()
  394.      */
  395.     function tableInfo($result$mode = null)
  396.     {
  397.         if (is_string($result)) {
  398.            return parent::tableInfo($result$mode);
  399.         }
  400.  
  401.         $db =$this->getDBInstance();
  402.         if (PEAR::isError($db)) {
  403.             return $db;
  404.         }
  405.  
  406.         $resource = MDB2::isResultCommon($result$result->getResource($result;
  407.         if (!is_resource($resource)) {
  408.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  409.                 'Could not generate result resource'__FUNCTION__);
  410.         }
  411.  
  412.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  413.             if ($db->options['field_case'== CASE_LOWER{
  414.                 $case_func 'strtolower';
  415.             else {
  416.                 $case_func 'strtoupper';
  417.             }
  418.         else {
  419.             $case_func 'strval';
  420.         }
  421.  
  422.         $count @OCINumCols($resource);
  423.         $res = array();
  424.  
  425.         if ($mode{
  426.             $res['num_fields'$count;
  427.         }
  428.  
  429.         $db->loadModule('Datatype'nulltrue);
  430.         for ($i = 0; $i $count$i++{
  431.             $column = array(
  432.                 'table'  => '',
  433.                 'name'   => $case_func(@OCIColumnName($resource$i+1)),
  434.                 'type'   => @OCIColumnType($resource$i+1),
  435.                 'length' => @OCIColumnSize($resource$i+1),
  436.                 'flags'  => '',
  437.             );
  438.             $res[$i$column;
  439.             $res[$i]['mdb2type'$db->datatype->mapNativeDatatype($res[$i]);
  440.             if ($mode MDB2_TABLEINFO_ORDER{
  441.                 $res['order'][$res[$i]['name']] $i;
  442.             }
  443.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  444.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  445.             }
  446.         }
  447.         return $res;
  448.     }
  449. }
  450. ?>

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