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

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