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-2008 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.82 2008/03/05 12:55:57 afz Exp $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 MySQL driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Paul Cooper <pgc@ucecom.com>
  55.  */
  56. class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
  57. {
  58.     // {{{ createDatabase()
  59.  
  60.     /**
  61.      * create a new database
  62.      *
  63.      * @param string $name    name of the database that should be created
  64.      * @param array  $options array with charset info
  65.      *
  66.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  67.      * @access public
  68.      */
  69.     function createDatabase($name$options = array())
  70.     {
  71.         $db =$this->getDBInstance();
  72.         if (PEAR::isError($db)) {
  73.             return $db;
  74.         }
  75.  
  76.         $name  $db->quoteIdentifier($nametrue);
  77.         $query 'CREATE DATABASE ' $name;
  78.         if (!empty($options['charset'])) {
  79.             $query .= ' WITH ENCODING ' $db->quote($options['charset']'text');
  80.         }
  81.         return $db->standaloneQuery($querynulltrue);
  82.     }
  83.  
  84.     // }}}
  85.     // {{{ alterDatabase()
  86.  
  87.     /**
  88.      * alter an existing database
  89.      *
  90.      * @param string $name    name of the database that is intended to be changed
  91.      * @param array  $options array with name, owner info
  92.      *
  93.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  94.      * @access public
  95.      */
  96.     function alterDatabase($name$options = array())
  97.     {
  98.         $db =$this->getDBInstance();
  99.         if (PEAR::isError($db)) {
  100.             return $db;
  101.         }
  102.  
  103.         $query 'ALTER DATABASE '$db->quoteIdentifier($nametrue);
  104.         if (!empty($options['name'])) {
  105.             $query .= ' RENAME TO ' $options['name'];
  106.         }
  107.         if (!empty($options['owner'])) {
  108.             $query .= ' OWNER TO ' $options['owner'];
  109.         }
  110.         return $db->standaloneQuery($querynulltrue);
  111.     }
  112.  
  113.     // }}}
  114.     // {{{ dropDatabase()
  115.  
  116.     /**
  117.      * drop an existing database
  118.      *
  119.      * @param string $name name of the database that should be dropped
  120.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  121.      * @access public
  122.      */
  123.     function dropDatabase($name)
  124.     {
  125.         $db =$this->getDBInstance();
  126.         if (PEAR::isError($db)) {
  127.             return $db;
  128.         }
  129.  
  130.         $name $db->quoteIdentifier($nametrue);
  131.         $query = "DROP DATABASE $name";
  132.         return $db->standaloneQuery($querynulltrue);
  133.     }
  134.  
  135.     // }}}
  136.     // {{{ _getAdvancedFKOptions()
  137.  
  138.     /**
  139.      * Return the FOREIGN KEY query section dealing with non-standard options
  140.      * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  141.      *
  142.      * @param array $definition 
  143.      * @return string 
  144.      * @access protected
  145.      */
  146.     function _getAdvancedFKOptions($definition)
  147.     {
  148.         $query '';
  149.         if (!empty($definition['match'])) {
  150.             $query .= ' MATCH '.$definition['match'];
  151.         }
  152.         if (!empty($definition['onupdate'])) {
  153.             $query .= ' ON UPDATE '.$definition['onupdate'];
  154.         }
  155.         if (!empty($definition['ondelete'])) {
  156.             $query .= ' ON DELETE '.$definition['ondelete'];
  157.         }
  158.         if (!empty($definition['deferrable'])) {
  159.             $query .= ' DEFERRABLE';
  160.         else {
  161.             $query .= ' NOT DEFERRABLE';
  162.         }
  163.         if (!empty($definition['initiallydeferred'])) {
  164.             $query .= ' INITIALLY DEFERRED';
  165.         else {
  166.             $query .= ' INITIALLY IMMEDIATE';
  167.         }
  168.         return $query;
  169.     }
  170.  
  171.     // }}}
  172.     // {{{ truncateTable()
  173.  
  174.     /**
  175.      * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
  176.      * it falls back to a DELETE FROM TABLE query)
  177.      *
  178.      * @param string $name name of the table that should be truncated
  179.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  180.      * @access public
  181.      */
  182.     function truncateTable($name)
  183.     {
  184.         $db =$this->getDBInstance();
  185.         if (PEAR::isError($db)) {
  186.             return $db;
  187.         }
  188.  
  189.         $name $db->quoteIdentifier($nametrue);
  190.         return $db->exec("TRUNCATE TABLE $name");
  191.     }
  192.  
  193.     // }}}
  194.     // {{{ vacuum()
  195.  
  196.     /**
  197.      * Optimize (vacuum) all the tables in the db (or only the specified table)
  198.      * and optionally run ANALYZE.
  199.      *
  200.      * @param string $table table name (all the tables if empty)
  201.      * @param array  $options an array with driver-specific options:
  202.      *                - timeout [int] (in seconds) [mssql-only]
  203.      *                - analyze [boolean] [pgsql and mysql]
  204.      *                - full [boolean] [pgsql-only]
  205.      *                - freeze [boolean] [pgsql-only]
  206.      *
  207.      * @return mixed MDB2_OK success, a MDB2 error on failure
  208.      * @access public
  209.      */
  210.     function vacuum($table = null$options = array())
  211.     {
  212.         $db =$this->getDBInstance();
  213.         if (PEAR::isError($db)) {
  214.             return $db;
  215.         }
  216.         $query 'VACUUM';
  217.  
  218.         if (!empty($options['full'])) {
  219.             $query .= ' FULL';
  220.         }
  221.         if (!empty($options['freeze'])) {
  222.             $query .= ' FREEZE';
  223.         }
  224.         if (!empty($options['analyze'])) {
  225.             $query .= ' ANALYZE';
  226.         }
  227.  
  228.         if (!empty($table)) {
  229.             $query .= ' '.$db->quoteIdentifier($tabletrue);
  230.         }
  231.         return $db->exec($query);
  232.     }
  233.  
  234.     // }}}
  235.     // {{{ alterTable()
  236.  
  237.     /**
  238.      * alter an existing table
  239.      *
  240.      * @param string $name         name of the table that is intended to be changed.
  241.      * @param array $changes     associative array that contains the details of each type
  242.      *                              of change that is intended to be performed. The types of
  243.      *                              changes that are currently supported are defined as follows:
  244.      *
  245.      *                              name
  246.      *
  247.      *                                 New name for the table.
  248.      *
  249.      *                             add
  250.      *
  251.      *                                 Associative array with the names of fields to be added as
  252.      *                                  indexes of the array. The value of each entry of the array
  253.      *                                  should be set to another associative array with the properties
  254.      *                                  of the fields to be added. The properties of the fields should
  255.      *                                  be the same as defined by the MDB2 parser.
  256.      *
  257.      *
  258.      *                             remove
  259.      *
  260.      *                                 Associative array with the names of fields to be removed as indexes
  261.      *                                  of the array. Currently the values assigned to each entry are ignored.
  262.      *                                  An empty array should be used for future compatibility.
  263.      *
  264.      *                             rename
  265.      *
  266.      *                                 Associative array with the names of fields to be renamed as indexes
  267.      *                                  of the array. The value of each entry of the array should be set to
  268.      *                                  another associative array with the entry named name with the new
  269.      *                                  field name and the entry named Declaration that is expected to contain
  270.      *                                  the portion of the field declaration already in DBMS specific SQL code
  271.      *                                  as it is used in the CREATE TABLE statement.
  272.      *
  273.      *                             change
  274.      *
  275.      *                                 Associative array with the names of the fields to be changed as indexes
  276.      *                                  of the array. Keep in mind that if it is intended to change either the
  277.      *                                  name of a field and any other properties, the change array entries
  278.      *                                  should have the new names of the fields as array indexes.
  279.      *
  280.      *                                 The value of each entry of the array should be set to another associative
  281.      *                                  array with the properties of the fields to that are meant to be changed as
  282.      *                                  array entries. These entries should be assigned to the new values of the
  283.      *                                  respective properties. The properties of the fields should be the same
  284.      *                                  as defined by the MDB2 parser.
  285.      *
  286.      *                             Example
  287.      *                                 array(
  288.      *                                     'name' => 'userlist',
  289.      *                                     'add' => array(
  290.      *                                         'quota' => array(
  291.      *                                             'type' => 'integer',
  292.      *                                             'unsigned' => 1
  293.      *                                         )
  294.      *                                     ),
  295.      *                                     'remove' => array(
  296.      *                                         'file_limit' => array(),
  297.      *                                         'time_limit' => array()
  298.      *                                     ),
  299.      *                                     'change' => array(
  300.      *                                         'name' => array(
  301.      *                                             'length' => '20',
  302.      *                                             'definition' => array(
  303.      *                                                 'type' => 'text',
  304.      *                                                 'length' => 20,
  305.      *                                             ),
  306.      *                                         )
  307.      *                                     ),
  308.      *                                     'rename' => array(
  309.      *                                         'sex' => array(
  310.      *                                             'name' => 'gender',
  311.      *                                             'definition' => array(
  312.      *                                                 'type' => 'text',
  313.      *                                                 'length' => 1,
  314.      *                                                 'default' => 'M',
  315.      *                                             ),
  316.      *                                         )
  317.      *                                     )
  318.      *                                 )
  319.      *
  320.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  321.      *                              can perform the requested table alterations if the value is true or
  322.      *                              actually perform them otherwise.
  323.      * @access public
  324.      *
  325.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  326.      */
  327.     function alterTable($name$changes$check)
  328.     {
  329.         $db =$this->getDBInstance();
  330.         if (PEAR::isError($db)) {
  331.             return $db;
  332.         }
  333.  
  334.         foreach ($changes as $change_name => $change{
  335.             switch ($change_name{
  336.             case 'add':
  337.             case 'remove':
  338.             case 'change':
  339.             case 'name':
  340.             case 'rename':
  341.                 break;
  342.             default:
  343.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  344.                     'change type "'.$change_name.'\" not yet supported'__FUNCTION__);
  345.             }
  346.         }
  347.  
  348.         if ($check{
  349.             return MDB2_OK;
  350.         }
  351.  
  352.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  353.             foreach ($changes['remove'as $field_name => $field{
  354.                 $field_name $db->quoteIdentifier($field_nametrue);
  355.                 $query 'DROP ' $field_name;
  356.                 $result $db->exec("ALTER TABLE $name $query");
  357.                 if (PEAR::isError($result)) {
  358.                     return $result;
  359.                 }
  360.             }
  361.         }
  362.  
  363.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  364.             foreach ($changes['rename'as $field_name => $field{
  365.                 $field_name $db->quoteIdentifier($field_nametrue);
  366.                 $result $db->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name']true));
  367.                 if (PEAR::isError($result)) {
  368.                     return $result;
  369.                 }
  370.             }
  371.         }
  372.  
  373.         if (!empty($changes['add']&& is_array($changes['add'])) {
  374.             foreach ($changes['add'as $field_name => $field{
  375.                 $query 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  376.                 $result $db->exec("ALTER TABLE $name $query");
  377.                 if (PEAR::isError($result)) {
  378.                     return $result;
  379.                 }
  380.             }
  381.         }
  382.  
  383.         if (!empty($changes['change']&& is_array($changes['change'])) {
  384.             foreach ($changes['change'as $field_name => $field{
  385.                 $field_name $db->quoteIdentifier($field_nametrue);
  386.                 if (!empty($field['definition']['type'])) {
  387.                     $server_info $db->getServerVersion();
  388.                     if (PEAR::isError($server_info)) {
  389.                         return $server_info;
  390.                     }
  391.                     if (is_array($server_info&& $server_info['major'< 8{
  392.                         return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  393.                             'changing column type for "'.$change_name.'\" requires PostgreSQL 8.0 or above'__FUNCTION__);
  394.                     }
  395.                     $db->loadModule('Datatype'nulltrue);
  396.                     $query = "ALTER $field_name TYPE ".$db->datatype->getTypeDeclaration($field['definition']);
  397.                     $result $db->exec("ALTER TABLE $name $query");
  398.                     if (PEAR::isError($result)) {
  399.                         return $result;
  400.                     }
  401.                 }
  402.                 if (array_key_exists('default'$field['definition'])) {
  403.                     $query = "ALTER $field_name SET DEFAULT ".$db->quote($field['definition']['default']$field['definition']['type']);
  404.                     $result $db->exec("ALTER TABLE $name $query");
  405.                     if (PEAR::isError($result)) {
  406.                         return $result;
  407.                     }
  408.                 }
  409.                 if (!empty($field['definition']['notnull'])) {
  410.                     $query = "ALTER $field_name ".($field['definition']['notnull''SET' 'DROP').' NOT NULL';
  411.                     $result $db->exec("ALTER TABLE $name $query");
  412.                     if (PEAR::isError($result)) {
  413.                         return $result;
  414.                     }
  415.                 }
  416.             }
  417.         }
  418.  
  419.         $name $db->quoteIdentifier($nametrue);
  420.         if (!empty($changes['name'])) {
  421.             $change_name $db->quoteIdentifier($changes['name']true);
  422.             $result $db->exec("ALTER TABLE $name RENAME TO ".$change_name);
  423.             if (PEAR::isError($result)) {
  424.                 return $result;
  425.             }
  426.         }
  427.  
  428.         return MDB2_OK;
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ listDatabases()
  433.  
  434.     /**
  435.      * list all databases
  436.      *
  437.      * @return mixed array of database names on success, a MDB2 error on failure
  438.      * @access public
  439.      */
  440.     function listDatabases()
  441.     {
  442.         $db =$this->getDBInstance();
  443.         if (PEAR::isError($db)) {
  444.             return $db;
  445.         }
  446.  
  447.         $query 'SELECT datname FROM pg_database';
  448.         $result2 $db->standaloneQuery($queryarray('text')false);
  449.         if (!MDB2::isResultCommon($result2)) {
  450.             return $result2;
  451.         }
  452.  
  453.         $result $result2->fetchCol();
  454.         $result2->free();
  455.         if (PEAR::isError($result)) {
  456.             return $result;
  457.         }
  458.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  459.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  460.         }
  461.         return $result;
  462.     }
  463.  
  464.     // }}}
  465.     // {{{ listUsers()
  466.  
  467.     /**
  468.      * list all users
  469.      *
  470.      * @return mixed array of user names on success, a MDB2 error on failure
  471.      * @access public
  472.      */
  473.     function listUsers()
  474.     {
  475.         $db =$this->getDBInstance();
  476.         if (PEAR::isError($db)) {
  477.             return $db;
  478.         }
  479.  
  480.         $query 'SELECT usename FROM pg_user';
  481.         $result2 $db->standaloneQuery($queryarray('text')false);
  482.         if (!MDB2::isResultCommon($result2)) {
  483.             return $result2;
  484.         }
  485.  
  486.         $result $result2->fetchCol();
  487.         $result2->free();
  488.         return $result;
  489.     }
  490.  
  491.     // }}}
  492.     // {{{ listViews()
  493.  
  494.     /**
  495.      * list all views in the current database
  496.      *
  497.      * @return mixed array of view names on success, a MDB2 error on failure
  498.      * @access public
  499.      */
  500.     function listViews()
  501.     {
  502.         $db =$this->getDBInstance();
  503.         if (PEAR::isError($db)) {
  504.             return $db;
  505.         }
  506.  
  507.         $query "SELECT viewname
  508.                     FROM pg_views
  509.                    WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
  510.                      AND viewname !~ '^pg_'";
  511.         $result $db->queryCol($query);
  512.         if (PEAR::isError($result)) {
  513.             return $result;
  514.         }
  515.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  516.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  517.         }
  518.         return $result;
  519.     }
  520.  
  521.     // }}}
  522.     // {{{ listTableViews()
  523.  
  524.     /**
  525.      * list the views in the database that reference a given table
  526.      *
  527.      * @param string table for which all referenced views should be found
  528.      * @return mixed array of view names on success, a MDB2 error on failure
  529.      * @access public
  530.      */
  531.     function listTableViews($table)
  532.     {
  533.         $db =$this->getDBInstance();
  534.         if (PEAR::isError($db)) {
  535.             return $db;
  536.         }
  537.  
  538.         $query 'SELECT viewname FROM pg_views NATURAL JOIN pg_tables';
  539.         $query.= ' WHERE tablename ='.$db->quote($table'text');
  540.         $result $db->queryCol($query);
  541.         if (PEAR::isError($result)) {
  542.             return $result;
  543.         }
  544.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  545.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  546.         }
  547.         return $result;
  548.     }
  549.  
  550.     // }}}
  551.     // {{{ listFunctions()
  552.  
  553.     /**
  554.      * list all functions in the current database
  555.      *
  556.      * @return mixed array of function names on success, a MDB2 error on failure
  557.      * @access public
  558.      */
  559.     function listFunctions()
  560.     {
  561.         $db =$this->getDBInstance();
  562.         if (PEAR::isError($db)) {
  563.             return $db;
  564.         }
  565.  
  566.         $query "
  567.             SELECT
  568.                 proname
  569.             FROM
  570.                 pg_proc pr,
  571.                 pg_type tp
  572.             WHERE
  573.                 tp.oid = pr.prorettype
  574.                 AND pr.proisagg = FALSE
  575.                 AND tp.typname <> 'trigger'
  576.                 AND pr.pronamespace IN
  577.                     (SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  578.         $result $db->queryCol($query);
  579.         if (PEAR::isError($result)) {
  580.             return $result;
  581.         }
  582.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  583.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  584.         }
  585.         return $result;
  586.     }
  587.  
  588.     // }}}
  589.     // {{{ listTableTriggers()
  590.  
  591.     /**
  592.      * list all triggers in the database that reference a given table
  593.      *
  594.      * @param string table for which all referenced triggers should be found
  595.      * @return mixed array of trigger names on success, a MDB2 error on failure
  596.      * @access public
  597.      */
  598.     function listTableTriggers($table = null)
  599.     {
  600.         $db =$this->getDBInstance();
  601.         if (PEAR::isError($db)) {
  602.             return $db;
  603.         }
  604.  
  605.         $query 'SELECT trg.tgname AS trigger_name
  606.                     FROM pg_trigger trg,
  607.                          pg_class tbl
  608.                    WHERE trg.tgrelid = tbl.oid';
  609.         if (!is_null($table)) {
  610.             $table $db->quote(strtoupper($table)'text');
  611.             $query .= " AND tbl.relname = $table";
  612.         }
  613.         $result $db->queryCol($query);
  614.         if (PEAR::isError($result)) {
  615.             return $result;
  616.         }
  617.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  618.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  619.         }
  620.         return $result;
  621.     }
  622.  
  623.     // }}}
  624.     // {{{ listTables()
  625.  
  626.     /**
  627.      * list all tables in the current database
  628.      *
  629.      * @return mixed array of table names on success, a MDB2 error on failure
  630.      * @access public
  631.      */
  632.     function listTables()
  633.     {
  634.         $db =$this->getDBInstance();
  635.         if (PEAR::isError($db)) {
  636.             return $db;
  637.         }
  638.  
  639.         // gratuitously stolen from PEAR DB _getSpecialQuery in pgsql.php
  640.         $query 'SELECT c.relname AS "Name"'
  641.             . ' FROM pg_class c, pg_user u'
  642.             . ' WHERE c.relowner = u.usesysid'
  643.             . " AND c.relkind = 'r'"
  644.             . ' AND NOT EXISTS'
  645.             . ' (SELECT 1 FROM pg_views'
  646.             . '  WHERE viewname = c.relname)'
  647.             . " AND c.relname !~ '^(pg_|sql_)'"
  648.             . ' UNION'
  649.             . ' SELECT c.relname AS "Name"'
  650.             . ' FROM pg_class c'
  651.             . " WHERE c.relkind = 'r'"
  652.             . ' AND NOT EXISTS'
  653.             . ' (SELECT 1 FROM pg_views'
  654.             . '  WHERE viewname = c.relname)'
  655.             . ' AND NOT EXISTS'
  656.             . ' (SELECT 1 FROM pg_user'
  657.             . '  WHERE usesysid = c.relowner)'
  658.             . " AND c.relname !~ '^pg_'";
  659.         $result $db->queryCol($query);
  660.         if (PEAR::isError($result)) {
  661.             return $result;
  662.         }
  663.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  664.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  665.         }
  666.         return $result;
  667.     }
  668.  
  669.     // }}}
  670.     // {{{ listTableFields()
  671.  
  672.     /**
  673.      * list all fields in a table in the current database
  674.      *
  675.      * @param string $table name of table that should be used in method
  676.      * @return mixed array of field names on success, a MDB2 error on failure
  677.      * @access public
  678.      */
  679.     function listTableFields($table)
  680.     {
  681.         $db =$this->getDBInstance();
  682.         if (PEAR::isError($db)) {
  683.             return $db;
  684.         }
  685.  
  686.         $table $db->quoteIdentifier($tabletrue);
  687.         $db->setLimit(1);
  688.         $result2 $db->query("SELECT * FROM $table");
  689.         if (PEAR::isError($result2)) {
  690.             return $result2;
  691.         }
  692.         $result $result2->getColumnNames();
  693.         $result2->free();
  694.         if (PEAR::isError($result)) {
  695.             return $result;
  696.         }
  697.         return array_flip($result);
  698.     }
  699.  
  700.     // }}}
  701.     // {{{ listTableIndexes()
  702.  
  703.     /**
  704.      * list all indexes in a table
  705.      *
  706.      * @param string $table name of table that should be used in method
  707.      * @return mixed array of index names on success, a MDB2 error on failure
  708.      * @access public
  709.      */
  710.     function listTableIndexes($table)
  711.     {
  712.         $db =$this->getDBInstance();
  713.         if (PEAR::isError($db)) {
  714.             return $db;
  715.         }
  716.  
  717.         $table $db->quote($table'text');
  718.         $subquery "SELECT indexrelid FROM pg_index, pg_class";
  719.         $subquery.= " WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
  720.         $query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
  721.         $indexes $db->queryCol($query'text');
  722.         if (PEAR::isError($indexes)) {
  723.             return $indexes;
  724.         }
  725.  
  726.         $result = array();
  727.         foreach ($indexes as $index{
  728.             $index $this->_fixIndexName($index);
  729.             if (!empty($index)) {
  730.                 $result[$index= true;
  731.             }
  732.         }
  733.  
  734.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  735.             $result array_change_key_case($result$db->options['field_case']);
  736.         }
  737.         return array_keys($result);
  738.     }
  739.  
  740.     // }}}
  741.     // {{{ listTableConstraints()
  742.  
  743.     /**
  744.      * list all constraints in a table
  745.      *
  746.      * @param string $table name of table that should be used in method
  747.      * @return mixed array of constraint names on success, a MDB2 error on failure
  748.      * @access public
  749.      */
  750.     function listTableConstraints($table)
  751.     {
  752.         $db =$this->getDBInstance();
  753.         if (PEAR::isError($db)) {
  754.             return $db;
  755.         }
  756.  
  757.         $table $db->quote($table'text');
  758.         $query 'SELECT conname
  759.                     FROM pg_constraint, pg_class
  760.                    WHERE pg_constraint.conrelid = pg_class.oid
  761.                      AND relname = ' .$table;
  762.         $constraints $db->queryCol($query);
  763.         if (PEAR::isError($constraints)) {
  764.             return $constraints;
  765.         }
  766.  
  767.         $result = array();
  768.         foreach ($constraints as $constraint{
  769.             $constraint $this->_fixIndexName($constraint);
  770.             if (!empty($constraint)) {
  771.                 $result[$constraint= true;
  772.             }
  773.         }
  774.  
  775.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  776.             && $db->options['field_case'== CASE_LOWER
  777.         {
  778.             $result array_change_key_case($result$db->options['field_case']);
  779.         }
  780.         return array_keys($result);
  781.     }
  782.  
  783.     // }}}
  784.     // {{{ createSequence()
  785.  
  786.     /**
  787.      * create sequence
  788.      *
  789.      * @param string $seq_name name of the sequence to be created
  790.      * @param string $start start value of the sequence; default is 1
  791.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  792.      * @access public
  793.      */
  794.     function createSequence($seq_name$start = 1)
  795.     {
  796.         $db =$this->getDBInstance();
  797.         if (PEAR::isError($db)) {
  798.             return $db;
  799.         }
  800.  
  801.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  802.         return $db->exec("CREATE SEQUENCE $sequence_name INCREMENT 1".
  803.             ($start < 1 ? " MINVALUE $start" : '')." START $start");
  804.     }
  805.  
  806.     // }}}
  807.     // {{{ dropSequence()
  808.  
  809.     /**
  810.      * drop existing sequence
  811.      *
  812.      * @param string $seq_name name of the sequence to be dropped
  813.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  814.      * @access public
  815.      */
  816.     function dropSequence($seq_name)
  817.     {
  818.         $db =$this->getDBInstance();
  819.         if (PEAR::isError($db)) {
  820.             return $db;
  821.         }
  822.  
  823.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  824.         return $db->exec("DROP SEQUENCE $sequence_name");
  825.     }
  826.  
  827.     // }}}
  828.     // {{{ listSequences()
  829.  
  830.     /**
  831.      * list all sequences in the current database
  832.      *
  833.      * @return mixed array of sequence names on success, a MDB2 error on failure
  834.      * @access public
  835.      */
  836.     function listSequences()
  837.     {
  838.         $db =$this->getDBInstance();
  839.         if (PEAR::isError($db)) {
  840.             return $db;
  841.         }
  842.  
  843.         $query "SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
  844.         $query.= "(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  845.         $table_names $db->queryCol($query);
  846.         if (PEAR::isError($table_names)) {
  847.             return $table_names;
  848.         }
  849.         $result = array();
  850.         foreach ($table_names as $table_name{
  851.             $result[$this->_fixSequenceName($table_name);
  852.         }
  853.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  854.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  855.         }
  856.         return $result;
  857.     }
  858. }
  859. ?>

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