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.74 2006/05/04 09:12:16 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.         $query.= "/*!50002  WHERE Table_type = 'BASE TABLE'*/";
  533.  
  534.         $table_names $db->queryAll($querynullMDB2_FETCHMODE_ORDERED);
  535.         if (PEAR::isError($table_names)) {
  536.             return $table_names;
  537.         }
  538.  
  539.         $result = array();
  540.         foreach ($table_names as $table{
  541.             if (!$this->_fixSequenceName($table[0]true)) {
  542.                 $result[$table[0];
  543.             }
  544.         }
  545.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  546.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  547.         }
  548.         return $result;
  549.     }
  550.  
  551.     // }}}
  552.     // {{{ listViews()
  553.  
  554.     /**
  555.      * list the views in the database
  556.      *
  557.      * @param string database, the current is default
  558.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  559.      * @access public
  560.      ***/
  561.     function listViews($database = null)
  562.     {
  563.         $db =$this->getDBInstance();
  564.         if (PEAR::isError($db)) {
  565.             return $db;
  566.         }
  567.  
  568.         $query 'SHOW FULL TABLES';
  569.         if (!is_null($database)) {
  570.             $query.= " FROM $database";
  571.         }
  572.         $query.= " WHERE Table_type = 'VIEW'";
  573.  
  574.         $result $db->queryCol($query);
  575.         if (PEAR::isError($result)) {
  576.             return $result;
  577.         }
  578.  
  579.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  580.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  581.         }
  582.         return $result;
  583.     }
  584.  
  585.     // }}}
  586.     // {{{ listTableFields()
  587.  
  588.     /**
  589.      * list all fields in a tables in the current database
  590.      *
  591.      * @param string $table name of table that should be used in method
  592.      * @return mixed data array on success, a MDB2 error on failure
  593.      * @access public
  594.      */
  595.     function listTableFields($table)
  596.     {
  597.         $db =$this->getDBInstance();
  598.         if (PEAR::isError($db)) {
  599.             return $db;
  600.         }
  601.  
  602.         $table $db->quoteIdentifier($tabletrue);
  603.         $result $db->queryCol("SHOW COLUMNS FROM $table");
  604.         if (PEAR::isError($result)) {
  605.             return $result;
  606.         }
  607.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  608.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  609.         }
  610.         return $result;
  611.     }
  612.  
  613.     // }}}
  614.     // {{{ createIndex()
  615.  
  616.     /**
  617.      * Get the stucture of a field into an array
  618.      *
  619.      * @author Leoncx
  620.      * @param string    $table         name of the table on which the index is to be created
  621.      * @param string    $name         name of the index to be created
  622.      * @param array     $definition        associative array that defines properties of the index to be created.
  623.      *                                  Currently, only one property named FIELDS is supported. This property
  624.      *                                  is also an associative with the names of the index fields as array
  625.      *                                  indexes. Each entry of this array is set to another type of associative
  626.      *                                  array that specifies properties of the index that are specific to
  627.      *                                  each field.
  628.      *
  629.      *                                 Currently, only the sorting property is supported. It should be used
  630.      *                                  to define the sorting direction of the index. It may be set to either
  631.      *                                  ascending or descending.
  632.      *
  633.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  634.      *                                  drivers of those that do not support it ignore this property. Use the
  635.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  636.      *
  637.      *                                  Example
  638.      *                                     array(
  639.      *                                         'fields' => array(
  640.      *                                             'user_name' => array(
  641.      *                                                 'sorting' => 'ascending'
  642.      *                                                 'length' => 10
  643.      *                                             ),
  644.      *                                             'last_login' => array()
  645.      *                                         )
  646.      *                                     )
  647.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  648.      * @access public
  649.      */
  650.     function createIndex($table$name$definition)
  651.     {
  652.         $db =$this->getDBInstance();
  653.         if (PEAR::isError($db)) {
  654.             return $db;
  655.         }
  656.  
  657.         $table $db->quoteIdentifier($tabletrue);
  658.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  659.         $query = "CREATE INDEX $name ON $table";
  660.         $fields = array();
  661.         foreach ($definition['fields'as $field => $fieldinfo{
  662.             if (array_key_exists('length'$fieldinfo)) {
  663.                 $fields[$db->quoteIdentifier($fieldtrue'(' $fieldinfo['length'')';
  664.             else {
  665.                 $fields[$db->quoteIdentifier($fieldtrue);
  666.             }
  667.         }
  668.         $query .= ' ('implode(', '$fields')';
  669.         return $db->exec($query);
  670.     }
  671.  
  672.     // }}}
  673.     // {{{ dropIndex()
  674.  
  675.     /**
  676.      * drop existing index
  677.      *
  678.      * @param string    $table         name of table that should be used in method
  679.      * @param string    $name         name of the index to be dropped
  680.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  681.      * @access public
  682.      */
  683.     function dropIndex($table$name)
  684.     {
  685.         $db =$this->getDBInstance();
  686.         if (PEAR::isError($db)) {
  687.             return $db;
  688.         }
  689.  
  690.         $table $db->quoteIdentifier($tabletrue);
  691.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  692.         return $db->exec("DROP INDEX $name ON $table");
  693.     }
  694.  
  695.     // }}}
  696.     // {{{ listTableIndexes()
  697.  
  698.     /**
  699.      * list all indexes in a table
  700.      *
  701.      * @param string    $table      name of table that should be used in method
  702.      * @return mixed data array on success, a MDB2 error on failure
  703.      * @access public
  704.      */
  705.     function listTableIndexes($table)
  706.     {
  707.         $db =$this->getDBInstance();
  708.         if (PEAR::isError($db)) {
  709.             return $db;
  710.         }
  711.  
  712.         $key_name 'Key_name';
  713.         $non_unique 'Non_unique';
  714.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  715.             if ($db->options['field_case'== CASE_LOWER{
  716.                 $key_name strtolower($key_name);
  717.                 $non_unique strtolower($non_unique);
  718.             else {
  719.                 $key_name strtoupper($key_name);
  720.                 $non_unique strtoupper($non_unique);
  721.             }
  722.         }
  723.  
  724.         $table $db->quoteIdentifier($tabletrue);
  725.         $query = "SHOW INDEX FROM $table";
  726.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  727.         if (PEAR::isError($indexes)) {
  728.             return $indexes;
  729.         }
  730.  
  731.         $result = array();
  732.         foreach ($indexes as $index_data{
  733.             if ($index_data[$non_unique]{
  734.                 $result[$this->_fixIndexName($index_data[$key_name])= true;
  735.             }
  736.         }
  737.  
  738.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  739.             $result array_change_key_case($result$db->options['field_case']);
  740.         }
  741.         return array_keys($result);
  742.     }
  743.  
  744.     // }}}
  745.     // {{{ createConstraint()
  746.  
  747.     /**
  748.      * create a constraint on a table
  749.      *
  750.      * @param string    $table         name of the table on which the constraint is to be created
  751.      * @param string    $name         name of the constraint to be created
  752.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  753.      *                                  Currently, only one property named FIELDS is supported. This property
  754.      *                                  is also an associative with the names of the constraint fields as array
  755.      *                                  constraints. Each entry of this array is set to another type of associative
  756.      *                                  array that specifies properties of the constraint that are specific to
  757.      *                                  each field.
  758.      *
  759.      *                                  Example
  760.      *                                     array(
  761.      *                                         'fields' => array(
  762.      *                                             'user_name' => array(),
  763.      *                                             'last_login' => array()
  764.      *                                         )
  765.      *                                     )
  766.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  767.      * @access public
  768.      */
  769.     function createConstraint($table$name$definition)
  770.     {
  771.         $db =$this->getDBInstance();
  772.         if (PEAR::isError($db)) {
  773.             return $db;
  774.         }
  775.  
  776.         $type '';
  777.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  778.         if (array_key_exists('primary'$definition&& $definition['primary']{
  779.             $type 'PRIMARY';
  780.             $name 'KEY';
  781.         elseif (array_key_exists('unique'$definition&& $definition['unique']{
  782.             $type 'UNIQUE';
  783.         }
  784.  
  785.         $table $db->quoteIdentifier($tabletrue);
  786.         $query = "ALTER TABLE $table ADD $type $name";
  787.         $fields = array();
  788.         foreach (array_keys($definition['fields']as $field{
  789.             $fields[$db->quoteIdentifier($fieldtrue);
  790.         }
  791.         $query .= ' ('implode(', '$fields')';
  792.         return $db->exec($query);
  793.     }
  794.  
  795.     // }}}
  796.     // {{{ dropConstraint()
  797.  
  798.     /**
  799.      * drop existing constraint
  800.      *
  801.      * @param string    $table        name of table that should be used in method
  802.      * @param string    $name         name of the constraint to be dropped
  803.      * @param string    $primary      hint if the constraint is primary
  804.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  805.      * @access public
  806.      */
  807.     function dropConstraint($table$name$primary = false)
  808.     {
  809.         $db =$this->getDBInstance();
  810.         if (PEAR::isError($db)) {
  811.             return $db;
  812.         }
  813.  
  814.         $table $db->quoteIdentifier($tabletrue);
  815.         if ($primary || strtolower($name== 'primary'{
  816.             $query = "ALTER TABLE $table DROP PRIMARY KEY";
  817.         else {
  818.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  819.             $query = "ALTER TABLE $table DROP INDEX $name";
  820.         }
  821.         return $db->exec($query);
  822.     }
  823.  
  824.     // }}}
  825.     // {{{ listTableConstraints()
  826.  
  827.     /**
  828.      * list all sonstraints in a table
  829.      *
  830.      * @param string    $table      name of table that should be used in method
  831.      * @return mixed data array on success, a MDB2 error on failure
  832.      * @access public
  833.      */
  834.     function listTableConstraints($table)
  835.     {
  836.         $db =$this->getDBInstance();
  837.         if (PEAR::isError($db)) {
  838.             return $db;
  839.         }
  840.  
  841.         $key_name 'Key_name';
  842.         $non_unique 'Non_unique';
  843.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  844.             if ($db->options['field_case'== CASE_LOWER{
  845.                 $key_name strtolower($key_name);
  846.                 $non_unique strtolower($non_unique);
  847.             else {
  848.                 $key_name strtoupper($key_name);
  849.                 $non_unique strtoupper($non_unique);
  850.             }
  851.         }
  852.  
  853.         $table $db->quoteIdentifier($tabletrue);
  854.         $query = "SHOW INDEX FROM $table";
  855.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  856.         if (PEAR::isError($indexes)) {
  857.             return $indexes;
  858.         }
  859.  
  860.         $result = array();
  861.         foreach ($indexes as $index_data{
  862.             if (!$index_data[$non_unique]{
  863.                 if ($index_data[$key_name!== 'PRIMARY'{
  864.                     $index $this->_fixIndexName($index_data[$key_name]);
  865.                 else {
  866.                     $index 'PRIMARY';
  867.                 }
  868.                 $result[$index= true;
  869.             }
  870.         }
  871.  
  872.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  873.             $result array_change_key_case($result$db->options['field_case']);
  874.         }
  875.         return array_keys($result);
  876.     }
  877.  
  878.     // }}}
  879.     // {{{ createSequence()
  880.  
  881.     /**
  882.      * create sequence
  883.      *
  884.      * @param string    $seq_name     name of the sequence to be created
  885.      * @param string    $start         start value of the sequence; default is 1
  886.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  887.      * @access public
  888.      */
  889.     function createSequence($seq_name$start = 1)
  890.     {
  891.         $db =$this->getDBInstance();
  892.         if (PEAR::isError($db)) {
  893.             return $db;
  894.         }
  895.  
  896.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  897.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  898.         $result $this->_verifyTableType($db->options['default_table_type']);
  899.         if (PEAR::isError($result)) {
  900.             return $result;
  901.         }
  902.  
  903.         $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
  904.         $query.= strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''';
  905.         $res $db->exec($query);
  906.  
  907.         if (PEAR::isError($res)) {
  908.             return $res;
  909.         }
  910.  
  911.         if ($start == 1{
  912.             return MDB2_OK;
  913.         }
  914.  
  915.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
  916.         $res $db->exec($query);
  917.         if (!PEAR::isError($res)) {
  918.             return MDB2_OK;
  919.         }
  920.  
  921.         // Handle error
  922.         $result $db->exec("DROP TABLE $sequence_name");
  923.         if (PEAR::isError($result)) {
  924.             return $db->raiseError($resultnullnull,
  925.                 'createSequence: could not drop inconsistent sequence table');
  926.         }
  927.  
  928.         return $db->raiseError($resnullnull,
  929.             'createSequence: could not create sequence table');
  930.     }
  931.  
  932.     // }}}
  933.     // {{{ dropSequence()
  934.  
  935.     /**
  936.      * drop existing sequence
  937.      *
  938.      * @param string    $seq_name     name of the sequence to be dropped
  939.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  940.      * @access public
  941.      */
  942.     function dropSequence($seq_name)
  943.     {
  944.         $db =$this->getDBInstance();
  945.         if (PEAR::isError($db)) {
  946.             return $db;
  947.         }
  948.  
  949.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  950.         return $db->exec("DROP TABLE $sequence_name");
  951.     }
  952.  
  953.     // }}}
  954.     // {{{ listSequences()
  955.  
  956.     /**
  957.      * list all sequences in the current database
  958.      *
  959.      * @param string database, the current is default
  960.      * @return mixed data array on success, a MDB2 error on failure
  961.      * @access public
  962.      */
  963.     function listSequences($database = null)
  964.     {
  965.         $db =$this->getDBInstance();
  966.         if (PEAR::isError($db)) {
  967.             return $db;
  968.         }
  969.  
  970.         $query "SHOW TABLES";
  971.         if (!is_null($database)) {
  972.             $query .= " FROM $database";
  973.         }
  974.         $table_names $db->queryCol($query);
  975.         if (PEAR::isError($table_names)) {
  976.             return $table_names;
  977.         }
  978.  
  979.         $result = array();
  980.         foreach ($table_names as $table_name{
  981.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  982.                 $result[$sqn;
  983.             }
  984.         }
  985.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  986.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  987.         }
  988.         return $result;
  989.     }
  990.  
  991.     // }}}
  992. }
  993. ?>

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