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