home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Table / Manager / pgsql.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  17.4 KB  |  441 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Index, constraint and alter methods for DB_Table usage with
  7.  * PEAR::DB as backend.
  8.  * 
  9.  * The code in this class was adopted from the MDB2 PEAR package.
  10.  * 
  11.  * PHP versions 4 and 5
  12.  *
  13.  * LICENSE:
  14.  * 
  15.  * Copyright (c) 1997-2007, Paul Cooper <pgc@ucecom.com>
  16.  *                          Paul M. Jones <pmjones@php.net>
  17.  *                          David C. Morse <morse@php.net>
  18.  *                          Mark Wiesemann <wiesemann@php.net>
  19.  * All rights reserved.
  20.  *
  21.  * Redistribution and use in source and binary forms, with or without
  22.  * modification, are permitted provided that the following conditions
  23.  * are met:
  24.  *
  25.  *    * Redistributions of source code must retain the above copyright
  26.  *      notice, this list of conditions and the following disclaimer.
  27.  *    * Redistributions in binary form must reproduce the above copyright
  28.  *      notice, this list of conditions and the following disclaimer in the 
  29.  *      documentation and/or other materials provided with the distribution.
  30.  *    * The names of the authors may not be used to endorse or promote products 
  31.  *      derived from this software without specific prior written permission.
  32.  *
  33.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  40.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  41.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  42.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  *
  45.  * @category Database
  46.  * @package  DB_Table
  47.  * @author   Paul Cooper <pgc@ucecom.com>
  48.  * @author   Mark Wiesemann <wiesemann@php.net>
  49.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  50.  * @version  CVS: $Id: pgsql.php,v 1.5 2007/12/13 16:52:15 wiesemann Exp $
  51.  * @link     http://pear.php.net/package/DB_Table
  52.  */
  53.  
  54. /**
  55.  * require DB_Table class
  56.  */
  57. require_once 'DB/Table.php';
  58.  
  59. /**
  60.  * Index, constraint and alter methods for DB_Table usage with
  61.  * PEAR::DB as backend.
  62.  * 
  63.  * The code in this class was adopted from the MDB2 PEAR package.
  64.  * 
  65.  * @category Database
  66.  * @package  DB_Table
  67.  * @author   Paul Cooper <pgc@ucecom.com>
  68.  * @author   Mark Wiesemann <wiesemann@php.net>
  69.  * @version  Release: 1.5.5
  70.  * @link     http://pear.php.net/package/DB_Table
  71.  */
  72. class DB_Table_Manager_pgsql {
  73.  
  74.     /**
  75.     * 
  76.     * The PEAR DB object that connects to the database.
  77.     * 
  78.     * @access private
  79.     * 
  80.     * @var object
  81.     * 
  82.     */
  83.     
  84.     var $_db = null;
  85.  
  86.  
  87.     /**
  88.      * list all indexes in a table
  89.      *
  90.      * @param string    $table      name of table that should be used in method
  91.      * @return mixed data array on success, a PEAR error on failure
  92.      * @access public
  93.      */
  94.     function listTableIndexes($table)
  95.     {
  96.         $subquery = "SELECT indexrelid FROM pg_index, pg_class";
  97.         $subquery.= " WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
  98.         $query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
  99.         $indexes = $this->_db->getCol($query);
  100.         if (PEAR::isError($indexes)) {
  101.             return $indexes;
  102.         }
  103.  
  104.         $result = array();
  105.         foreach ($indexes as $index) {
  106.             $result[$index] = true;
  107.         }
  108.         $result = array_change_key_case($result, CASE_LOWER);
  109.  
  110.         return array_keys($result);
  111.     }
  112.  
  113.  
  114.     /**
  115.      * list all constraints in a table
  116.      *
  117.      * @param string    $table      name of table that should be used in method
  118.      * @return mixed data array on success, a PEAR error on failure
  119.      * @access public
  120.      */
  121.     function listTableConstraints($table)
  122.     {
  123.         $subquery = "SELECT indexrelid FROM pg_index, pg_class";
  124.         $subquery.= " WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
  125.         $query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
  126.         $constraints = $this->_db->getCol($query);
  127.         if (PEAR::isError($constraints)) {
  128.             return $constraints;
  129.         }
  130.  
  131.         $result = array();
  132.         foreach ($constraints as $constraint) {
  133.             $result[$constraint] = true;
  134.         }
  135.         $result = array_change_key_case($result, CASE_LOWER);
  136.  
  137.         return array_keys($result);
  138.     }
  139.  
  140.  
  141.     /**
  142.      * get the structure of an index into an array
  143.      *
  144.      * @param string    $table      name of table that should be used in method
  145.      * @param string    $index_name name of index that should be used in method
  146.      * @return mixed data array on success, a PEAR error on failure
  147.      * @access public
  148.      */
  149.     function getTableIndexDefinition($table, $index_name)
  150.     {
  151.         $query = "SELECT relname, indkey FROM pg_index, pg_class
  152.             WHERE pg_class.relname = ".$this->_db->quoteSmart($index_name)." AND pg_class.oid = pg_index.indexrelid
  153.                AND indisunique != 't' AND indisprimary != 't'";
  154.         $row = $this->_db->getRow($query, null, DB_FETCHMODE_ASSOC);
  155.         if (PEAR::isError($row)) {
  156.             return $row;
  157.         }
  158.  
  159.         $columns = $this->_listTableFields($table);
  160.  
  161.         $definition = array();
  162.  
  163.         $index_column_numbers = explode(' ', $row['indkey']);
  164.  
  165.         foreach ($index_column_numbers as $number) {
  166.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  167.         }
  168.         return $definition;
  169.     }
  170.  
  171.  
  172.     /**
  173.      * get the structure of a constraint into an array
  174.      *
  175.      * @param string    $table      name of table that should be used in method
  176.      * @param string    $index_name name of index that should be used in method
  177.      * @return mixed data array on success, a PEAR error on failure
  178.      * @access public
  179.      */
  180.     function getTableConstraintDefinition($table, $index_name)
  181.     {
  182.         $query = "SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class
  183.             WHERE pg_class.relname = ".$this->_db->quoteSmart($index_name)." AND pg_class.oid = pg_index.indexrelid
  184.               AND (indisunique = 't' OR indisprimary = 't')";
  185.         $row = $this->_db->getRow($query, null, DB_FETCHMODE_ASSOC);
  186.         if (PEAR::isError($row)) {
  187.             return $row;
  188.         }
  189.  
  190.         $columns = $this->_listTableFields($table);
  191.  
  192.         $definition = array();
  193.         if ($row['indisprimary'] == 't') {
  194.             $definition['primary'] = true;
  195.         } elseif ($row['indisunique'] == 't') {
  196.             $definition['unique'] = true;
  197.         }
  198.  
  199.         $index_column_numbers = explode(' ', $row['indkey']);
  200.  
  201.         foreach ($index_column_numbers as $number) {
  202.             $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
  203.         }
  204.         return $definition;
  205.     }
  206.  
  207.     /**
  208.      * list all fields in a tables in the current database
  209.      *
  210.      * @param string $table name of table that should be used in method
  211.      * @return mixed data array on success, a PEAR error on failure
  212.      * @access private
  213.      */
  214.     function _listTableFields($table)
  215.     {
  216.         $table = $this->_db->quoteIdentifier($table);
  217.         $result2 = $this->_db->query("SELECT * FROM $table");
  218.         if (PEAR::isError($result2)) {
  219.             return $result2;
  220.         }
  221.         $columns = array();
  222.         $numcols = $result2->numCols();
  223.         for ($column = 0; $column < $numcols; $column++) {
  224.             $column_name = @pg_field_name($result2->result, $column);
  225.             $columns[$column_name] = $column;
  226.         }
  227.         $result2->free();
  228.         return array_flip($columns);
  229.     }
  230.  
  231.     
  232.     /**
  233.      * drop existing index
  234.      *
  235.      * @param string    $table         name of table that should be used in method
  236.      * @param string    $name         name of the index to be dropped
  237.      * @return mixed DB_OK on success, a PEAR error on failure
  238.      * @access public
  239.      */
  240.     function dropIndex($table, $name)
  241.     {
  242.         $name = $this->_db->quoteIdentifier($name);
  243.         return $this->_db->query("DROP INDEX $name");
  244.     }
  245.  
  246.  
  247.     /**
  248.      * drop existing constraint
  249.      *
  250.      * @param string    $table         name of table that should be used in method
  251.      * @param string    $name         name of the constraint to be dropped
  252.      * @return mixed DB_OK on success, a PEAR error on failure
  253.      * @access public
  254.      */
  255.     function dropConstraint($table, $name)
  256.     {
  257.         $table = $this->_db->quoteIdentifier($table);
  258.         $name = $this->_db->quoteIdentifier($name);
  259.         return $this->_db->query("ALTER TABLE $table DROP CONSTRAINT $name");
  260.     }
  261.  
  262.  
  263.     /**
  264.      * alter an existing table
  265.      *
  266.      * @param string $name         name of the table that is intended to be changed.
  267.      * @param array $changes     associative array that contains the details of each type
  268.      *                             of change that is intended to be performed. The types of
  269.      *                             changes that are currently supported are defined as follows:
  270.      *
  271.      *                             name
  272.      *
  273.      *                                New name for the table.
  274.      *
  275.      *                            add
  276.      *
  277.      *                                Associative array with the names of fields to be added as
  278.      *                                 indexes of the array. The value of each entry of the array
  279.      *                                 should be set to another associative array with the properties
  280.      *                                 of the fields to be added. The properties of the fields should
  281.      *                                 be the same as defined by the Metabase parser.
  282.      *
  283.      *
  284.      *                            remove
  285.      *
  286.      *                                Associative array with the names of fields to be removed as indexes
  287.      *                                 of the array. Currently the values assigned to each entry are ignored.
  288.      *                                 An empty array should be used for future compatibility.
  289.      *
  290.      *                            rename
  291.      *
  292.      *                                Associative array with the names of fields to be renamed as indexes
  293.      *                                 of the array. The value of each entry of the array should be set to
  294.      *                                 another associative array with the entry named name with the new
  295.      *                                 field name and the entry named Declaration that is expected to contain
  296.      *                                 the portion of the field declaration already in DBMS specific SQL code
  297.      *                                 as it is used in the CREATE TABLE statement.
  298.      *
  299.      *                            change
  300.      *
  301.      *                                Associative array with the names of the fields to be changed as indexes
  302.      *                                 of the array. Keep in mind that if it is intended to change either the
  303.      *                                 name of a field and any other properties, the change array entries
  304.      *                                 should have the new names of the fields as array indexes.
  305.      *
  306.      *                                The value of each entry of the array should be set to another associative
  307.      *                                 array with the properties of the fields to that are meant to be changed as
  308.      *                                 array entries. These entries should be assigned to the new values of the
  309.      *                                 respective properties. The properties of the fields should be the same
  310.      *                                 as defined by the Metabase parser.
  311.      *
  312.      *                            Example
  313.      *                                array(
  314.      *                                    'name' => 'userlist',
  315.      *                                    'add' => array(
  316.      *                                        'quota' => array(
  317.      *                                            'type' => 'integer',
  318.      *                                            'unsigned' => 1
  319.      *                                        )
  320.      *                                    ),
  321.      *                                    'remove' => array(
  322.      *                                        'file_limit' => array(),
  323.      *                                        'time_limit' => array()
  324.      *                                    ),
  325.      *                                    'change' => array(
  326.      *                                        'name' => array(
  327.      *                                            'length' => '20',
  328.      *                                            'definition' => array(
  329.      *                                                'type' => 'text',
  330.      *                                                'length' => 20,
  331.      *                                            ),
  332.      *                                        )
  333.      *                                    ),
  334.      *                                    'rename' => array(
  335.      *                                        'sex' => array(
  336.      *                                            'name' => 'gender',
  337.      *                                            'definition' => array(
  338.      *                                                'type' => 'text',
  339.      *                                                'length' => 1,
  340.      *                                                'default' => 'M',
  341.      *                                            ),
  342.      *                                        )
  343.      *                                    )
  344.      *                                )
  345.      *
  346.      * @param boolean $check     (ignored in DB_Table)
  347.      * @access public
  348.      *
  349.      * @return mixed DB_OK on success, a PEAR error on failure
  350.      */
  351.     function alterTable($name, $changes, $check)
  352.     {
  353.         foreach ($changes as $change_name => $change) {
  354.             switch ($change_name) {
  355.             case 'add':
  356.             case 'remove':
  357.             case 'change':
  358.             case 'name':
  359.             case 'rename':
  360.                 break;
  361.             default:
  362.                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
  363.             }
  364.         }
  365.  
  366.         if (array_key_exists('add', $changes)) {
  367.             foreach ($changes['add'] as $field_name => $field) {
  368.                 $query = 'ADD ' . $field_name . ' ' . $field;
  369.                 $result = $this->_db->query("ALTER TABLE $name $query");
  370.                 if (PEAR::isError($result)) {
  371.                     return $result;
  372.                 }
  373.             }
  374.         }
  375.  
  376.         if (array_key_exists('remove', $changes)) {
  377.             foreach ($changes['remove'] as $field_name => $field) {
  378.                 $field_name = $this->_db->quoteIdentifier($field_name);
  379.                 $query = 'DROP ' . $field_name;
  380.                 $result = $this->_db->query("ALTER TABLE $name $query");
  381.                 if (PEAR::isError($result)) {
  382.                     return $result;
  383.                 }
  384.             }
  385.         }
  386.  
  387.         if (array_key_exists('change', $changes)) {
  388.             foreach ($changes['change'] as $field_name => $field) {
  389.                 $field_name = $this->_db->quoteIdentifier($field_name);
  390.                 if (array_key_exists('type', $field)) {
  391.                     $query = "ALTER $field_name TYPE " . $field['definition'];
  392.                     $result = $this->_db->query("ALTER TABLE $name $query");
  393.                     if (PEAR::isError($result)) {
  394.                         return $result;
  395.                     }
  396.                 }
  397. /* default / notnull changes not (yet) supported in DB_Table                
  398.                 if (array_key_exists('default', $field)) {
  399.                     $query = "ALTER $field_name SET DEFAULT ".$db->quote($field['definition']['default'], $field['definition']['type']);
  400.                     $result = $db->exec("ALTER TABLE $name $query");
  401.                     if (PEAR::isError($result)) {
  402.                         return $result;
  403.                     }
  404.                 }
  405.                 if (array_key_exists('notnull', $field)) {
  406.                     $query.= "ALTER $field_name ".($field['definition']['notnull'] ? "SET" : "DROP").' NOT NULL';
  407.                     $result = $db->exec("ALTER TABLE $name $query");
  408.                     if (PEAR::isError($result)) {
  409.                         return $result;
  410.                     }
  411.                 }
  412. */
  413.             }
  414.         }
  415.  
  416.         if (array_key_exists('rename', $changes)) {
  417.             foreach ($changes['rename'] as $field_name => $field) {
  418.                 $field_name = $this->_db->quoteIdentifier($field_name);
  419.                 $result = $this->_db->query("ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->_db->quoteIdentifier($field['name']));
  420.                 if (PEAR::isError($result)) {
  421.                     return $result;
  422.                 }
  423.             }
  424.         }
  425.  
  426.         $name = $this->_db->quoteIdentifier($name);
  427.         if (array_key_exists('name', $changes)) {
  428.             $change_name = $this->_db->quoteIdentifier($changes['name']);
  429.             $result = $this->_db->query("ALTER TABLE $name RENAME TO ".$change_name);
  430.             if (PEAR::isError($result)) {
  431.                 return $result;
  432.             }
  433.         }
  434.  
  435.         return DB_OK;
  436.     }
  437.  
  438. }
  439.  
  440. ?>
  441.