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

Source for file ibase.php

Documentation is available at ibase.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, Lorenzo Alberton     |
  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: Lorenzo Alberton <l.alberton@quipo.it>                       |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: ibase.php,v 1.57 2006/08/23 06:55:06 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 InterbaseBase driver for the reverse engineering module
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author Lorenzo Alberton  <l.alberton@quipo.it>
  56.  */
  57. class MDB2_Driver_Reverse_ibase extends MDB2_Driver_Reverse_Common
  58. {
  59.     /**
  60.      * Array for converting constant values to text values
  61.      * @var    array 
  62.      * @access public
  63.      */
  64.     var $types = array(
  65.         7   => 'smallint',
  66.         8   => 'integer',
  67.         9   => 'quad',
  68.         10  => 'float',
  69.         11  => 'd_float',
  70.         12  => 'date',      //dialect 3 DATE
  71.         13  => 'time',
  72.         14  => 'char',
  73.         16  => 'int64',
  74.         27  => 'double',
  75.         35  => 'timestamp'//DATE in older versions
  76.         37  => 'varchar',
  77.         40  => 'cstring',
  78.         261 => 'blob',
  79.     );
  80.  
  81.     /**
  82.      * Array for converting constant values to text values
  83.      * @var    array 
  84.      * @access public
  85.      */
  86.     var $subtypes = array(
  87.         //char subtypes
  88.         14 => array(
  89.             0 => 'unspecified',
  90.             1 => 'fixed'//BINARY data
  91.         ),
  92.         //blob subtypes
  93.         261 => array(
  94.             0 => 'unspecified',
  95.             1 => 'text',
  96.             2 => 'BLR'//Binary Language Representation
  97.             3 => 'access control list',
  98.             4 => 'reserved for future use',
  99.             5 => 'encoded description of a table\'s current metadata',
  100.             6 => 'description of multi-database transaction that finished irregularly',
  101.         ),
  102.         //smallint subtypes
  103.         7 => array(
  104.             0 => 'RDB$FIELD_TYPE',
  105.             1 => 'numeric',
  106.             2 => 'decimal',
  107.         ),
  108.         //integer subtypes
  109.         8 => array(
  110.             0 => 'RDB$FIELD_TYPE',
  111.             1 => 'numeric',
  112.             2 => 'decimal',
  113.         ),
  114.         //int64 subtypes
  115.         16 => array(
  116.             0 => 'RDB$FIELD_TYPE',
  117.             1 => 'numeric',
  118.             2 => 'decimal',
  119.         ),
  120.     );
  121.  
  122.     // {{{ getTableFieldDefinition()
  123.  
  124.     /**
  125.      * Get the stucture of a field into an array
  126.      *
  127.      * @param string    $table         name of table that should be used in method
  128.      * @param string    $field_name     name of field that should be used in method
  129.      * @return mixed data array on success, a MDB2 error on failure
  130.      * @access public
  131.      */
  132.     function getTableFieldDefinition($table$field_name)
  133.     {
  134.         $db =$this->getDBInstance();
  135.         if (PEAR::isError($db)) {
  136.             return $db;
  137.         }
  138.  
  139.         $result $db->loadModule('Datatype'nulltrue);
  140.         if (PEAR::isError($result)) {
  141.             return $result;
  142.         }
  143.         $table $db->quote(strtoupper($table)'text');
  144.         $field_name $db->quote(strtoupper($field_name)'text');
  145.         $query = "SELECT RDB\$RELATION_FIELDS.RDB\$FIELD_NAME AS name,
  146.                          RDB\$FIELDS.RDB\$FIELD_LENGTH AS \"length\",
  147.                          RDB\$FIELDS.RDB\$FIELD_TYPE AS field_type_code,
  148.                          RDB\$FIELDS.RDB\$FIELD_SUB_TYPE AS field_sub_type_code,
  149.                          RDB\$RELATION_FIELDS.RDB\$DESCRIPTION AS description,
  150.                          RDB\$RELATION_FIELDS.RDB\$NULL_FLAG AS null_flag,
  151.                          RDB\$FIELDS.RDB\$DEFAULT_SOURCE AS default_source
  152.                     FROM RDB\$FIELDS
  153.                LEFT JOIN RDB\$RELATION_FIELDS ON RDB\$FIELDS.RDB\$FIELD_NAME = RDB\$RELATION_FIELDS.RDB\$FIELD_SOURCE
  154.                    WHERE UPPER(RDB\$RELATION_FIELDS.RDB\$RELATION_NAME)=$table
  155.                      AND UPPER(RDB\$RELATION_FIELDS.RDB\$FIELD_NAME)=$field_name;";
  156.         $column $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  157.         if (PEAR::isError($column)) {
  158.             return $column;
  159.         }
  160.         if (empty($column)) {
  161.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  162.                 'it was not specified an existing table column'__FUNCTION__);
  163.         }
  164.         $column array_change_key_case($columnCASE_LOWER);
  165.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  166.             if ($db->options['field_case'== CASE_LOWER{
  167.                 $column['name'strtolower($column['name']);
  168.             else {
  169.                 $column['name'strtoupper($column['name']);
  170.             }
  171.         }
  172.  
  173.         $column['type'array_key_exists((int)$column['field_type_code']$this->types)
  174.             ? $this->types[(int)$column['field_type_code']] 'undefined';
  175.         if ($column['field_sub_type_code']
  176.             && array_key_exists((int)$column['field_type_code']$this->subtypes)
  177.             && array_key_exists($column['field_sub_type_code']$this->subtypes[(int)$column['field_type_code']])
  178.         {
  179.             $column['field_sub_type'$this->subtypes[(int)$column['field_type_code']][$column['field_sub_type_code']];
  180.         else {
  181.             $column['field_sub_type'= null;
  182.         }
  183.         list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  184.         $notnull !empty($column['null_flag']);
  185.         $default $column['default_source'];
  186.         if (is_null($default&& $notnull{
  187.             $default ($types[0== 'integer'? 0 : '';
  188.         }
  189.  
  190.         $definition[0= array('notnull' => $notnull'nativetype' => $column['type']);
  191.         if ($length > 0{
  192.             $definition[0]['length'$length;
  193.         }
  194.         if (!is_null($unsigned)) {
  195.             $definition[0]['unsigned'$unsigned;
  196.         }
  197.         if (!is_null($fixed)) {
  198.             $definition[0]['fixed'$fixed;
  199.         }
  200.         if ($default !== false{
  201.             $definition[0]['default'$default;
  202.         }
  203.         foreach ($types as $key => $type{
  204.             $definition[$key$definition[0];
  205.             if ($type == 'clob' || $type == 'blob'{
  206.                 unset($definition[$key]['default']);
  207.             }
  208.             $definition[$key]['type'$type;
  209.             $definition[$key]['mdb2type'$type;
  210.         }
  211.         return $definition;
  212.     }
  213.  
  214.     // }}}
  215.     // {{{ getTableIndexDefinition()
  216.  
  217.     /**
  218.      * Get the stucture of an index into an array
  219.      *
  220.      * @param string    $table      name of table that should be used in method
  221.      * @param string    $index_name name of index that should be used in method
  222.      * @return mixed data array on success, a MDB2 error on failure
  223.      * @access public
  224.      */
  225.     function getTableIndexDefinition($table$index_name)
  226.     {
  227.         $db =$this->getDBInstance();
  228.         if (PEAR::isError($db)) {
  229.             return $db;
  230.         }
  231.         $table $db->quote(strtoupper($table)'text');
  232.         $index_name $db->quote(strtoupper($db->getIndexName($index_name))'text');
  233.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  234.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  235.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  236.                          RDB\$INDICES.RDB\$DESCRIPTION AS description
  237.                     FROM RDB\$INDEX_SEGMENTS
  238.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  239.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  240.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)=$table
  241.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)=$index_name
  242.                      AND RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE IS NULL
  243.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  244.         $result $db->query($query);
  245.         if (PEAR::isError($result)) {
  246.             return $result;
  247.         }
  248.  
  249.         $index $row $result->fetchRow(MDB2_FETCHMODE_ASSOC);
  250.         if (empty($index)) {
  251.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  252.                 'it was not specified an existing table index'__FUNCTION__);
  253.         }
  254.  
  255.         $fields = array();
  256.         do {
  257.             $row array_change_key_case($rowCASE_LOWER);
  258.             $fields[$row['field_name'];
  259.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)));
  260.         $result->free();
  261.  
  262.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  263.             $fields array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$fields);
  264.         }
  265.  
  266.         $definition = array();
  267.         foreach ($fields as $field{
  268.             $definition['fields'][$field= array();
  269.             // todo: collation?!?
  270.             /*
  271.             if (!empty($row['collation'])) {
  272.                 $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A'
  273.                     ? 'ascending' : 'descending');
  274.             }
  275.             */
  276.         }
  277.         return $definition;
  278.     }
  279.  
  280.     // }}}
  281.     // {{{ getTableConstraintDefinition()
  282.  
  283.     /**
  284.      * Get the stucture of a constraint into an array
  285.      *
  286.      * @param string    $table      name of table that should be used in method
  287.      * @param string    $index_name name of index that should be used in method
  288.      * @return mixed data array on success, a MDB2 error on failure
  289.      * @access public
  290.      */
  291.     function getTableConstraintDefinition($table$index_name)
  292.     {
  293.         $db =$this->getDBInstance();
  294.         if (PEAR::isError($db)) {
  295.             return $db;
  296.         }
  297.         $table $db->quote(strtoupper($table)'text');
  298.         $index_name $db->quote(strtoupper($db->getIndexName($index_name))'text');
  299.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  300.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  301.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  302.                          RDB\$INDICES.RDB\$DESCRIPTION AS description,
  303.                          RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE AS constraint_type
  304.                     FROM RDB\$INDEX_SEGMENTS
  305.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  306.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  307.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)=$table
  308.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)=$index_name
  309.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  310.         $result $db->query($query);
  311.         if (PEAR::isError($result)) {
  312.             return $result;
  313.         }
  314.  
  315.         $index $row $result->fetchRow(MDB2_FETCHMODE_ASSOC);
  316.         if (empty($index)) {
  317.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  318.                 'it was not specified an existing table constraint'__FUNCTION__);
  319.         }
  320.         $fields = array();
  321.         do {
  322.             $row array_change_key_case($rowCASE_LOWER);
  323.             $fields[$row['field_name'];
  324.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)));
  325.         $result->free();
  326.  
  327.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  328.             $fields array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$fields);
  329.         }
  330.  
  331.         $definition = array();
  332.         if ($index['constraint_type'== 'PRIMARY KEY'{
  333.             $definition['primary'= true;
  334.         elseif ($index['unique_flag']{
  335.             $definition['unique'= true;
  336.         elseif ($index['foreign_key']{
  337.             $definition['foreign'= true;
  338.         }
  339.         if (!$index['unique_flag'&& !$index['foreign_key']{
  340.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  341.                 'it was not specified an existing table constraint'__FUNCTION__);
  342.         }
  343.         foreach ($fields as $field{
  344.             $definition['fields'][$field= array();
  345.             //collation?!?
  346.             /*
  347.             if (!empty($row['collation'])) {
  348.                 $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A'
  349.                     ? 'ascending' : 'descending');
  350.             }
  351.             */
  352.         }
  353.         return $definition;
  354.     }
  355.  
  356.     // }}}
  357.     // {{{ getTriggerDefinition()
  358.  
  359.     /**
  360.      * Get the stucture of an trigger into an array
  361.      *
  362.      * @param string    $trigger    name of trigger that should be used in method
  363.      * @return mixed data array on success, a MDB2 error on failure
  364.      * @access public
  365.      */
  366.     function getTriggerDefinition($trigger)
  367.     {
  368.         $db =$this->getDBInstance();
  369.         if (PEAR::isError($db)) {
  370.             return $db;
  371.         }
  372.  
  373.         $trigger $db->quote(strtoupper($trigger)'text');
  374.         $query = "SELECT RDB\$TRIGGER_NAME AS trigger_name,
  375.                          RDB\$RELATION_NAME AS table_name,
  376.                          RDB\$TRIGGER_SOURCE AS trigger_body,
  377.                          RDB\$TRIGGER_TYPE AS trigger_type,
  378.                          RDB\$DESCRIPTION AS comment
  379.                     FROM RDB\$TRIGGERS
  380.                    WHERE UPPER(RDB\$TRIGGER_NAME)=$trigger";
  381.         return $db->queryRow();
  382.     }
  383.  
  384.     // }}}
  385.     // {{{ tableInfo()
  386.  
  387.     /**
  388.      * Returns information about a table or a result set
  389.      *
  390.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  391.      * is a table name.
  392.      *
  393.      * @param object|string $result  MDB2_result object from a query or a
  394.      *                                  string containing the name of a table.
  395.      *                                  While this also accepts a query result
  396.      *                                  resource identifier, this behavior is
  397.      *                                  deprecated.
  398.      * @param int            $mode    a valid tableInfo mode
  399.      *
  400.      * @return array  an associative array with the information requested.
  401.      *                  A MDB2_Error object on failure.
  402.      *
  403.      * @see MDB2_Driver_Common::tableInfo()
  404.      */
  405.     function tableInfo($result$mode = null)
  406.     {
  407.         if (is_string($result)) {
  408.            return parent::tableInfo($result$mode);
  409.         }
  410.  
  411.         $db =$this->getDBInstance();
  412.         if (PEAR::isError($db)) {
  413.             return $db;
  414.         }
  415.  
  416.         $id = MDB2::isResultCommon($result$result->getResource($result;
  417.         if (!is_resource($id)) {
  418.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  419.                 'Could not generate result ressource'__FUNCTION__);
  420.         }
  421.  
  422.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  423.             if ($db->options['field_case'== CASE_LOWER{
  424.                 $case_func 'strtolower';
  425.             else {
  426.                 $case_func 'strtoupper';
  427.             }
  428.         else {
  429.             $case_func 'strval';
  430.         }
  431.  
  432.         $count @ibase_num_fields($id);
  433.         $res   = array();
  434.  
  435.         if ($mode{
  436.             $res['num_fields'$count;
  437.         }
  438.  
  439.         $db->loadModule('Datatype'nulltrue);
  440.         for ($i = 0; $i $count$i++{
  441.             $info @ibase_field_info($id$i);
  442.             if (($pos strpos($info['type']'(')) !== false{
  443.                 $info['type'substr($info['type']0$pos);
  444.             }
  445.             $res[$i= array(
  446.                 'table'  => '',
  447.                 'name'   => $case_func($info['name']),
  448.                 'type'   => $info['type'],
  449.                 'length' => $info['length'],
  450.                 'flags'  => '',
  451.             );
  452.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  453.             if (PEAR::isError($mdb2type_info)) {
  454.                return $mdb2type_info;
  455.             }
  456.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  457.             if ($mode MDB2_TABLEINFO_ORDER{
  458.                 $res['order'][$res[$i]['name']] $i;
  459.             }
  460.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  461.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  462.             }
  463.         }
  464.  
  465.         return $res;
  466.     }
  467. }
  468. ?>

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