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.54 2006/07/17 16:12:31 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'nativetype' => $column['type']);
  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.                     $definition[$key]['mdb2type'$type;
  136.                 }
  137.                 return $definition;
  138.             }
  139.         }
  140.  
  141.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  142.             'it was not specified an existing table column'__FUNCTION__);
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ getTableIndexDefinition()
  147.  
  148.     /**
  149.      * Get the stucture of an index into an array
  150.      *
  151.      * @param string    $table      name of table that should be used in method
  152.      * @param string    $index_name name of index that should be used in method
  153.      * @return mixed data array on success, a MDB2 error on failure
  154.      * @access public
  155.      */
  156.     function getTableIndexDefinition($table$index_name)
  157.     {
  158.         $db =$this->getDBInstance();
  159.         if (PEAR::isError($db)) {
  160.             return $db;
  161.         }
  162.  
  163.         $index_name $db->getIndexName($index_name);
  164.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  165.         $result $db->query($query);
  166.         if (PEAR::isError($result)) {
  167.             return $result;
  168.         }
  169.         $definition = array();
  170.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  171.             $row array_change_key_case($rowCASE_LOWER);
  172.             $key_name $row['key_name'];
  173.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  174.                 if ($db->options['field_case'== CASE_LOWER{
  175.                     $key_name strtolower($key_name);
  176.                 else {
  177.                     $key_name strtoupper($key_name);
  178.                 }
  179.             }
  180.             if ($index_name == $key_name{
  181.                 if (!$row['non_unique']{
  182.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  183.                         'it was not specified an existing table index'__FUNCTION__);
  184.                 }
  185.                 $column_name $row['column_name'];
  186.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  187.                     if ($db->options['field_case'== CASE_LOWER{
  188.                         $column_name strtolower($column_name);
  189.                     else {
  190.                         $column_name strtoupper($column_name);
  191.                     }
  192.                 }
  193.                 $definition['fields'][$column_name= array();
  194.                 if (!empty($row['collation'])) {
  195.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  196.                         ? 'ascending' 'descending');
  197.                 }
  198.             }
  199.         }
  200.         $result->free();
  201.         if (empty($definition['fields'])) {
  202.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  203.                 'it was not specified an existing table index'__FUNCTION__);
  204.         }
  205.         return $definition;
  206.     }
  207.  
  208.     // }}}
  209.     // {{{ getTableConstraintDefinition()
  210.  
  211.     /**
  212.      * Get the stucture of a constraint into an array
  213.      *
  214.      * @param string    $table      name of table that should be used in method
  215.      * @param string    $index_name name of index that should be used in method
  216.      * @return mixed data array on success, a MDB2 error on failure
  217.      * @access public
  218.      */
  219.     function getTableConstraintDefinition($table$index_name)
  220.     {
  221.         $db =$this->getDBInstance();
  222.         if (PEAR::isError($db)) {
  223.             return $db;
  224.         }
  225.  
  226.         if (strtolower($index_name!= 'primary'{
  227.             $index_name $db->getIndexName($index_name);
  228.         }
  229.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  230.         $result $db->query($query);
  231.         if (PEAR::isError($result)) {
  232.             return $result;
  233.         }
  234.         $definition = array();
  235.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  236.             $row array_change_key_case($rowCASE_LOWER);
  237.             $key_name $row['key_name'];
  238.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  239.                 if ($db->options['field_case'== CASE_LOWER{
  240.                     $key_name strtolower($key_name);
  241.                 else {
  242.                     $key_name strtoupper($key_name);
  243.                 }
  244.             }
  245.             if ($index_name == $key_name{
  246.                 if ($row['non_unique']{
  247.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  248.                         'it was not specified an existing table constraint'__FUNCTION__);
  249.                 }
  250.                 if ($row['key_name'== 'PRIMARY'{
  251.                     $definition['primary'= true;
  252.                 else {
  253.                     $definition['unique'= true;
  254.                 }
  255.                 $column_name $row['column_name'];
  256.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  257.                     if ($db->options['field_case'== CASE_LOWER{
  258.                         $column_name strtolower($column_name);
  259.                     else {
  260.                         $column_name strtoupper($column_name);
  261.                     }
  262.                 }
  263.                 $definition['fields'][$column_name= array();
  264.                 if (!empty($row['collation'])) {
  265.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  266.                         ? 'ascending' 'descending');
  267.                 }
  268.             }
  269.         }
  270.         $result->free();
  271.         if (empty($definition['fields'])) {
  272.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  273.                 'it was not specified an existing table constraint'__FUNCTION__);
  274.         }
  275.         return $definition;
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ tableInfo()
  280.  
  281.     /**
  282.      * Returns information about a table or a result set
  283.      *
  284.      * @param object|string $result  MDB2_result object from a query or a
  285.      *                                  string containing the name of a table.
  286.      *                                  While this also accepts a query result
  287.      *                                  resource identifier, this behavior is
  288.      *                                  deprecated.
  289.      * @param int            $mode    a valid tableInfo mode
  290.      *
  291.      * @return array  an associative array with the information requested.
  292.      *                  A MDB2_Error object on failure.
  293.      *
  294.      * @see MDB2_Driver_Common::setOption()
  295.      */
  296.     function tableInfo($result$mode = null)
  297.     {
  298.         if (is_string($result)) {
  299.            return parent::tableInfo($result$mode);
  300.         }
  301.  
  302.         $db =$this->getDBInstance();
  303.         if (PEAR::isError($db)) {
  304.             return $db;
  305.         }
  306.  
  307.         $id = MDB2::isResultCommon($result$result->getResource($result;
  308.         if (!is_resource($id)) {
  309.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  310.                 'Could not generate result ressource'__FUNCTION__);
  311.         }
  312.  
  313.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  314.             if ($db->options['field_case'== CASE_LOWER{
  315.                 $case_func 'strtolower';
  316.             else {
  317.                 $case_func 'strtoupper';
  318.             }
  319.         else {
  320.             $case_func 'strval';
  321.         }
  322.  
  323.         $count @mysql_num_fields($id);
  324.         $res   = array();
  325.  
  326.         if ($mode{
  327.             $res['num_fields'$count;
  328.         }
  329.  
  330.         $db->loadModule('Datatype'nulltrue);
  331.         for ($i = 0; $i $count$i++{
  332.             $res[$i= array(
  333.                 'table' => $case_func(@mysql_field_table($id$i)),
  334.                 'name'  => $case_func(@mysql_field_name($id$i)),
  335.                 'type'  => @mysql_field_type($id$i),
  336.                 'length'   => @mysql_field_len($id$i),
  337.                 'flags' => @mysql_field_flags($id$i),
  338.             );
  339.             if ($res[$i]['type'== 'string'{
  340.                 $res[$i]['type''char';
  341.             elseif ($res[$i]['type'== 'unknown'{
  342.                 $res[$i]['type''decimal';
  343.             }
  344.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  345.             if (PEAR::isError($mdb2type_info)) {
  346.                return $mdb2type_info;
  347.             }
  348.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  349.             if ($mode MDB2_TABLEINFO_ORDER{
  350.                 $res['order'][$res[$i]['name']] $i;
  351.             }
  352.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  353.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  354.             }
  355.         }
  356.  
  357.         return $res;
  358.     }
  359. }
  360. ?>

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