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.28 2005/10/07 13:01:22 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 =$this->getDBInstance();
  70.         if (PEAR::isError($db)) {
  71.             return $db;
  72.         }
  73.  
  74.         return $db->standaloneQuery("CREATE DATABASE $name");
  75.     }
  76.  
  77.     // }}}
  78.     // {{{ dropDatabase()
  79.  
  80.     /**
  81.      * drop an existing database
  82.      *
  83.      * @param string $name name of the database that should be dropped
  84.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  85.      * @access public
  86.      ***/
  87.     function dropDatabase($name)
  88.     {
  89.         $db =$this->getDBInstance();
  90.         if (PEAR::isError($db)) {
  91.             return $db;
  92.         }
  93.  
  94.         return $db->standaloneQuery("DROP DATABASE $name");
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ alterTable()
  99.  
  100.     /**
  101.      * alter an existing table
  102.      *
  103.      * @param string $name name of the table that is intended to be changed.
  104.      * @param array $changes associative array that contains the details of each type
  105.      *                               of change that is intended to be performed. The types of
  106.      *                               changes that are currently supported are defined as follows:
  107.      *
  108.      *                               name
  109.      *
  110.      *                                  New name for the table.
  111.      *
  112.      *                              add
  113.      *
  114.      *                                  Associative array with the names of fields to be added as
  115.      *                                   indexes of the array. The value of each entry of the array
  116.      *                                   should be set to another associative array with the properties
  117.      *                                   of the fields to be added. The properties of the fields should
  118.      *                                   be the same as defined by the Metabase parser.
  119.      *
  120.      *
  121.      *                              remove
  122.      *
  123.      *                                  Associative array with the names of fields to be removed as indexes
  124.      *                                   of the array. Currently the values assigned to each entry are ignored.
  125.      *                                   An empty array should be used for future compatibility.
  126.      *
  127.      *                              rename
  128.      *
  129.      *                                  Associative array with the names of fields to be renamed as indexes
  130.      *                                   of the array. The value of each entry of the array should be set to
  131.      *                                   another associative array with the entry named name with the new
  132.      *                                   field name and the entry named Declaration that is expected to contain
  133.      *                                   the portion of the field declaration already in DBMS specific SQL code
  134.      *                                   as it is used in the CREATE TABLE statement.
  135.      *
  136.      *                              change
  137.      *
  138.      *                                  Associative array with the names of the fields to be changed as indexes
  139.      *                                   of the array. Keep in mind that if it is intended to change either the
  140.      *                                   name of a field and any other properties, the change array entries
  141.      *                                   should have the new names of the fields as array indexes.
  142.      *
  143.      *                                  The value of each entry of the array should be set to another associative
  144.      *                                   array with the properties of the fields to that are meant to be changed as
  145.      *                                   array entries. These entries should be assigned to the new values of the
  146.      *                                   respective properties. The properties of the fields should be the same
  147.      *                                   as defined by the Metabase parser.
  148.      *
  149.      *                              Example
  150.      *                                  array(
  151.      *                                      'name' => 'userlist',
  152.      *                                      'add' => array(
  153.      *                                          'quota' => array(
  154.      *                                              'type' => 'integer',
  155.      *                                              'unsigned' => 1
  156.      *                                          )
  157.      *                                      ),
  158.      *                                      'remove' => array(
  159.      *                                          'file_limit' => array(),
  160.      *                                          'time_limit' => array()
  161.      *                                          ),
  162.      *                                      'change' => array(
  163.      *                                          'gender' => array(
  164.      *                                              'default' => 'M',
  165.      *                                          )
  166.      *                                      ),
  167.      *                                      'rename' => array(
  168.      *                                          'sex' => array(
  169.      *                                              'name' => 'gender',
  170.      *                                          )
  171.      *                                      )
  172.      *                                  )
  173.      * @param boolean $check indicates whether the function should just check if the DBMS driver
  174.      *                               can perform the requested table alterations if the value is true or
  175.      *                               actually perform them otherwise.
  176.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  177.      * @access public
  178.      ***/
  179.     function alterTable($name$changes$check)
  180.     {
  181.         $db =$this->getDBInstance();
  182.         if (PEAR::isError($db)) {
  183.             return $db;
  184.         }
  185.  
  186.         foreach ($changes as $change_name => $change{
  187.             switch ($change_name{
  188.             case 'add':
  189.             case 'remove':
  190.             case 'change':
  191.             case 'name':
  192.                 break;
  193.             case 'rename':
  194.             default:
  195.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  196.                     'alterTable: change type "'.$change_name.'\" not yet supported');
  197.             }
  198.         }
  199.  
  200.         if ($check{
  201.             return MDB2_OK;
  202.         }
  203.  
  204.         $query (array_key_exists('name'$changes'RENAME TO '.$changes['name''');
  205.  
  206.         if (array_key_exists('add'$changes)) {
  207.             foreach ($changes['add'as $field_name => $field{
  208.                 $type_declaration $db->getDeclaration($field['type']$field_name$field);
  209.                 if (PEAR::isError($type_declaration)) {
  210.                     return $err;
  211.                 }
  212.                 if ($query{
  213.                     $query.= ', ';
  214.                 }
  215.                 $query.= 'ADD ' $type_declaration;
  216.             }
  217.         }
  218.  
  219.         if (array_key_exists('remove'$changes)) {
  220.             foreach ($changes['remove'as $field_name => $field{
  221.                 if ($query{
  222.                     $query.= ', ';
  223.                 }
  224.                 $query.= 'DROP ' $field_name;
  225.             }
  226.         }
  227.  
  228.         if (array_key_exists('change'$changes)) {
  229.             // missing support to change DEFAULT and NULLability
  230.             foreach ($changes['change'as $field_name => $field{
  231.                 if ($query{
  232.                     $query.= ', ';
  233.                 }
  234.                 $db->loadModule('Datatype');
  235.                 $query.= "ALTER $field_name TYPE ".$db->datatype->getTypeDeclaration($field);
  236.             }
  237.         }
  238.  
  239.         if (!$query{
  240.             return MDB2_OK;
  241.         }
  242.  
  243.         return $db->query("ALTER TABLE $name $query");
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ listDatabases()
  248.  
  249.     /**
  250.      * list all databases
  251.      *
  252.      * @return mixed data array on success, a MDB2 error on failure
  253.      * @access public
  254.      ***/
  255.     function listDatabases()
  256.     {
  257.         $db =$this->getDBInstance();
  258.         if (PEAR::isError($db)) {
  259.             return $db;
  260.         }
  261.  
  262.         $result $db->standaloneQuery('SELECT datname FROM pg_database');
  263.         if (!MDB2::isResultCommon($result)) {
  264.             return $result;
  265.         }
  266.  
  267.         $col $result->fetchCol();
  268.         $result->free();
  269.         return $col;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ listUsers()
  274.  
  275.     /**
  276.      * list all users
  277.      *
  278.      * @return mixed data array on success, a MDB2 error on failure
  279.      * @access public
  280.      ***/
  281.     function listUsers()
  282.     {
  283.         $db =$this->getDBInstance();
  284.         if (PEAR::isError($db)) {
  285.             return $db;
  286.         }
  287.  
  288.         $result $db->standaloneQuery('SELECT usename FROM pg_user');
  289.         if (!MDB2::isResultCommon($result)) {
  290.             return $result;
  291.         }
  292.  
  293.         $col $result->fetchCol();
  294.         $result->free();
  295.         return $col;
  296.     }
  297.  
  298.     // }}}
  299.     // {{{ listViews()
  300.  
  301.     /**
  302.      * list the views in the database
  303.      *
  304.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  305.      * @access public
  306.      ***/
  307.     function listViews()
  308.     {
  309.         $db =$this->getDBInstance();
  310.         if (PEAR::isError($db)) {
  311.             return $db;
  312.         }
  313.  
  314.         $query 'SELECT viewname FROM pg_views';
  315.         return $db->queryCol($query);
  316.     }
  317.  
  318.     // }}}
  319.     // {{{ listFunctions()
  320.  
  321.     /**
  322.      * list all functions in the current database
  323.      *
  324.      * @return mixed data array on success, a MDB2 error on failure
  325.      * @access public
  326.      */
  327.     function listFunctions()
  328.     {
  329.         $db =$this->getDBInstance();
  330.         if (PEAR::isError($db)) {
  331.             return $db;
  332.         }
  333.  
  334.         $query "
  335.             SELECT
  336.                 proname
  337.             FROM
  338.                 pg_proc pr,
  339.                 pg_type tp
  340.             WHERE
  341.                 tp.oid = pr.prorettype
  342.                 AND pr.proisagg = FALSE
  343.                 AND tp.typname <> 'trigger'
  344.                 AND pr.pronamespace IN
  345.                     (SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  346.         return $db->queryCol($query);
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ listTables()
  351.  
  352.     /**
  353.      * list all tables in the current database
  354.      *
  355.      * @return mixed data array on success, a MDB2 error on failure
  356.      * @access public
  357.      ***/
  358.     function listTables()
  359.     {
  360.         $db =$this->getDBInstance();
  361.         if (PEAR::isError($db)) {
  362.             return $db;
  363.         }
  364.  
  365.         // gratuitously stolen from PEAR DB _getSpecialQuery in pgsql.php
  366.         $query 'SELECT c.relname AS "Name"'
  367.             . ' FROM pg_class c, pg_user u'
  368.             . ' WHERE c.relowner = u.usesysid'
  369.             . " AND c.relkind = 'r'"
  370.             . ' AND NOT EXISTS'
  371.             . ' (SELECT 1 FROM pg_views'
  372.             . '  WHERE viewname = c.relname)'
  373.             . " AND c.relname !~ '^(pg_|sql_)'"
  374.             . ' UNION'
  375.             . ' SELECT c.relname AS "Name"'
  376.             . ' FROM pg_class c'
  377.             . " WHERE c.relkind = 'r'"
  378.             . ' AND NOT EXISTS'
  379.             . ' (SELECT 1 FROM pg_views'
  380.             . '  WHERE viewname = c.relname)'
  381.             . ' AND NOT EXISTS'
  382.             . ' (SELECT 1 FROM pg_user'
  383.             . '  WHERE usesysid = c.relowner)'
  384.             . " AND c.relname !~ '^pg_'";
  385.         return $db->queryCol($query);
  386.     }
  387.  
  388.     // }}}
  389.     // {{{ listTableFields()
  390.  
  391.     /**
  392.      * list all fields in a tables in the current database
  393.      *
  394.      * @param string $table name of table that should be used in method
  395.      * @return mixed data array on success, a MDB2 error on failure
  396.      * @access public
  397.      */
  398.     function listTableFields($table)
  399.     {
  400.         $db =$this->getDBInstance();
  401.         if (PEAR::isError($db)) {
  402.             return $db;
  403.         }
  404.  
  405.         $result $db->query("SELECT * FROM $table");
  406.         if (PEAR::isError($result)) {
  407.             return $result;
  408.         }
  409.         $columns $result->getColumnNames();
  410.         $result->free();
  411.         if (PEAR::isError($columns)) {
  412.             return $columns;
  413.         }
  414.         return array_flip($columns);
  415.     }
  416.  
  417.     // }}}
  418.     // {{{ createIndex()
  419.  
  420.     /**
  421.      * get the stucture of a field into an array
  422.      *
  423.      * @param string    $table         name of the table on which the index is to be created
  424.      * @param string    $name         name of the index to be created
  425.      * @param array     $definition        associative array that defines properties of the index to be created.
  426.      *                                  Currently, only one property named FIELDS is supported. This property
  427.      *                                  is also an associative with the names of the index fields as array
  428.      *                                  indexes. Each entry of this array is set to another type of associative
  429.      *                                  array that specifies properties of the index that are specific to
  430.      *                                  each field.
  431.      *
  432.      *                                 Currently, only the sorting property is supported. It should be used
  433.      *                                  to define the sorting direction of the index. It may be set to either
  434.      *                                  ascending or descending.
  435.      *
  436.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  437.      *                                  drivers of those that do not support it ignore this property. Use the
  438.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  439.      *
  440.      *                                  Example
  441.      *                                     array(
  442.      *                                         'fields' => array(
  443.      *                                             'user_name' => array(
  444.      *                                                 'sorting' => 'ascending'
  445.      *                                             ),
  446.      *                                             'last_login' => array()
  447.      *                                         )
  448.      *                                     )
  449.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  450.      * @access public
  451.      */
  452.     function createIndex($table$name$definition)
  453.     {
  454.         $db =$this->getDBInstance();
  455.         if (PEAR::isError($db)) {
  456.             return $db;
  457.         }
  458.  
  459.         if (array_key_exists('primary'$definition&& $definition['primary']{
  460.             $query = "ALTER TABLE $table ADD CONSTRAINT $name PRIMARY KEY (";
  461.         else {
  462.             $query 'CREATE';
  463.             if (array_key_exists('unique'$definition&& $definition['unique']{
  464.                 $query.= ' UNIQUE';
  465.             }
  466.             $query.= " INDEX $name ON $table (";
  467.         }
  468.         $query.= implode(', 'array_keys($definition['fields']));
  469.         $query.= ')';
  470.  
  471.         return $db->query($query);
  472.     }
  473.  
  474.     // }}}
  475.     // {{{ listTableIndexes()
  476.  
  477.     /**
  478.      * list all indexes in a table
  479.      *
  480.      * @param string    $table      name of table that should be used in method
  481.      * @return mixed data array on success, a MDB2 error on failure
  482.      * @access public
  483.      */
  484.     function listTableIndexes($table)
  485.     {
  486.         $db =$this->getDBInstance();
  487.         if (PEAR::isError($db)) {
  488.             return $db;
  489.         }
  490.  
  491.         $subquery "SELECT indexrelid FROM pg_index, pg_class";
  492.         $subquery.= " WHERE (pg_class.relname='$table') AND (pg_class.oid=pg_index.indrelid)";
  493.         return $db->queryCol("SELECT relname FROM pg_class WHERE oid IN ($subquery)");
  494.     }
  495.  
  496.     // }}}
  497.     // {{{ createSequence()
  498.  
  499.     /**
  500.      * create sequence
  501.      *
  502.      * @param string $seq_name name of the sequence to be created
  503.      * @param string $start start value of the sequence; default is 1
  504.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  505.      * @access public
  506.      ***/
  507.     function createSequence($seq_name$start = 1)
  508.     {
  509.         $db =$this->getDBInstance();
  510.         if (PEAR::isError($db)) {
  511.             return $db;
  512.         }
  513.  
  514.         $sequence_name $db->getSequenceName($seq_name);
  515.         return $db->query("CREATE SEQUENCE $sequence_name INCREMENT 1".
  516.             ($start < 1 ? " MINVALUE $start" : '')." START $start");
  517.     }
  518.  
  519.     // }}}
  520.     // {{{ dropSequence()
  521.  
  522.     /**
  523.      * drop existing sequence
  524.      *
  525.      * @param string $seq_name name of the sequence to be dropped
  526.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  527.      * @access public
  528.      ***/
  529.     function dropSequence($seq_name)
  530.     {
  531.         $db =$this->getDBInstance();
  532.         if (PEAR::isError($db)) {
  533.             return $db;
  534.         }
  535.  
  536.         $sequence_name $db->getSequenceName($seq_name);
  537.         return $db->query("DROP SEQUENCE $sequence_name");
  538.     }
  539.  
  540.     // }}}
  541.     // {{{ listSequences()
  542.  
  543.     /**
  544.      * list all sequences in the current database
  545.      *
  546.      * @return mixed data array on success, a MDB2 error on failure
  547.      * @access public
  548.      ***/
  549.     function listSequences()
  550.     {
  551.         $db =$this->getDBInstance();
  552.         if (PEAR::isError($db)) {
  553.             return $db;
  554.         }
  555.  
  556.         $query "SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
  557.         $query.= "(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  558.         $table_names $db->queryCol($query);
  559.         if (PEAR::isError($table_names)) {
  560.             return $table_names;
  561.         }
  562.         $sequences = array();
  563.         for ($i = 0$j count($table_names)$i $j; ++$i{
  564.             if ($sqn $this->_isSequenceName($table_names[$i]))
  565.                 $sequences[$sqn;
  566.         }
  567.         return $sequences;
  568.     }
  569. }
  570. ?>

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