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

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