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.54 2006/07/19 06:58:02 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'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.             $definition[$key]['type'$type;
  206.         }
  207.         return $definition;
  208.     }
  209.  
  210.     // }}}
  211.     // {{{ getTableIndexDefinition()
  212.  
  213.     /**
  214.      * Get the stucture of an index into an array
  215.      *
  216.      * @param string    $table      name of table that should be used in method
  217.      * @param string    $index_name name of index that should be used in method
  218.      * @return mixed data array on success, a MDB2 error on failure
  219.      * @access public
  220.      */
  221.     function getTableIndexDefinition($table$index_name)
  222.     {
  223.         $db =$this->getDBInstance();
  224.         if (PEAR::isError($db)) {
  225.             return $db;
  226.         }
  227.         $table $db->quote(strtoupper($table)'text');
  228.         $index_name $db->quote(strtoupper($db->getIndexName($index_name))'text');
  229.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  230.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  231.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  232.                          RDB\$INDICES.RDB\$DESCRIPTION AS description
  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
  239.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  240.         $result $db->query($query);
  241.         if (PEAR::isError($result)) {
  242.             return $result;
  243.         }
  244.  
  245.         $index $row $result->fetchRow(MDB2_FETCHMODE_ASSOC);
  246.         if (empty($index)) {
  247.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  248.                 'it was not specified an existing table index'__FUNCTION__);
  249.         }
  250.  
  251.         $fields = array();
  252.         do {
  253.             $row array_change_key_case($rowCASE_LOWER);
  254.             $fields[$row['field_name'];
  255.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)));
  256.         $result->free();
  257.  
  258.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  259.             $fields array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$fields);
  260.         }
  261.  
  262.         $definition = array();
  263.         foreach ($fields as $field{
  264.             $definition['fields'][$field= array();
  265.             //collation?!?
  266.             /*
  267.             if (!empty($row['collation'])) {
  268.                 $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A'
  269.                     ? 'ascending' : 'descending');
  270.             }
  271.             */
  272.         }
  273.         return $definition;
  274.     }
  275.  
  276.     // }}}
  277.     // {{{ getTableConstraintDefinition()
  278.  
  279.     /**
  280.      * Get the stucture of a constraint into an array
  281.      *
  282.      * @param string    $table      name of table that should be used in method
  283.      * @param string    $index_name name of index that should be used in method
  284.      * @return mixed data array on success, a MDB2 error on failure
  285.      * @access public
  286.      */
  287.     function getTableConstraintDefinition($table$index_name)
  288.     {
  289.         $db =$this->getDBInstance();
  290.         if (PEAR::isError($db)) {
  291.             return $db;
  292.         }
  293.         $table $db->quote(strtoupper($table)'text');
  294.         $index_name $db->quote(strtoupper($db->getIndexName($index_name))'text');
  295.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  296.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  297.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  298.                          RDB\$INDICES.RDB\$DESCRIPTION AS description,
  299.                          RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE AS constraint_type
  300.                     FROM RDB\$INDEX_SEGMENTS
  301.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  302.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  303.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)=$table
  304.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)=$index_name
  305.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  306.         $result $db->query($query);
  307.         if (PEAR::isError($result)) {
  308.             return $result;
  309.         }
  310.  
  311.         $index $row $result->fetchRow(MDB2_FETCHMODE_ASSOC);
  312.         if (empty($index)) {
  313.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  314.                 'it was not specified an existing table constraint'__FUNCTION__);
  315.         }
  316.         $fields = array();
  317.         do {
  318.             $row array_change_key_case($rowCASE_LOWER);
  319.             $fields[$row['field_name'];
  320.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC)));
  321.         $result->free();
  322.  
  323.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  324.             $fields array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$fields);
  325.         }
  326.  
  327.         $definition = array();
  328.         if ($index['constraint_type'== 'PRIMARY KEY'{
  329.             $definition['primary'= true;
  330.         elseif ($index['unique_flag']{
  331.             $definition['unique'= true;
  332.         elseif ($index['foreign_key']{
  333.             $definition['foreign'= true;
  334.         }
  335.         if (!$index['unique_flag'&& !$index['foreign_key']{
  336.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  337.                 'it was not specified an existing table constraint'__FUNCTION__);
  338.         }
  339.         foreach ($fields as $field{
  340.             $definition['fields'][$field= array();
  341.             //collation?!?
  342.             /*
  343.             if (!empty($row['collation'])) {
  344.                 $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A'
  345.                     ? 'ascending' : 'descending');
  346.             }
  347.             */
  348.         }
  349.         return $definition;
  350.     }
  351.  
  352.     // }}}
  353.     // {{{ getTriggerDefinition()
  354.  
  355.     /**
  356.      * Get the stucture of an trigger into an array
  357.      *
  358.      * @param string    $trigger    name of trigger that should be used in method
  359.      * @return mixed data array on success, a MDB2 error on failure
  360.      * @access public
  361.      */
  362.     function getTriggerDefinition($trigger)
  363.     {
  364.         $db =$this->getDBInstance();
  365.         if (PEAR::isError($db)) {
  366.             return $db;
  367.         }
  368.  
  369.         $trigger $db->quote(strtoupper($trigger)'text');
  370.         $query = "SELECT RDB\$TRIGGER_NAME AS trigger_name,
  371.                          RDB\$RELATION_NAME AS table_name,
  372.                          RDB\$TRIGGER_SOURCE AS trigger_body,
  373.                          RDB\$TRIGGER_TYPE AS trigger_type,
  374.                          RDB\$DESCRIPTION AS comment
  375.                     FROM RDB\$TRIGGERS
  376.                    WHERE UPPER(RDB\$TRIGGER_NAME)=$trigger";
  377.         return $db->queryRow();
  378.     }
  379.  
  380.     // }}}
  381.     // {{{ tableInfo()
  382.  
  383.     /**
  384.      * Returns information about a table or a result set
  385.      *
  386.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  387.      * is a table name.
  388.      *
  389.      * @param object|string $result  MDB2_result object from a query or a
  390.      *                                  string containing the name of a table.
  391.      *                                  While this also accepts a query result
  392.      *                                  resource identifier, this behavior is
  393.      *                                  deprecated.
  394.      * @param int            $mode    a valid tableInfo mode
  395.      *
  396.      * @return array  an associative array with the information requested.
  397.      *                  A MDB2_Error object on failure.
  398.      *
  399.      * @see MDB2_Driver_Common::tableInfo()
  400.      */
  401.     function tableInfo($result$mode = null)
  402.     {
  403.         if (is_string($result)) {
  404.            return parent::tableInfo($result$mode);
  405.         }
  406.  
  407.         $db =$this->getDBInstance();
  408.         if (PEAR::isError($db)) {
  409.             return $db;
  410.         }
  411.  
  412.         $id = MDB2::isResultCommon($result$result->getResource($result;
  413.         if (!is_resource($id)) {
  414.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  415.                 'Could not generate result ressource'__FUNCTION__);
  416.         }
  417.  
  418.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  419.             if ($db->options['field_case'== CASE_LOWER{
  420.                 $case_func 'strtolower';
  421.             else {
  422.                 $case_func 'strtoupper';
  423.             }
  424.         else {
  425.             $case_func 'strval';
  426.         }
  427.  
  428.         $count @ibase_num_fields($id);
  429.         $res   = array();
  430.  
  431.         if ($mode{
  432.             $res['num_fields'$count;
  433.         }
  434.  
  435.         $db->loadModule('Datatype'nulltrue);
  436.         for ($i = 0; $i $count$i++{
  437.             $info @ibase_field_info($id$i);
  438.             if (($pos strpos($info['type']'(')) !== false{
  439.                 $info['type'substr($info['type']0$pos);
  440.             }
  441.             $res[$i= array(
  442.                 'table'  => '',
  443.                 'name'   => $case_func($info['name']),
  444.                 'type'   => $info['type'],
  445.                 'length' => $info['length'],
  446.                 'flags'  => '',
  447.             );
  448.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  449.             if (PEAR::isError($mdb2type_info)) {
  450.                return $mdb2type_info;
  451.             }
  452.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  453.             if ($mode MDB2_TABLEINFO_ORDER{
  454.                 $res['order'][$res[$i]['name']] $i;
  455.             }
  456.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  457.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  458.             }
  459.         }
  460.  
  461.         return $res;
  462.     }
  463. }
  464. ?>

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