MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file pgsql.php

Documentation is available at pgsql.php

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

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