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.47 2006/07/17 16:12:31 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.             $definition[$key]['type'$type;
  142.             $definition[$key]['mdb2type'$type;
  143.         }
  144.         return $definition;
  145.     }
  146.  
  147.     // }}}
  148.     // {{{ getTableIndexDefinition()
  149.     /**
  150.      * Get the stucture of an index into an array
  151.      *
  152.      * @param string    $table      name of table that should be used in method
  153.      * @param string    $index_name name of index that should be used in method
  154.      * @return mixed data array on success, a MDB2 error on failure
  155.      * @access public
  156.      */
  157.     function getTableIndexDefinition($table$index_name)
  158.     {
  159.         $db =$this->getDBInstance();
  160.         if (PEAR::isError($db)) {
  161.             return $db;
  162.         }
  163.  
  164.         $index_name $db->getIndexName($index_name);
  165.         $query 'SELECT relname, indkey FROM pg_index, pg_class';
  166.         $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
  167.         $query.= " AND indisunique != 't' AND indisprimary != 't'";
  168.         $query.= ' AND pg_class.relname = '.$db->quote($index_name'text');
  169.         $row $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  170.         if (PEAR::isError($row)) {
  171.             return $row;
  172.         }
  173.  
  174.         if (empty($row)) {
  175.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  176.                 'it was not specified an existing table index'__FUNCTION__);
  177.         }
  178.  
  179.         $row array_change_key_case($rowCASE_LOWER);
  180.  
  181.         $db->loadModule('Manager'nulltrue);
  182.         $columns $db->manager->listTableFields($table);
  183.  
  184.         $definition = array();
  185.  
  186.         $index_column_numbers explode(' '$row['indkey']);
  187.  
  188.         foreach ($index_column_numbers as $number{
  189.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  190.         }
  191.         return $definition;
  192.     }
  193.  
  194.     // }}}
  195.     // {{{ getTableConstraintDefinition()
  196.     /**
  197.      * Get the stucture of a constraint into an array
  198.      *
  199.      * @param string    $table      name of table that should be used in method
  200.      * @param string    $index_name name of index that should be used in method
  201.      * @return mixed data array on success, a MDB2 error on failure
  202.      * @access public
  203.      */
  204.     function getTableConstraintDefinition($table$index_name)
  205.     {
  206.         $db =$this->getDBInstance();
  207.         if (PEAR::isError($db)) {
  208.             return $db;
  209.         }
  210.  
  211.         $index_name $db->getIndexName($index_name);
  212.         $query 'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class';
  213.         $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
  214.         $query.= " AND (indisunique = 't' OR indisprimary = 't')";
  215.         $query.= ' AND pg_class.relname = '.$db->quote($index_name'text');
  216.         $row $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  217.         if (PEAR::isError($row)) {
  218.             return $row;
  219.         }
  220.  
  221.         if (empty($row)) {
  222.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  223.                 'it was not specified an existing table constraint'__FUNCTION__);
  224.         }
  225.  
  226.         $row array_change_key_case($rowCASE_LOWER);
  227.  
  228.         $db->loadModule('Manager'nulltrue);
  229.         $columns $db->manager->listTableFields($table);
  230.  
  231.         $definition = array();
  232.         if ($row['indisprimary'== 't'{
  233.             $definition['primary'= true;
  234.         elseif ($row['indisunique'== 't'{
  235.             $definition['unique'= true;
  236.         }
  237.  
  238.         $index_column_numbers explode(' '$row['indkey']);
  239.  
  240.         foreach ($index_column_numbers as $number{
  241.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  242.         }
  243.         return $definition;
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ tableInfo()
  248.  
  249.     /**
  250.      * Returns information about a table or a result set
  251.      *
  252.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  253.      * is a table name.
  254.      *
  255.      * @param object|string $result  MDB2_result object from a query or a
  256.      *                                  string containing the name of a table.
  257.      *                                  While this also accepts a query result
  258.      *                                  resource identifier, this behavior is
  259.      *                                  deprecated.
  260.      * @param int            $mode    a valid tableInfo mode
  261.      *
  262.      * @return array  an associative array with the information requested.
  263.      *                  A MDB2_Error object on failure.
  264.      *
  265.      * @see MDB2_Driver_Common::tableInfo()
  266.      */
  267.     function tableInfo($result$mode = null)
  268.     {
  269.         if (is_string($result)) {
  270.            return parent::tableInfo($result$mode);
  271.         }
  272.  
  273.         $db =$this->getDBInstance();
  274.         if (PEAR::isError($db)) {
  275.             return $db;
  276.         }
  277.  
  278.         $id = MDB2::isResultCommon($result$result->getResource($result;
  279.         if (!is_resource($id)) {
  280.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  281.                 'Could not generate result ressource'__FUNCTION__);
  282.         }
  283.  
  284.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  285.             if ($db->options['field_case'== CASE_LOWER{
  286.                 $case_func 'strtolower';
  287.             else {
  288.                 $case_func 'strtoupper';
  289.             }
  290.         else {
  291.             $case_func 'strval';
  292.         }
  293.  
  294.         $count @pg_num_fields($id);
  295.         $res   = array();
  296.  
  297.         if ($mode{
  298.             $res['num_fields'$count;
  299.         }
  300.  
  301.         $db->loadModule('Datatype'nulltrue);
  302.         for ($i = 0; $i $count$i++{
  303.             $res[$i= array(
  304.                 'table' => '',
  305.                 'name'  => $case_func(@pg_field_name($id$i)),
  306.                 'type'  => @pg_field_type($id$i),
  307.                 'length' => @pg_field_size($id$i),
  308.                 'flags' => '',
  309.             );
  310.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  311.             if (PEAR::isError($mdb2type_info)) {
  312.                return $mdb2type_info;
  313.             }
  314.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  315.             if ($mode MDB2_TABLEINFO_ORDER{
  316.                 $res['order'][$res[$i]['name']] $i;
  317.             }
  318.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  319.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  320.             }
  321.         }
  322.  
  323.         return $res;
  324.     }
  325. }
  326. ?>

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