MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file mysql.php

Documentation is available at mysql.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: mysql.php,v 1.52 2006/05/31 14:38:07 lsmith 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.  */
  57. class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
  58. {
  59.     // {{{ getTableFieldDefinition()
  60.  
  61.     /**
  62.      * Get the stucture of a field into an array
  63.      *
  64.      * @param string    $table         name of table that should be used in method
  65.      * @param string    $field_name     name of field that should be used in method
  66.      * @return mixed data array on success, a MDB2 error on failure
  67.      * @access public
  68.      */
  69.     function getTableFieldDefinition($table$field_name)
  70.     {
  71.         $db =$this->getDBInstance();
  72.         if (PEAR::isError($db)) {
  73.             return $db;
  74.         }
  75.  
  76.         $result $db->loadModule('Datatype'nulltrue);
  77.         if (PEAR::isError($result)) {
  78.             return $result;
  79.         }
  80.         $query = "SHOW COLUMNS FROM $table LIKE ".$db->quote($field_name);
  81.         $columns $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  82.         if (PEAR::isError($columns)) {
  83.             return $columns;
  84.         }
  85.         foreach ($columns as $column{
  86.             $column array_change_key_case($columnCASE_LOWER);
  87.             $column['name'$column['field'];
  88.             unset($column['field']);
  89.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  90.                 if ($db->options['field_case'== CASE_LOWER{
  91.                     $column['name'strtolower($column['name']);
  92.                 else {
  93.                     $column['name'strtoupper($column['name']);
  94.                 }
  95.             else {
  96.                 $column array_change_key_case($column$db->options['field_case']);
  97.             }
  98.             if ($field_name == $column['name']{
  99.                 list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  100.                 $notnull = false;
  101.                 if (!empty($column['null']&& $column['null'!= 'YES'{
  102.                     $notnull = true;
  103.                 }
  104.                 $default = false;
  105.                 if (array_key_exists('default'$column)) {
  106.                     $default $column['default'];
  107.                     if (is_null($default&& $notnull{
  108.                         $default '';
  109.                     }
  110.                 }
  111.                 $autoincrement = false;
  112.                 if (!empty($column['extra']&& $column['extra'== 'auto_increment'{
  113.                     $autoincrement = true;
  114.                 }
  115.  
  116.                 $definition[0= array('notnull' => $notnull);
  117.                 if ($length > 0{
  118.                     $definition[0]['length'$length;
  119.                 }
  120.                 if (!is_null($unsigned)) {
  121.                     $definition[0]['unsigned'$unsigned;
  122.                 }
  123.                 if (!is_null($fixed)) {
  124.                     $definition[0]['fixed'$fixed;
  125.                 }
  126.                 if ($default !== false{
  127.                     $definition[0]['default'$default;
  128.                 }
  129.                 if ($autoincrement !== false{
  130.                     $definition[0]['autoincrement'$autoincrement;
  131.                 }
  132.                 foreach ($types as $key => $type{
  133.                     $definition[$key$definition[0];
  134.                     $definition[$key]['type'$type;
  135.                 }
  136.                 return $definition;
  137.             }
  138.         }
  139.  
  140.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  141.             'getTableFieldDefinition: it was not specified an existing table column');
  142.     }
  143.  
  144.     // }}}
  145.     // {{{ getTableIndexDefinition()
  146.  
  147.     /**
  148.      * Get the stucture of an index into an array
  149.      *
  150.      * @param string    $table      name of table that should be used in method
  151.      * @param string    $index_name name of index that should be used in method
  152.      * @return mixed data array on success, a MDB2 error on failure
  153.      * @access public
  154.      */
  155.     function getTableIndexDefinition($table$index_name)
  156.     {
  157.         $db =$this->getDBInstance();
  158.         if (PEAR::isError($db)) {
  159.             return $db;
  160.         }
  161.  
  162.         $index_name $db->getIndexName($index_name);
  163.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  164.         $result $db->query($query);
  165.         if (PEAR::isError($result)) {
  166.             return $result;
  167.         }
  168.         $definition = array();
  169.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  170.             $row array_change_key_case($rowCASE_LOWER);
  171.             $key_name $row['key_name'];
  172.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  173.                 if ($db->options['field_case'== CASE_LOWER{
  174.                     $key_name strtolower($key_name);
  175.                 else {
  176.                     $key_name strtoupper($key_name);
  177.                 }
  178.             }
  179.             if ($index_name == $key_name{
  180.                 if (!$row['non_unique']{
  181.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  182.                         'getTableIndexDefinition: it was not specified an existing table index');
  183.                 }
  184.                 $column_name $row['column_name'];
  185.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  186.                     if ($db->options['field_case'== CASE_LOWER{
  187.                         $column_name strtolower($column_name);
  188.                     else {
  189.                         $column_name strtoupper($column_name);
  190.                     }
  191.                 }
  192.                 $definition['fields'][$column_name= array();
  193.                 if (!empty($row['collation'])) {
  194.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  195.                         ? 'ascending' 'descending');
  196.                 }
  197.             }
  198.         }
  199.         $result->free();
  200.         if (empty($definition['fields'])) {
  201.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  202.                 'getTableIndexDefinition: it was not specified an existing table index');
  203.         }
  204.         return $definition;
  205.     }
  206.  
  207.     // }}}
  208.     // {{{ getTableConstraintDefinition()
  209.  
  210.     /**
  211.      * Get the stucture of a constraint into an array
  212.      *
  213.      * @param string    $table      name of table that should be used in method
  214.      * @param string    $index_name name of index that should be used in method
  215.      * @return mixed data array on success, a MDB2 error on failure
  216.      * @access public
  217.      */
  218.     function getTableConstraintDefinition($table$index_name)
  219.     {
  220.         $db =$this->getDBInstance();
  221.         if (PEAR::isError($db)) {
  222.             return $db;
  223.         }
  224.  
  225.         if (strtolower($index_name!= 'primary'{
  226.             $index_name $db->getIndexName($index_name);
  227.         }
  228.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  229.         $result $db->query($query);
  230.         if (PEAR::isError($result)) {
  231.             return $result;
  232.         }
  233.         $definition = array();
  234.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  235.             $row array_change_key_case($rowCASE_LOWER);
  236.             $key_name $row['key_name'];
  237.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  238.                 if ($db->options['field_case'== CASE_LOWER{
  239.                     $key_name strtolower($key_name);
  240.                 else {
  241.                     $key_name strtoupper($key_name);
  242.                 }
  243.             }
  244.             if ($index_name == $key_name{
  245.                 if ($row['non_unique']{
  246.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  247.                         'getTableConstraintDefinition: it was not specified an existing table constraint');
  248.                 }
  249.                 if ($row['key_name'== 'PRIMARY'{
  250.                     $definition['primary'= true;
  251.                 else {
  252.                     $definition['unique'= true;
  253.                 }
  254.                 $column_name $row['column_name'];
  255.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  256.                     if ($db->options['field_case'== CASE_LOWER{
  257.                         $column_name strtolower($column_name);
  258.                     else {
  259.                         $column_name strtoupper($column_name);
  260.                     }
  261.                 }
  262.                 $definition['fields'][$column_name= array();
  263.                 if (!empty($row['collation'])) {
  264.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  265.                         ? 'ascending' 'descending');
  266.                 }
  267.             }
  268.         }
  269.         $result->free();
  270.         if (empty($definition['fields'])) {
  271.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  272.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  273.         }
  274.         return $definition;
  275.     }
  276.  
  277.     // }}}
  278.     // {{{ tableInfo()
  279.  
  280.     /**
  281.      * Returns information about a table or a result set
  282.      *
  283.      * @param object|string $result  MDB2_result object from a query or a
  284.      *                                  string containing the name of a table.
  285.      *                                  While this also accepts a query result
  286.      *                                  resource identifier, this behavior is
  287.      *                                  deprecated.
  288.      * @param int            $mode    a valid tableInfo mode
  289.      *
  290.      * @return array  an associative array with the information requested.
  291.      *                  A MDB2_Error object on failure.
  292.      *
  293.      * @see MDB2_Driver_Common::setOption()
  294.      */
  295.     function tableInfo($result$mode = null)
  296.     {
  297.         $db =$this->getDBInstance();
  298.         if (PEAR::isError($db)) {
  299.             return $db;
  300.         }
  301.  
  302.         if (is_string($result)) {
  303.             /*
  304.              * Probably received a table name.
  305.              * Create a result resource identifier.
  306.              */
  307.             $query 'SELECT * FROM '.$db->quoteIdentifier($result).' LIMIT 0';
  308.             $id =$db->_doQuery($queryfalse);
  309.             if (PEAR::isError($id)) {
  310.                 return $id;
  311.             }
  312.             $got_string = true;
  313.         elseif (MDB2::isResultCommon($result)) {
  314.             /*
  315.              * Probably received a result object.
  316.              * Extract the result resource identifier.
  317.              */
  318.             $id $result->getResource();
  319.             $got_string = false;
  320.         else {
  321.             /*
  322.              * Probably received a result resource identifier.
  323.              * Copy it.
  324.              * Deprecated.  Here for compatibility only.
  325.              */
  326.             $id $result;
  327.             $got_string = false;
  328.         }
  329.  
  330.         if (!is_resource($id)) {
  331.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  332.         }
  333.  
  334.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  335.             if ($db->options['field_case'== CASE_LOWER{
  336.                 $case_func 'strtolower';
  337.             else {
  338.                 $case_func 'strtoupper';
  339.             }
  340.         else {
  341.             $case_func 'strval';
  342.         }
  343.  
  344.         $count @mysql_num_fields($id);
  345.         $res   = array();
  346.  
  347.         if ($mode{
  348.             $res['num_fields'$count;
  349.         }
  350.  
  351.         $db->loadModule('Datatype'nulltrue);
  352.         for ($i = 0; $i $count$i++{
  353.             $res[$i= array(
  354.                 'table' => $case_func(@mysql_field_table($id$i)),
  355.                 'name'  => $case_func(@mysql_field_name($id$i)),
  356.                 'type'  => @mysql_field_type($id$i),
  357.                 'length'   => @mysql_field_len($id$i),
  358.                 'flags' => @mysql_field_flags($id$i),
  359.             );
  360.             if ($res[$i]['type'== 'string'{
  361.                 $res[$i]['type''char';
  362.             elseif ($res[$i]['type'== 'unknown'{
  363.                 $res[$i]['type''decimal';
  364.             }
  365.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  366.             if (PEAR::isError($mdb2type_info)) {
  367.                return $mdb2type_info;
  368.             }
  369.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  370.             if ($mode MDB2_TABLEINFO_ORDER{
  371.                 $res['order'][$res[$i]['name']] $i;
  372.             }
  373.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  374.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  375.             }
  376.         }
  377.  
  378.         // free the result only if we were called on a table
  379.         if ($got_string{
  380.             @mysql_free_result($id);
  381.         }
  382.         return $res;
  383.     }
  384. }
  385. ?>

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