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

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