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 / ibase.php next >
Encoding:
PHP Script  |  2008-07-02  |  17.2 KB  |  434 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, Lorenzo Alberton <l.alberton@quipo.it>
  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   Lorenzo Alberton <l.alberton@quipo.it>
  48.  * @author   Mark Wiesemann <wiesemann@php.net>
  49.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  50.  * @version  CVS: $Id: ibase.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   Lorenzo Alberton <l.alberton@quipo.it>
  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_ibase {
  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.         $table = strtoupper($table);
  97.         $query = "SELECT RDB\$INDEX_NAME
  98.                     FROM RDB\$INDICES
  99.                    WHERE UPPER(RDB\$RELATION_NAME)='$table'
  100.                      AND RDB\$UNIQUE_FLAG IS NULL
  101.                      AND RDB\$FOREIGN_KEY IS NULL";
  102.         $indexes = $this->_db->getCol($query);
  103.         if (PEAR::isError($indexes)) {
  104.             return $indexes;
  105.         }
  106.  
  107.         $result = array();
  108.         foreach ($indexes as $index) {
  109.             $result[trim($index)] = true;
  110.         }
  111.         $result = array_change_key_case($result, CASE_LOWER);
  112.  
  113.         return array_keys($result);
  114.     }
  115.  
  116.  
  117.     /**
  118.      * list all constraints in a table
  119.      *
  120.      * @param string    $table      name of table that should be used in method
  121.      * @return mixed data array on success, a PEAR error on failure
  122.      * @access public
  123.      */
  124.     function listTableConstraints($table)
  125.     {
  126.         $table = strtoupper($table);
  127.         $query = "SELECT RDB\$INDEX_NAME
  128.                     FROM RDB\$INDICES
  129.                    WHERE UPPER(RDB\$RELATION_NAME)='$table'
  130.                      AND (
  131.                            RDB\$UNIQUE_FLAG IS NOT NULL
  132.                         OR RDB\$FOREIGN_KEY IS NOT NULL
  133.                      )";
  134.         $constraints = $this->_db->getCol($query);
  135.         if (PEAR::isError($constraints)) {
  136.             return $constraints;
  137.         }
  138.  
  139.         $result = array();
  140.         foreach ($constraints as $constraint) {
  141.             $result[trim($constraint)] = true;
  142.         }
  143.         $result = array_change_key_case($result, CASE_LOWER);
  144.  
  145.         return array_keys($result);
  146.     }
  147.  
  148.     /**
  149.      * get the structure of an index into an array
  150.      *
  151.      * @param string    $table      name of table that should be used in method
  152.      * @param string    $index_name name of index that should be used in method
  153.      * @return mixed data array on success, a PEAR error on failure
  154.      * @access public
  155.      */
  156.     function getTableIndexDefinition($table, $index_name)
  157.     {
  158.         $table = strtoupper($table);
  159.         $index_name = strtoupper($index_name);
  160.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  161.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  162.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  163.                          RDB\$INDICES.RDB\$DESCRIPTION AS description
  164.                     FROM RDB\$INDEX_SEGMENTS
  165.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  166.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  167.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)='$table'
  168.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)='$index_name'
  169.                      AND RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE IS NULL
  170.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  171.         $result = $this->_db->query($query);
  172.         if (PEAR::isError($result)) {
  173.             return $result;
  174.         }
  175.  
  176.         $index = $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  177.         $fields = array();
  178.         do {
  179.             $row = array_change_key_case($row, CASE_LOWER);
  180.             $fields[] = $row['field_name'];
  181.         } while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC)));
  182.         $result->free();
  183.  
  184.         $fields = array_map('strtolower', $fields);
  185.  
  186.         $definition = array();
  187.         $index = array_change_key_case($index, CASE_LOWER);
  188.         foreach ($fields as $field) {
  189.             $definition['fields'][$field] = array();
  190.         }
  191.         return $definition;
  192.     }
  193.  
  194.  
  195.     /**
  196.      * get the structure of a constraint into an array
  197.      *
  198.      * @param string    $table      name of table that should be used in method
  199.      * @param string    $index_name name of index that should be used in method
  200.      * @return mixed data array on success, a PEAR error on failure
  201.      * @access public
  202.      */
  203.     function getTableConstraintDefinition($table, $index_name)
  204.     {
  205.         $table = strtoupper($table);
  206.         $index_name = strtoupper($index_name);
  207.         $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name,
  208.                          RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag,
  209.                          RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key,
  210.                          RDB\$INDICES.RDB\$DESCRIPTION AS description,
  211.                          RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE AS constraint_type
  212.                     FROM RDB\$INDEX_SEGMENTS
  213.                LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  214.                LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME
  215.                    WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)='$table'
  216.                      AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)='$index_name'
  217.                 ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;";
  218.         $result = $this->_db->query($query);
  219.         if (PEAR::isError($result)) {
  220.             return $result;
  221.         }
  222.  
  223.         $index = $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  224.         $fields = array();
  225.         do {
  226.             $row = array_change_key_case($row, CASE_LOWER);
  227.             $fields[] = $row['field_name'];
  228.         } while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC)));
  229.         $result->free();
  230.  
  231.         $fields = array_map('strtolower', $fields);
  232.  
  233.         $definition = array();
  234.         $index = array_change_key_case($index, CASE_LOWER);
  235.         if ($index['constraint_type'] == 'PRIMARY KEY') {
  236.             $definition['primary'] = true;
  237.         }
  238.         if ($index['unique_flag']) {
  239.             $definition['unique'] = true;
  240.         }
  241.         foreach ($fields as $field) {
  242.             $definition['fields'][$field] = array();
  243.         }
  244.         return $definition;
  245.     }
  246.  
  247.     
  248.     /**
  249.      * drop existing index
  250.      *
  251.      * @param string    $table         name of table that should be used in method
  252.      * @param string    $name         name of the index to be dropped
  253.      * @return mixed DB_OK on success, a PEAR error on failure
  254.      * @access public
  255.      */
  256.     function dropIndex($table, $name)
  257.     {
  258.         $name = $this->_db->quoteIdentifier($name);
  259.         return $this->_db->query("DROP INDEX $name");
  260.     }
  261.  
  262.  
  263.     /**
  264.      * drop existing constraint
  265.      *
  266.      * @param string    $table         name of table that should be used in method
  267.      * @param string    $name         name of the constraint to be dropped
  268.      * @return mixed DB_OK on success, a PEAR error on failure
  269.      * @access public
  270.      */
  271.     function dropConstraint($table, $name)
  272.     {
  273.         $table = $this->_db->quoteIdentifier($table);
  274.         $name = $this->_db->quoteIdentifier($name);
  275.         return $this->_db->query("ALTER TABLE $table DROP CONSTRAINT $name");
  276.     }
  277.  
  278.  
  279.     /**
  280.      * alter an existing table
  281.      *
  282.      * @param string $name         name of the table that is intended to be changed.
  283.      * @param array $changes     associative array that contains the details of each type
  284.      *                             of change that is intended to be performed. The types of
  285.      *                             changes that are currently supported are defined as follows:
  286.      *
  287.      *                             name
  288.      *
  289.      *                                New name for the table.
  290.      *
  291.      *                            add
  292.      *
  293.      *                                Associative array with the names of fields to be added as
  294.      *                                 indexes of the array. The value of each entry of the array
  295.      *                                 should be set to another associative array with the properties
  296.      *                                 of the fields to be added. The properties of the fields should
  297.      *                                 be the same as defined by the Metabase parser.
  298.      *
  299.      *
  300.      *                            remove
  301.      *
  302.      *                                Associative array with the names of fields to be removed as indexes
  303.      *                                 of the array. Currently the values assigned to each entry are ignored.
  304.      *                                 An empty array should be used for future compatibility.
  305.      *
  306.      *                            rename
  307.      *
  308.      *                                Associative array with the names of fields to be renamed as indexes
  309.      *                                 of the array. The value of each entry of the array should be set to
  310.      *                                 another associative array with the entry named name with the new
  311.      *                                 field name and the entry named Declaration that is expected to contain
  312.      *                                 the portion of the field declaration already in DBMS specific SQL code
  313.      *                                 as it is used in the CREATE TABLE statement.
  314.      *
  315.      *                            change
  316.      *
  317.      *                                Associative array with the names of the fields to be changed as indexes
  318.      *                                 of the array. Keep in mind that if it is intended to change either the
  319.      *                                 name of a field and any other properties, the change array entries
  320.      *                                 should have the new names of the fields as array indexes.
  321.      *
  322.      *                                The value of each entry of the array should be set to another associative
  323.      *                                 array with the properties of the fields to that are meant to be changed as
  324.      *                                 array entries. These entries should be assigned to the new values of the
  325.      *                                 respective properties. The properties of the fields should be the same
  326.      *                                 as defined by the Metabase parser.
  327.      *
  328.      *                            Example
  329.      *                                array(
  330.      *                                    'name' => 'userlist',
  331.      *                                    'add' => array(
  332.      *                                        'quota' => array(
  333.      *                                            'type' => 'integer',
  334.      *                                            'unsigned' => 1
  335.      *                                        )
  336.      *                                    ),
  337.      *                                    'remove' => array(
  338.      *                                        'file_limit' => array(),
  339.      *                                        'time_limit' => array()
  340.      *                                    ),
  341.      *                                    'change' => array(
  342.      *                                        'name' => array(
  343.      *                                            'length' => '20',
  344.      *                                            'definition' => array(
  345.      *                                                'type' => 'text',
  346.      *                                                'length' => 20,
  347.      *                                            ),
  348.      *                                        )
  349.      *                                    ),
  350.      *                                    'rename' => array(
  351.      *                                        'sex' => array(
  352.      *                                            'name' => 'gender',
  353.      *                                            'definition' => array(
  354.      *                                                'type' => 'text',
  355.      *                                                'length' => 1,
  356.      *                                                'default' => 'M',
  357.      *                                            ),
  358.      *                                        )
  359.      *                                    )
  360.      *                                )
  361.      *
  362.      * @param boolean $check     (ignored in DB_Table)
  363.      * @access public
  364.      *
  365.      * @return mixed DB_OK on success, a PEAR error on failure
  366.      */
  367.     function alterTable($name, $changes, $check)
  368.     {
  369.         foreach ($changes as $change_name => $change) {
  370.             switch ($change_name) {
  371.             case 'add':
  372.             case 'remove':
  373.             case 'rename':
  374.             case 'change':
  375.                 break;
  376.             default:
  377.                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
  378.             }
  379.         }
  380.  
  381.         $query = '';
  382.         if (array_key_exists('add', $changes)) {
  383.             foreach ($changes['add'] as $field_name => $field) {
  384.                 if ($query) {
  385.                     $query.= ', ';
  386.                 }
  387.                 $query.= 'ADD ' . $field_name . ' ' . $field;
  388.             }
  389.         }
  390.  
  391.         if (array_key_exists('remove', $changes)) {
  392.             foreach ($changes['remove'] as $field_name => $field) {
  393.                 if ($query) {
  394.                     $query.= ', ';
  395.                 }
  396.                 $field_name = $this->_db->quoteIdentifier($field_name);
  397.                 $query.= 'DROP ' . $field_name;
  398.             }
  399.         }
  400.  
  401.         if (array_key_exists('rename', $changes)) {
  402.             foreach ($changes['rename'] as $field_name => $field) {
  403.                 if ($query) {
  404.                     $query.= ', ';
  405.                 }
  406.                 $field_name = $this->_db->quoteIdentifier($field_name);
  407.                 $query.= 'ALTER ' . $field_name . ' TO ' . $this->_db->quoteIdentifier($field['name']);
  408.             }
  409.         }
  410.  
  411.         if (array_key_exists('change', $changes)) {
  412.             // missing support to change DEFAULT and NULLability
  413.             foreach ($changes['change'] as $field_name => $field) {
  414.                 if ($query) {
  415.                     $query.= ', ';
  416.                 }
  417.                 $field_name = $this->_db->quoteIdentifier($field_name);
  418.                 $query.= 'ALTER ' . $field_name.' TYPE ' . $field['definition'];
  419.             }
  420.         }
  421.  
  422.         if (!strlen($query)) {
  423.             return DB_OK;
  424.         }
  425.  
  426.         $name = $this->_db->quoteIdentifier($name);
  427.         $result = $this->_db->query("ALTER TABLE $name $query");
  428.         return $result;
  429.     }
  430.  
  431. }
  432.  
  433. ?>
  434.