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

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