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.11 2005/03/06 17:37:38 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.         if ($check{
  190.             foreach ($changes as $change_name => $change{
  191.                 switch ($change_name{
  192.                 case 'added_fields':
  193.                     break;
  194.                 case 'removed_fields':
  195.                     return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  196.                     'alterTable: database server does not support dropping table columns');
  197.                 case 'name':
  198.                 case 'renamed_fields':
  199.                 case 'changed_fields':
  200.                 default:
  201.                     return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  202.                         'alterTable: change type "'.$change_name.'\" not yet supported');
  203.                 }
  204.             }
  205.             return MDB2_OK;
  206.         else {
  207.             if (isset($changes[$change 'name'])
  208.                 || isset($changes[$change 'renamed_fields'])
  209.                 || isset($changes[$change 'changed_fields'])
  210.             {
  211.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  212.                     'alterTable: change type "'.$change.'" not yet supported');
  213.             }
  214.             $query '';
  215.             if (isSet($changes['added_fields'])) {
  216.                 $fields $changes['added_fields'];
  217.                 foreach ($fields as $field{
  218.                     $result $db->query("ALTER TABLE $name ADD ".$field['declaration']);
  219.                     if (MDB2::isError($result)) {
  220.                         return $result;
  221.                     }
  222.                 }
  223.             }
  224.             if (isSet($changes['removed_fields'])) {
  225.                 $fields $changes['removed_fields'];
  226.                 foreach ($fields as $field_name => $field{
  227.                     $result $db->query("ALTER TABLE $name DROP ".$field_name);
  228.                     if (MDB2::isError($result)) {
  229.                         return $result;
  230.                     }
  231.                 }
  232.             }
  233.             return MDB2_OK;
  234.         }
  235.     }
  236.  
  237.     // }}}
  238.     // {{{ listDatabases()
  239.  
  240.     /**
  241.      * list all databases
  242.      *
  243.      * @return mixed data array on success, a MDB2 error on failure
  244.      * @access public
  245.      ***/
  246.     function listDatabases()
  247.     {
  248.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  249.         $result $db->standaloneQuery('SELECT datname FROM pg_database');
  250.         if (!MDB2::isResultCommon($result)) {
  251.             return $result;
  252.         }
  253.  
  254.         $col $result->fetchCol();
  255.         $result->free();
  256.         return $col;
  257.     }
  258.  
  259.     // }}}
  260.     // {{{ listUsers()
  261.  
  262.     /**
  263.      * list all users
  264.      *
  265.      * @return mixed data array on success, a MDB2 error on failure
  266.      * @access public
  267.      ***/
  268.     function listUsers()
  269.     {
  270.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  271.         $result $db->standaloneQuery('SELECT usename FROM pg_user');
  272.         if (!MDB2::isResultCommon($result)) {
  273.             return $result;
  274.         }
  275.  
  276.         $col $result->fetchCol();
  277.         $result->free();
  278.         return $col;
  279.     }
  280.  
  281.     // }}}
  282.     // {{{ listTables()
  283.  
  284.     /**
  285.      * list all tables in the current database
  286.      *
  287.      * @return mixed data array on success, a MDB2 error on failure
  288.      * @access public
  289.      ***/
  290.     function listTables()
  291.     {
  292.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  293.         // gratuitously stolen from PEAR DB _getSpecialQuery in pgsql.php
  294.         $query 'SELECT c.relname as "Name"
  295.             FROM pg_class c, pg_user u
  296.             WHERE c.relowner = u.usesysid AND c.relkind = \'r\'
  297.             AND not exists (select 1 from pg_views where viewname = c.relname)
  298.             AND c.relname !~ \'^pg_\'
  299.             AND c.relname !~ \'^pga_\'
  300.             UNION
  301.             SELECT c.relname as "Name"
  302.             FROM pg_class c
  303.             WHERE c.relkind = \'r\'
  304.             AND not exists (select 1 from pg_views where viewname = c.relname)
  305.             AND not exists (select 1 from pg_user where usesysid = c.relowner)
  306.             AND c.relname !~ \'^pg_\'
  307.             AND c.relname !~ \'^pga_\'';
  308.         return $db->queryCol($query);
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ listTableFields()
  313.  
  314.     /**
  315.      * list all fields in a tables in the current database
  316.      *
  317.      * @param string $table name of table that should be used in method
  318.      * @return mixed data array on success, a MDB2 error on failure
  319.      * @access public
  320.      */
  321.     function listTableFields($table)
  322.     {
  323.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  324.         $result $db->query("SELECT * FROM $table"nullfalse);
  325.         if (MDB2::isError($result)) {
  326.             return $result;
  327.         }
  328.         $columns $result->getColumnNames();
  329.         $result->free();
  330.         if (MDB2::isError($columns)) {
  331.             return $columns;
  332.         }
  333.         return array_flip($columns);
  334.     }
  335.  
  336.     // }}}
  337.     // {{{ listViews()
  338.  
  339.     /**
  340.      * list the views in the database
  341.      *
  342.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  343.      * @access public
  344.      ***/
  345.     function listViews()
  346.     {
  347.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  348.         // gratuitously stolen from PEAR DB _getSpecialQuery in pgsql.php
  349.         return $db->queryCol('SELECT viewname FROM pg_views');
  350.     }
  351.  
  352.     // }}}
  353.     // {{{ listTableIndexes()
  354.  
  355.     /**
  356.      * list all indexes in a table
  357.      *
  358.      * @param string    $table      name of table that should be used in method
  359.      * @return mixed data array on success, a MDB2 error on failure
  360.      * @access public
  361.      */
  362.     function listTableIndexes($table)
  363.     {
  364.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  365.         return $db->queryCol("SELECT relname
  366.                                 FROM pg_class WHERE oid IN
  367.                                   (SELECR indexrelid FROM pg_index, pg_class
  368.                                    WHERE (pg_class.relname='$table')
  369.                                    AND (pg_class.oid=pg_index.indrelid))");
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ createSequence()
  374.  
  375.     /**
  376.      * create sequence
  377.      *
  378.      * @param string $seq_name name of the sequence to be created
  379.      * @param string $start start value of the sequence; default is 1
  380.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  381.      * @access public
  382.      ***/
  383.     function createSequence($seq_name$start = 1)
  384.     {
  385.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  386.         $sequence_name $db->getSequenceName($seq_name);
  387.         return $db->query("CREATE SEQUENCE $sequence_name INCREMENT 1".
  388.             ($start < 1 ? " MINVALUE $start" : '')." START $start");
  389.     }
  390.  
  391.     // }}}
  392.     // {{{ dropSequence()
  393.  
  394.     /**
  395.      * drop existing sequence
  396.      *
  397.      * @param string $seq_name name of the sequence to be dropped
  398.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  399.      * @access public
  400.      ***/
  401.     function dropSequence($seq_name)
  402.     {
  403.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  404.         $sequence_name $db->getSequenceName($seq_name);
  405.         return $db->query("DROP SEQUENCE $sequence_name");
  406.     }
  407.  
  408.     // }}}
  409.     // {{{ listSequences()
  410.  
  411.     /**
  412.      * list all sequences in the current database
  413.      *
  414.      * @return mixed data array on success, a MDB2 error on failure
  415.      * @access public
  416.      ***/
  417.     function listSequences()
  418.     {
  419.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  420.         $query "SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
  421.         $query.= "(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  422.         $table_names $db->queryCol($query);
  423.         if (MDB2::isError($table_names)) {
  424.             return $table_names;
  425.         }
  426.         $sequences = array();
  427.         for ($i = 0$j count($table_names)$i $j; ++$i{
  428.             if ($sqn $this->_isSequenceName($table_names[$i]))
  429.                 $sequences[$sqn;
  430.         }
  431.         return $sequences;
  432.     }
  433. }
  434. ?>

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