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-2005 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.31 2005/12/22 18:11:03 quipo 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');
  140.         if (PEAR::isError($result)) {
  141.             return $result;
  142.         }
  143.         $table strtoupper($table);
  144.         $field_name strtoupper($field_name);
  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\$RELATION_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_ERRORnullnull,
  162.                 'getTableFieldDefinition: it was not specified an existing table column');
  163.         }
  164.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  165.             if ($db->options['field_case'== CASE_LOWER{
  166.                 $column['name'strtolower($column['name']);
  167.             else {
  168.                 $column['name'strtoupper($column['name']);
  169.             }
  170.         else {
  171.             $column array_change_key_case($column$db->options['field_case']);
  172.         }
  173.  
  174.         $column['type'array_key_exists($column['field_type_code']$this->types)
  175.             ? $this->types[(int)$column['field_type_code']] 'undefined';
  176.         if ($column['field_sub_type_code']
  177.             && array_key_exists($column['field_type_code']$this->subtypes)
  178.             && array_key_exists($column['field_sub_type_code']$this->subtypes[(int)$column['field_type_code']])
  179.         {
  180.             $column['field_sub_type'$this->subtypes[(int)$column['field_type_code']][$column['field_sub_type_code']];
  181.         else {
  182.             $column['field_sub_type'= null;
  183.         }
  184.         list($types$length$unsigned$db->datatype->mapNativeDatatype($column);
  185.         $notnull !empty($column['null_flag']);
  186.         $default $column['default_source'];
  187.         if (is_null($default&& $notnull{
  188.             $default ($types[0== 'integer'? 0 : '';
  189.         }
  190.         $definition = array();
  191.         foreach ($types as $key => $type{
  192.             $definition[$key= array(
  193.                 'type'    => $type,
  194.                 'notnull' => $notnull,
  195.             );
  196.             if ($length > 0{
  197.                 $definition[$key]['length'$length;
  198.             }
  199.             if ($unsigned{
  200.                 $definition[$key]['unsigned'= true;
  201.             }
  202.             if ($default !== false{
  203.                 $definition[$key]['default'$default;
  204.             }
  205.         }
  206.         return $definition;
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ getTableIndexDefinition()
  211.  
  212.     /**
  213.      * get the stucture of an index into an array
  214.      *
  215.      * @param string    $table      name of table that should be used in method
  216.      * @param string    $index_name name of index that should be used in method
  217.      * @return mixed data array on success, a MDB2 error on failure
  218.      * @access public
  219.      */
  220.     function getTableIndexDefinition($table$index_name)
  221.     {
  222.         $db =$this->getDBInstance();
  223.         if (PEAR::isError($db)) {
  224.             return $db;
  225.         }
  226.         $table strtoupper($table);
  227.         $index_name strtoupper($db->getIndexName($index_name));
  228.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  229.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  230.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  231.                          RDB\$INDICES.RDB\$DESCRIPTION AS description,
  232.                          RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE as constraint_type
  233.                     FROM RDB\$INDEX_SEGMENTS
  234.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  235.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  236.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)='$table'
  237.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)='$index_name'
  238.                      AND (RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE IS NULL OR
  239.                           RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE <> 'PRIMARY KEY')
  240.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  241.         $result $db->query($query);
  242.         if (PEAR::isError($result)) {
  243.             return $result;
  244.         }
  245.  
  246.         $index $row $result->fetchRow(MDB2_FETCHMODE_ASSOC);
  247.         if (empty($index)) {
  248.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  249.                 'getTableIndexDefinition: it was not specified an existing table index');
  250.         }
  251.         $fields = array();
  252.         do {
  253.             $fields[$row['field_name'];
  254.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)));
  255.         $result->free();
  256.  
  257.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  258.             $fields array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$fields);
  259.         }
  260.  
  261.         $definition = array();
  262.         if ($index['unique_flag']{
  263.             $definition['unique'= true;
  264.         }
  265.         foreach ($fields as $field{
  266.             $definition['fields'][$field= array();
  267.             //collation?!?
  268.             /*
  269.             if (array_key_exists('collation', $row)) {
  270.                 $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A'
  271.                     ? 'ascending' : 'descending');
  272.             }
  273.             */
  274.         }
  275.         return $definition;
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ getTableConstraintDefinition()
  280.  
  281.     /**
  282.      * get the stucture of a constraint into an array
  283.      *
  284.      * @param string    $table      name of table that should be used in method
  285.      * @param string    $index_name name of index that should be used in method
  286.      * @return mixed data array on success, a MDB2 error on failure
  287.      * @access public
  288.      */
  289.     function getTableConstraintDefinition($table$index_name)
  290.     {
  291.         $db =$this->getDBInstance();
  292.         if (PEAR::isError($db)) {
  293.             return $db;
  294.         }
  295.         $table strtoupper($table);
  296.         $index_name strtoupper($db->getIndexName($index_name));
  297.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  298.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  299.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  300.                          RDB\$INDICES.RDB\$DESCRIPTION AS description,
  301.                          RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE as constraint_type
  302.                     FROM RDB\$INDEX_SEGMENTS
  303.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  304.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  305.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)='$table'
  306.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)='$index_name'
  307.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  308.         $result $db->query($query);
  309.         if (PEAR::isError($result)) {
  310.             return $result;
  311.         }
  312.  
  313.         $index $row $result->fetchRow(MDB2_FETCHMODE_ASSOC);
  314.         if (empty($index)) {
  315.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  316.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  317.         }
  318.         $fields = array();
  319.         do {
  320.             $fields[$row['field_name'];
  321.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)));
  322.         $result->free();
  323.  
  324.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  325.             $fields array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$fields);
  326.         }
  327.  
  328.         $definition = array();
  329.         if ($index['constraint_type'== 'PRIMARY KEY'{
  330.             $definition['primary'= true;
  331.         }
  332.         if ($index['unique_flag']{
  333.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  334.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  335.         }
  336.         foreach ($fields as $field{
  337.             $definition['fields'][$field= array();
  338.             //collation?!?
  339.             /*
  340.             if (array_key_exists('collation', $row)) {
  341.                 $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A'
  342.                     ? 'ascending' : 'descending');
  343.             }
  344.             */
  345.         }
  346.         return $definition;
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ tableInfo()
  351.  
  352.     /**
  353.      * Returns information about a table or a result set
  354.      *
  355.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  356.      * is a table name.
  357.      *
  358.      * @param object|string $result  MDB2_result object from a query or a
  359.      *                                  string containing the name of a table.
  360.      *                                  While this also accepts a query result
  361.      *                                  resource identifier, this behavior is
  362.      *                                  deprecated.
  363.      * @param int            $mode    a valid tableInfo mode
  364.      *
  365.      * @return array  an associative array with the information requested.
  366.      *                  A MDB2_Error object on failure.
  367.      *
  368.      * @see MDB2_Driver_Common::tableInfo()
  369.      */
  370.     function tableInfo($result$mode = null)
  371.     {
  372.         $db =$this->getDBInstance();
  373.         if (PEAR::isError($db)) {
  374.             return $db;
  375.         }
  376.  
  377.         if (is_string($result)) {
  378.             /*
  379.              * Probably received a table name.
  380.              * Create a result resource identifier.
  381.              */
  382.             $id $db->_doQuery("SELECT * FROM $result WHERE 1=0"false);
  383.             if (PEAR::isError($id)) {
  384.                 return $id;
  385.             }
  386.             $got_string = true;
  387.         elseif (MDB2::isResultCommon($result)) {
  388.             /*
  389.              * Probably received a result object.
  390.              * Extract the result resource identifier.
  391.              */
  392.             $id $result->getResource();
  393.             $got_string = false;
  394.         else {
  395.             /*
  396.              * Probably received a result resource identifier.
  397.              * Copy it.
  398.              * Deprecated.  Here for compatibility only.
  399.              */
  400.             $id $result;
  401.             $got_string = false;
  402.         }
  403.  
  404.         if (!is_resource($id)) {
  405.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  406.         }
  407.  
  408.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  409.             if ($db->options['field_case'== CASE_LOWER{
  410.                 $case_func 'strtolower';
  411.             else {
  412.                 $case_func 'strtoupper';
  413.             }
  414.         else {
  415.             $case_func 'strval';
  416.         }
  417.  
  418.         $count @ibase_num_fields($id);
  419.         $res   = array();
  420.  
  421.         if ($mode{
  422.             $res['num_fields'$count;
  423.         }
  424.  
  425.         $db->loadModule('Datatype');
  426.         for ($i = 0; $i $count$i++{
  427.             $info @ibase_field_info($id$i);
  428.             $res[$i= array(
  429.                 'table'  => $got_string $case_func($result'',
  430.                 'name'   => $case_func($info['name']),
  431.                 'type'   => $info['type'],
  432.                 'length' => $info['length'],
  433.                 'flags'  => ($got_string)
  434.                             ? $this->_ibaseFieldFlags($info['name']$result'',
  435.             );
  436.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  437.             if (PEAR::isError($mdb2type_info)) {
  438.                return $mdb2type_info;
  439.             }
  440.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  441.             if ($mode MDB2_TABLEINFO_ORDER{
  442.                 $res['order'][$res[$i]['name']] $i;
  443.             }
  444.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  445.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  446.             }
  447.         }
  448.  
  449.         // free the result only if we were called on a table
  450.         if ($got_string{
  451.             @ibase_free_result($id);
  452.         }
  453.         return $res;
  454.     }
  455.  
  456.     // }}}
  457.     // {{{ _ibaseFieldFlags()
  458.  
  459.     /**
  460.      * Get the column's flags
  461.      *
  462.      * Supports "primary_key", "unique_key", "not_null", "default",
  463.      * "computed" and "blob".
  464.      *
  465.      * @param string $field_name  the name of the field
  466.      * @param string $table_name  the name of the table
  467.      *
  468.      * @return string  the flags
  469.      *
  470.      * @access protected
  471.      */
  472.     function _ibaseFieldFlags($field_name$table_name)
  473.     {
  474.         $db =$this->getDBInstance();
  475.         if (PEAR::isError($db)) {
  476.             return $db;
  477.         }
  478.  
  479.         $query 'SELECT R.RDB$CONSTRAINT_TYPE CTYPE'
  480.                .' FROM RDB$INDEX_SEGMENTS I'
  481.                .'  JOIN RDB$RELATION_CONSTRAINTS R ON I.RDB$INDEX_NAME=R.RDB$INDEX_NAME'
  482.                .' WHERE I.RDB$FIELD_NAME=\'' $field_name '\''
  483.                .'  AND UPPER(R.RDB$RELATION_NAME)=\'' strtoupper($table_name'\'';
  484.  
  485.         $result $db->_doQuery($queryfalse);
  486.         if (PEAR::isError($result)) {
  487.             return $result;
  488.         }
  489.  
  490.         $flags '';
  491.         if ($obj @ibase_fetch_object($result)) {
  492.             @ibase_free_result($result);
  493.             if (isset($obj->CTYPE)  && trim($obj->CTYPE== 'PRIMARY KEY'{
  494.                 $flags.= 'primary_key ';
  495.             }
  496.             if (isset($obj->CTYPE)  && trim($obj->CTYPE== 'UNIQUE'{
  497.                 $flags.= 'unique_key ';
  498.             }
  499.         }
  500.  
  501.         $query 'SELECT R.RDB$NULL_FLAG AS NFLAG,'
  502.                .'  R.RDB$DEFAULT_SOURCE AS DSOURCE,'
  503.                .'  F.RDB$FIELD_TYPE AS FTYPE,'
  504.                .'  F.RDB$COMPUTED_SOURCE AS CSOURCE'
  505.                .' FROM RDB$RELATION_FIELDS R '
  506.                .'  JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME'
  507.                .' WHERE UPPER(R.RDB$RELATION_NAME)=\'' strtoupper($table_name'\''
  508.                .'  AND R.RDB$FIELD_NAME=\'' $field_name '\'';
  509.  
  510.         $result $db->_doQuery($queryfalse);
  511.         if (PEAR::isError($result)) {
  512.             return $result;
  513.         }
  514.  
  515.         if ($obj @ibase_fetch_object($result)) {
  516.             @ibase_free_result($result);
  517.             if (isset($obj->NFLAG)) {
  518.                 $flags.= 'not_null ';
  519.             }
  520.             if (isset($obj->DSOURCE)) {
  521.                 $flags.= 'default ';
  522.             }
  523.             if (isset($obj->CSOURCE)) {
  524.                 $flags.= 'computed ';
  525.             }
  526.             if (isset($obj->FTYPE)  && $obj->FTYPE == 261{
  527.                 $flags.= 'blob ';
  528.             }
  529.         }
  530.  
  531.         return trim($flags);
  532.     }
  533. }
  534. ?>

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