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-2007 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. // | Authors: Paul Cooper <pgc@ucecom.com>                                |
  43. // |          Lorenzo Alberton <l.alberton@quipo.it>                      |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: pgsql.php,v 1.57 2007/03/04 23:40:51 quipo Exp $
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 PostGreSQL driver for the schema reverse engineering module
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Paul Cooper <pgc@ucecom.com>
  56.  */
  57. class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
  58. {
  59.     // {{{ getTableFieldDefinition()
  60.  
  61.     /**
  62.      * Get the structure of a field into an array
  63.      *
  64.      * @param string    $table       name of table that should be used in method
  65.      * @param string    $field_name  name of field that should be used in method
  66.      * @return mixed data array on success, a MDB2 error on failure
  67.      * @access public
  68.      */
  69.     function getTableFieldDefinition($table$field_name)
  70.     {
  71.         $db =$this->getDBInstance();
  72.         if (PEAR::isError($db)) {
  73.             return $db;
  74.         }
  75.  
  76.         $result $db->loadModule('Datatype'nulltrue);
  77.         if (PEAR::isError($result)) {
  78.             return $result;
  79.         }
  80.  
  81.         $query "SELECT
  82.                     a.attname AS name, t.typname AS type, a.attlen AS length, a.attnotnull,
  83.                     a.atttypmod, a.atthasdef,
  84.                     (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
  85.                         FROM pg_attrdef d
  86.                         WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) as default
  87.                     FROM pg_attribute a, pg_class c, pg_type t
  88.                     WHERE c.relname = ".$db->quote($table'text')."
  89.                         AND a.atttypid = t.oid
  90.                         AND c.oid = a.attrelid
  91.                         AND NOT a.attisdropped
  92.                         AND a.attnum > 0
  93.                         AND a.attname = ".$db->quote($field_name'text')."
  94.                     ORDER BY a.attnum";
  95.         $column $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  96.         if (PEAR::isError($column)) {
  97.             return $column;
  98.         }
  99.  
  100.         if (empty($column)) {
  101.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  102.                 'it was not specified an existing table column'__FUNCTION__);
  103.         }
  104.  
  105.         $column array_change_key_case($columnCASE_LOWER);
  106.         $mapped_datatype $db->datatype->mapNativeDatatype($column);
  107.         if (PEAR::IsError($mapped_datatype)) {
  108.             return $mapped_datatype;
  109.         }
  110.         list($types$length$unsigned$fixed$mapped_datatype;
  111.         $notnull = false;
  112.         if (!empty($column['attnotnull']&& $column['attnotnull'== 't'{
  113.             $notnull = true;
  114.         }
  115.         $default = null;
  116.         if ($column['atthasdef'=== 't'
  117.             && !preg_match("/nextval\('([^']+)'/"$column['default'])
  118.         {
  119.             $default $column['default'];#substr($column['adsrc'], 1, -1);
  120.             if (is_null($default&& $notnull{
  121.                 $default '';
  122.             }
  123.         }
  124.         $autoincrement = false;
  125.         if (preg_match("/nextval\('([^']+)'/"$column['default']$nextvals)) {
  126.             $autoincrement = true;
  127.         }
  128.         $definition[0= array('notnull' => $notnull'nativetype' => $column['type']);
  129.         if (!is_null($length)) {
  130.             $definition[0]['length'$length;
  131.         }
  132.         if (!is_null($unsigned)) {
  133.             $definition[0]['unsigned'$unsigned;
  134.         }
  135.         if (!is_null($fixed)) {
  136.             $definition[0]['fixed'$fixed;
  137.         }
  138.         if ($default !== false{
  139.             $definition[0]['default'$default;
  140.         }
  141.         if ($autoincrement !== false{
  142.             $definition[0]['autoincrement'$autoincrement;
  143.         }
  144.         foreach ($types as $key => $type{
  145.             $definition[$key$definition[0];
  146.             if ($type == 'clob' || $type == 'blob'{
  147.                 unset($definition[$key]['default']);
  148.             }
  149.             $definition[$key]['type'$type;
  150.             $definition[$key]['mdb2type'$type;
  151.         }
  152.         return $definition;
  153.     }
  154.  
  155.     // }}}
  156.     // {{{ getTableIndexDefinition()
  157.     /**
  158.      * Get the structure of an index into an array
  159.      *
  160.      * @param string    $table      name of table that should be used in method
  161.      * @param string    $index_name name of index that should be used in method
  162.      * @return mixed data array on success, a MDB2 error on failure
  163.      * @access public
  164.      */
  165.     function getTableIndexDefinition($table$index_name)
  166.     {
  167.         $db =$this->getDBInstance();
  168.         if (PEAR::isError($db)) {
  169.             return $db;
  170.         }
  171.         
  172.         $query 'SELECT relname, indkey FROM pg_index, pg_class';
  173.         $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
  174.         $query.= " AND indisunique != 't' AND indisprimary != 't'";
  175.         $query.= ' AND pg_class.relname = %s';
  176.         $index_name_mdb2 $db->getIndexName($index_name);
  177.         $row $db->queryRow(sprintf($query$db->quote($index_name_mdb2'text'))nullMDB2_FETCHMODE_ASSOC);
  178.         if (PEAR::isError($row|| empty($row)) {
  179.             // fallback to the given $index_name, without transformation
  180.             $row $db->queryRow(sprintf($query$db->quote($index_name'text'))nullMDB2_FETCHMODE_ASSOC);
  181.         }
  182.         if (PEAR::isError($row)) {
  183.             return $row;
  184.         }
  185.  
  186.         if (empty($row)) {
  187.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  188.                 'it was not specified an existing table index'__FUNCTION__);
  189.         }
  190.  
  191.         $row array_change_key_case($rowCASE_LOWER);
  192.  
  193.         $db->loadModule('Manager'nulltrue);
  194.         $columns $db->manager->listTableFields($table);
  195.  
  196.         $definition = array();
  197.  
  198.         $index_column_numbers explode(' '$row['indkey']);
  199.  
  200.         foreach ($index_column_numbers as $number{
  201.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  202.         }
  203.         return $definition;
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ getTableConstraintDefinition()
  208.     /**
  209.      * Get the structure of a constraint into an array
  210.      *
  211.      * @param string    $table      name of table that should be used in method
  212.      * @param string    $index_name name of index that should be used in method
  213.      * @return mixed data array on success, a MDB2 error on failure
  214.      * @access public
  215.      */
  216.     function getTableConstraintDefinition($table$index_name)
  217.     {
  218.         $db =$this->getDBInstance();
  219.         if (PEAR::isError($db)) {
  220.             return $db;
  221.         }
  222.         
  223.         $query 'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class';
  224.         $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
  225.         $query.= " AND (indisunique = 't' OR indisprimary = 't')";
  226.         $query.= ' AND pg_class.relname = %s';
  227.         $index_name_mdb2 $db->getIndexName($index_name);
  228.         $row $db->queryRow(sprintf($query$db->quote($index_name_mdb2'text'))nullMDB2_FETCHMODE_ASSOC);
  229.         if (PEAR::isError($row|| empty($row)) {
  230.             // fallback to the given $index_name, without transformation
  231.             $row $db->queryRow(sprintf($query$db->quote($index_name'text'))nullMDB2_FETCHMODE_ASSOC);
  232.         }
  233.         if (PEAR::isError($row)) {
  234.             return $row;
  235.         }
  236.  
  237.         if (empty($row)) {
  238.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  239.                 'it was not specified an existing table constraint'__FUNCTION__);
  240.         }
  241.  
  242.         $row array_change_key_case($rowCASE_LOWER);
  243.  
  244.         $db->loadModule('Manager'nulltrue);
  245.         $columns $db->manager->listTableFields($table);
  246.  
  247.         $definition = array();
  248.         if ($row['indisprimary'== 't'{
  249.             $definition['primary'= true;
  250.         elseif ($row['indisunique'== 't'{
  251.             $definition['unique'= true;
  252.         }
  253.  
  254.         $index_column_numbers explode(' '$row['indkey']);
  255.  
  256.         foreach ($index_column_numbers as $number{
  257.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  258.         }
  259.         return $definition;
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ getTriggerDefinition()
  264.  
  265.     /**
  266.      * Get the structure of a trigger into an array
  267.      *
  268.      * EXPERIMENTAL
  269.      *
  270.      * WARNING: this function is experimental and may change the returned value
  271.      * at any time until labelled as non-experimental
  272.      *
  273.      * @param string    $trigger    name of trigger that should be used in method
  274.      * @return mixed data array on success, a MDB2 error on failure
  275.      * @access public
  276.      *
  277.      * @TODO: add support for plsql functions and functions with args
  278.      */
  279.     function getTriggerDefinition($trigger)
  280.     {
  281.         $db =$this->getDBInstance();
  282.         if (PEAR::isError($db)) {
  283.             return $db;
  284.         }
  285.  
  286.         $query "SELECT trg.tgname AS trigger_name,
  287.                          tbl.relname AS table_name,
  288.                          CASE
  289.                             WHEN p.proname IS NOT NULL THEN 'EXECUTE PROCEDURE ' || p.proname || '();'
  290.                             ELSE ''
  291.                          END AS trigger_body,
  292.                          CASE trg.tgtype & cast(2 as int2)
  293.                             WHEN 0 THEN 'AFTER'
  294.                             ELSE 'BEFORE'
  295.                          END AS trigger_type,
  296.                          CASE trg.tgtype & cast(28 as int2)
  297.                             WHEN 16 THEN 'UPDATE'
  298.                             WHEN 8 THEN 'DELETE'
  299.                             WHEN 4 THEN 'INSERT'
  300.                             WHEN 20 THEN 'INSERT, UPDATE'
  301.                             WHEN 28 THEN 'INSERT, UPDATE, DELETE'
  302.                             WHEN 24 THEN 'UPDATE, DELETE'
  303.                             WHEN 12 THEN 'INSERT, DELETE'
  304.                          END AS trigger_event,
  305.                          trg.tgenabled AS trigger_enabled,
  306.                          obj_description(trg.oid, 'pg_trigger') AS trigger_comment
  307.                     FROM pg_trigger trg,
  308.                          pg_class tbl,
  309.                          pg_proc p
  310.                    WHERE trg.tgrelid = tbl.oid
  311.                      AND trg.tgfoid = p.oid
  312.                      AND trg.tgname = "$db->quote($trigger'text');
  313.         $types = array(
  314.             'trigger_name'    => 'text',
  315.             'table_name'      => 'text',
  316.             'trigger_body'    => 'text',
  317.             'trigger_type'    => 'text',
  318.             'trigger_event'   => 'text',
  319.             'trigger_comment' => 'text',
  320.             'trigger_enabled' => 'boolean',
  321.         );
  322.         return $db->queryRow($query$typesMDB2_FETCHMODE_ASSOC);
  323.     }
  324.     
  325.     // }}}
  326.     // {{{ tableInfo()
  327.  
  328.     /**
  329.      * Returns information about a table or a result set
  330.      *
  331.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  332.      * is a table name.
  333.      *
  334.      * @param object|string $result  MDB2_result object from a query or a
  335.      *                                  string containing the name of a table.
  336.      *                                  While this also accepts a query result
  337.      *                                  resource identifier, this behavior is
  338.      *                                  deprecated.
  339.      * @param int            $mode    a valid tableInfo mode
  340.      *
  341.      * @return array  an associative array with the information requested.
  342.      *                  A MDB2_Error object on failure.
  343.      *
  344.      * @see MDB2_Driver_Common::tableInfo()
  345.      */
  346.     function tableInfo($result$mode = null)
  347.     {
  348.         if (is_string($result)) {
  349.            return parent::tableInfo($result$mode);
  350.         }
  351.  
  352.         $db =$this->getDBInstance();
  353.         if (PEAR::isError($db)) {
  354.             return $db;
  355.         }
  356.  
  357.         $resource = MDB2::isResultCommon($result$result->getResource($result;
  358.         if (!is_resource($resource)) {
  359.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  360.                 'Could not generate result resource'__FUNCTION__);
  361.         }
  362.  
  363.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  364.             if ($db->options['field_case'== CASE_LOWER{
  365.                 $case_func 'strtolower';
  366.             else {
  367.                 $case_func 'strtoupper';
  368.             }
  369.         else {
  370.             $case_func 'strval';
  371.         }
  372.  
  373.         $count @pg_num_fields($resource);
  374.         $res   = array();
  375.  
  376.         if ($mode{
  377.             $res['num_fields'$count;
  378.         }
  379.  
  380.         $db->loadModule('Datatype'nulltrue);
  381.         for ($i = 0; $i $count$i++{
  382.             $res[$i= array(
  383.                 'table' => function_exists('pg_field_table'@pg_field_table($resource$i'',
  384.                 'name'  => $case_func(@pg_field_name($resource$i)),
  385.                 'type'  => @pg_field_type($resource$i),
  386.                 'length' => @pg_field_size($resource$i),
  387.                 'flags' => '',
  388.             );
  389.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  390.             if (PEAR::isError($mdb2type_info)) {
  391.                return $mdb2type_info;
  392.             }
  393.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  394.             if ($mode MDB2_TABLEINFO_ORDER{
  395.                 $res['order'][$res[$i]['name']] $i;
  396.             }
  397.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  398.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  399.             }
  400.         }
  401.  
  402.         return $res;
  403.     }
  404. }
  405. ?>

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