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.46 2006/03/23 12:30:34 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.         $columns $db->queryAll("SHOW COLUMNS FROM $table"nullMDB2_FETCHMODE_ASSOC);
  81.         if (PEAR::isError($columns)) {
  82.             return $columns;
  83.         }
  84.         foreach ($columns as $column{
  85.             $column array_change_key_case($columnCASE_LOWER);
  86.             $column['name'$column['field'];
  87.             unset($column['field']);
  88.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  89.                 if ($db->options['field_case'== CASE_LOWER{
  90.                     $column['name'strtolower($column['name']);
  91.                 else {
  92.                     $column['name'strtoupper($column['name']);
  93.                 }
  94.             else {
  95.                 $column array_change_key_case($column$db->options['field_case']);
  96.             }
  97.             if ($field_name == $column['name']{
  98.                 list($types$length$unsigned$db->datatype->mapNativeDatatype($column);
  99.                 $notnull = false;
  100.                 if (array_key_exists('null'$column&& $column['null'!= 'YES'{
  101.                     $notnull = true;
  102.                 }
  103.                 $default = false;
  104.                 if (array_key_exists('default'$column)) {
  105.                     $default $column['default'];
  106.                     if (is_null($default&& $notnull{
  107.                         $default '';
  108.                     }
  109.                 }
  110.                 $autoincrement = false;
  111.                 if (array_key_exists('extra'$column&& $column['extra'== 'auto_increment'{
  112.                     $autoincrement = true;
  113.                 }
  114.                 $definition = array();
  115.                 foreach ($types as $key => $type{
  116.                     $definition[$key= array(
  117.                         'type' => $type,
  118.                         'notnull' => $notnull,
  119.                     );
  120.                     if ($length > 0{
  121.                         $definition[$key]['length'$length;
  122.                     }
  123.                     if ($unsigned{
  124.                         $definition[$key]['unsigned'= true;
  125.                     }
  126.                     if ($default !== false{
  127.                         $definition[$key]['default'$default;
  128.                     }
  129.                     if ($autoincrement !== false{
  130.                         $definition[$key]['autoincrement'$autoincrement;
  131.                     }
  132.                 }
  133.                 return $definition;
  134.             }
  135.         }
  136.  
  137.         return $db->raiseError(MDB2_ERRORnullnull,
  138.             'getTableFieldDefinition: it was not specified an existing table column');
  139.     }
  140.  
  141.     // }}}
  142.     // {{{ getTableIndexDefinition()
  143.  
  144.     /**
  145.      * get the stucture of an index into an array
  146.      *
  147.      * @param string    $table      name of table that should be used in method
  148.      * @param string    $index_name name of index that should be used in method
  149.      * @return mixed data array on success, a MDB2 error on failure
  150.      * @access public
  151.      */
  152.     function getTableIndexDefinition($table$index_name)
  153.     {
  154.         $db =$this->getDBInstance();
  155.         if (PEAR::isError($db)) {
  156.             return $db;
  157.         }
  158.  
  159.         $index_name $db->getIndexName($index_name);
  160.         $result $db->query("SHOW INDEX FROM $table");
  161.         if (PEAR::isError($result)) {
  162.             return $result;
  163.         }
  164.         $definition = array();
  165.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  166.             $row array_change_key_case($rowCASE_LOWER);
  167.             $key_name $row['key_name'];
  168.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  169.                 if ($db->options['field_case'== CASE_LOWER{
  170.                     $key_name strtolower($key_name);
  171.                 else {
  172.                     $key_name strtoupper($key_name);
  173.                 }
  174.             }
  175.             if ($index_name == $key_name{
  176.                 if (!$row['non_unique']{
  177.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  178.                         'getTableIndexDefinition: it was not specified an existing table index');
  179.                 }
  180.                 $column_name $row['column_name'];
  181.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  182.                     if ($db->options['field_case'== CASE_LOWER{
  183.                         $column_name strtolower($column_name);
  184.                     else {
  185.                         $column_name strtoupper($column_name);
  186.                     }
  187.                 }
  188.                 $definition['fields'][$column_name= array();
  189.                 if (array_key_exists('collation'$row)) {
  190.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  191.                         ? 'ascending' 'descending');
  192.                 }
  193.             }
  194.         }
  195.         $result->free();
  196.         if (!array_key_exists('fields'$definition)) {
  197.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  198.                 'getTableIndexDefinition: it was not specified an existing table index');
  199.         }
  200.         return $definition;
  201.     }
  202.  
  203.     // }}}
  204.     // {{{ getTableConstraintDefinition()
  205.  
  206.     /**
  207.      * get the stucture of a constraint into an array
  208.      *
  209.      * @param string    $table      name of table that should be used in method
  210.      * @param string    $index_name name of index that should be used in method
  211.      * @return mixed data array on success, a MDB2 error on failure
  212.      * @access public
  213.      */
  214.     function getTableConstraintDefinition($table$index_name)
  215.     {
  216.         $db =$this->getDBInstance();
  217.         if (PEAR::isError($db)) {
  218.             return $db;
  219.         }
  220.  
  221.         if (strtolower($index_name!= 'primary'{
  222.             $index_name $db->getIndexName($index_name);
  223.         }
  224.         $result $db->query("SHOW INDEX FROM $table");
  225.         if (PEAR::isError($result)) {
  226.             return $result;
  227.         }
  228.         $definition = array();
  229.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  230.             $row array_change_key_case($rowCASE_LOWER);
  231.             $key_name $row['key_name'];
  232.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  233.                 if ($db->options['field_case'== CASE_LOWER{
  234.                     $key_name strtolower($key_name);
  235.                 else {
  236.                     $key_name strtoupper($key_name);
  237.                 }
  238.             }
  239.             if ($index_name == $key_name{
  240.                 if ($row['non_unique']{
  241.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  242.                         'getTableConstraintDefinition: it was not specified an existing table constraint');
  243.                 }
  244.                 if ($row['key_name'== 'PRIMARY'{
  245.                     $definition['primary'= true;
  246.                 else {
  247.                     $definition['unique'= true;
  248.                 }
  249.                 $column_name $row['column_name'];
  250.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  251.                     if ($db->options['field_case'== CASE_LOWER{
  252.                         $column_name strtolower($column_name);
  253.                     else {
  254.                         $column_name strtoupper($column_name);
  255.                     }
  256.                 }
  257.                 $definition['fields'][$column_name= array();
  258.                 if (array_key_exists('collation'$row)) {
  259.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  260.                         ? 'ascending' 'descending');
  261.                 }
  262.             }
  263.         }
  264.         $result->free();
  265.         if (!array_key_exists('fields'$definition)) {
  266.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  267.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  268.         }
  269.         return $definition;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ tableInfo()
  274.  
  275.     /**
  276.      * Returns information about a table or a result set
  277.      *
  278.      * @param object|string $result  MDB2_result object from a query or a
  279.      *                                  string containing the name of a table.
  280.      *                                  While this also accepts a query result
  281.      *                                  resource identifier, this behavior is
  282.      *                                  deprecated.
  283.      * @param int            $mode    a valid tableInfo mode
  284.      *
  285.      * @return array  an associative array with the information requested.
  286.      *                  A MDB2_Error object on failure.
  287.      *
  288.      * @see MDB2_Driver_Common::setOption()
  289.      */
  290.     function tableInfo($result$mode = null)
  291.     {
  292.         $db =$this->getDBInstance();
  293.         if (PEAR::isError($db)) {
  294.             return $db;
  295.         }
  296.  
  297.         if (is_string($result)) {
  298.             /*
  299.              * Probably received a table name.
  300.              * Create a result resource identifier.
  301.              */
  302.             $id $db->_doQuery('SELECT * FROM '.$db->quoteIdentifier($result).' LIMIT 0'false);
  303.             if (PEAR::isError($id)) {
  304.                 return $id;
  305.             }
  306.             $got_string = true;
  307.         elseif (MDB2::isResultCommon($result)) {
  308.             /*
  309.              * Probably received a result object.
  310.              * Extract the result resource identifier.
  311.              */
  312.             $id $result->getResource();
  313.             $got_string = false;
  314.         else {
  315.             /*
  316.              * Probably received a result resource identifier.
  317.              * Copy it.
  318.              * Deprecated.  Here for compatibility only.
  319.              */
  320.             $id $result;
  321.             $got_string = false;
  322.         }
  323.  
  324.         if (!is_resource($id)) {
  325.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  326.         }
  327.  
  328.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  329.             if ($db->options['field_case'== CASE_LOWER{
  330.                 $case_func 'strtolower';
  331.             else {
  332.                 $case_func 'strtoupper';
  333.             }
  334.         else {
  335.             $case_func 'strval';
  336.         }
  337.  
  338.         $count @mysql_num_fields($id);
  339.         $res   = array();
  340.  
  341.         if ($mode{
  342.             $res['num_fields'$count;
  343.         }
  344.  
  345.         $db->loadModule('Datatype'nulltrue);
  346.         for ($i = 0; $i $count$i++{
  347.             $res[$i= array(
  348.                 'table' => $case_func(@mysql_field_table($id$i)),
  349.                 'name'  => $case_func(@mysql_field_name($id$i)),
  350.                 'type'  => @mysql_field_type($id$i),
  351.                 'length'   => @mysql_field_len($id$i),
  352.                 'flags' => @mysql_field_flags($id$i),
  353.             );
  354.             if ($res[$i]['type'== 'string'{
  355.                 $res[$i]['type''char';
  356.             elseif ($res[$i]['type'== 'unknown'{
  357.                 $res[$i]['type''decimal';
  358.             }
  359.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  360.             if (PEAR::isError($mdb2type_info)) {
  361.                return $mdb2type_info;
  362.             }
  363.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  364.             if ($mode MDB2_TABLEINFO_ORDER{
  365.                 $res['order'][$res[$i]['name']] $i;
  366.             }
  367.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  368.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  369.             }
  370.         }
  371.  
  372.         // free the result only if we were called on a table
  373.         if ($got_string{
  374.             @mysql_free_result($id);
  375.         }
  376.         return $res;
  377.     }
  378. }
  379. ?>

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