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.57 2005/12/15 22:52:56 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 (array_key_exists('name'$changes'RENAME TO '.$changes['name''');
  369.  
  370.         if (array_key_exists('add'$changes)) {
  371.             foreach ($changes['add'as $field_name => $field{
  372.                 if ($query{
  373.                     $query.= ', ';
  374.                 }
  375.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  376.             }
  377.         }
  378.  
  379.         if (array_key_exists('remove'$changes)) {
  380.             foreach ($changes['remove'as $field_name => $field{
  381.                 if ($query{
  382.                     $query.= ', ';
  383.                 }
  384.                 $field_name $db->quoteIdentifier($field_nametrue);
  385.                 $query.= 'DROP ' $field_name;
  386.             }
  387.         }
  388.  
  389.         $rename = array();
  390.         if (array_key_exists('rename'$changes)) {
  391.             foreach ($changes['rename'as $field_name => $field{
  392.                 $rename[$field['name']] $field_name;
  393.             }
  394.         }
  395.  
  396.         if (array_key_exists('change'$changes)) {
  397.             foreach ($changes['change'as $field_name => $field{
  398.                 if ($query{
  399.                     $query.= ', ';
  400.                 }
  401.                 if (isset($rename[$field_name])) {
  402.                     $old_field_name $rename[$field_name];
  403.                     unset($rename[$field_name]);
  404.                 else {
  405.                     $old_field_name $field_name;
  406.                 }
  407.                 $old_field_name $db->quoteIdentifier($old_field_nametrue);
  408.                 $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type']$field_name$field['definition']);
  409.             }
  410.         }
  411.  
  412.         if (!empty($rename)) {
  413.             foreach ($rename as $rename_name => $renamed_field{
  414.                 if ($query{
  415.                     $query.= ', ';
  416.                 }
  417.                 $field $changes['rename'][$renamed_field];
  418.                 $renamed_field $db->quoteIdentifier($renamed_fieldtrue);
  419.                 $query.= 'CHANGE ' $renamed_field ' ' $db->getDeclaration($field['definition']['type']$field['name']$field['definition']);
  420.             }
  421.         }
  422.  
  423.         if (!$query{
  424.             return MDB2_OK;
  425.         }
  426.  
  427.         $name $db->quoteIdentifier($nametrue);
  428.         return $db->exec("ALTER TABLE $name $query");
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ listDatabases()
  433.  
  434.     /**
  435.      * list all databases
  436.      *
  437.      * @return mixed data array on success, a MDB2 error on failure
  438.      * @access public
  439.      */
  440.     function listDatabases()
  441.     {
  442.         $db =$this->getDBInstance();
  443.         if (PEAR::isError($db)) {
  444.             return $db;
  445.         }
  446.  
  447.         $result $db->queryCol('SHOW DATABASES');
  448.         if (PEAR::isError($result)) {
  449.             return $result;
  450.         }
  451.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  452.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  453.         }
  454.         return $result;
  455.     }
  456.  
  457.     // }}}
  458.     // {{{ listUsers()
  459.  
  460.     /**
  461.      * list all users
  462.      *
  463.      * @return mixed data array on success, a MDB2 error on failure
  464.      * @access public
  465.      */
  466.     function listUsers()
  467.     {
  468.         $db =$this->getDBInstance();
  469.         if (PEAR::isError($db)) {
  470.             return $db;
  471.         }
  472.  
  473.         return $db->queryCol('SELECT DISTINCT USER FROM USER');
  474.     }
  475.  
  476.     // }}}
  477.     // {{{ listTables()
  478.  
  479.     /**
  480.      * list all tables in the current database
  481.      *
  482.      * @return mixed data array on success, a MDB2 error on failure
  483.      * @access public
  484.      */
  485.     function listTables()
  486.     {
  487.         $db =$this->getDBInstance();
  488.         if (PEAR::isError($db)) {
  489.             return $db;
  490.         }
  491.  
  492.         $table_names $db->queryCol('SHOW TABLES');
  493.         if (PEAR::isError($table_names)) {
  494.             return $table_names;
  495.         }
  496.  
  497.         $result = array();
  498.         for ($i = 0$j count($table_names)$i $j; ++$i{
  499.             if (!$this->_isSequenceName($table_names[$i])) {
  500.                 $result[$table_names[$i];
  501.             }
  502.         }
  503.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  504.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  505.         }
  506.         return $result;
  507.     }
  508.  
  509.     // }}}
  510.     // {{{ listTableFields()
  511.  
  512.     /**
  513.      * list all fields in a tables in the current database
  514.      *
  515.      * @param string $table name of table that should be used in method
  516.      * @return mixed data array on success, a MDB2 error on failure
  517.      * @access public
  518.      */
  519.     function listTableFields($table)
  520.     {
  521.         $db =$this->getDBInstance();
  522.         if (PEAR::isError($db)) {
  523.             return $db;
  524.         }
  525.  
  526.         $table $db->quoteIdentifier($tabletrue);
  527.         $result $db->queryCol("SHOW COLUMNS FROM $table");
  528.         if (PEAR::isError($result)) {
  529.             return $result;
  530.         }
  531.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  532.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  533.         }
  534.         return $result;
  535.     }
  536.  
  537.     // }}}
  538.     // {{{ createIndex()
  539.  
  540.     /**
  541.      * get the stucture of a field into an array
  542.      *
  543.      * @author Leoncx
  544.      * @param string    $table         name of the table on which the index is to be created
  545.      * @param string    $name         name of the index to be created
  546.      * @param array     $definition        associative array that defines properties of the index to be created.
  547.      *                                  Currently, only one property named FIELDS is supported. This property
  548.      *                                  is also an associative with the names of the index fields as array
  549.      *                                  indexes. Each entry of this array is set to another type of associative
  550.      *                                  array that specifies properties of the index that are specific to
  551.      *                                  each field.
  552.      *
  553.      *                                 Currently, only the sorting property is supported. It should be used
  554.      *                                  to define the sorting direction of the index. It may be set to either
  555.      *                                  ascending or descending.
  556.      *
  557.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  558.      *                                  drivers of those that do not support it ignore this property. Use the
  559.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  560.      *
  561.      *                                  Example
  562.      *                                     array(
  563.      *                                         'fields' => array(
  564.      *                                             'user_name' => array(
  565.      *                                                 'sorting' => 'ascending'
  566.      *                                                 'length' => 10
  567.      *                                             ),
  568.      *                                             'last_login' => array()
  569.      *                                         )
  570.      *                                     )
  571.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  572.      * @access public
  573.      */
  574.     function createIndex($table$name$definition)
  575.     {
  576.         $db =$this->getDBInstance();
  577.         if (PEAR::isError($db)) {
  578.             return $db;
  579.         }
  580.  
  581.         $table $db->quoteIdentifier($tabletrue);
  582.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  583.         $query = "CREATE INDEX $name ON $table";
  584.         $fields = array();
  585.         foreach ($definition['fields'as $field => $fieldinfo{
  586.             if (array_key_exists('length'$fieldinfo)) {
  587.                 $fields[$db->quoteIdentifier($fieldtrue'(' $fieldinfo['length'')';
  588.             else {
  589.                 $fields[$db->quoteIdentifier($fieldtrue);
  590.             }
  591.         }
  592.         $query .= ' ('implode(', '$fields')';
  593.         return $db->exec($query);
  594.     }
  595.  
  596.     // }}}
  597.     // {{{ dropIndex()
  598.  
  599.     /**
  600.      * drop existing index
  601.      *
  602.      * @param string    $table         name of table that should be used in method
  603.      * @param string    $name         name of the index to be dropped
  604.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  605.      * @access public
  606.      */
  607.     function dropIndex($table$name)
  608.     {
  609.         $db =$this->getDBInstance();
  610.         if (PEAR::isError($db)) {
  611.             return $db;
  612.         }
  613.  
  614.         $table $db->quoteIdentifier($tabletrue);
  615.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  616.         return $db->exec("DROP INDEX $name ON $table");
  617.     }
  618.  
  619.     // }}}
  620.     // {{{ listTableIndexes()
  621.  
  622.     /**
  623.      * list all indexes in a table
  624.      *
  625.      * @param string    $table      name of table that should be used in method
  626.      * @return mixed data array on success, a MDB2 error on failure
  627.      * @access public
  628.      */
  629.     function listTableIndexes($table)
  630.     {
  631.         $db =$this->getDBInstance();
  632.         if (PEAR::isError($db)) {
  633.             return $db;
  634.         }
  635.  
  636.         $key_name 'Key_name';
  637.         $non_unique 'Non_unique';
  638.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  639.             if ($db->options['field_case'== CASE_LOWER{
  640.                 $key_name strtolower($key_name);
  641.                 $non_unique strtolower($non_unique);
  642.             else {
  643.                 $key_name strtoupper($key_name);
  644.                 $non_unique strtoupper($non_unique);
  645.             }
  646.         }
  647.  
  648.         $table $db->quoteIdentifier($tabletrue);
  649.         $query = "SHOW INDEX FROM $table";
  650.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  651.         if (PEAR::isError($indexes)) {
  652.             return $indexes;
  653.         }
  654.  
  655.         $result = array();
  656.         foreach ($indexes as $index_data{
  657.             if ($index_data[$non_unique]{
  658.                 $index $this->_isIndexName($index_data[$key_name]);
  659.                 $result[$index= true;
  660.             }
  661.         }
  662.  
  663.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  664.             $result array_change_key_case($result$db->options['field_case']);
  665.         }
  666.         return array_keys($result);
  667.     }
  668.  
  669.     // }}}
  670.     // {{{ createConstraint()
  671.  
  672.     /**
  673.      * create a constraint on a table
  674.      *
  675.      * @param string    $table         name of the table on which the constraint is to be created
  676.      * @param string    $name         name of the constraint to be created
  677.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  678.      *                                  Currently, only one property named FIELDS is supported. This property
  679.      *                                  is also an associative with the names of the constraint fields as array
  680.      *                                  constraints. Each entry of this array is set to another type of associative
  681.      *                                  array that specifies properties of the constraint that are specific to
  682.      *                                  each field.
  683.      *
  684.      *                                  Example
  685.      *                                     array(
  686.      *                                         'fields' => array(
  687.      *                                             'user_name' => array(),
  688.      *                                             'last_login' => array()
  689.      *                                         )
  690.      *                                     )
  691.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  692.      * @access public
  693.      */
  694.     function createConstraint($table$name$definition)
  695.     {
  696.         $db =$this->getDBInstance();
  697.         if (PEAR::isError($db)) {
  698.             return $db;
  699.         }
  700.  
  701.         $type '';
  702.         if (array_key_exists('primary'$definition&& $definition['primary']{
  703.             if (strtolower($name!= 'primary'{
  704.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  705.                     'Primary Key may must be named primary');
  706.             }
  707.             $type 'PRIMARY';
  708.             $name 'KEY';
  709.         else {
  710.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  711.             if (array_key_exists('unique'$definition&& $definition['unique']{
  712.                 $type ' UNIQUE';
  713.             }
  714.         }
  715.  
  716.         $table $db->quoteIdentifier($tabletrue);
  717.         $query = "ALTER TABLE $table ADD $type $name";
  718.         $fields = array();
  719.         foreach (array_keys($definition['fields']as $field{
  720.             $fields[$db->quoteIdentifier($fieldtrue);
  721.         }
  722.         $query .= ' ('implode(', '$fields')';
  723.         return $db->exec($query);
  724.     }
  725.  
  726.     // }}}
  727.     // {{{ dropConstraint()
  728.  
  729.     /**
  730.      * drop existing constraint
  731.      *
  732.      * @param string    $table         name of table that should be used in method
  733.      * @param string    $name         name of the constraint to be dropped
  734.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  735.      * @access public
  736.      */
  737.     function dropConstraint($table$name)
  738.     {
  739.         $db =$this->getDBInstance();
  740.         if (PEAR::isError($db)) {
  741.             return $db;
  742.         }
  743.  
  744.         $table $db->quoteIdentifier($tabletrue);
  745.         if (strtolower($name== 'primary'{
  746.             $query = "ALTER TABLE $table DROP PRIMARY KEY";
  747.         else {
  748.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  749.             $query = "ALTER TABLE $table DROP INDEX $name";
  750.         }
  751.         return $db->exec($query);
  752.     }
  753.  
  754.     // }}}
  755.     // {{{ listTableConstraints()
  756.  
  757.     /**
  758.      * list all sonstraints in a table
  759.      *
  760.      * @param string    $table      name of table that should be used in method
  761.      * @return mixed data array on success, a MDB2 error on failure
  762.      * @access public
  763.      */
  764.     function listTableConstraints($table)
  765.     {
  766.         $db =$this->getDBInstance();
  767.         if (PEAR::isError($db)) {
  768.             return $db;
  769.         }
  770.  
  771.         $key_name 'Key_name';
  772.         $non_unique 'Non_unique';
  773.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  774.             if ($db->options['field_case'== CASE_LOWER{
  775.                 $key_name strtolower($key_name);
  776.                 $non_unique strtolower($non_unique);
  777.             else {
  778.                 $key_name strtoupper($key_name);
  779.                 $non_unique strtoupper($non_unique);
  780.             }
  781.         }
  782.  
  783.         $table $db->quoteIdentifier($tabletrue);
  784.         $query = "SHOW INDEX FROM $table";
  785.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  786.         if (PEAR::isError($indexes)) {
  787.             return $indexes;
  788.         }
  789.  
  790.         $result = array();
  791.         foreach ($indexes as $index_data{
  792.             if (!$index_data[$non_unique]{
  793.                 $index $this->_isIndexName($index_data[$key_name]);
  794.                 $result[$index= true;
  795.             }
  796.         }
  797.  
  798.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  799.             $result array_change_key_case($result$db->options['field_case']);
  800.         }
  801.         return array_keys($result);
  802.     }
  803.  
  804.     // }}}
  805.     // {{{ createSequence()
  806.  
  807.     /**
  808.      * create sequence
  809.      *
  810.      * @param string    $seq_name     name of the sequence to be created
  811.      * @param string    $start         start value of the sequence; default is 1
  812.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  813.      * @access public
  814.      */
  815.     function createSequence($seq_name$start = 1)
  816.     {
  817.         $db =$this->getDBInstance();
  818.         if (PEAR::isError($db)) {
  819.             return $db;
  820.         }
  821.  
  822.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  823.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  824.         $result $this->_verifyTableType($db->options['default_table_type']);
  825.         if (PEAR::isError($result)) {
  826.             return $result;
  827.         }
  828.  
  829.         $res $db->exec("CREATE TABLE $sequence_name".
  830.             "($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))".
  831.             (strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''')
  832.         );
  833.  
  834.         if (PEAR::isError($res)) {
  835.             return $res;
  836.         }
  837.  
  838.         if ($start == 1{
  839.             return MDB2_OK;
  840.         }
  841.  
  842.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  843.         if (!PEAR::isError($res)) {
  844.             return MDB2_OK;
  845.         }
  846.  
  847.         // Handle error
  848.         $result $db->exec("DROP TABLE $sequence_name");
  849.         if (PEAR::isError($result)) {
  850.             return $db->raiseError(MDB2_ERRORnullnull,
  851.                 'createSequence: could not drop inconsistent sequence table ('.
  852.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  853.         }
  854.  
  855.         return $db->raiseError(MDB2_ERRORnullnull,
  856.             'createSequence: could not create sequence table ('.
  857.             $res->getMessage().' ('.$res->getUserinfo().'))');
  858.     }
  859.  
  860.     // }}}
  861.     // {{{ dropSequence()
  862.  
  863.     /**
  864.      * drop existing sequence
  865.      *
  866.      * @param string    $seq_name     name of the sequence to be dropped
  867.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  868.      * @access public
  869.      */
  870.     function dropSequence($seq_name)
  871.     {
  872.         $db =$this->getDBInstance();
  873.         if (PEAR::isError($db)) {
  874.             return $db;
  875.         }
  876.  
  877.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  878.         return $db->exec("DROP TABLE $sequence_name");
  879.     }
  880.  
  881.     // }}}
  882.     // {{{ listSequences()
  883.  
  884.     /**
  885.      * list all sequences in the current database
  886.      *
  887.      * @return mixed data array on success, a MDB2 error on failure
  888.      * @access public
  889.      */
  890.     function listSequences()
  891.     {
  892.         $db =$this->getDBInstance();
  893.         if (PEAR::isError($db)) {
  894.             return $db;
  895.         }
  896.  
  897.         $table_names $db->queryCol('SHOW TABLES');
  898.         if (PEAR::isError($table_names)) {
  899.             return $table_names;
  900.         }
  901.  
  902.         $result = array();
  903.         for ($i = 0$j count($table_names)$i $j; ++$i{
  904.             if ($sqn $this->_isSequenceName($table_names[$i])) {
  905.                 $result[$sqn;
  906.             }
  907.         }
  908.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  909.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  910.         }
  911.         return $result;
  912.     }
  913.  
  914.     // }}}
  915. }
  916. ?>

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