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

Source for file ibase.php

Documentation is available at ibase.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 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@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: ibase.php,v 1.7 2004/03/10 14:12:12 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  Lukas Smith <smith@dybnet.de>
  56.  */
  57. {
  58.     // }}}
  59.     // {{{ constructor
  60.  
  61.     /**
  62.      * Constructor
  63.      */
  64.     function MDB2_Driver_Reverse_ibase($db_index)
  65.     {
  66.         $this->MDB2_Driver_Reverse_Common($db_index);
  67.     }
  68.  
  69.     // }}}
  70.     // {{{ tableInfo()
  71.  
  72.     /**
  73.      * Returns information about a table or a result set.
  74.      *
  75.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  76.      * is a table name.
  77.      *
  78.      * @param object|string $result  MDB2_result object from a query or a
  79.      *                                 string containing the name of a table
  80.      * @param int            $mode    a valid tableInfo mode
  81.      * @return array  an associative array with the information requested
  82.      *                 or an error object if something is wrong
  83.      * @access public
  84.      * @internal
  85.      * @see MDB2_Driver_Common::tableInfo()
  86.      */
  87.     function tableInfo($result$mode = null)
  88.     {
  89.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  90.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  91.             $case_func 'strtolower';
  92.         else {
  93.             $case_func 'strval';
  94.         }
  95.  
  96.         if (is_string($result)) {
  97.             /*
  98.              * Probably received a table name.
  99.              * Create a result resource identifier.
  100.              */
  101.             if (MDB2::isError($connect $db->connect())) {
  102.                 return $connect;
  103.             }
  104.             $id @ibase_query($db->connection,
  105.                 "SELECT * FROM $result WHERE 1=0");
  106.             $got_string = true;
  107.         else {
  108.             /*
  109.              * Probably received a result object.
  110.              * Extract the result resource identifier.
  111.              */
  112.             $id $result->getResource();
  113.             if (empty($id)) {
  114.                 return $db->raiseError();
  115.             }
  116.             $got_string = false;
  117.         }
  118.  
  119.         if (!is_resource($id)) {
  120.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  121.         }
  122.  
  123.         $count @ibase_num_fields($id);
  124.  
  125.         // made this IF due to performance (one if is faster than $count if's)
  126.         if (!$mode{
  127.             for ($i=0; $i<$count$i++{
  128.                 $info @ibase_field_info($id$i);
  129.                 $res[$i]['table'$got_string $case_func($result'';
  130.                 $res[$i]['name']  $case_func($info['name']);
  131.                 $res[$i]['type']  $info['type'];
  132.                 $res[$i]['len']   $info['length'];
  133.                 $res[$i]['flags'($got_string$this->_ibaseFieldFlags($info['name']$result'';
  134.             }
  135.         else // full
  136.             $res['num_fields']$count;
  137.  
  138.             for ($i=0; $i<$count$i++{
  139.                 $info @ibase_field_info($id$i);
  140.                 $res[$i]['table'$got_string $case_func($result'';
  141.                 $res[$i]['name']  $case_func($info['name']);
  142.                 $res[$i]['type']  $info['type'];
  143.                 $res[$i]['len']   $info['length'];
  144.                 $res[$i]['flags'($got_string$this->_ibaseFieldFlags($info['name']$result'';
  145.  
  146.                 if ($mode MDB2_TABLEINFO_ORDER{
  147.                     $res['order'][$res[$i]['name']] $i;
  148.                 }
  149.                 if ($mode MDB2_TABLEINFO_ORDERTABLE{
  150.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  151.                 }
  152.             }
  153.         }
  154.  
  155.         // free the result only if we were called on a table
  156.         if ($got_string{
  157.             @ibase_free_result($id);
  158.         }
  159.         return $res;
  160.     }
  161.  
  162.  
  163.     // }}}
  164.     // {{{ _ibaseFieldFlags()
  165.  
  166.     /**
  167.      * get the Flags of a Field
  168.      *
  169.      * @param string $field_name the name of the field
  170.      * @param string $table_name the name of the table
  171.      *
  172.      * @return string The flags of the field ("primary_key", "unique_key", "not_null"
  173.      *                 "default", "computed" and "blob" are supported)
  174.      * @access private
  175.      */
  176.     function _ibaseFieldFlags($field_name$table_name)
  177.     {
  178.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  179.  
  180.         $sql 'SELECT R.RDB$CONSTRAINT_TYPE CTYPE'
  181.                .' FROM RDB$INDEX_SEGMENTS I'
  182.                .'  JOIN RDB$RELATION_CONSTRAINTS R ON I.RDB$INDEX_NAME=R.RDB$INDEX_NAME'
  183.                .' WHERE I.RDB$FIELD_NAME=\'' $field_name '\''
  184.                .'  AND UPPER(R.RDB$RELATION_NAME)=\'' strtoupper($table_name'\'';
  185.  
  186.         $result @ibase_query($db->connection$sql);
  187.         if (!$result{
  188.             return $db->raiseError();
  189.         }
  190.  
  191.         $flags '';
  192.         if ($obj @ibase_fetch_object($result)) {
  193.             @ibase_free_result($result);
  194.             if (isset($obj->CTYPE)  && trim($obj->CTYPE== 'PRIMARY KEY'{
  195.                 $flags .= 'primary_key ';
  196.             }
  197.             if (isset($obj->CTYPE)  && trim($obj->CTYPE== 'UNIQUE'{
  198.                 $flags .= 'unique_key ';
  199.             }
  200.         }
  201.  
  202.         $sql 'SELECT R.RDB$NULL_FLAG AS NFLAG,'
  203.                .'  R.RDB$DEFAULT_SOURCE AS DSOURCE,'
  204.                .'  F.RDB$FIELD_TYPE AS FTYPE,'
  205.                .'  F.RDB$COMPUTED_SOURCE AS CSOURCE'
  206.                .' FROM RDB$RELATION_FIELDS R '
  207.                .'  JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME'
  208.                .' WHERE UPPER(R.RDB$RELATION_NAME)=\'' strtoupper($table_name'\''
  209.                .'  AND R.RDB$FIELD_NAME=\'' $field_name '\'';
  210.  
  211.         $result @ibase_query($db->connection$sql);
  212.         if (!$result{
  213.             return $db->raiseError();
  214.         }
  215.         if ($obj @ibase_fetch_object($result)) {
  216.             @ibase_free_result($result);
  217.             if (isset($obj->NFLAG)) {
  218.                 $flags .= 'not_null ';
  219.             }
  220.             if (isset($obj->DSOURCE)) {
  221.                 $flags .= 'default ';
  222.             }
  223.             if (isset($obj->CSOURCE)) {
  224.                 $flags .= 'computed ';
  225.             }
  226.             if (isset($obj->FTYPE)  && $obj->FTYPE == 261{
  227.                 $flags .= 'blob ';
  228.             }
  229.         }
  230.  
  231.         return trim($flags);
  232.     }
  233. }
  234. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.