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.20 2005/03/04 12:37:58 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. {
  58.     // {{{ getTableFieldDefinition()
  59.  
  60.     /**
  61.      * get the stucture of a field into an array
  62.      *
  63.      * @param string    $table         name of table that should be used in method
  64.      * @param string    $field_name     name of field that should be used in method
  65.      * @return mixed data array on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function getTableFieldDefinition($table$field_name)
  69.     {
  70.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  71.         if ($field_name == $db->dummy_primary_key{
  72.             return $db->raiseError(MDB2_ERRORnullnull,
  73.                 'getTableFieldDefinition: '.$db->dummy_primary_key.' is an hidden column');
  74.         }
  75.         $result $db->query("SHOW COLUMNS FROM $table");
  76.         if (MDB2::isError($result)) {
  77.             return $result;
  78.         }
  79.         $columns $result->getColumnNames();
  80.         if (MDB2::isError($columns)) {
  81.             return $columns;
  82.         }
  83.         if (!($db->options['portability'MDB2_PORTABILITY_LOWERCASE)) {
  84.             $columns array_change_key_case($columnsCASE_LOWER);
  85.         }
  86.         if (!isset($columns[$column 'field'])
  87.             || !isset($columns[$column 'type'])
  88.         {
  89.             return $db->raiseError(MDB2_ERRORnullnull,
  90.                 'getTableFieldDefinition: show columns does not return the column '.$column);
  91.         }
  92.         $field_column $columns['field'];
  93.         $type_column $columns['type'];
  94.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ORDERED))) {
  95.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  96.                 $row[$field_columnstrtolower($row[$field_column]);
  97.             }
  98.             if ($field_name == $row[$field_column]{
  99.                 $db_type strtolower($row[$type_column]);
  100.                 $db_type strtok($db_type'(), ');
  101.                 if ($db_type == 'national'{
  102.                     $db_type strtok('(), ');
  103.                 }
  104.                 $length strtok('(), ');
  105.                 $decimal strtok('(), ');
  106.                 $type = array();
  107.                 switch ($db_type{
  108.                 case 'tinyint':
  109.                 case 'smallint':
  110.                 case 'mediumint':
  111.                 case 'int':
  112.                 case 'integer':
  113.                 case 'bigint':
  114.                     $type[0'integer';
  115.                     if ($length == '1'{
  116.                         $type[1'boolean';
  117.                         if (preg_match('/^[is|has]/'$field_name)) {
  118.                             $type array_reverse($type);
  119.                         }
  120.                     }
  121.                     break;
  122.                 case 'tinytext':
  123.                 case 'mediumtext':
  124.                 case 'longtext':
  125.                 case 'text':
  126.                 case 'char':
  127.                 case 'varchar':
  128.                     $type[0'text';
  129.                     if ($decimal == 'binary'{
  130.                         $type[1'blob';
  131.                     elseif ($length == '1'{
  132.                         $type[1'boolean';
  133.                         if (preg_match('/[is|has]/'$field_name)) {
  134.                             $type array_reverse($type);
  135.                         }
  136.                     elseif (strstr($db_type'text'))
  137.                         $type[1'clob';
  138.                     break;
  139.                 case 'enum':
  140.                     preg_match_all('/\'.+\'/U',$row[$type_column]$matches);
  141.                     $length = 0;
  142.                     if (is_array($matches)) {
  143.                         foreach ($matches[0as $value{
  144.                             $length max($lengthstrlen($value)-2);
  145.                         }
  146.                     }
  147.                     unset($decimal);
  148.                 case 'set':
  149.                     $type[0'text';
  150.                     $type[1'integer';
  151.                     break;
  152.                 case 'date':
  153.                     $type[0'date';
  154.                     break;
  155.                 case 'datetime':
  156.                 case 'timestamp':
  157.                     $type[0'timestamp';
  158.                     break;
  159.                 case 'time':
  160.                     $type[0'time';
  161.                     break;
  162.                 case 'float':
  163.                 case 'double':
  164.                 case 'real':
  165.                     $type[0'float';
  166.                     break;
  167.                 case 'decimal':
  168.                 case 'numeric':
  169.                     $type[0'decimal';
  170.                     break;
  171.                 case 'tinyblob':
  172.                 case 'mediumblob':
  173.                 case 'longblob':
  174.                 case 'blob':
  175.                     $type[0'blob';
  176.                     $type[1'text';
  177.                     break;
  178.                 case 'year':
  179.                     $type[0'integer';
  180.                     $type[1'date';
  181.                     break;
  182.                 default:
  183.                     return $db->raiseError(MDB2_ERRORnullnull,
  184.                         'getTableFieldDefinition: unknown database attribute type');
  185.                 }
  186.                 unset($notnull);
  187.                 if (isset($columns['null'])
  188.                     && $row[$columns['null']] != 'YES'
  189.                 {
  190.                     $notnull = true;
  191.                 }
  192.                 unset($default);
  193.                 if (isset($columns['default'])
  194.                     && isset($row[$columns['default']])
  195.                 {
  196.                     $default $row[$columns['default']];
  197.                 }
  198.                 $definition = array();
  199.                 for ($field_choices = array()$datatype = 0; $datatype count($type)$datatype++{
  200.                     $field_choices[$datatype= array('type' => $type[$datatype]);
  201.                     if (isset($notnull)) {
  202.                         $field_choices[$datatype]['notnull'= true;
  203.                     }
  204.                     if (isset($default)) {
  205.                         $field_choices[$datatype]['default'$default;
  206.                     }
  207.                     if ($type[$datatype!= 'boolean'
  208.                         && $type[$datatype!= 'time'
  209.                         && $type[$datatype!= 'date'
  210.                         && $type[$datatype!= 'timestamp'
  211.                     {
  212.                         if (strlen($length)) {
  213.                             $field_choices[$datatype]['length'$length;
  214.                         }
  215.                     }
  216.                 }
  217.                 $definition[0$field_choices;
  218.                 if (isset($row[$columns['extra']])
  219.                     && $row[$columns['extra']] == 'auto_increment'
  220.                 {
  221.                     $implicit_sequence = array();
  222.                     $implicit_sequence['on'= array();
  223.                     $implicit_sequence['on']['table'$table;
  224.                     $implicit_sequence['on']['field'$field_name;
  225.                     $definition[1]['name'$table.'_'.$field_name;
  226.                     $definition[1]['definition'$implicit_sequence;
  227.                 }
  228.                 if (isset($row[$columns['key']]&& $row[$columns['key']] == 'PRI'{
  229.                     // check that its not just a unique field
  230.                     $query = "SHOW INDEX FROM $table";
  231.                     $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  232.                     if (MDB2::isError($indexes)) {
  233.                         return $indexes;
  234.                     }
  235.                     $is_primary = false;
  236.                     foreach ($indexes as $index{
  237.                         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  238.                             $index['column_name'strtolower($index['column_name']);
  239.                         else {
  240.                             $index array_change_key_case($indexCASE_LOWER);
  241.                         }
  242.                         if ($index['key_name'== 'PRIMARY' && $index['column_name'== $field_name{
  243.                             $is_primary = true;
  244.                             break;
  245.                         }
  246.                     }
  247.                     if ($is_primary{
  248.                         $implicit_index = array();
  249.                         $implicit_index['unique'= true;
  250.                         $implicit_index['fields'][$field_name'';
  251.                         $definition[2]['name'$field_name;
  252.                         $definition[2]['definition'$implicit_index;
  253.                     }
  254.                 }
  255.                 return $definition;
  256.             }
  257.         }
  258.         $db->free($result);
  259.  
  260.         if (MDB2::isError($row)) {
  261.             return $row;
  262.         }
  263.         return $db->raiseError(MDB2_ERRORnullnull,
  264.             'getTableFieldDefinition: it was not specified an existing table column');
  265.     }
  266.  
  267.     // }}}
  268.     // {{{ getTableIndexDefinition()
  269.  
  270.     /**
  271.      * get the stucture of an index into an array
  272.      *
  273.      * @param string    $table      name of table that should be used in method
  274.      * @param string    $index_name name of index that should be used in method
  275.      * @return mixed data array on success, a MDB2 error on failure
  276.      * @access public
  277.      */
  278.     function getTableIndexDefinition($table$index_name)
  279.     {
  280.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  281.         if ($index_name == 'PRIMARY'{
  282.             return $db->raiseError(MDB2_ERRORnullnull,
  283.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  284.         }
  285.         if (MDB2::isError($result $db->query("SHOW INDEX FROM $table"))) {
  286.             return $result;
  287.         }
  288.         $definition = array();
  289.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  290.             if (!($db->options['portability'MDB2_PORTABILITY_LOWERCASE)) {
  291.                 $row array_change_key_case($rowCASE_LOWER);
  292.             }
  293.             $key_name $row['key_name'];
  294.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  295.                 $key_name strtolower($key_name);
  296.             }
  297.             if ($index_name == $key_name{
  298.                 if (!$row['non_unique']{
  299.                     $definition['unique'= true;
  300.                 }
  301.                 $column_name $row['column_name'];
  302.                 if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  303.                     $column_name strtolower($column_name);
  304.                 }
  305.                 $definition['fields'][$column_name= array();
  306.                 if (isset($row['collation'])) {
  307.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A' 'ascending' 'descending');
  308.                 }
  309.             }
  310.         }
  311.         $result->free();
  312.         if (!isset($definition['fields'])) {
  313.             return $db->raiseError(MDB2_ERRORnullnull,
  314.                 'getTableIndexDefinition: it was not specified an existing table index');
  315.         }
  316.         return $definition;
  317.     }
  318.  
  319.     // }}}
  320.     // {{{ tableInfo()
  321.  
  322.     /**
  323.      * Returns information about a table or a result set
  324.      *
  325.      * @param object|string $result  MDB2_result object from a query or a
  326.      *                                  string containing the name of a table.
  327.      *                                  While this also accepts a query result
  328.      *                                  resource identifier, this behavior is
  329.      *                                  deprecated.
  330.      * @param int            $mode    a valid tableInfo mode
  331.      *
  332.      * @return array  an associative array with the information requested.
  333.      *                  A MDB2_Error object on failure.
  334.      *
  335.      * @see MDB2_common::tableInfo()
  336.      */
  337.     function tableInfo($result$mode = null)
  338.     {
  339.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  340.         if (is_string($result)) {
  341.             /*
  342.              * Probably received a table name.
  343.              * Create a result resource identifier.
  344.              */
  345.             $id @mysql_list_fields($db->database_name$result$db->connection);
  346.             $got_string = true;
  347.         elseif (MDB2::isResultCommon($result)) {
  348.             /*
  349.              * Probably received a result object.
  350.              * Extract the result resource identifier.
  351.              */
  352.             $id $result->getResource();
  353.             $got_string = false;
  354.         else {
  355.             /*
  356.              * Probably received a result resource identifier.
  357.              * Copy it.
  358.              * Deprecated.  Here for compatibility only.
  359.              */
  360.             $id $result;
  361.             $got_string = false;
  362.         }
  363.  
  364.         if (!is_resource($id)) {
  365.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  366.         }
  367.  
  368.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  369.             $case_func 'strtolower';
  370.         else {
  371.             $case_func 'strval';
  372.         }
  373.  
  374.         $count @mysql_num_fields($id);
  375.         $res   = array();
  376.  
  377.         if ($mode{
  378.             $res['num_fields'$count;
  379.         }
  380.  
  381.         for ($i = 0; $i $count$i++{
  382.             $res[$i= array(
  383.                 'table' => $case_func(@mysql_field_table($id$i)),
  384.                 'name'  => $case_func(@mysql_field_name($id$i)),
  385.                 'type'  => @mysql_field_type($id$i),
  386.                 'len'   => @mysql_field_len($id$i),
  387.                 'flags' => @mysql_field_flags($id$i),
  388.             );
  389.             if ($mode MDB2_TABLEINFO_ORDER{
  390.                 $res['order'][$res[$i]['name']] $i;
  391.             }
  392.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  393.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  394.             }
  395.         }
  396.  
  397.         // free the result only if we were called on a table
  398.         if ($got_string{
  399.             @mysql_free_result($id);
  400.         }
  401.         return $res;
  402.     }
  403. }
  404. ?>

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