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.61 2006/01/17 10:48:49 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: '.$query_fields->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.         foreach ($table_names as $table_name{
  503.             if (!$this->_fixSequenceName($table_nametrue)) {
  504.                 $result[$table_name;
  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.                 $result[$this->_fixIndexName($index_data[$key_name])= true;
  663.             }
  664.         }
  665.  
  666.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  667.             $result array_change_key_case($result$db->options['field_case']);
  668.         }
  669.         return array_keys($result);
  670.     }
  671.  
  672.     // }}}
  673.     // {{{ createConstraint()
  674.  
  675.     /**
  676.      * create a constraint on a table
  677.      *
  678.      * @param string    $table         name of the table on which the constraint is to be created
  679.      * @param string    $name         name of the constraint to be created
  680.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  681.      *                                  Currently, only one property named FIELDS is supported. This property
  682.      *                                  is also an associative with the names of the constraint fields as array
  683.      *                                  constraints. Each entry of this array is set to another type of associative
  684.      *                                  array that specifies properties of the constraint that are specific to
  685.      *                                  each field.
  686.      *
  687.      *                                  Example
  688.      *                                     array(
  689.      *                                         'fields' => array(
  690.      *                                             'user_name' => array(),
  691.      *                                             'last_login' => array()
  692.      *                                         )
  693.      *                                     )
  694.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  695.      * @access public
  696.      */
  697.     function createConstraint($table$name$definition)
  698.     {
  699.         $db =$this->getDBInstance();
  700.         if (PEAR::isError($db)) {
  701.             return $db;
  702.         }
  703.  
  704.         $type '';
  705.         if (array_key_exists('primary'$definition&& $definition['primary']{
  706.             if (strtolower($name!= 'primary'{
  707.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  708.                     'Primary Key may must be named primary');
  709.             }
  710.             $type 'PRIMARY';
  711.             $name 'KEY';
  712.         else {
  713.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  714.             if (array_key_exists('unique'$definition&& $definition['unique']{
  715.                 $type ' UNIQUE';
  716.             }
  717.         }
  718.  
  719.         $table $db->quoteIdentifier($tabletrue);
  720.         $query = "ALTER TABLE $table ADD $type $name";
  721.         $fields = array();
  722.         foreach (array_keys($definition['fields']as $field{
  723.             $fields[$db->quoteIdentifier($fieldtrue);
  724.         }
  725.         $query .= ' ('implode(', '$fields')';
  726.         return $db->exec($query);
  727.     }
  728.  
  729.     // }}}
  730.     // {{{ dropConstraint()
  731.  
  732.     /**
  733.      * drop existing constraint
  734.      *
  735.      * @param string    $table         name of table that should be used in method
  736.      * @param string    $name         name of the constraint to be dropped
  737.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  738.      * @access public
  739.      */
  740.     function dropConstraint($table$name)
  741.     {
  742.         $db =$this->getDBInstance();
  743.         if (PEAR::isError($db)) {
  744.             return $db;
  745.         }
  746.  
  747.         $table $db->quoteIdentifier($tabletrue);
  748.         if (strtolower($name== 'primary'{
  749.             $query = "ALTER TABLE $table DROP PRIMARY KEY";
  750.         else {
  751.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  752.             $query = "ALTER TABLE $table DROP INDEX $name";
  753.         }
  754.         return $db->exec($query);
  755.     }
  756.  
  757.     // }}}
  758.     // {{{ listTableConstraints()
  759.  
  760.     /**
  761.      * list all sonstraints in a table
  762.      *
  763.      * @param string    $table      name of table that should be used in method
  764.      * @return mixed data array on success, a MDB2 error on failure
  765.      * @access public
  766.      */
  767.     function listTableConstraints($table)
  768.     {
  769.         $db =$this->getDBInstance();
  770.         if (PEAR::isError($db)) {
  771.             return $db;
  772.         }
  773.  
  774.         $key_name 'Key_name';
  775.         $non_unique 'Non_unique';
  776.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  777.             if ($db->options['field_case'== CASE_LOWER{
  778.                 $key_name strtolower($key_name);
  779.                 $non_unique strtolower($non_unique);
  780.             else {
  781.                 $key_name strtoupper($key_name);
  782.                 $non_unique strtoupper($non_unique);
  783.             }
  784.         }
  785.  
  786.         $table $db->quoteIdentifier($tabletrue);
  787.         $query = "SHOW INDEX FROM $table";
  788.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  789.         if (PEAR::isError($indexes)) {
  790.             return $indexes;
  791.         }
  792.  
  793.         $result = array();
  794.         foreach ($indexes as $index_data{
  795.             if (!$index_data[$non_unique]{
  796.                 if ($index_data[$key_name!== 'PRIMARY'{
  797.                     $index $this->_fixIndexName($index_data[$key_name]);
  798.                 else {
  799.                     $index 'PRIMARY';
  800.                 }
  801.                 $result[$index= true;
  802.             }
  803.         }
  804.  
  805.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  806.             $result array_change_key_case($result$db->options['field_case']);
  807.         }
  808.         return array_keys($result);
  809.     }
  810.  
  811.     // }}}
  812.     // {{{ createSequence()
  813.  
  814.     /**
  815.      * create sequence
  816.      *
  817.      * @param string    $seq_name     name of the sequence to be created
  818.      * @param string    $start         start value of the sequence; default is 1
  819.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  820.      * @access public
  821.      */
  822.     function createSequence($seq_name$start = 1)
  823.     {
  824.         $db =$this->getDBInstance();
  825.         if (PEAR::isError($db)) {
  826.             return $db;
  827.         }
  828.  
  829.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  830.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  831.         $result $this->_verifyTableType($db->options['default_table_type']);
  832.         if (PEAR::isError($result)) {
  833.             return $result;
  834.         }
  835.  
  836.         $res $db->exec("CREATE TABLE $sequence_name".
  837.             "($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))".
  838.             (strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''')
  839.         );
  840.  
  841.         if (PEAR::isError($res)) {
  842.             return $res;
  843.         }
  844.  
  845.         if ($start == 1{
  846.             return MDB2_OK;
  847.         }
  848.  
  849.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  850.         if (!PEAR::isError($res)) {
  851.             return MDB2_OK;
  852.         }
  853.  
  854.         // Handle error
  855.         $result $db->exec("DROP TABLE $sequence_name");
  856.         if (PEAR::isError($result)) {
  857.             return $db->raiseError(MDB2_ERRORnullnull,
  858.                 'createSequence: could not drop inconsistent sequence table ('.
  859.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  860.         }
  861.  
  862.         return $db->raiseError(MDB2_ERRORnullnull,
  863.             'createSequence: could not create sequence table ('.
  864.             $res->getMessage().' ('.$res->getUserinfo().'))');
  865.     }
  866.  
  867.     // }}}
  868.     // {{{ dropSequence()
  869.  
  870.     /**
  871.      * drop existing sequence
  872.      *
  873.      * @param string    $seq_name     name of the sequence to be dropped
  874.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  875.      * @access public
  876.      */
  877.     function dropSequence($seq_name)
  878.     {
  879.         $db =$this->getDBInstance();
  880.         if (PEAR::isError($db)) {
  881.             return $db;
  882.         }
  883.  
  884.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  885.         return $db->exec("DROP TABLE $sequence_name");
  886.     }
  887.  
  888.     // }}}
  889.     // {{{ listSequences()
  890.  
  891.     /**
  892.      * list all sequences in the current database
  893.      *
  894.      * @return mixed data array on success, a MDB2 error on failure
  895.      * @access public
  896.      */
  897.     function listSequences()
  898.     {
  899.         $db =$this->getDBInstance();
  900.         if (PEAR::isError($db)) {
  901.             return $db;
  902.         }
  903.  
  904.         $table_names $db->queryCol('SHOW TABLES');
  905.         if (PEAR::isError($table_names)) {
  906.             return $table_names;
  907.         }
  908.  
  909.         $result = array();
  910.         foreach ($table_names as $table_name{
  911.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  912.                 $result[$sqn;
  913.             }
  914.         }
  915.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  916.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  917.         }
  918.         return $result;
  919.     }
  920.  
  921.     // }}}
  922. }
  923. ?>

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