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-2004 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.18 2005/04/04 08:08:35 lsmith 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 =$GLOBALS['_MDB2_databases'][$this->db_index];
  70.         return $db->standaloneQuery("CREATE DATABASE $name");
  71.     }
  72.  
  73.     // }}}
  74.     // {{{ dropDatabase()
  75.  
  76.     /**
  77.      * drop an existing database
  78.      *
  79.      * @param string $name name of the database that should be dropped
  80.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  81.      * @access public
  82.      ***/
  83.     function dropDatabase($name)
  84.     {
  85.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  86.         return $db->standaloneQuery("DROP DATABASE $name");
  87.     }
  88.  
  89.     // }}}
  90.     // {{{ alterTable()
  91.  
  92.     /**
  93.      * alter an existing table
  94.      *
  95.      * @param string $name name of the table that is intended to be changed.
  96.      * @param array $changes associative array that contains the details of each type
  97.      *                               of change that is intended to be performed. The types of
  98.      *                               changes that are currently supported are defined as follows:
  99.      *
  100.      *                               name
  101.      *
  102.      *                                  New name for the table.
  103.      *
  104.      *                              added_fields
  105.      *
  106.      *                                  Associative array with the names of fields to be added as
  107.      *                                   indexes of the array. The value of each entry of the array
  108.      *                                   should be set to another associative array with the properties
  109.      *                                   of the fields to be added. The properties of the fields should
  110.      *                                   be the same as defined by the Metabase parser.
  111.      *
  112.      *                                  Additionally, there should be an entry named Declaration that
  113.      *                                   is expected to contain the portion of the field declaration already
  114.      *                                   in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  115.      *
  116.      *                              removed_fields
  117.      *
  118.      *                                  Associative array with the names of fields to be removed as indexes
  119.      *                                   of the array. Currently the values assigned to each entry are ignored.
  120.      *                                   An empty array should be used for future compatibility.
  121.      *
  122.      *                              renamed_fields
  123.      *
  124.      *                                  Associative array with the names of fields to be renamed as indexes
  125.      *                                   of the array. The value of each entry of the array should be set to
  126.      *                                   another associative array with the entry named name with the new
  127.      *                                   field name and the entry named Declaration that is expected to contain
  128.      *                                   the portion of the field declaration already in DBMS specific SQL code
  129.      *                                   as it is used in the CREATE TABLE statement.
  130.      *
  131.      *                              changed_fields
  132.      *
  133.      *                                  Associative array with the names of the fields to be changed as indexes
  134.      *                                   of the array. Keep in mind that if it is intended to change either the
  135.      *                                   name of a field and any other properties, the changed_fields array entries
  136.      *                                   should have the new names of the fields as array indexes.
  137.      *
  138.      *                                  The value of each entry of the array should be set to another associative
  139.      *                                   array with the properties of the fields to that are meant to be changed as
  140.      *                                   array entries. These entries should be assigned to the new values of the
  141.      *                                   respective properties. The properties of the fields should be the same
  142.      *                                   as defined by the Metabase parser.
  143.      *
  144.      *                                  If the default property is meant to be added, removed or changed, there
  145.      *                                   should also be an entry with index ChangedDefault assigned to 1. Similarly,
  146.      *                                   if the notnull constraint is to be added or removed, there should also be
  147.      *                                   an entry with index ChangedNotNull assigned to 1.
  148.      *
  149.      *                                  Additionally, there should be an entry named Declaration that is expected
  150.      *                                   to contain the portion of the field changed declaration already in DBMS
  151.      *                                   specific SQL code as it is used in the CREATE TABLE statement.
  152.      *                              Example
  153.      *                                  array(
  154.      *                                      'name' => 'userlist',
  155.      *                                      'added_fields' => array(
  156.      *                                          'quota' => array(
  157.      *                                              'type' => 'integer',
  158.      *                                              'unsigned' => 1
  159.      *                                              'declaration' => 'quota INT'
  160.      *                                          )
  161.      *                                      ),
  162.      *                                      'removed_fields' => array(
  163.      *                                          'file_limit' => array(),
  164.      *                                          'time_limit' => array()
  165.      *                                          ),
  166.      *                                      'changed_fields' => array(
  167.      *                                          'gender' => array(
  168.      *                                              'default' => 'M',
  169.      *                                              'change_default' => 1,
  170.      *                                              'declaration' => "gender CHAR(1) DEFAULT 'M'"
  171.      *                                          )
  172.      *                                      ),
  173.      *                                      'renamed_fields' => array(
  174.      *                                          'sex' => array(
  175.      *                                              'name' => 'gender',
  176.      *                                              'declaration' => "gender CHAR(1) DEFAULT 'M'"
  177.      *                                          )
  178.      *                                      )
  179.      *                                  )
  180.      * @param boolean $check indicates whether the function should just check if the DBMS driver
  181.      *                               can perform the requested table alterations if the value is true or
  182.      *                               actually perform them otherwise.
  183.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  184.      * @access public
  185.      ***/
  186.     function alterTable($name&$changes$check)
  187.     {
  188.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  189.         foreach ($changes as $change_name => $change{
  190.             switch ($change_name{
  191.             case 'added_fields':
  192.                 break;
  193.             case 'removed_fields':
  194.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  195.                 'alterTable: database server does not support dropping table columns');
  196.             case 'name':
  197.             case 'renamed_fields':
  198.             case 'changed_fields':
  199.             default:
  200.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  201.                     'alterTable: change type "'.$change_name.'\" not yet supported');
  202.             }
  203.         }
  204.         if ($check{
  205.             return MDB2_OK;
  206.         }
  207.         if (isset($changes['added_fields'])) {
  208.             $fields $changes['added_fields'];
  209.             foreach ($fields as $field{
  210.                 $result $db->query("ALTER TABLE $name ADD ".$field['declaration']);
  211.                 if (PEAR::isError($result)) {
  212.                     return $result;
  213.                 }
  214.             }
  215.         }
  216.         if (isset($changes['removed_fields'])) {
  217.             $fields $changes['removed_fields'];
  218.             foreach ($fields as $field_name => $field{
  219.                 $result $db->query("ALTER TABLE $name DROP ".$field_name);
  220.                 if (PEAR::isError($result)) {
  221.                     return $result;
  222.                 }
  223.             }
  224.         }
  225.         return MDB2_OK;
  226.     }
  227.  
  228.     // }}}
  229.     // {{{ listDatabases()
  230.  
  231.     /**
  232.      * list all databases
  233.      *
  234.      * @return mixed data array on success, a MDB2 error on failure
  235.      * @access public
  236.      ***/
  237.     function listDatabases()
  238.     {
  239.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  240.         $result $db->standaloneQuery('SELECT datname FROM pg_database');
  241.         if (!MDB2::isResultCommon($result)) {
  242.             return $result;
  243.         }
  244.  
  245.         $col $result->fetchCol();
  246.         $result->free();
  247.         return $col;
  248.     }
  249.  
  250.     // }}}
  251.     // {{{ listUsers()
  252.  
  253.     /**
  254.      * list all users
  255.      *
  256.      * @return mixed data array on success, a MDB2 error on failure
  257.      * @access public
  258.      ***/
  259.     function listUsers()
  260.     {
  261.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  262.         $result $db->standaloneQuery('SELECT usename FROM pg_user');
  263.         if (!MDB2::isResultCommon($result)) {
  264.             return $result;
  265.         }
  266.  
  267.         $col $result->fetchCol();
  268.         $result->free();
  269.         return $col;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ listViews()
  274.  
  275.     /**
  276.      * list the views in the database
  277.      *
  278.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  279.      * @access public
  280.      ***/
  281.     function listViews()
  282.     {
  283.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  284.         $query 'SELECT viewname FROM pg_views';
  285.         return $db->queryCol($query);
  286.     }
  287.  
  288.     // }}}
  289.     // {{{ listFunctions()
  290.  
  291.     /**
  292.      * list all functions in the current database
  293.      *
  294.      * @return mixed data array on success, a MDB2 error on failure
  295.      * @access public
  296.      */
  297.     function listFunctions()
  298.     {
  299.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  300.         $query "
  301.             SELECT
  302.                 proname
  303.             FROM
  304.                 pg_proc pr,
  305.                 pg_type tp
  306.             WHERE
  307.                 tp.oid = pr.prorettype
  308.                 AND pr.proisagg = FALSE
  309.                 AND tp.typname <> 'trigger'
  310.                 AND pr.pronamespace IN
  311.                     (SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  312.         return $db->queryCol($query);
  313.     }
  314.  
  315.     // }}}
  316.     // {{{ listTables()
  317.  
  318.     /**
  319.      * list all tables in the current database
  320.      *
  321.      * @return mixed data array on success, a MDB2 error on failure
  322.      * @access public
  323.      ***/
  324.     function listTables()
  325.     {
  326.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  327.         // gratuitously stolen from PEAR DB _getSpecialQuery in pgsql.php
  328.         $query 'SELECT c.relname AS "Name"'
  329.                         . ' FROM pg_class c, pg_user u'
  330.                         . ' WHERE c.relowner = u.usesysid'
  331.                         . " AND c.relkind = 'r'"
  332.                         . ' AND NOT EXISTS'
  333.                         . ' (SELECT 1 FROM pg_views'
  334.                         . '  WHERE viewname = c.relname)'
  335.                         . " AND c.relname !~ '^(pg_|sql_)'"
  336.                         . ' UNION'
  337.                         . ' SELECT c.relname AS "Name"'
  338.                         . ' FROM pg_class c'
  339.                         . " WHERE c.relkind = 'r'"
  340.                         . ' AND NOT EXISTS'
  341.                         . ' (SELECT 1 FROM pg_views'
  342.                         . '  WHERE viewname = c.relname)'
  343.                         . ' AND NOT EXISTS'
  344.                         . ' (SELECT 1 FROM pg_user'
  345.                         . '  WHERE usesysid = c.relowner)'
  346.                         . " AND c.relname !~ '^pg_'";
  347.         return $db->queryCol($query);
  348.     }
  349.  
  350.     // }}}
  351.     // {{{ listTableFields()
  352.  
  353.     /**
  354.      * list all fields in a tables in the current database
  355.      *
  356.      * @param string $table name of table that should be used in method
  357.      * @return mixed data array on success, a MDB2 error on failure
  358.      * @access public
  359.      */
  360.     function listTableFields($table)
  361.     {
  362.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  363.         $result $db->query("SELECT * FROM $table");
  364.         if (PEAR::isError($result)) {
  365.             return $result;
  366.         }
  367.         $columns $result->getColumnNames();
  368.         $result->free();
  369.         if (PEAR::isError($columns)) {
  370.             return $columns;
  371.         }
  372.         return array_flip($columns);
  373.     }
  374.  
  375.     // }}}
  376.     // {{{ listTableIndexes()
  377.  
  378.     /**
  379.      * list all indexes in a table
  380.      *
  381.      * @param string    $table      name of table that should be used in method
  382.      * @return mixed data array on success, a MDB2 error on failure
  383.      * @access public
  384.      */
  385.     function listTableIndexes($table)
  386.     {
  387.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  388.         return $db->queryCol("SELECT relname
  389.                                 FROM pg_class WHERE oid IN
  390.                                   (SELECT indexrelid FROM pg_index, pg_class
  391.                                    WHERE (pg_class.relname='$table')
  392.                                    AND (pg_class.oid=pg_index.indrelid))");
  393.     }
  394.  
  395.     // }}}
  396.     // {{{ createSequence()
  397.  
  398.     /**
  399.      * create sequence
  400.      *
  401.      * @param string $seq_name name of the sequence to be created
  402.      * @param string $start start value of the sequence; default is 1
  403.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  404.      * @access public
  405.      ***/
  406.     function createSequence($seq_name$start = 1)
  407.     {
  408.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  409.         $sequence_name $db->getSequenceName($seq_name);
  410.         return $db->query("CREATE SEQUENCE $sequence_name INCREMENT 1".
  411.             ($start < 1 ? " MINVALUE $start" : '')." START $start");
  412.     }
  413.  
  414.     // }}}
  415.     // {{{ dropSequence()
  416.  
  417.     /**
  418.      * drop existing sequence
  419.      *
  420.      * @param string $seq_name name of the sequence to be dropped
  421.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  422.      * @access public
  423.      ***/
  424.     function dropSequence($seq_name)
  425.     {
  426.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  427.         $sequence_name $db->getSequenceName($seq_name);
  428.         return $db->query("DROP SEQUENCE $sequence_name");
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ listSequences()
  433.  
  434.     /**
  435.      * list all sequences in the current database
  436.      *
  437.      * @return mixed data array on success, a MDB2 error on failure
  438.      * @access public
  439.      ***/
  440.     function listSequences()
  441.     {
  442.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  443.         $query "SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
  444.         $query.= "(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  445.         $table_names $db->queryCol($query);
  446.         if (PEAR::isError($table_names)) {
  447.             return $table_names;
  448.         }
  449.         $sequences = array();
  450.         for ($i = 0$j count($table_names)$i $j; ++$i{
  451.             if ($sqn $this->_isSequenceName($table_names[$i]))
  452.                 $sequences[$sqn;
  453.         }
  454.         return $sequences;
  455.     }
  456. }
  457. ?>

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