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

Source for file mysql.php

Documentation is available at mysql.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: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: mysql.php,v 1.58 2005/12/27 10:25:20 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. /**
  51.  * MDB2 MySQL driver for the management modules
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@pooteeweet.org>
  56.  */
  57. class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
  58. {
  59.     // {{{ properties
  60.     var $verified_table_types = array();#
  61.     // }}}
  62.  
  63.     // }}}
  64.     // {{{ _verifyTableType()
  65.  
  66.     /**
  67.      * verify that chosen transactional table hanlder is available in the database
  68.      *
  69.      * @param string $table_type name of the table handler
  70.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  71.      * @access protected
  72.      */
  73.     function _verifyTableType($table_type)
  74.     {
  75.         $db =$this->getDBInstance();
  76.         if (PEAR::isError($db)) {
  77.             return $db;
  78.         }
  79.  
  80.         switch (strtoupper($table_type)) {
  81.         case 'BERKELEYDB':
  82.         case 'BDB':
  83.             $check = array('have_bdb');
  84.             break;
  85.         case 'INNODB':
  86.             $check = array('have_innobase''have_innodb');
  87.             break;
  88.         case 'GEMINI':
  89.             $check = array('have_gemini');
  90.             break;
  91.         case 'HEAP':
  92.         case 'ISAM':
  93.         case 'MERGE':
  94.         case 'MRG_MYISAM':
  95.         case 'MYISAM':
  96.         case '':
  97.             return MDB2_OK;
  98.         default:
  99.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  100.                 $table_type.' is not a supported table type');
  101.         }
  102.         $connection $db->getConnection();
  103.         if (PEAR::isError($connection)) {
  104.             return $connection;
  105.         }
  106.         if (isset($this->verified_table_types[$table_type])
  107.             && $this->verified_table_types[$table_type== $connection
  108.         {
  109.             return MDB2_OK;
  110.         }
  111.         $not_supported = false;
  112.         for ($i = 0$j count($check)$i $j; ++$i{
  113.             $query 'SHOW VARIABLES LIKE '.$db->quote($check[$i]'text');
  114.             $has $db->queryRow($querynullMDB2_FETCHMODE_ORDERED);
  115.             if (PEAR::isError($has)) {
  116.                 return $has;
  117.             }
  118.             if (is_array($has)) {
  119.                 $not_supported = true;
  120.                 if ($has[1== 'YES'{
  121.                     $this->verified_table_types[$table_type$connection;
  122.                     return MDB2_OK;
  123.                 }
  124.             }
  125.         }
  126.         if ($not_supported{
  127.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  128.                 $table_type.' is not a supported table type by this MySQL database server');
  129.         }
  130.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  131.             'could not tell if '.$table_type.' is a supported table type');
  132.     }
  133.  
  134.     // }}}
  135.     // {{{ createDatabase()
  136.  
  137.     /**
  138.      * create a new database
  139.      *
  140.      * @param string $name name of the database that should be created
  141.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  142.      * @access public
  143.      */
  144.     function createDatabase($name)
  145.     {
  146.         $db =$this->getDBInstance();
  147.         if (PEAR::isError($db)) {
  148.             return $db;
  149.         }
  150.  
  151.         $name $db->quoteIdentifier($nametrue);
  152.         $query = "CREATE DATABASE $name";
  153.         $result $db->exec($query);
  154.         if (PEAR::isError($result)) {
  155.             return $result;
  156.         }
  157.         return MDB2_OK;
  158.     }
  159.  
  160.     // }}}
  161.     // {{{ dropDatabase()
  162.  
  163.     /**
  164.      * drop an existing database
  165.      *
  166.      * @param string $name name of the database that should be dropped
  167.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  168.      * @access public
  169.      */
  170.     function dropDatabase($name)
  171.     {
  172.         $db =$this->getDBInstance();
  173.         if (PEAR::isError($db)) {
  174.             return $db;
  175.         }
  176.  
  177.         $name $db->quoteIdentifier($nametrue);
  178.         $query = "DROP DATABASE $name";
  179.         $result $db->exec($query);
  180.         if (PEAR::isError($result)) {
  181.             return $result;
  182.         }
  183.         return MDB2_OK;
  184.     }
  185.  
  186.     // }}}
  187.     // {{{ createTable()
  188.  
  189.     /**
  190.      * create a new table
  191.      *
  192.      * @param string $name     Name of the database that should be created
  193.      * @param array $fields Associative array that contains the definition of each field of the new table
  194.      *                         The indexes of the array entries are the names of the fields of the table an
  195.      *                         the array entry values are associative arrays like those that are meant to be
  196.      *                          passed with the field definitions to get[Type]Declaration() functions.
  197.      *
  198.      *                         Example
  199.      *                         array(
  200.      *
  201.      *                             'id' => array(
  202.      *                                 'type' => 'integer',
  203.      *                                 'unsigned' => 1
  204.      *                                 'notnull' => 1
  205.      *                                 'default' => 0
  206.      *                             ),
  207.      *                             'name' => array(
  208.      *                                 'type' => 'text',
  209.      *                                 'length' => 12
  210.      *                             ),
  211.      *                             'password' => array(
  212.      *                                 'type' => 'text',
  213.      *                                 'length' => 12
  214.      *                             )
  215.      *                         );
  216.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  217.      * @access public
  218.      */
  219.     function createTable($name$fields)
  220.     {
  221.         $db =$this->getDBInstance();
  222.         if (PEAR::isError($db)) {
  223.             return $db;
  224.         }
  225.  
  226.         if (!$name{
  227.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  228.                 'createTable: no valid table name specified');
  229.         }
  230.         if (empty($fields)) {
  231.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  232.                 'createTable: no fields specified for table "'.$name.'"');
  233.         }
  234.         $verify $this->_verifyTableType($db->options['default_table_type']);
  235.         if (PEAR::isError($verify)) {
  236.             return $verify;
  237.         }
  238.         $query_fields $this->getFieldDeclarationList($fields);
  239.         if (PEAR::isError($query_fields)) {
  240.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  241.                 'createTable: '.$this->getUserinfo());
  242.         }
  243.         $name $db->quoteIdentifier($nametrue);
  244.         $query = "CREATE TABLE $name ($query_fields)".(strlen($db->options['default_table_type'])
  245.             ? ' TYPE='.$db->options['default_table_type''');
  246.  
  247.         return $db->exec($query);
  248.     }
  249.  
  250.     // }}}
  251.     // {{{ alterTable()
  252.  
  253.     /**
  254.      * alter an existing table
  255.      *
  256.      * @param string $name         name of the table that is intended to be changed.
  257.      * @param array $changes     associative array that contains the details of each type
  258.      *                              of change that is intended to be performed. The types of
  259.      *                              changes that are currently supported are defined as follows:
  260.      *
  261.      *                              name
  262.      *
  263.      *                                 New name for the table.
  264.      *
  265.      *                             add
  266.      *
  267.      *                                 Associative array with the names of fields to be added as
  268.      *                                  indexes of the array. The value of each entry of the array
  269.      *                                  should be set to another associative array with the properties
  270.      *                                  of the fields to be added. The properties of the fields should
  271.      *                                  be the same as defined by the Metabase parser.
  272.      *
  273.      *
  274.      *                             remove
  275.      *
  276.      *                                 Associative array with the names of fields to be removed as indexes
  277.      *                                  of the array. Currently the values assigned to each entry are ignored.
  278.      *                                  An empty array should be used for future compatibility.
  279.      *
  280.      *                             rename
  281.      *
  282.      *                                 Associative array with the names of fields to be renamed as indexes
  283.      *                                  of the array. The value of each entry of the array should be set to
  284.      *                                  another associative array with the entry named name with the new
  285.      *                                  field name and the entry named Declaration that is expected to contain
  286.      *                                  the portion of the field declaration already in DBMS specific SQL code
  287.      *                                  as it is used in the CREATE TABLE statement.
  288.      *
  289.      *                             change
  290.      *
  291.      *                                 Associative array with the names of the fields to be changed as indexes
  292.      *                                  of the array. Keep in mind that if it is intended to change either the
  293.      *                                  name of a field and any other properties, the change array entries
  294.      *                                  should have the new names of the fields as array indexes.
  295.      *
  296.      *                                 The value of each entry of the array should be set to another associative
  297.      *                                  array with the properties of the fields to that are meant to be changed as
  298.      *                                  array entries. These entries should be assigned to the new values of the
  299.      *                                  respective properties. The properties of the fields should be the same
  300.      *                                  as defined by the Metabase parser.
  301.      *
  302.      *                             Example
  303.      *                                 array(
  304.      *                                     'name' => 'userlist',
  305.      *                                     'add' => array(
  306.      *                                         'quota' => array(
  307.      *                                             'type' => 'integer',
  308.      *                                             'unsigned' => 1
  309.      *                                         )
  310.      *                                     ),
  311.      *                                     'remove' => array(
  312.      *                                         'file_limit' => array(),
  313.      *                                         'time_limit' => array()
  314.      *                                     ),
  315.      *                                     'change' => array(
  316.      *                                         'name' => array(
  317.      *                                             'length' => '20',
  318.      *                                             'definition' => array(
  319.      *                                                 'type' => 'text',
  320.      *                                                 'length' => 20,
  321.      *                                             ),
  322.      *                                         )
  323.      *                                     ),
  324.      *                                     'rename' => array(
  325.      *                                         'sex' => array(
  326.      *                                             'name' => 'gender',
  327.      *                                             'definition' => array(
  328.      *                                                 'type' => 'text',
  329.      *                                                 'length' => 1,
  330.      *                                                 'default' => 'M',
  331.      *                                             ),
  332.      *                                         )
  333.      *                                     )
  334.      *                                 )
  335.      *
  336.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  337.      *                              can perform the requested table alterations if the value is true or
  338.      *                              actually perform them otherwise.
  339.      * @access public
  340.      *
  341.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  342.      */
  343.     function alterTable($name$changes$check)
  344.     {
  345.         $db =$this->getDBInstance();
  346.         if (PEAR::isError($db)) {
  347.             return $db;
  348.         }
  349.  
  350.         foreach ($changes as $change_name => $change{
  351.             switch ($change_name{
  352.             case 'add':
  353.             case 'remove':
  354.             case 'change':
  355.             case 'rename':
  356.             case 'name':
  357.                 break;
  358.             default:
  359.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  360.                     'alterTable: change type "'.$change_name.'" not yet supported');
  361.             }
  362.         }
  363.  
  364.         if ($check{
  365.             return MDB2_OK;
  366.         }
  367.  
  368.         $query '';
  369.         if (array_key_exists('name'$changes)) {
  370.             $change_name $db->quoteIdentifier($changes['name']true);
  371.             $query .= 'RENAME TO ' $change_name;
  372.         }
  373.  
  374.         if (array_key_exists('add'$changes)) {
  375.             foreach ($changes['add'as $field_name => $field{
  376.                 if ($query{
  377.                     $query.= ', ';
  378.                 }
  379.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  380.             }
  381.         }
  382.  
  383.         if (array_key_exists('remove'$changes)) {
  384.             foreach ($changes['remove'as $field_name => $field{
  385.                 if ($query{
  386.                     $query.= ', ';
  387.                 }
  388.                 $field_name $db->quoteIdentifier($field_nametrue);
  389.                 $query.= 'DROP ' $field_name;
  390.             }
  391.         }
  392.  
  393.         $rename = array();
  394.         if (array_key_exists('rename'$changes)) {
  395.             foreach ($changes['rename'as $field_name => $field{
  396.                 $rename[$field['name']] $field_name;
  397.             }
  398.         }
  399.  
  400.         if (array_key_exists('change'$changes)) {
  401.             foreach ($changes['change'as $field_name => $field{
  402.                 if ($query{
  403.                     $query.= ', ';
  404.                 }
  405.                 if (isset($rename[$field_name])) {
  406.                     $old_field_name $rename[$field_name];
  407.                     unset($rename[$field_name]);
  408.                 else {
  409.                     $old_field_name $field_name;
  410.                 }
  411.                 $old_field_name $db->quoteIdentifier($old_field_nametrue);
  412.                 $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type']$field_name$field['definition']);
  413.             }
  414.         }
  415.  
  416.         if (!empty($rename)) {
  417.             foreach ($rename as $rename_name => $renamed_field{
  418.                 if ($query{
  419.                     $query.= ', ';
  420.                 }
  421.                 $field $changes['rename'][$renamed_field];
  422.                 $renamed_field $db->quoteIdentifier($renamed_fieldtrue);
  423.                 $query.= 'CHANGE ' $renamed_field ' ' $db->getDeclaration($field['definition']['type']$field['name']$field['definition']);
  424.             }
  425.         }
  426.  
  427.         if (!$query{
  428.             return MDB2_OK;
  429.         }
  430.  
  431.         $name $db->quoteIdentifier($nametrue);
  432.         return $db->exec("ALTER TABLE $name $query");
  433.     }
  434.  
  435.     // }}}
  436.     // {{{ listDatabases()
  437.  
  438.     /**
  439.      * list all databases
  440.      *
  441.      * @return mixed data array on success, a MDB2 error on failure
  442.      * @access public
  443.      */
  444.     function listDatabases()
  445.     {
  446.         $db =$this->getDBInstance();
  447.         if (PEAR::isError($db)) {
  448.             return $db;
  449.         }
  450.  
  451.         $result $db->queryCol('SHOW DATABASES');
  452.         if (PEAR::isError($result)) {
  453.             return $result;
  454.         }
  455.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  456.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  457.         }
  458.         return $result;
  459.     }
  460.  
  461.     // }}}
  462.     // {{{ listUsers()
  463.  
  464.     /**
  465.      * list all users
  466.      *
  467.      * @return mixed data array on success, a MDB2 error on failure
  468.      * @access public
  469.      */
  470.     function listUsers()
  471.     {
  472.         $db =$this->getDBInstance();
  473.         if (PEAR::isError($db)) {
  474.             return $db;
  475.         }
  476.  
  477.         return $db->queryCol('SELECT DISTINCT USER FROM USER');
  478.     }
  479.  
  480.     // }}}
  481.     // {{{ listTables()
  482.  
  483.     /**
  484.      * list all tables in the current database
  485.      *
  486.      * @return mixed data array on success, a MDB2 error on failure
  487.      * @access public
  488.      */
  489.     function listTables()
  490.     {
  491.         $db =$this->getDBInstance();
  492.         if (PEAR::isError($db)) {
  493.             return $db;
  494.         }
  495.  
  496.         $table_names $db->queryCol('SHOW TABLES');
  497.         if (PEAR::isError($table_names)) {
  498.             return $table_names;
  499.         }
  500.  
  501.         $result = array();
  502.         for ($i = 0$j count($table_names)$i $j; ++$i{
  503.             if (!$this->_isSequenceName($table_names[$i])) {
  504.                 $result[$table_names[$i];
  505.             }
  506.         }
  507.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  508.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  509.         }
  510.         return $result;
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ listTableFields()
  515.  
  516.     /**
  517.      * list all fields in a tables in the current database
  518.      *
  519.      * @param string $table name of table that should be used in method
  520.      * @return mixed data array on success, a MDB2 error on failure
  521.      * @access public
  522.      */
  523.     function listTableFields($table)
  524.     {
  525.         $db =$this->getDBInstance();
  526.         if (PEAR::isError($db)) {
  527.             return $db;
  528.         }
  529.  
  530.         $table $db->quoteIdentifier($tabletrue);
  531.         $result $db->queryCol("SHOW COLUMNS FROM $table");
  532.         if (PEAR::isError($result)) {
  533.             return $result;
  534.         }
  535.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  536.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  537.         }
  538.         return $result;
  539.     }
  540.  
  541.     // }}}
  542.     // {{{ createIndex()
  543.  
  544.     /**
  545.      * get the stucture of a field into an array
  546.      *
  547.      * @author Leoncx
  548.      * @param string    $table         name of the table on which the index is to be created
  549.      * @param string    $name         name of the index to be created
  550.      * @param array     $definition        associative array that defines properties of the index to be created.
  551.      *                                  Currently, only one property named FIELDS is supported. This property
  552.      *                                  is also an associative with the names of the index fields as array
  553.      *                                  indexes. Each entry of this array is set to another type of associative
  554.      *                                  array that specifies properties of the index that are specific to
  555.      *                                  each field.
  556.      *
  557.      *                                 Currently, only the sorting property is supported. It should be used
  558.      *                                  to define the sorting direction of the index. It may be set to either
  559.      *                                  ascending or descending.
  560.      *
  561.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  562.      *                                  drivers of those that do not support it ignore this property. Use the
  563.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  564.      *
  565.      *                                  Example
  566.      *                                     array(
  567.      *                                         'fields' => array(
  568.      *                                             'user_name' => array(
  569.      *                                                 'sorting' => 'ascending'
  570.      *                                                 'length' => 10
  571.      *                                             ),
  572.      *                                             'last_login' => array()
  573.      *                                         )
  574.      *                                     )
  575.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  576.      * @access public
  577.      */
  578.     function createIndex($table$name$definition)
  579.     {
  580.         $db =$this->getDBInstance();
  581.         if (PEAR::isError($db)) {
  582.             return $db;
  583.         }
  584.  
  585.         $table $db->quoteIdentifier($tabletrue);
  586.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  587.         $query = "CREATE INDEX $name ON $table";
  588.         $fields = array();
  589.         foreach ($definition['fields'as $field => $fieldinfo{
  590.             if (array_key_exists('length'$fieldinfo)) {
  591.                 $fields[$db->quoteIdentifier($fieldtrue'(' $fieldinfo['length'')';
  592.             else {
  593.                 $fields[$db->quoteIdentifier($fieldtrue);
  594.             }
  595.         }
  596.         $query .= ' ('implode(', '$fields')';
  597.         return $db->exec($query);
  598.     }
  599.  
  600.     // }}}
  601.     // {{{ dropIndex()
  602.  
  603.     /**
  604.      * drop existing index
  605.      *
  606.      * @param string    $table         name of table that should be used in method
  607.      * @param string    $name         name of the index to be dropped
  608.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  609.      * @access public
  610.      */
  611.     function dropIndex($table$name)
  612.     {
  613.         $db =$this->getDBInstance();
  614.         if (PEAR::isError($db)) {
  615.             return $db;
  616.         }
  617.  
  618.         $table $db->quoteIdentifier($tabletrue);
  619.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  620.         return $db->exec("DROP INDEX $name ON $table");
  621.     }
  622.  
  623.     // }}}
  624.     // {{{ listTableIndexes()
  625.  
  626.     /**
  627.      * list all indexes in a table
  628.      *
  629.      * @param string    $table      name of table that should be used in method
  630.      * @return mixed data array on success, a MDB2 error on failure
  631.      * @access public
  632.      */
  633.     function listTableIndexes($table)
  634.     {
  635.         $db =$this->getDBInstance();
  636.         if (PEAR::isError($db)) {
  637.             return $db;
  638.         }
  639.  
  640.         $key_name 'Key_name';
  641.         $non_unique 'Non_unique';
  642.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  643.             if ($db->options['field_case'== CASE_LOWER{
  644.                 $key_name strtolower($key_name);
  645.                 $non_unique strtolower($non_unique);
  646.             else {
  647.                 $key_name strtoupper($key_name);
  648.                 $non_unique strtoupper($non_unique);
  649.             }
  650.         }
  651.  
  652.         $table $db->quoteIdentifier($tabletrue);
  653.         $query = "SHOW INDEX FROM $table";
  654.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  655.         if (PEAR::isError($indexes)) {
  656.             return $indexes;
  657.         }
  658.  
  659.         $result = array();
  660.         foreach ($indexes as $index_data{
  661.             if ($index_data[$non_unique]{
  662.                 $index $this->_isIndexName($index_data[$key_name]);
  663.                 $result[$index= true;
  664.             }
  665.         }
  666.  
  667.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  668.             $result array_change_key_case($result$db->options['field_case']);
  669.         }
  670.         return array_keys($result);
  671.     }
  672.  
  673.     // }}}
  674.     // {{{ createConstraint()
  675.  
  676.     /**
  677.      * create a constraint on a table
  678.      *
  679.      * @param string    $table         name of the table on which the constraint is to be created
  680.      * @param string    $name         name of the constraint to be created
  681.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  682.      *                                  Currently, only one property named FIELDS is supported. This property
  683.      *                                  is also an associative with the names of the constraint fields as array
  684.      *                                  constraints. Each entry of this array is set to another type of associative
  685.      *                                  array that specifies properties of the constraint that are specific to
  686.      *                                  each field.
  687.      *
  688.      *                                  Example
  689.      *                                     array(
  690.      *                                         'fields' => array(
  691.      *                                             'user_name' => array(),
  692.      *                                             'last_login' => array()
  693.      *                                         )
  694.      *                                     )
  695.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  696.      * @access public
  697.      */
  698.     function createConstraint($table$name$definition)
  699.     {
  700.         $db =$this->getDBInstance();
  701.         if (PEAR::isError($db)) {
  702.             return $db;
  703.         }
  704.  
  705.         $type '';
  706.         if (array_key_exists('primary'$definition&& $definition['primary']{
  707.             if (strtolower($name!= 'primary'{
  708.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  709.                     'Primary Key may must be named primary');
  710.             }
  711.             $type 'PRIMARY';
  712.             $name 'KEY';
  713.         else {
  714.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  715.             if (array_key_exists('unique'$definition&& $definition['unique']{
  716.                 $type ' UNIQUE';
  717.             }
  718.         }
  719.  
  720.         $table $db->quoteIdentifier($tabletrue);
  721.         $query = "ALTER TABLE $table ADD $type $name";
  722.         $fields = array();
  723.         foreach (array_keys($definition['fields']as $field{
  724.             $fields[$db->quoteIdentifier($fieldtrue);
  725.         }
  726.         $query .= ' ('implode(', '$fields')';
  727.         return $db->exec($query);
  728.     }
  729.  
  730.     // }}}
  731.     // {{{ dropConstraint()
  732.  
  733.     /**
  734.      * drop existing constraint
  735.      *
  736.      * @param string    $table         name of table that should be used in method
  737.      * @param string    $name         name of the constraint to be dropped
  738.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  739.      * @access public
  740.      */
  741.     function dropConstraint($table$name)
  742.     {
  743.         $db =$this->getDBInstance();
  744.         if (PEAR::isError($db)) {
  745.             return $db;
  746.         }
  747.  
  748.         $table $db->quoteIdentifier($tabletrue);
  749.         if (strtolower($name== 'primary'{
  750.             $query = "ALTER TABLE $table DROP PRIMARY KEY";
  751.         else {
  752.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  753.             $query = "ALTER TABLE $table DROP INDEX $name";
  754.         }
  755.         return $db->exec($query);
  756.     }
  757.  
  758.     // }}}
  759.     // {{{ listTableConstraints()
  760.  
  761.     /**
  762.      * list all sonstraints in a table
  763.      *
  764.      * @param string    $table      name of table that should be used in method
  765.      * @return mixed data array on success, a MDB2 error on failure
  766.      * @access public
  767.      */
  768.     function listTableConstraints($table)
  769.     {
  770.         $db =$this->getDBInstance();
  771.         if (PEAR::isError($db)) {
  772.             return $db;
  773.         }
  774.  
  775.         $key_name 'Key_name';
  776.         $non_unique 'Non_unique';
  777.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  778.             if ($db->options['field_case'== CASE_LOWER{
  779.                 $key_name strtolower($key_name);
  780.                 $non_unique strtolower($non_unique);
  781.             else {
  782.                 $key_name strtoupper($key_name);
  783.                 $non_unique strtoupper($non_unique);
  784.             }
  785.         }
  786.  
  787.         $table $db->quoteIdentifier($tabletrue);
  788.         $query = "SHOW INDEX FROM $table";
  789.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  790.         if (PEAR::isError($indexes)) {
  791.             return $indexes;
  792.         }
  793.  
  794.         $result = array();
  795.         foreach ($indexes as $index_data{
  796.             if (!$index_data[$non_unique]{
  797.                 $index $this->_isIndexName($index_data[$key_name]);
  798.                 $result[$index= true;
  799.             }
  800.         }
  801.  
  802.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  803.             $result array_change_key_case($result$db->options['field_case']);
  804.         }
  805.         return array_keys($result);
  806.     }
  807.  
  808.     // }}}
  809.     // {{{ createSequence()
  810.  
  811.     /**
  812.      * create sequence
  813.      *
  814.      * @param string    $seq_name     name of the sequence to be created
  815.      * @param string    $start         start value of the sequence; default is 1
  816.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  817.      * @access public
  818.      */
  819.     function createSequence($seq_name$start = 1)
  820.     {
  821.         $db =$this->getDBInstance();
  822.         if (PEAR::isError($db)) {
  823.             return $db;
  824.         }
  825.  
  826.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  827.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  828.         $result $this->_verifyTableType($db->options['default_table_type']);
  829.         if (PEAR::isError($result)) {
  830.             return $result;
  831.         }
  832.  
  833.         $res $db->exec("CREATE TABLE $sequence_name".
  834.             "($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))".
  835.             (strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''')
  836.         );
  837.  
  838.         if (PEAR::isError($res)) {
  839.             return $res;
  840.         }
  841.  
  842.         if ($start == 1{
  843.             return MDB2_OK;
  844.         }
  845.  
  846.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  847.         if (!PEAR::isError($res)) {
  848.             return MDB2_OK;
  849.         }
  850.  
  851.         // Handle error
  852.         $result $db->exec("DROP TABLE $sequence_name");
  853.         if (PEAR::isError($result)) {
  854.             return $db->raiseError(MDB2_ERRORnullnull,
  855.                 'createSequence: could not drop inconsistent sequence table ('.
  856.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  857.         }
  858.  
  859.         return $db->raiseError(MDB2_ERRORnullnull,
  860.             'createSequence: could not create sequence table ('.
  861.             $res->getMessage().' ('.$res->getUserinfo().'))');
  862.     }
  863.  
  864.     // }}}
  865.     // {{{ dropSequence()
  866.  
  867.     /**
  868.      * drop existing sequence
  869.      *
  870.      * @param string    $seq_name     name of the sequence to be dropped
  871.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  872.      * @access public
  873.      */
  874.     function dropSequence($seq_name)
  875.     {
  876.         $db =$this->getDBInstance();
  877.         if (PEAR::isError($db)) {
  878.             return $db;
  879.         }
  880.  
  881.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  882.         return $db->exec("DROP TABLE $sequence_name");
  883.     }
  884.  
  885.     // }}}
  886.     // {{{ listSequences()
  887.  
  888.     /**
  889.      * list all sequences in the current database
  890.      *
  891.      * @return mixed data array on success, a MDB2 error on failure
  892.      * @access public
  893.      */
  894.     function listSequences()
  895.     {
  896.         $db =$this->getDBInstance();
  897.         if (PEAR::isError($db)) {
  898.             return $db;
  899.         }
  900.  
  901.         $table_names $db->queryCol('SHOW TABLES');
  902.         if (PEAR::isError($table_names)) {
  903.             return $table_names;
  904.         }
  905.  
  906.         $result = array();
  907.         for ($i = 0$j count($table_names)$i $j; ++$i{
  908.             if ($sqn $this->_isSequenceName($table_names[$i])) {
  909.                 $result[$sqn;
  910.             }
  911.         }
  912.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  913.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  914.         }
  915.         return $result;
  916.     }
  917.  
  918.     // }}}
  919. }
  920. ?>

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