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.66 2007/03/04 23:40:51 quipo Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 MySQL driver for the schema reverse engineering module
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@pooteeweet.org>
  56.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  57.  */
  58. class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
  59. {
  60.     // {{{ getTableFieldDefinition()
  61.  
  62.     /**
  63.      * Get the structure of a field into an array
  64.      *
  65.      * @param string    $table       name of table that should be used in method
  66.      * @param string    $field_name  name of field that should be used in method
  67.      * @return mixed data array on success, a MDB2 error on failure
  68.      * @access public
  69.      */
  70.     function getTableFieldDefinition($table$field_name)
  71.     {
  72.         $db =$this->getDBInstance();
  73.         if (PEAR::isError($db)) {
  74.             return $db;
  75.         }
  76.  
  77.         $result $db->loadModule('Datatype'nulltrue);
  78.         if (PEAR::isError($result)) {
  79.             return $result;
  80.         }
  81.         $table $db->quoteIdentifier($tabletrue);
  82.         $query = "SHOW COLUMNS FROM $table LIKE ".$db->quote($field_name);
  83.         $columns $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  84.         if (PEAR::isError($columns)) {
  85.             return $columns;
  86.         }
  87.         foreach ($columns as $column{
  88.             $column array_change_key_case($columnCASE_LOWER);
  89.             $column['name'$column['field'];
  90.             unset($column['field']);
  91.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  92.                 if ($db->options['field_case'== CASE_LOWER{
  93.                     $column['name'strtolower($column['name']);
  94.                 else {
  95.                     $column['name'strtoupper($column['name']);
  96.                 }
  97.             else {
  98.                 $column array_change_key_case($column$db->options['field_case']);
  99.             }
  100.             if ($field_name == $column['name']{
  101.                 $mapped_datatype $db->datatype->mapNativeDatatype($column);
  102.                 if (PEAR::IsError($mapped_datatype)) {
  103.                     return $mapped_datatype;
  104.                 }
  105.                 list($types$length$unsigned$fixed$mapped_datatype;
  106.                 $notnull = false;
  107.                 if (empty($column['null']|| $column['null'!== 'YES'{
  108.                     $notnull = true;
  109.                 }
  110.                 $default = false;
  111.                 if (array_key_exists('default'$column)) {
  112.                     $default $column['default'];
  113.                     if (is_null($default&& $notnull{
  114.                         $default '';
  115.                     }
  116.                 }
  117.                 $autoincrement = false;
  118.                 if (!empty($column['extra']&& $column['extra'== 'auto_increment'{
  119.                     $autoincrement = true;
  120.                 }
  121.  
  122.                 $definition[0= array(
  123.                     'notnull' => $notnull,
  124.                     'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i''\\1'$column['type'])
  125.                 );
  126.                 if (!is_null($length)) {
  127.                     $definition[0]['length'$length;
  128.                 }
  129.                 if (!is_null($unsigned)) {
  130.                     $definition[0]['unsigned'$unsigned;
  131.                 }
  132.                 if (!is_null($fixed)) {
  133.                     $definition[0]['fixed'$fixed;
  134.                 }
  135.                 if ($default !== false{
  136.                     $definition[0]['default'$default;
  137.                 }
  138.                 if ($autoincrement !== false{
  139.                     $definition[0]['autoincrement'$autoincrement;
  140.                 }
  141.                 foreach ($types as $key => $type{
  142.                     $definition[$key$definition[0];
  143.                     if ($type == 'clob' || $type == 'blob'{
  144.                         unset($definition[$key]['default']);
  145.                     }
  146.                     $definition[$key]['type'$type;
  147.                     $definition[$key]['mdb2type'$type;
  148.                 }
  149.                 return $definition;
  150.             }
  151.         }
  152.  
  153.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  154.             'it was not specified an existing table column'__FUNCTION__);
  155.     }
  156.  
  157.     // }}}
  158.     // {{{ getTableIndexDefinition()
  159.  
  160.     /**
  161.      * Get the structure of an index into an array
  162.      *
  163.      * @param string    $table      name of table that should be used in method
  164.      * @param string    $index_name name of index that should be used in method
  165.      * @return mixed data array on success, a MDB2 error on failure
  166.      * @access public
  167.      */
  168.     function getTableIndexDefinition($table$index_name)
  169.     {
  170.         $db =$this->getDBInstance();
  171.         if (PEAR::isError($db)) {
  172.             return $db;
  173.         }
  174.  
  175.         $table $db->quoteIdentifier($tabletrue);
  176.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  177.         $index_name_mdb2 $db->getIndexName($index_name);
  178.         $result $db->queryRow(sprintf($query$db->quote($index_name_mdb2)));
  179.         if (!PEAR::isError($result&& !is_null($result)) {
  180.             // apply 'idxname_format' only if the query succeeded, otherwise
  181.             // fallback to the given $index_name, without transformation
  182.             $index_name $index_name_mdb2;
  183.         }
  184.         $result $db->query(sprintf($query$db->quote($index_name)));
  185.         if (PEAR::isError($result)) {
  186.             return $result;
  187.         }
  188.         $definition = array();
  189.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  190.             $row array_change_key_case($rowCASE_LOWER);
  191.             $key_name $row['key_name'];
  192.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  193.                 if ($db->options['field_case'== CASE_LOWER{
  194.                     $key_name strtolower($key_name);
  195.                 else {
  196.                     $key_name strtoupper($key_name);
  197.                 }
  198.             }
  199.             if ($index_name == $key_name{
  200.                 if (!$row['non_unique']{
  201.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  202.                         'it was not specified an existing table index'__FUNCTION__);
  203.                 }
  204.                 $column_name $row['column_name'];
  205.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  206.                     if ($db->options['field_case'== CASE_LOWER{
  207.                         $column_name strtolower($column_name);
  208.                     else {
  209.                         $column_name strtoupper($column_name);
  210.                     }
  211.                 }
  212.                 $definition['fields'][$column_name= array();
  213.                 if (!empty($row['collation'])) {
  214.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  215.                         ? 'ascending' 'descending');
  216.                 }
  217.             }
  218.         }
  219.         $result->free();
  220.         if (empty($definition['fields'])) {
  221.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  222.                 'it was not specified an existing table index'__FUNCTION__);
  223.         }
  224.         return $definition;
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ getTableConstraintDefinition()
  229.  
  230.     /**
  231.      * Get the structure of a constraint into an array
  232.      *
  233.      * @param string    $table      name of table that should be used in method
  234.      * @param string    $index_name name of index that should be used in method
  235.      * @return mixed data array on success, a MDB2 error on failure
  236.      * @access public
  237.      */
  238.     function getTableConstraintDefinition($table$index_name)
  239.     {
  240.         $db =$this->getDBInstance();
  241.         if (PEAR::isError($db)) {
  242.             return $db;
  243.         }
  244.  
  245.         $table $db->quoteIdentifier($tabletrue);
  246.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  247.         if (strtolower($index_name!= 'primary'{
  248.             $index_name_mdb2 $db->getIndexName($index_name);
  249.             $result $db->queryRow(sprintf($query$db->quote($index_name_mdb2)));
  250.             if (!PEAR::isError($result&& !is_null($result)) {
  251.                 // apply 'idxname_format' only if the query succeeded, otherwise
  252.                 // fallback to the given $index_name, without transformation
  253.                 $index_name $index_name_mdb2;
  254.             }
  255.         }
  256.         $result $db->query(sprintf($query$db->quote($index_name)));
  257.         if (PEAR::isError($result)) {
  258.             return $result;
  259.         }
  260.         $definition = array();
  261.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  262.             $row array_change_key_case($rowCASE_LOWER);
  263.             $key_name $row['key_name'];
  264.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  265.                 if ($db->options['field_case'== CASE_LOWER{
  266.                     $key_name strtolower($key_name);
  267.                 else {
  268.                     $key_name strtoupper($key_name);
  269.                 }
  270.             }
  271.             if ($index_name == $key_name{
  272.                 if ($row['non_unique']{
  273.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  274.                         'it was not specified an existing table constraint'__FUNCTION__);
  275.                 }
  276.                 if ($row['key_name'== 'PRIMARY'{
  277.                     $definition['primary'= true;
  278.                 else {
  279.                     $definition['unique'= true;
  280.                 }
  281.                 $column_name $row['column_name'];
  282.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  283.                     if ($db->options['field_case'== CASE_LOWER{
  284.                         $column_name strtolower($column_name);
  285.                     else {
  286.                         $column_name strtoupper($column_name);
  287.                     }
  288.                 }
  289.                 $definition['fields'][$column_name= array();
  290.                 if (!empty($row['collation'])) {
  291.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  292.                         ? 'ascending' 'descending');
  293.                 }
  294.             }
  295.         }
  296.         $result->free();
  297.         if (empty($definition['fields'])) {
  298.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  299.                 'it was not specified an existing table constraint'__FUNCTION__);
  300.         }
  301.         return $definition;
  302.     }
  303.  
  304.     // }}}
  305.     // {{{ getTriggerDefinition()
  306.  
  307.     /**
  308.      * Get the structure of a trigger into an array
  309.      *
  310.      * EXPERIMENTAL
  311.      *
  312.      * WARNING: this function is experimental and may change the returned value
  313.      * at any time until labelled as non-experimental
  314.      *
  315.      * @param string    $trigger    name of trigger that should be used in method
  316.      * @return mixed data array on success, a MDB2 error on failure
  317.      * @access public
  318.      */
  319.     function getTriggerDefinition($trigger)
  320.     {
  321.         $db =$this->getDBInstance();
  322.         if (PEAR::isError($db)) {
  323.             return $db;
  324.         }
  325.  
  326.         $query 'SELECT trigger_name,
  327.                          event_object_table AS table_name,
  328.                          action_statement AS trigger_body,
  329.                          action_timing AS trigger_type,
  330.                          event_manipulation AS trigger_event
  331.                     FROM information_schema.triggers
  332.                    WHERE trigger_name = '$db->quote($trigger'text');
  333.         $types = array(
  334.             'trigger_name'    => 'text',
  335.             'table_name'      => 'text',
  336.             'trigger_body'    => 'text',
  337.             'trigger_type'    => 'text',
  338.             'trigger_event'   => 'text',
  339.         );
  340.         $def $db->queryRow($query$typesMDB2_FETCHMODE_ASSOC);
  341.         if (PEAR::isError($def)) {
  342.             return $def;
  343.         }
  344.         $def['trigger_comment''';
  345.         $def['trigger_enabled'= true;
  346.         return $def;
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ tableInfo()
  351.  
  352.     /**
  353.      * Returns information about a table or a result set
  354.      *
  355.      * @param object|string $result  MDB2_result object from a query or a
  356.      *                                  string containing the name of a table.
  357.      *                                  While this also accepts a query result
  358.      *                                  resource identifier, this behavior is
  359.      *                                  deprecated.
  360.      * @param int            $mode    a valid tableInfo mode
  361.      *
  362.      * @return array  an associative array with the information requested.
  363.      *                  A MDB2_Error object on failure.
  364.      *
  365.      * @see MDB2_Driver_Common::setOption()
  366.      */
  367.     function tableInfo($result$mode = null)
  368.     {
  369.         if (is_string($result)) {
  370.            return parent::tableInfo($result$mode);
  371.         }
  372.  
  373.         $db =$this->getDBInstance();
  374.         if (PEAR::isError($db)) {
  375.             return $db;
  376.         }
  377.  
  378.         $resource = MDB2::isResultCommon($result$result->getResource($result;
  379.         if (!is_resource($resource)) {
  380.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  381.                 'Could not generate result resource'__FUNCTION__);
  382.         }
  383.  
  384.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  385.             if ($db->options['field_case'== CASE_LOWER{
  386.                 $case_func 'strtolower';
  387.             else {
  388.                 $case_func 'strtoupper';
  389.             }
  390.         else {
  391.             $case_func 'strval';
  392.         }
  393.  
  394.         $count @mysql_num_fields($resource);
  395.         $res   = array();
  396.  
  397.         if ($mode{
  398.             $res['num_fields'$count;
  399.         }
  400.  
  401.         $db->loadModule('Datatype'nulltrue);
  402.         for ($i = 0; $i $count$i++{
  403.             $res[$i= array(
  404.                 'table' => $case_func(@mysql_field_table($resource$i)),
  405.                 'name'  => $case_func(@mysql_field_name($resource$i)),
  406.                 'type'  => @mysql_field_type($resource$i),
  407.                 'length'   => @mysql_field_len($resource$i),
  408.                 'flags' => @mysql_field_flags($resource$i),
  409.             );
  410.             if ($res[$i]['type'== 'string'{
  411.                 $res[$i]['type''char';
  412.             elseif ($res[$i]['type'== 'unknown'{
  413.                 $res[$i]['type''decimal';
  414.             }
  415.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  416.             if (PEAR::isError($mdb2type_info)) {
  417.                return $mdb2type_info;
  418.             }
  419.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  420.             if ($mode MDB2_TABLEINFO_ORDER{
  421.                 $res['order'][$res[$i]['name']] $i;
  422.             }
  423.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  424.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  425.             }
  426.         }
  427.  
  428.         return $res;
  429.     }
  430. }
  431. ?>

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