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-2006 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.67 2006/04/16 09:48:05 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.      *                           array(
  198.      *                               'id' => array(
  199.      *                                   'type' => 'integer',
  200.      *                                   'unsigned' => 1
  201.      *                                   'notnull' => 1
  202.      *                                   'default' => 0
  203.      *                               ),
  204.      *                               'name' => array(
  205.      *                                   'type' => 'text',
  206.      *                                   'length' => 12
  207.      *                               ),
  208.      *                               'password' => array(
  209.      *                                   'type' => 'text',
  210.      *                                   'length' => 12
  211.      *                               )
  212.      *                           );
  213.      * @param array $options  An associative array of table options:
  214.      *                           array(
  215.      *                               'comment' => 'Foo',
  216.      *                               'character_set' => 'utf8',
  217.      *                               'collate' => 'utf8_unicode_ci',
  218.      *                               'collate' => 'utf8_unicode_ci',
  219.      *                               'type'    => 'innodb',
  220.      *                           );
  221.      *
  222.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  223.      * @access public
  224.      */
  225.     function createTable($name$fields$options = array())
  226.     {
  227.         $db =$this->getDBInstance();
  228.         if (PEAR::isError($db)) {
  229.             return $db;
  230.         }
  231.  
  232.         if (!$name{
  233.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  234.                 'createTable: no valid table name specified');
  235.         }
  236.         if (empty($fields)) {
  237.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  238.                 'createTable: no fields specified for table "'.$name.'"');
  239.         }
  240.         $query_fields $this->getFieldDeclarationList($fields);
  241.         if (PEAR::isError($query_fields)) {
  242.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  243.                 'createTable: '.$query_fields->getUserinfo());
  244.         }
  245.         $name $db->quoteIdentifier($nametrue);
  246.         $query = "CREATE TABLE $name ($query_fields)";
  247.  
  248.         $options_strings = array();
  249.  
  250.         if (array_key_exists('comment'$options)) {
  251.             $options_strings['comment''COMMENT = '.$db->quote($options['comment']'text');
  252.         }
  253.  
  254.         if (array_key_exists('charset'$options)) {
  255.             $options_strings['charset''DEFAULT CHARACTER SET '.$options['charset'];
  256.             if (array_key_exists('collate'$options)) {
  257.                 $options_strings['charset'].= ' COLLATE '.$options['collate'];
  258.             }
  259.         }
  260.  
  261.         $type = false;
  262.         if (array_key_exists('type'$options)) {
  263.             $type $options['type'];
  264.         elseif ($db->options['default_table_type']{
  265.             $type $db->options['default_table_type'];
  266.         }
  267.         if ($type{
  268.             $verify_type $this->_verifyTableType($type);
  269.             if (PEAR::isError($verify_type)) {
  270.                 return $verify_type;
  271.             }
  272.             $options_strings[= "TYPE $type";
  273.         }
  274.  
  275.         if (!empty($options_strings)) {
  276.             $query.= ' '.implode(' '$options_strings);
  277.         }
  278.         return $db->exec($query);
  279.     }
  280.  
  281.     // }}}
  282.     // {{{ alterTable()
  283.  
  284.     /**
  285.      * alter an existing table
  286.      *
  287.      * @param string $name         name of the table that is intended to be changed.
  288.      * @param array $changes     associative array that contains the details of each type
  289.      *                              of change that is intended to be performed. The types of
  290.      *                              changes that are currently supported are defined as follows:
  291.      *
  292.      *                              name
  293.      *
  294.      *                                 New name for the table.
  295.      *
  296.      *                             add
  297.      *
  298.      *                                 Associative array with the names of fields to be added as
  299.      *                                  indexes of the array. The value of each entry of the array
  300.      *                                  should be set to another associative array with the properties
  301.      *                                  of the fields to be added. The properties of the fields should
  302.      *                                  be the same as defined by the Metabase parser.
  303.      *
  304.      *
  305.      *                             remove
  306.      *
  307.      *                                 Associative array with the names of fields to be removed as indexes
  308.      *                                  of the array. Currently the values assigned to each entry are ignored.
  309.      *                                  An empty array should be used for future compatibility.
  310.      *
  311.      *                             rename
  312.      *
  313.      *                                 Associative array with the names of fields to be renamed as indexes
  314.      *                                  of the array. The value of each entry of the array should be set to
  315.      *                                  another associative array with the entry named name with the new
  316.      *                                  field name and the entry named Declaration that is expected to contain
  317.      *                                  the portion of the field declaration already in DBMS specific SQL code
  318.      *                                  as it is used in the CREATE TABLE statement.
  319.      *
  320.      *                             change
  321.      *
  322.      *                                 Associative array with the names of the fields to be changed as indexes
  323.      *                                  of the array. Keep in mind that if it is intended to change either the
  324.      *                                  name of a field and any other properties, the change array entries
  325.      *                                  should have the new names of the fields as array indexes.
  326.      *
  327.      *                                 The value of each entry of the array should be set to another associative
  328.      *                                  array with the properties of the fields to that are meant to be changed as
  329.      *                                  array entries. These entries should be assigned to the new values of the
  330.      *                                  respective properties. The properties of the fields should be the same
  331.      *                                  as defined by the Metabase parser.
  332.      *
  333.      *                             Example
  334.      *                                 array(
  335.      *                                     'name' => 'userlist',
  336.      *                                     'add' => array(
  337.      *                                         'quota' => array(
  338.      *                                             'type' => 'integer',
  339.      *                                             'unsigned' => 1
  340.      *                                         )
  341.      *                                     ),
  342.      *                                     'remove' => array(
  343.      *                                         'file_limit' => array(),
  344.      *                                         'time_limit' => array()
  345.      *                                     ),
  346.      *                                     'change' => array(
  347.      *                                         'name' => array(
  348.      *                                             'length' => '20',
  349.      *                                             'definition' => array(
  350.      *                                                 'type' => 'text',
  351.      *                                                 'length' => 20,
  352.      *                                             ),
  353.      *                                         )
  354.      *                                     ),
  355.      *                                     'rename' => array(
  356.      *                                         'sex' => array(
  357.      *                                             'name' => 'gender',
  358.      *                                             'definition' => array(
  359.      *                                                 'type' => 'text',
  360.      *                                                 'length' => 1,
  361.      *                                                 'default' => 'M',
  362.      *                                             ),
  363.      *                                         )
  364.      *                                     )
  365.      *                                 )
  366.      *
  367.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  368.      *                              can perform the requested table alterations if the value is true or
  369.      *                              actually perform them otherwise.
  370.      * @access public
  371.      *
  372.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  373.      */
  374.     function alterTable($name$changes$check)
  375.     {
  376.         $db =$this->getDBInstance();
  377.         if (PEAR::isError($db)) {
  378.             return $db;
  379.         }
  380.  
  381.         foreach ($changes as $change_name => $change{
  382.             switch ($change_name{
  383.             case 'add':
  384.             case 'remove':
  385.             case 'change':
  386.             case 'rename':
  387.             case 'name':
  388.                 break;
  389.             default:
  390.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  391.                     'alterTable: change type "'.$change_name.'" not yet supported');
  392.             }
  393.         }
  394.  
  395.         if ($check{
  396.             return MDB2_OK;
  397.         }
  398.  
  399.         $query '';
  400.         if (array_key_exists('name'$changes)) {
  401.             $change_name $db->quoteIdentifier($changes['name']true);
  402.             $query .= 'RENAME TO ' $change_name;
  403.         }
  404.  
  405.         if (array_key_exists('add'$changes)) {
  406.             foreach ($changes['add'as $field_name => $field{
  407.                 if ($query{
  408.                     $query.= ', ';
  409.                 }
  410.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  411.             }
  412.         }
  413.  
  414.         if (array_key_exists('remove'$changes)) {
  415.             foreach ($changes['remove'as $field_name => $field{
  416.                 if ($query{
  417.                     $query.= ', ';
  418.                 }
  419.                 $field_name $db->quoteIdentifier($field_nametrue);
  420.                 $query.= 'DROP ' $field_name;
  421.             }
  422.         }
  423.  
  424.         $rename = array();
  425.         if (array_key_exists('rename'$changes)) {
  426.             foreach ($changes['rename'as $field_name => $field{
  427.                 $rename[$field['name']] $field_name;
  428.             }
  429.         }
  430.  
  431.         if (array_key_exists('change'$changes)) {
  432.             foreach ($changes['change'as $field_name => $field{
  433.                 if ($query{
  434.                     $query.= ', ';
  435.                 }
  436.                 if (isset($rename[$field_name])) {
  437.                     $old_field_name $rename[$field_name];
  438.                     unset($rename[$field_name]);
  439.                 else {
  440.                     $old_field_name $field_name;
  441.                 }
  442.                 $old_field_name $db->quoteIdentifier($old_field_nametrue);
  443.                 $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type']$field_name$field['definition']);
  444.             }
  445.         }
  446.  
  447.         if (!empty($rename)) {
  448.             foreach ($rename as $rename_name => $renamed_field{
  449.                 if ($query{
  450.                     $query.= ', ';
  451.                 }
  452.                 $field $changes['rename'][$renamed_field];
  453.                 $renamed_field $db->quoteIdentifier($renamed_fieldtrue);
  454.                 $query.= 'CHANGE ' $renamed_field ' ' $db->getDeclaration($field['definition']['type']$field['name']$field['definition']);
  455.             }
  456.         }
  457.  
  458.         if (!$query{
  459.             return MDB2_OK;
  460.         }
  461.  
  462.         $name $db->quoteIdentifier($nametrue);
  463.         return $db->exec("ALTER TABLE $name $query");
  464.     }
  465.  
  466.     // }}}
  467.     // {{{ listDatabases()
  468.  
  469.     /**
  470.      * list all databases
  471.      *
  472.      * @return mixed data array on success, a MDB2 error on failure
  473.      * @access public
  474.      */
  475.     function listDatabases()
  476.     {
  477.         $db =$this->getDBInstance();
  478.         if (PEAR::isError($db)) {
  479.             return $db;
  480.         }
  481.  
  482.         $result $db->queryCol('SHOW DATABASES');
  483.         if (PEAR::isError($result)) {
  484.             return $result;
  485.         }
  486.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  487.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  488.         }
  489.         return $result;
  490.     }
  491.  
  492.     // }}}
  493.     // {{{ listUsers()
  494.  
  495.     /**
  496.      * list all users
  497.      *
  498.      * @return mixed data array on success, a MDB2 error on failure
  499.      * @access public
  500.      */
  501.     function listUsers()
  502.     {
  503.         $db =$this->getDBInstance();
  504.         if (PEAR::isError($db)) {
  505.             return $db;
  506.         }
  507.  
  508.         return $db->queryCol('SELECT DISTINCT USER FROM USER');
  509.     }
  510.  
  511.     // }}}
  512.     // {{{ listTables()
  513.  
  514.     /**
  515.      * list all tables in the current database
  516.      *
  517.      * @param string database, the current is default
  518.      * @return mixed data array on success, a MDB2 error on failure
  519.      * @access public
  520.      */
  521.     function listTables($database = null)
  522.     {
  523.         $db =$this->getDBInstance();
  524.         if (PEAR::isError($db)) {
  525.             return $db;
  526.         }
  527.  
  528.         $query "SHOW /*!50002 FULL*/ TABLES";
  529.         if(!is_null($database)) {
  530.             $query .= " FROM $database";
  531.         }
  532.  
  533.         $table_names $db->queryAll($querynullMDB2_FETCHMODE_ORDERED);
  534.         if (PEAR::isError($table_names)) {
  535.             return $table_names;
  536.         }
  537.  
  538.         $result = array();
  539.         foreach ($table_names as $table{
  540.             if (isset($table[1]&& strtolower($table[1]== 'view'{
  541.                 continue;
  542.             }
  543.             if (!$this->_fixSequenceName($table[0]true)) {
  544.                 $result[$table[0];
  545.             }
  546.         }
  547.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  548.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  549.         }
  550.         return $result;
  551.     }
  552.  
  553.     // }}}
  554.     // {{{ listViews()
  555.  
  556.     /**
  557.      * list the views in the database
  558.      *
  559.      * @param string database, the current is default
  560.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  561.      * @access public
  562.      ***/
  563.     function listViews($database = null)
  564.     {
  565.         $db =$this->getDBInstance();
  566.         if (PEAR::isError($db)) {
  567.             return $db;
  568.         }
  569.  
  570.         $query "SHOW FULL TABLES";
  571.         if(!is_null($database)) {
  572.             $query .= " FROM $database";
  573.         }
  574.  
  575.         $table_names $db->queryAll($querynullMDB2_FETCHMODE_ORDERED);
  576.         if (PEAR::isError($table_names)) {
  577.             return $table_names;
  578.         }
  579.  
  580.         $result = array();
  581.         foreach ($table_names as $table{
  582.             if (strtolower($table[1]== 'view'{
  583.                 $result[$table[0];
  584.             }
  585.         }
  586.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  587.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  588.         }
  589.         return $result;
  590.     }
  591.  
  592.     // }}}
  593.     // {{{ listTableFields()
  594.  
  595.     /**
  596.      * list all fields in a tables in the current database
  597.      *
  598.      * @param string $table name of table that should be used in method
  599.      * @return mixed data array on success, a MDB2 error on failure
  600.      * @access public
  601.      */
  602.     function listTableFields($table)
  603.     {
  604.         $db =$this->getDBInstance();
  605.         if (PEAR::isError($db)) {
  606.             return $db;
  607.         }
  608.  
  609.         $table $db->quoteIdentifier($tabletrue);
  610.         $result $db->queryCol("SHOW COLUMNS FROM $table");
  611.         if (PEAR::isError($result)) {
  612.             return $result;
  613.         }
  614.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  615.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  616.         }
  617.         return $result;
  618.     }
  619.  
  620.     // }}}
  621.     // {{{ createIndex()
  622.  
  623.     /**
  624.      * get the stucture of a field into an array
  625.      *
  626.      * @author Leoncx
  627.      * @param string    $table         name of the table on which the index is to be created
  628.      * @param string    $name         name of the index to be created
  629.      * @param array     $definition        associative array that defines properties of the index to be created.
  630.      *                                  Currently, only one property named FIELDS is supported. This property
  631.      *                                  is also an associative with the names of the index fields as array
  632.      *                                  indexes. Each entry of this array is set to another type of associative
  633.      *                                  array that specifies properties of the index that are specific to
  634.      *                                  each field.
  635.      *
  636.      *                                 Currently, only the sorting property is supported. It should be used
  637.      *                                  to define the sorting direction of the index. It may be set to either
  638.      *                                  ascending or descending.
  639.      *
  640.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  641.      *                                  drivers of those that do not support it ignore this property. Use the
  642.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  643.      *
  644.      *                                  Example
  645.      *                                     array(
  646.      *                                         'fields' => array(
  647.      *                                             'user_name' => array(
  648.      *                                                 'sorting' => 'ascending'
  649.      *                                                 'length' => 10
  650.      *                                             ),
  651.      *                                             'last_login' => array()
  652.      *                                         )
  653.      *                                     )
  654.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  655.      * @access public
  656.      */
  657.     function createIndex($table$name$definition)
  658.     {
  659.         $db =$this->getDBInstance();
  660.         if (PEAR::isError($db)) {
  661.             return $db;
  662.         }
  663.  
  664.         $table $db->quoteIdentifier($tabletrue);
  665.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  666.         $query = "CREATE INDEX $name ON $table";
  667.         $fields = array();
  668.         foreach ($definition['fields'as $field => $fieldinfo{
  669.             if (array_key_exists('length'$fieldinfo)) {
  670.                 $fields[$db->quoteIdentifier($fieldtrue'(' $fieldinfo['length'')';
  671.             else {
  672.                 $fields[$db->quoteIdentifier($fieldtrue);
  673.             }
  674.         }
  675.         $query .= ' ('implode(', '$fields')';
  676.         return $db->exec($query);
  677.     }
  678.  
  679.     // }}}
  680.     // {{{ dropIndex()
  681.  
  682.     /**
  683.      * drop existing index
  684.      *
  685.      * @param string    $table         name of table that should be used in method
  686.      * @param string    $name         name of the index to be dropped
  687.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  688.      * @access public
  689.      */
  690.     function dropIndex($table$name)
  691.     {
  692.         $db =$this->getDBInstance();
  693.         if (PEAR::isError($db)) {
  694.             return $db;
  695.         }
  696.  
  697.         $table $db->quoteIdentifier($tabletrue);
  698.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  699.         return $db->exec("DROP INDEX $name ON $table");
  700.     }
  701.  
  702.     // }}}
  703.     // {{{ listTableIndexes()
  704.  
  705.     /**
  706.      * list all indexes in a table
  707.      *
  708.      * @param string    $table      name of table that should be used in method
  709.      * @return mixed data array on success, a MDB2 error on failure
  710.      * @access public
  711.      */
  712.     function listTableIndexes($table)
  713.     {
  714.         $db =$this->getDBInstance();
  715.         if (PEAR::isError($db)) {
  716.             return $db;
  717.         }
  718.  
  719.         $key_name 'Key_name';
  720.         $non_unique 'Non_unique';
  721.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  722.             if ($db->options['field_case'== CASE_LOWER{
  723.                 $key_name strtolower($key_name);
  724.                 $non_unique strtolower($non_unique);
  725.             else {
  726.                 $key_name strtoupper($key_name);
  727.                 $non_unique strtoupper($non_unique);
  728.             }
  729.         }
  730.  
  731.         $table $db->quoteIdentifier($tabletrue);
  732.         $query = "SHOW INDEX FROM $table";
  733.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  734.         if (PEAR::isError($indexes)) {
  735.             return $indexes;
  736.         }
  737.  
  738.         $result = array();
  739.         foreach ($indexes as $index_data{
  740.             if ($index_data[$non_unique]{
  741.                 $result[$this->_fixIndexName($index_data[$key_name])= true;
  742.             }
  743.         }
  744.  
  745.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  746.             $result array_change_key_case($result$db->options['field_case']);
  747.         }
  748.         return array_keys($result);
  749.     }
  750.  
  751.     // }}}
  752.     // {{{ createConstraint()
  753.  
  754.     /**
  755.      * create a constraint on a table
  756.      *
  757.      * @param string    $table         name of the table on which the constraint is to be created
  758.      * @param string    $name         name of the constraint to be created
  759.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  760.      *                                  Currently, only one property named FIELDS is supported. This property
  761.      *                                  is also an associative with the names of the constraint fields as array
  762.      *                                  constraints. Each entry of this array is set to another type of associative
  763.      *                                  array that specifies properties of the constraint that are specific to
  764.      *                                  each field.
  765.      *
  766.      *                                  Example
  767.      *                                     array(
  768.      *                                         'fields' => array(
  769.      *                                             'user_name' => array(),
  770.      *                                             'last_login' => array()
  771.      *                                         )
  772.      *                                     )
  773.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  774.      * @access public
  775.      */
  776.     function createConstraint($table$name$definition)
  777.     {
  778.         $db =$this->getDBInstance();
  779.         if (PEAR::isError($db)) {
  780.             return $db;
  781.         }
  782.  
  783.         $type '';
  784.         if (array_key_exists('primary'$definition&& $definition['primary']{
  785.             if (strtolower($name!= 'primary'{
  786.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  787.                     'Primary Key may must be named primary');
  788.             }
  789.             $type 'PRIMARY';
  790.             $name 'KEY';
  791.         else {
  792.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  793.             if (array_key_exists('unique'$definition&& $definition['unique']{
  794.                 $type 'UNIQUE';
  795.             }
  796.         }
  797.  
  798.         $table $db->quoteIdentifier($tabletrue);
  799.         $query = "ALTER TABLE $table ADD $type $name";
  800.         $fields = array();
  801.         foreach (array_keys($definition['fields']as $field{
  802.             $fields[$db->quoteIdentifier($fieldtrue);
  803.         }
  804.         $query .= ' ('implode(', '$fields')';
  805.         return $db->exec($query);
  806.     }
  807.  
  808.     // }}}
  809.     // {{{ dropConstraint()
  810.  
  811.     /**
  812.      * drop existing constraint
  813.      *
  814.      * @param string    $table         name of table that should be used in method
  815.      * @param string    $name         name of the constraint to be dropped
  816.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  817.      * @access public
  818.      */
  819.     function dropConstraint($table$name)
  820.     {
  821.         $db =$this->getDBInstance();
  822.         if (PEAR::isError($db)) {
  823.             return $db;
  824.         }
  825.  
  826.         $table $db->quoteIdentifier($tabletrue);
  827.         if (strtolower($name== 'primary'{
  828.             $query = "ALTER TABLE $table DROP PRIMARY KEY";
  829.         else {
  830.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  831.             $query = "ALTER TABLE $table DROP INDEX $name";
  832.         }
  833.         return $db->exec($query);
  834.     }
  835.  
  836.     // }}}
  837.     // {{{ listTableConstraints()
  838.  
  839.     /**
  840.      * list all sonstraints in a table
  841.      *
  842.      * @param string    $table      name of table that should be used in method
  843.      * @return mixed data array on success, a MDB2 error on failure
  844.      * @access public
  845.      */
  846.     function listTableConstraints($table)
  847.     {
  848.         $db =$this->getDBInstance();
  849.         if (PEAR::isError($db)) {
  850.             return $db;
  851.         }
  852.  
  853.         $key_name 'Key_name';
  854.         $non_unique 'Non_unique';
  855.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  856.             if ($db->options['field_case'== CASE_LOWER{
  857.                 $key_name strtolower($key_name);
  858.                 $non_unique strtolower($non_unique);
  859.             else {
  860.                 $key_name strtoupper($key_name);
  861.                 $non_unique strtoupper($non_unique);
  862.             }
  863.         }
  864.  
  865.         $table $db->quoteIdentifier($tabletrue);
  866.         $query = "SHOW INDEX FROM $table";
  867.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  868.         if (PEAR::isError($indexes)) {
  869.             return $indexes;
  870.         }
  871.  
  872.         $result = array();
  873.         foreach ($indexes as $index_data{
  874.             if (!$index_data[$non_unique]{
  875.                 if ($index_data[$key_name!== 'PRIMARY'{
  876.                     $index $this->_fixIndexName($index_data[$key_name]);
  877.                 else {
  878.                     $index 'PRIMARY';
  879.                 }
  880.                 $result[$index= true;
  881.             }
  882.         }
  883.  
  884.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  885.             $result array_change_key_case($result$db->options['field_case']);
  886.         }
  887.         return array_keys($result);
  888.     }
  889.  
  890.     // }}}
  891.     // {{{ createSequence()
  892.  
  893.     /**
  894.      * create sequence
  895.      *
  896.      * @param string    $seq_name     name of the sequence to be created
  897.      * @param string    $start         start value of the sequence; default is 1
  898.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  899.      * @access public
  900.      */
  901.     function createSequence($seq_name$start = 1)
  902.     {
  903.         $db =$this->getDBInstance();
  904.         if (PEAR::isError($db)) {
  905.             return $db;
  906.         }
  907.  
  908.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  909.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  910.         $result $this->_verifyTableType($db->options['default_table_type']);
  911.         if (PEAR::isError($result)) {
  912.             return $result;
  913.         }
  914.  
  915.         $res $db->exec("CREATE TABLE $sequence_name".
  916.             "($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))".
  917.             (strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''')
  918.         );
  919.  
  920.         if (PEAR::isError($res)) {
  921.             return $res;
  922.         }
  923.  
  924.         if ($start == 1{
  925.             return MDB2_OK;
  926.         }
  927.  
  928.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  929.         if (!PEAR::isError($res)) {
  930.             return MDB2_OK;
  931.         }
  932.  
  933.         // Handle error
  934.         $result $db->exec("DROP TABLE $sequence_name");
  935.         if (PEAR::isError($result)) {
  936.             return $db->raiseError(MDB2_ERRORnullnull,
  937.                 'createSequence: could not drop inconsistent sequence table ('.
  938.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  939.         }
  940.  
  941.         return $db->raiseError(MDB2_ERRORnullnull,
  942.             'createSequence: could not create sequence table ('.
  943.             $res->getMessage().' ('.$res->getUserinfo().'))');
  944.     }
  945.  
  946.     // }}}
  947.     // {{{ dropSequence()
  948.  
  949.     /**
  950.      * drop existing sequence
  951.      *
  952.      * @param string    $seq_name     name of the sequence to be dropped
  953.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  954.      * @access public
  955.      */
  956.     function dropSequence($seq_name)
  957.     {
  958.         $db =$this->getDBInstance();
  959.         if (PEAR::isError($db)) {
  960.             return $db;
  961.         }
  962.  
  963.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  964.         return $db->exec("DROP TABLE $sequence_name");
  965.     }
  966.  
  967.     // }}}
  968.     // {{{ listSequences()
  969.  
  970.     /**
  971.      * list all sequences in the current database
  972.      *
  973.      * @param string database, the current is default
  974.      * @return mixed data array on success, a MDB2 error on failure
  975.      * @access public
  976.      */
  977.     function listSequences($database = null)
  978.     {
  979.         $db =$this->getDBInstance();
  980.         if (PEAR::isError($db)) {
  981.             return $db;
  982.         }
  983.  
  984.         $query "SHOW TABLES";
  985.         if(!is_null($database)) {
  986.             $query .= " FROM $database";
  987.         }
  988.         $table_names $db->queryCol($query);
  989.         if (PEAR::isError($table_names)) {
  990.             return $table_names;
  991.         }
  992.  
  993.         $result = array();
  994.         foreach ($table_names as $table_name{
  995.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  996.                 $result[$sqn;
  997.             }
  998.         }
  999.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1000.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  1001.         }
  1002.         return $result;
  1003.     }
  1004.  
  1005.     // }}}
  1006. }
  1007. ?>

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