MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file mysqli.php

Documentation is available at mysqli.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: mysqli.php,v 1.2 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.     /**
  59.      * Array for converting MYSQLI_*_FLAG constants to text values
  60.      * @var    array 
  61.      * @access public
  62.      * @since  Property available since Release 1.6.5
  63.      */
  64.     var $mysqli_flags = array(
  65.         MYSQLI_NOT_NULL_FLAG        => 'not_null',
  66.         MYSQLI_PRI_KEY_FLAG         => 'primary_key',
  67.         MYSQLI_UNIQUE_KEY_FLAG      => 'unique_key',
  68.         MYSQLI_MULTIPLE_KEY_FLAG    => 'multiple_key',
  69.         MYSQLI_BLOB_FLAG            => 'blob',
  70.         MYSQLI_UNSIGNED_FLAG        => 'unsigned',
  71.         MYSQLI_ZEROFILL_FLAG        => 'zerofill',
  72.         MYSQLI_AUTO_INCREMENT_FLAG  => 'auto_increment',
  73.         MYSQLI_TIMESTAMP_FLAG       => 'timestamp',
  74.         MYSQLI_SET_FLAG             => 'set',
  75.         // MYSQLI_NUM_FLAG             => 'numeric',  // unnecessary
  76.         // MYSQLI_PART_KEY_FLAG        => 'multiple_key',  // duplicatvie
  77.         MYSQLI_GROUP_FLAG           => 'group_by'
  78.     );
  79.  
  80.     /**
  81.      * Array for converting MYSQLI_TYPE_* constants to text values
  82.      * @var    array 
  83.      * @access public
  84.      * @since  Property available since Release 1.6.5
  85.      */
  86.     var $mysqli_types = array(
  87.         MYSQLI_TYPE_DECIMAL     => 'decimal',
  88.         MYSQLI_TYPE_TINY        => 'tinyint',
  89.         MYSQLI_TYPE_SHORT       => 'int',
  90.         MYSQLI_TYPE_LONG        => 'int',
  91.         MYSQLI_TYPE_FLOAT       => 'float',
  92.         MYSQLI_TYPE_DOUBLE      => 'double',
  93.         // MYSQLI_TYPE_NULL        => 'DEFAULT NULL',  // let flags handle it
  94.         MYSQLI_TYPE_TIMESTAMP   => 'timestamp',
  95.         MYSQLI_TYPE_LONGLONG    => 'bigint',
  96.         MYSQLI_TYPE_INT24       => 'mediumint',
  97.         MYSQLI_TYPE_DATE        => 'date',
  98.         MYSQLI_TYPE_TIME        => 'time',
  99.         MYSQLI_TYPE_DATETIME    => 'datetime',
  100.         MYSQLI_TYPE_YEAR        => 'year',
  101.         MYSQLI_TYPE_NEWDATE     => 'date',
  102.         MYSQLI_TYPE_ENUM        => 'enum',
  103.         MYSQLI_TYPE_SET         => 'set',
  104.         MYSQLI_TYPE_TINY_BLOB   => 'tinyblob',
  105.         MYSQLI_TYPE_MEDIUM_BLOB => 'mediumblob',
  106.         MYSQLI_TYPE_LONG_BLOB   => 'longblob',
  107.         MYSQLI_TYPE_BLOB        => 'blob',
  108.         MYSQLI_TYPE_VAR_STRING  => 'varchar',
  109.         MYSQLI_TYPE_STRING      => 'char',
  110.         MYSQLI_TYPE_GEOMETRY    => 'geometry',
  111.     );
  112.  
  113.     // {{{ getTableFieldDefinition()
  114.  
  115.     /**
  116.      * get the stucture of a field into an array
  117.      *
  118.      * @param string    $table         name of table that should be used in method
  119.      * @param string    $field_name     name of field that should be used in method
  120.      * @return mixed data array on success, a MDB2 error on failure
  121.      * @access public
  122.      */
  123.     function getTableFieldDefinition($table$field_name)
  124.     {
  125.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  126.         if ($field_name == $db->dummy_primary_key{
  127.             return $db->raiseError(MDB2_ERRORnullnull,
  128.                 'getTableFieldDefinition: '.$db->dummy_primary_key.' is an hidden column');
  129.         }
  130.         $result $db->query("SHOW COLUMNS FROM $table");
  131.         if (MDB2::isError($result)) {
  132.             return $result;
  133.         }
  134.         $columns $result->getColumnNames();
  135.         if (MDB2::isError($columns)) {
  136.             return $columns;
  137.         }
  138.         if (!($db->options['portability'MDB2_PORTABILITY_LOWERCASE)) {
  139.             $columns array_change_key_case($columnsCASE_LOWER);
  140.         }
  141.         if (!isset($columns[$column 'field'])
  142.             || !isset($columns[$column 'type'])
  143.         {
  144.             return $db->raiseError(MDB2_ERRORnullnull,
  145.                 'getTableFieldDefinition: show columns does not return the column '.$column);
  146.         }
  147.         $field_column $columns['field'];
  148.         $type_column $columns['type'];
  149.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ORDERED))) {
  150.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  151.                 $row[$field_columnstrtolower($row[$field_column]);
  152.             }
  153.             if ($field_name == $row[$field_column]{
  154.                 $db_type strtolower($row[$type_column]);
  155.                 $db_type strtok($db_type'(), ');
  156.                 if ($db_type == 'national'{
  157.                     $db_type strtok('(), ');
  158.                 }
  159.                 $length strtok('(), ');
  160.                 $decimal strtok('(), ');
  161.                 $type = array();
  162.                 switch ($db_type{
  163.                 case 'tinyint':
  164.                 case 'smallint':
  165.                 case 'mediumint':
  166.                 case 'int':
  167.                 case 'integer':
  168.                 case 'bigint':
  169.                     $type[0'integer';
  170.                     if ($length == '1'{
  171.                         $type[1'boolean';
  172.                         if (preg_match('/^[is|has]/'$field_name)) {
  173.                             $type array_reverse($type);
  174.                         }
  175.                     }
  176.                     break;
  177.                 case 'tinytext':
  178.                 case 'mediumtext':
  179.                 case 'longtext':
  180.                 case 'text':
  181.                 case 'char':
  182.                 case 'varchar':
  183.                     $type[0'text';
  184.                     if ($decimal == 'binary'{
  185.                         $type[1'blob';
  186.                     elseif ($length == '1'{
  187.                         $type[1'boolean';
  188.                         if (preg_match('/[is|has]/'$field_name)) {
  189.                             $type array_reverse($type);
  190.                         }
  191.                     elseif (strstr($db_type'text'))
  192.                         $type[1'clob';
  193.                     break;
  194.                 case 'enum':
  195.                     preg_match_all('/\'.+\'/U',$row[$type_column]$matches);
  196.                     $length = 0;
  197.                     if (is_array($matches)) {
  198.                         foreach ($matches[0as $value{
  199.                             $length max($lengthstrlen($value)-2);
  200.                         }
  201.                     }
  202.                     unset($decimal);
  203.                 case 'set':
  204.                     $type[0'text';
  205.                     $type[1'integer';
  206.                     break;
  207.                 case 'date':
  208.                     $type[0'date';
  209.                     break;
  210.                 case 'datetime':
  211.                 case 'timestamp':
  212.                     $type[0'timestamp';
  213.                     break;
  214.                 case 'time':
  215.                     $type[0'time';
  216.                     break;
  217.                 case 'float':
  218.                 case 'double':
  219.                 case 'real':
  220.                     $type[0'float';
  221.                     break;
  222.                 case 'decimal':
  223.                 case 'numeric':
  224.                     $type[0'decimal';
  225.                     break;
  226.                 case 'tinyblob':
  227.                 case 'mediumblob':
  228.                 case 'longblob':
  229.                 case 'blob':
  230.                     $type[0'blob';
  231.                     $type[1'text';
  232.                     break;
  233.                 case 'year':
  234.                     $type[0'integer';
  235.                     $type[1'date';
  236.                     break;
  237.                 default:
  238.                     return $db->raiseError(MDB2_ERRORnullnull,
  239.                         'getTableFieldDefinition: unknown database attribute type');
  240.                 }
  241.                 unset($notnull);
  242.                 if (isset($columns['null'])
  243.                     && $row[$columns['null']] != 'YES'
  244.                 {
  245.                     $notnull = true;
  246.                 }
  247.                 unset($default);
  248.                 if (isset($columns['default'])
  249.                     && isset($row[$columns['default']])
  250.                 {
  251.                     $default $row[$columns['default']];
  252.                 }
  253.                 $definition = array();
  254.                 for ($field_choices = array()$datatype = 0; $datatype count($type)$datatype++{
  255.                     $field_choices[$datatype= array('type' => $type[$datatype]);
  256.                     if (isset($notnull)) {
  257.                         $field_choices[$datatype]['notnull'= true;
  258.                     }
  259.                     if (isset($default)) {
  260.                         $field_choices[$datatype]['default'$default;
  261.                     }
  262.                     if ($type[$datatype!= 'boolean'
  263.                         && $type[$datatype!= 'time'
  264.                         && $type[$datatype!= 'date'
  265.                         && $type[$datatype!= 'timestamp'
  266.                     {
  267.                         if (strlen($length)) {
  268.                             $field_choices[$datatype]['length'$length;
  269.                         }
  270.                     }
  271.                 }
  272.                 $definition[0$field_choices;
  273.                 if (isset($row[$columns['extra']])
  274.                     && $row[$columns['extra']] == 'auto_increment'
  275.                 {
  276.                     $implicit_sequence = array();
  277.                     $implicit_sequence['on'= array();
  278.                     $implicit_sequence['on']['table'$table;
  279.                     $implicit_sequence['on']['field'$field_name;
  280.                     $definition[1]['name'$table.'_'.$field_name;
  281.                     $definition[1]['definition'$implicit_sequence;
  282.                 }
  283.                 if (isset($row[$columns['key']]&& $row[$columns['key']] == 'PRI'{
  284.                     // check that its not just a unique field
  285.                     $query = "SHOW INDEX FROM $table";
  286.                     $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  287.                     if (MDB2::isError($indexes)) {
  288.                         return $indexes;
  289.                     }
  290.                     $is_primary = false;
  291.                     foreach ($indexes as $index{
  292.                         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  293.                             $index['column_name'strtolower($index['column_name']);
  294.                         else {
  295.                             $index array_change_key_case($indexCASE_LOWER);
  296.                         }
  297.                         if ($index['key_name'== 'PRIMARY' && $index['column_name'== $field_name{
  298.                             $is_primary = true;
  299.                             break;
  300.                         }
  301.                     }
  302.                     if ($is_primary{
  303.                         $implicit_index = array();
  304.                         $implicit_index['unique'= true;
  305.                         $implicit_index['fields'][$field_name'';
  306.                         $definition[2]['name'$field_name;
  307.                         $definition[2]['definition'$implicit_index;
  308.                     }
  309.                 }
  310.                 return $definition;
  311.             }
  312.         }
  313.         $db->free($result);
  314.  
  315.         if (MDB2::isError($row)) {
  316.             return $row;
  317.         }
  318.         return $db->raiseError(MDB2_ERRORnullnull,
  319.             'getTableFieldDefinition: it was not specified an existing table column');
  320.     }
  321.  
  322.     // }}}
  323.     // {{{ getTableIndexDefinition()
  324.  
  325.     /**
  326.      * get the stucture of an index into an array
  327.      *
  328.      * @param string    $table      name of table that should be used in method
  329.      * @param string    $index_name name of index that should be used in method
  330.      * @return mixed data array on success, a MDB2 error on failure
  331.      * @access public
  332.      */
  333.     function getTableIndexDefinition($table$index_name)
  334.     {
  335.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  336.         if ($index_name == 'PRIMARY'{
  337.             return $db->raiseError(MDB2_ERRORnullnull,
  338.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  339.         }
  340.         if (MDB2::isError($result $db->query("SHOW INDEX FROM $table"))) {
  341.             return $result;
  342.         }
  343.         $definition = array();
  344.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  345.             if (!($db->options['portability'MDB2_PORTABILITY_LOWERCASE)) {
  346.                 $row array_change_key_case($rowCASE_LOWER);
  347.             }
  348.             $key_name $row['key_name'];
  349.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  350.                 $key_name strtolower($key_name);
  351.             }
  352.             if ($index_name == $key_name{
  353.                 if (!$row['non_unique']{
  354.                     $definition['unique'= true;
  355.                 }
  356.                 $column_name $row['column_name'];
  357.                 if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  358.                     $column_name strtolower($column_name);
  359.                 }
  360.                 $definition['fields'][$column_name= array();
  361.                 if (isset($row['collation'])) {
  362.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A' 'ascending' 'descending');
  363.                 }
  364.             }
  365.         }
  366.         $result->free();
  367.         if (!isset($definition['fields'])) {
  368.             return $db->raiseError(MDB2_ERRORnullnull,
  369.                 'getTableIndexDefinition: it was not specified an existing table index');
  370.         }
  371.         return $definition;
  372.     }
  373.  
  374.     // }}}
  375.     // {{{ tableInfo()
  376.  
  377.     /**
  378.      * Returns information about a table or a result set
  379.      *
  380.      * @param object|string $result  MDB2_result object from a query or a
  381.      *                                  string containing the name of a table.
  382.      *                                  While this also accepts a query result
  383.      *                                  resource identifier, this behavior is
  384.      *                                  deprecated.
  385.      * @param int            $mode    a valid tableInfo mode
  386.      *
  387.      * @return array  an associative array with the information requested.
  388.      *                  A MDB2_Error object on failure.
  389.      *
  390.      * @see MDB2_common::setOption()
  391.      */
  392.     function tableInfo($result$mode = null)
  393.     {
  394.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  395.         if (is_string($result)) {
  396.             /*
  397.              * Probably received a table name.
  398.              * Create a result resource identifier.
  399.              */
  400.             $id $db->_doQuery("SELECT * FROM $result LIMIT 0");
  401.             if (PEAR::isError($id)) {
  402.                 return $id;
  403.             }
  404.             $got_string = true;
  405.         elseif (MDB2::isResultCommon($result)) {
  406.             /*
  407.              * Probably received a result object.
  408.              * Extract the result resource identifier.
  409.              */
  410.             $id $result->getResource();
  411.             $got_string = false;
  412.         else {
  413.             /*
  414.              * Probably received a result resource identifier.
  415.              * Copy it.
  416.              * Deprecated.  Here for compatibility only.
  417.              */
  418.             $id $result;
  419.             $got_string = false;
  420.         }
  421.  
  422.         if (!is_a($id'mysqli_result')) {
  423.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  424.         }
  425.  
  426.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  427.             $case_func 'strtolower';
  428.         else {
  429.             $case_func 'strval';
  430.         }
  431.  
  432.         $count @mysqli_num_fields($id);
  433.         $res   = array();
  434.  
  435.         if ($mode{
  436.             $res['num_fields'$count;
  437.         }
  438.  
  439.         for ($i = 0; $i $count$i++{
  440.             $tmp @mysqli_fetch_field($id);
  441.  
  442.             $flags '';
  443.             foreach ($this->mysqli_flags as $const => $means{
  444.                 if ($tmp->flags $const{
  445.                     $flags .= $means ' ';
  446.                 }
  447.             }
  448.             if ($tmp->def{
  449.                 $flags .= 'default_' rawurlencode($tmp->def);
  450.             }
  451.             $flags trim($flags);
  452.  
  453.             $res[$i= array(
  454.                 'table' => $case_func($tmp->table),
  455.                 'name'  => $case_func($tmp->name),
  456.                 'type'  => isset($this->mysqli_types[$tmp->type])
  457.                                     ? $this->mysqli_types[$tmp->type]
  458.                                     : 'unknown',
  459.                 'len'   => $tmp->max_length,
  460.                 'flags' => $flags,
  461.             );
  462.  
  463.             if ($mode MDB2_TABLEINFO_ORDER{
  464.                 $res['order'][$res[$i]['name']] $i;
  465.             }
  466.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  467.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  468.             }
  469.         }
  470.  
  471.         // free the result only if we were called on a table
  472.         if ($got_string{
  473.             @mysqli_free_result($id);
  474.         }
  475.         return $res;
  476.     }
  477. }
  478. ?>

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