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

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