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@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: mysql.php,v 1.23 2005/06/07 10:12:16 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@backendmedia.com>
  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 =$GLOBALS['_MDB2_databases'][$this->db_index];
  72.         $result $db->loadModule('Datatype');
  73.         if (PEAR::isError($result)) {
  74.             return $result;
  75.         }
  76.         if ($field_name == $db->dummy_primary_key{
  77.             return $db->raiseError(MDB2_ERRORnullnull,
  78.                 'getTableFieldDefinition: '.$db->dummy_primary_key.' is an hidden column');
  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.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  86.                 $column['field'strtolower($column['field']);
  87.             else {
  88.                 $column array_change_key_case($columnCASE_LOWER);
  89.             }
  90.             if ($field_name == $column['field']{
  91.                 list($types$length$db->datatype->mapNativeDatatype($column);
  92.                 unset($notnull);
  93.                 if (isset($column['null']&& $column['null'!= 'YES'{
  94.                     $notnull = true;
  95.                 }
  96.                 unset($default);
  97.                 if (isset($column['default'])) {
  98.                     $default $column['default'];
  99.                 }
  100.                 $definition = array();
  101.                 foreach ($types as $key => $type{
  102.                     $definition[0][$key= array('type' => $type);
  103.                     if (isset($notnull)) {
  104.                         $definition[0][$key]['notnull'= true;
  105.                     }
  106.                     if (isset($default)) {
  107.                         $definition[0][$key]['default'$default;
  108.                     }
  109.                     if (isset($length)) {
  110.                         $definition[0][$key]['length'$length;
  111.                     }
  112.                 }
  113.                 if (isset($column['extra']&& $column['extra'== 'auto_increment'{
  114.                     $implicit_sequence = array();
  115.                     $implicit_sequence['on'= array();
  116.                     $implicit_sequence['on']['table'$table;
  117.                     $implicit_sequence['on']['field'$field_name;
  118.                     $definition[1]['name'$table;
  119.                     $definition[1]['definition'$implicit_sequence;
  120.                 }
  121.                 if (isset($column['key']&& $column['key'== 'PRI'{
  122.                     // check that its not just a unique field
  123.                     $query = "SHOW INDEX FROM $table";
  124.                     $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  125.                     if (PEAR::isError($indexes)) {
  126.                         return $indexes;
  127.                     }
  128.                     $is_primary = false;
  129.                     foreach ($indexes as $index{
  130.                         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  131.                             $index['column_name'strtolower($index['column_name']);
  132.                         else {
  133.                             $index array_change_key_case($indexCASE_LOWER);
  134.                         }
  135.                         if ($index['key_name'== 'PRIMARY' && $index['column_name'== $field_name{
  136.                             $is_primary = true;
  137.                             break;
  138.                         }
  139.                     }
  140.                     if ($is_primary{
  141.                         $implicit_index = array();
  142.                         $implicit_index['unique'= true;
  143.                         $implicit_index['fields'][$field_name'';
  144.                         $definition[2]['name'$field_name;
  145.                         $definition[2]['definition'$implicit_index;
  146.                     }
  147.                 }
  148.                 return $definition;
  149.             }
  150.         }
  151.  
  152.         return $db->raiseError(MDB2_ERRORnullnull,
  153.             'getTableFieldDefinition: it was not specified an existing table column');
  154.     }
  155.  
  156.     // }}}
  157.     // {{{ getTableIndexDefinition()
  158.  
  159.     /**
  160.      * get the stucture of an index into an array
  161.      *
  162.      * @param string    $table      name of table that should be used in method
  163.      * @param string    $index_name name of index that should be used in method
  164.      * @return mixed data array on success, a MDB2 error on failure
  165.      * @access public
  166.      */
  167.     function getTableIndexDefinition($table$index_name)
  168.     {
  169.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  170.         if ($index_name == 'PRIMARY'{
  171.             return $db->raiseError(MDB2_ERRORnullnull,
  172.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  173.         }
  174.         $result $db->query("SHOW INDEX FROM $table");
  175.         if (PEAR::isError($result)) {
  176.             return $result;
  177.         }
  178.         $definition = array();
  179.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  180.             if (!($db->options['portability'MDB2_PORTABILITY_LOWERCASE)) {
  181.                 $row array_change_key_case($rowCASE_LOWER);
  182.             }
  183.             $key_name $row['key_name'];
  184.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  185.                 $key_name strtolower($key_name);
  186.             }
  187.             if ($index_name == $key_name{
  188.                 if (!$row['non_unique']{
  189.                     $definition['unique'= true;
  190.                 }
  191.                 $column_name $row['column_name'];
  192.                 if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  193.                     $column_name strtolower($column_name);
  194.                 }
  195.                 $definition['fields'][$column_name= array();
  196.                 if (isset($row['collation'])) {
  197.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  198.                         ? 'ascending' 'descending');
  199.                 }
  200.             }
  201.         }
  202.         $result->free();
  203.         if (!isset($definition['fields'])) {
  204.             return $db->raiseError(MDB2_ERRORnullnull,
  205.                 'getTableIndexDefinition: it was not specified an existing table index');
  206.         }
  207.         return $definition;
  208.     }
  209.  
  210.     // }}}
  211.     // {{{ tableInfo()
  212.  
  213.     /**
  214.      * Returns information about a table or a result set
  215.      *
  216.      * @param object|string $result  MDB2_result object from a query or a
  217.      *                                  string containing the name of a table.
  218.      *                                  While this also accepts a query result
  219.      *                                  resource identifier, this behavior is
  220.      *                                  deprecated.
  221.      * @param int            $mode    a valid tableInfo mode
  222.      *
  223.      * @return array  an associative array with the information requested.
  224.      *                  A MDB2_Error object on failure.
  225.      *
  226.      * @see MDB2_common::tableInfo()
  227.      */
  228.     function tableInfo($result$mode = null)
  229.     {
  230.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  231.         if (is_string($result)) {
  232.             /*
  233.              * Probably received a table name.
  234.              * Create a result resource identifier.
  235.              */
  236.             $id @mysql_list_fields($db->database_name$result$db->connection);
  237.             $got_string = true;
  238.         elseif (MDB2::isResultCommon($result)) {
  239.             /*
  240.              * Probably received a result object.
  241.              * Extract the result resource identifier.
  242.              */
  243.             $id $result->getResource();
  244.             $got_string = false;
  245.         else {
  246.             /*
  247.              * Probably received a result resource identifier.
  248.              * Copy it.
  249.              * Deprecated.  Here for compatibility only.
  250.              */
  251.             $id $result;
  252.             $got_string = false;
  253.         }
  254.  
  255.         if (!is_resource($id)) {
  256.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  257.         }
  258.  
  259.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  260.             $case_func 'strtolower';
  261.         else {
  262.             $case_func 'strval';
  263.         }
  264.  
  265.         $count @mysql_num_fields($id);
  266.         $res   = array();
  267.  
  268.         if ($mode{
  269.             $res['num_fields'$count;
  270.         }
  271.  
  272.         for ($i = 0; $i $count$i++{
  273.             $res[$i= array(
  274.                 'table' => $case_func(@mysql_field_table($id$i)),
  275.                 'name'  => $case_func(@mysql_field_name($id$i)),
  276.                 'type'  => @mysql_field_type($id$i),
  277.                 'len'   => @mysql_field_len($id$i),
  278.                 'flags' => @mysql_field_flags($id$i),
  279.             );
  280.             if ($mode MDB2_TABLEINFO_ORDER{
  281.                 $res['order'][$res[$i]['name']] $i;
  282.             }
  283.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  284.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  285.             }
  286.         }
  287.  
  288.         // free the result only if we were called on a table
  289.         if ($got_string{
  290.             @mysql_free_result($id);
  291.         }
  292.         return $res;
  293.     }
  294. }
  295. ?>

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