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

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