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

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