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

Source for file pgsql.php

Documentation is available at pgsql.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                                         |
  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: Paul Cooper <pgc@ucecom.com>                                 |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: pgsql.php,v 1.49 2006/08/12 15:44:03 lsmith Exp $
  46.  
  47. require_once 'MDB2/Driver/Reverse/Common.php';
  48.  
  49. /**
  50.  * MDB2 PostGreSQL driver for the schema reverse engineering module
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Paul Cooper <pgc@ucecom.com>
  55.  */
  56. class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
  57. {
  58.     // {{{ getTableFieldDefinition()
  59.  
  60.     /**
  61.      * Get the stucture of a field into an array
  62.      *
  63.      * @param string    $table         name of table that should be used in method
  64.      * @param string    $field_name     name of field that should be used in method
  65.      * @return mixed data array on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function getTableFieldDefinition($table$field_name)
  69.     {
  70.         $db =$this->getDBInstance();
  71.         if (PEAR::isError($db)) {
  72.             return $db;
  73.         }
  74.  
  75.         $result $db->loadModule('Datatype'nulltrue);
  76.         if (PEAR::isError($result)) {
  77.             return $result;
  78.         }
  79.  
  80.         $query "SELECT
  81.                     a.attname AS name, t.typname AS type, a.attlen AS length, a.attnotnull,
  82.                     a.atttypmod, a.atthasdef,
  83.                     (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
  84.                         FROM pg_attrdef d
  85.                         WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) as default
  86.                     FROM pg_attribute a, pg_class c, pg_type t
  87.                     WHERE c.relname = ".$db->quote($table'text')."
  88.                         AND a.atttypid = t.oid
  89.                         AND c.oid = a.attrelid
  90.                         AND NOT a.attisdropped
  91.                         AND a.attnum > 0
  92.                         AND a.attname = ".$db->quote($field_name'text')."
  93.                     ORDER BY a.attnum";
  94.         $column $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  95.         if (PEAR::isError($column)) {
  96.             return $column;
  97.         }
  98.  
  99.         if (empty($column)) {
  100.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  101.                 'it was not specified an existing table column'__FUNCTION__);
  102.         }
  103.  
  104.         $column array_change_key_case($columnCASE_LOWER);
  105.         list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  106.         $notnull = false;
  107.         if (!empty($column['attnotnull']&& $column['attnotnull'== 't'{
  108.             $notnull = true;
  109.         }
  110.         $default = null;
  111.         if ($column['atthasdef'=== 't'
  112.             && !preg_match("/nextval\('([^']+)'/"$column['default'])
  113.         {
  114.             $default $column['default'];#substr($column['adsrc'], 1, -1);
  115.             if (is_null($default&& $notnull{
  116.                 $default '';
  117.             }
  118.         }
  119.         $autoincrement = false;
  120.         if (preg_match("/nextval\('([^']+)'/"$column['default']$nextvals)) {
  121.             $autoincrement = true;
  122.         }
  123.         $definition[0= array('notnull' => $notnull'nativetype' => $column['type']);
  124.         if ($length > 0{
  125.             $definition[0]['length'$length;
  126.         }
  127.         if (!is_null($unsigned)) {
  128.             $definition[0]['unsigned'$unsigned;
  129.         }
  130.         if (!is_null($fixed)) {
  131.             $definition[0]['fixed'$fixed;
  132.         }
  133.         if ($default !== false{
  134.             $definition[0]['default'$default;
  135.         }
  136.         if ($autoincrement !== false{
  137.             $definition[0]['autoincrement'$autoincrement;
  138.         }
  139.         foreach ($types as $key => $type{
  140.             $definition[$key$definition[0];
  141.             if ($type == 'clob' || $type == 'blob'{
  142.                 unset($definition[$key]['default']);
  143.             }
  144.             $definition[$key]['type'$type;
  145.             $definition[$key]['mdb2type'$type;
  146.         }
  147.         return $definition;
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ getTableIndexDefinition()
  152.     /**
  153.      * Get the stucture of an index into an array
  154.      *
  155.      * @param string    $table      name of table that should be used in method
  156.      * @param string    $index_name name of index that should be used in method
  157.      * @return mixed data array on success, a MDB2 error on failure
  158.      * @access public
  159.      */
  160.     function getTableIndexDefinition($table$index_name)
  161.     {
  162.         $db =$this->getDBInstance();
  163.         if (PEAR::isError($db)) {
  164.             return $db;
  165.         }
  166.  
  167.         $index_name $db->getIndexName($index_name);
  168.         $query 'SELECT relname, indkey FROM pg_index, pg_class';
  169.         $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
  170.         $query.= " AND indisunique != 't' AND indisprimary != 't'";
  171.         $query.= ' AND pg_class.relname = '.$db->quote($index_name'text');
  172.         $row $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  173.         if (PEAR::isError($row)) {
  174.             return $row;
  175.         }
  176.  
  177.         if (empty($row)) {
  178.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  179.                 'it was not specified an existing table index'__FUNCTION__);
  180.         }
  181.  
  182.         $row array_change_key_case($rowCASE_LOWER);
  183.  
  184.         $db->loadModule('Manager'nulltrue);
  185.         $columns $db->manager->listTableFields($table);
  186.  
  187.         $definition = array();
  188.  
  189.         $index_column_numbers explode(' '$row['indkey']);
  190.  
  191.         foreach ($index_column_numbers as $number{
  192.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  193.         }
  194.         return $definition;
  195.     }
  196.  
  197.     // }}}
  198.     // {{{ getTableConstraintDefinition()
  199.     /**
  200.      * Get the stucture of a constraint into an array
  201.      *
  202.      * @param string    $table      name of table that should be used in method
  203.      * @param string    $index_name name of index that should be used in method
  204.      * @return mixed data array on success, a MDB2 error on failure
  205.      * @access public
  206.      */
  207.     function getTableConstraintDefinition($table$index_name)
  208.     {
  209.         $db =$this->getDBInstance();
  210.         if (PEAR::isError($db)) {
  211.             return $db;
  212.         }
  213.  
  214.         $index_name $db->getIndexName($index_name);
  215.         $query 'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class';
  216.         $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
  217.         $query.= " AND (indisunique = 't' OR indisprimary = 't')";
  218.         $query.= ' AND pg_class.relname = '.$db->quote($index_name'text');
  219.         $row $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  220.         if (PEAR::isError($row)) {
  221.             return $row;
  222.         }
  223.  
  224.         if (empty($row)) {
  225.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  226.                 'it was not specified an existing table constraint'__FUNCTION__);
  227.         }
  228.  
  229.         $row array_change_key_case($rowCASE_LOWER);
  230.  
  231.         $db->loadModule('Manager'nulltrue);
  232.         $columns $db->manager->listTableFields($table);
  233.  
  234.         $definition = array();
  235.         if ($row['indisprimary'== 't'{
  236.             $definition['primary'= true;
  237.         elseif ($row['indisunique'== 't'{
  238.             $definition['unique'= true;
  239.         }
  240.  
  241.         $index_column_numbers explode(' '$row['indkey']);
  242.  
  243.         foreach ($index_column_numbers as $number{
  244.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  245.         }
  246.         return $definition;
  247.     }
  248.  
  249.     // }}}
  250.     // {{{ tableInfo()
  251.  
  252.     /**
  253.      * Returns information about a table or a result set
  254.      *
  255.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  256.      * is a table name.
  257.      *
  258.      * @param object|string $result  MDB2_result object from a query or a
  259.      *                                  string containing the name of a table.
  260.      *                                  While this also accepts a query result
  261.      *                                  resource identifier, this behavior is
  262.      *                                  deprecated.
  263.      * @param int            $mode    a valid tableInfo mode
  264.      *
  265.      * @return array  an associative array with the information requested.
  266.      *                  A MDB2_Error object on failure.
  267.      *
  268.      * @see MDB2_Driver_Common::tableInfo()
  269.      */
  270.     function tableInfo($result$mode = null)
  271.     {
  272.         if (is_string($result)) {
  273.            return parent::tableInfo($result$mode);
  274.         }
  275.  
  276.         $db =$this->getDBInstance();
  277.         if (PEAR::isError($db)) {
  278.             return $db;
  279.         }
  280.  
  281.         $id = MDB2::isResultCommon($result$result->getResource($result;
  282.         if (!is_resource($id)) {
  283.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  284.                 'Could not generate result ressource'__FUNCTION__);
  285.         }
  286.  
  287.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  288.             if ($db->options['field_case'== CASE_LOWER{
  289.                 $case_func 'strtolower';
  290.             else {
  291.                 $case_func 'strtoupper';
  292.             }
  293.         else {
  294.             $case_func 'strval';
  295.         }
  296.  
  297.         $count @pg_num_fields($id);
  298.         $res   = array();
  299.  
  300.         if ($mode{
  301.             $res['num_fields'$count;
  302.         }
  303.  
  304.         $db->loadModule('Datatype'nulltrue);
  305.         for ($i = 0; $i $count$i++{
  306.             $res[$i= array(
  307.                 'table' => '',
  308.                 'name'  => $case_func(@pg_field_name($id$i)),
  309.                 'type'  => @pg_field_type($id$i),
  310.                 'length' => @pg_field_size($id$i),
  311.                 'flags' => '',
  312.             );
  313.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  314.             if (PEAR::isError($mdb2type_info)) {
  315.                return $mdb2type_info;
  316.             }
  317.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  318.             if ($mode MDB2_TABLEINFO_ORDER{
  319.                 $res['order'][$res[$i]['name']] $i;
  320.             }
  321.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  322.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  323.             }
  324.         }
  325.  
  326.         return $res;
  327.     }
  328. }
  329. ?>

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