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@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: mysqli.php,v 1.28 2006/01/13 22:48:39 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_mysqli extends MDB2_Driver_Reverse_Common
  58. {
  59.     /**
  60.      * Array for converting MYSQLI_*_FLAG constants to text values
  61.      * @var    array 
  62.      * @access public
  63.      */
  64.     var $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.      */
  85.     var $types = array(
  86.         MYSQLI_TYPE_DECIMAL     => 'decimal',
  87.         246                     => '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 =$this->getDBInstance();
  126.         if (PEAR::isError($db)) {
  127.             return $db;
  128.         }
  129.  
  130.         $result $db->loadModule('Datatype'nulltrue);
  131.         if (PEAR::isError($result)) {
  132.             return $result;
  133.         }
  134.         $columns $db->queryAll("SHOW COLUMNS FROM $table"nullMDB2_FETCHMODE_ASSOC);
  135.         if (PEAR::isError($columns)) {
  136.             return $columns;
  137.         }
  138.         foreach ($columns as $column{
  139.             $column['name'$column['field'];
  140.             unset($column['field']);
  141.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  142.                 if ($db->options['field_case'== CASE_LOWER{
  143.                     $column['name'strtolower($column['name']);
  144.                 else {
  145.                     $column['name'strtoupper($column['name']);
  146.                 }
  147.             else {
  148.                 $column array_change_key_case($column$db->options['field_case']);
  149.             }
  150.             if ($field_name == $column['name']{
  151.                 list($types$length$db->datatype->mapNativeDatatype($column);
  152.                 $notnull = false;
  153.                 if (array_key_exists('null'$column&& $column['null'!= 'YES'{
  154.                     $notnull = true;
  155.                 }
  156.                 $default = false;
  157.                 if (array_key_exists('default'$column)) {
  158.                     $default $column['default'];
  159.                     if (is_null($default&& $notnull{
  160.                         $default '';
  161.                     }
  162.                 }
  163.                 $autoincrement = false;
  164.                 if (array_key_exists('extra'$column&& $column['extra'== 'auto_increment'{
  165.                     $autoincrement = true;
  166.                 }
  167.                 $definition = array();
  168.                 foreach ($types as $key => $type{
  169.                     $definition[$key= array(
  170.                         'type' => $type,
  171.                         'notnull' => $notnull,
  172.                     );
  173.                     if ($length > 0{
  174.                         $definition[$key]['length'$length;
  175.                     }
  176.                     if ($default !== false{
  177.                         $definition[$key]['default'$default;
  178.                     }
  179.                     if ($autoincrement !== false{
  180.                         $definition[$key]['autoincrement'$autoincrement;
  181.                     }
  182.                 }
  183.                 return $definition;
  184.             }
  185.         }
  186.  
  187.         return $db->raiseError(MDB2_ERRORnullnull,
  188.             'getTableFieldDefinition: it was not specified an existing table column');
  189.     }
  190.  
  191.     // }}}
  192.     // {{{ getTableIndexDefinition()
  193.  
  194.     /**
  195.      * get the stucture of an index into an array
  196.      *
  197.      * @param string    $table      name of table that should be used in method
  198.      * @param string    $index_name name of index that should be used in method
  199.      * @return mixed data array on success, a MDB2 error on failure
  200.      * @access public
  201.      */
  202.     function getTableIndexDefinition($table$index_name)
  203.     {
  204.         $db =$this->getDBInstance();
  205.         if (PEAR::isError($db)) {
  206.             return $db;
  207.         }
  208.  
  209.         $index_name $db->getIndexName($index_name);
  210.         $result $db->query("SHOW INDEX FROM $table");
  211.         if (PEAR::isError($result)) {
  212.             return $result;
  213.         }
  214.         $definition = array();
  215.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  216.             if (!($db->options['portability'MDB2_PORTABILITY_FIX_CASE)
  217.                 || $db->options['field_case'!= CASE_LOWER
  218.             {
  219.                 $row array_change_key_case($rowCASE_LOWER);
  220.             }
  221.             $key_name $row['key_name'];
  222.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  223.                 if ($db->options['field_case'== CASE_LOWER{
  224.                     $key_name strtolower($key_name);
  225.                 else {
  226.                     $key_name strtoupper($key_name);
  227.                 }
  228.             }
  229.             if ($index_name == $key_name{
  230.                 if (!$row['non_unique']{
  231.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  232.                         'getTableIndexDefinition: it was not specified an existing table index');
  233.                 }
  234.                 $column_name $row['column_name'];
  235.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  236.                     if ($db->options['field_case'== CASE_LOWER{
  237.                         $column_name strtolower($column_name);
  238.                     else {
  239.                         $column_name strtoupper($column_name);
  240.                     }
  241.                 }
  242.                 $definition['fields'][$column_name= array();
  243.                 if (array_key_exists('collation'$row)) {
  244.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  245.                         ? 'ascending' 'descending');
  246.                 }
  247.             }
  248.         }
  249.         $result->free();
  250.         if (!array_key_exists('fields'$definition)) {
  251.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  252.                 'getTableIndexDefinition: it was not specified an existing table index');
  253.         }
  254.         return $definition;
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ getTableConstraintDefinition()
  259.  
  260.     /**
  261.      * get the stucture of a constraint into an array
  262.      *
  263.      * @param string    $table      name of table that should be used in method
  264.      * @param string    $index_name name of index that should be used in method
  265.      * @return mixed data array on success, a MDB2 error on failure
  266.      * @access public
  267.      */
  268.     function getTableConstraintDefinition($table$index_name)
  269.     {
  270.         $db =$this->getDBInstance();
  271.         if (PEAR::isError($db)) {
  272.             return $db;
  273.         }
  274.  
  275.         if (strtolower($index_name!= 'primary'{
  276.             $index_name $db->getIndexName($index_name);
  277.         }
  278.         $result $db->query("SHOW INDEX FROM $table");
  279.         if (PEAR::isError($result)) {
  280.             return $result;
  281.         }
  282.         $definition = array();
  283.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  284.             if (!($db->options['portability'MDB2_PORTABILITY_FIX_CASE)
  285.                 || $db->options['field_case'!= CASE_LOWER
  286.             {
  287.                 $row array_change_key_case($rowCASE_LOWER);
  288.             }
  289.             $key_name $row['key_name'];
  290.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  291.                 if ($db->options['field_case'== CASE_LOWER{
  292.                     $key_name strtolower($key_name);
  293.                 else {
  294.                     $key_name strtoupper($key_name);
  295.                 }
  296.             }
  297.             if ($index_name == $key_name{
  298.                 if ($row['non_unique']{
  299.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  300.                         'getTableConstraintDefinition: it was not specified an existing table constraint');
  301.                 }
  302.                 if ($row['key_name'== 'PRIMARY'{
  303.                     $definition['primary'= true;
  304.                 else {
  305.                     $definition['unique'= true;
  306.                 }
  307.                 $column_name $row['column_name'];
  308.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  309.                     if ($db->options['field_case'== CASE_LOWER{
  310.                         $column_name strtolower($column_name);
  311.                     else {
  312.                         $column_name strtoupper($column_name);
  313.                     }
  314.                 }
  315.                 $definition['fields'][$column_name= array();
  316.                 if (array_key_exists('collation'$row)) {
  317.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  318.                         ? 'ascending' 'descending');
  319.                 }
  320.             }
  321.         }
  322.         $result->free();
  323.         if (!array_key_exists('fields'$definition)) {
  324.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  325.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  326.         }
  327.         return $definition;
  328.     }
  329.  
  330.     // }}}
  331.     // {{{ tableInfo()
  332.  
  333.     /**
  334.      * Returns information about a table or a result set
  335.      *
  336.      * @param object|string $result  MDB2_result object from a query or a
  337.      *                                  string containing the name of a table.
  338.      *                                  While this also accepts a query result
  339.      *                                  resource identifier, this behavior is
  340.      *                                  deprecated.
  341.      * @param int            $mode    a valid tableInfo mode
  342.      *
  343.      * @return array  an associative array with the information requested.
  344.      *                  A MDB2_Error object on failure.
  345.      *
  346.      * @see MDB2_Driver_Common::setOption()
  347.      */
  348.     function tableInfo($result$mode = null)
  349.     {
  350.         $db =$this->getDBInstance();
  351.         if (PEAR::isError($db)) {
  352.             return $db;
  353.         }
  354.  
  355.         if (is_string($result)) {
  356.             /*
  357.              * Probably received a table name.
  358.              * Create a result resource identifier.
  359.              */
  360.             $id $db->_doQuery("SELECT * FROM $result LIMIT 0"false);
  361.             if (PEAR::isError($id)) {
  362.                 return $id;
  363.             }
  364.             $got_string = true;
  365.         elseif (MDB2::isResultCommon($result)) {
  366.             /*
  367.              * Probably received a result object.
  368.              * Extract the result resource identifier.
  369.              */
  370.             $id $result->getResource();
  371.             $got_string = false;
  372.         else {
  373.             /*
  374.              * Probably received a result resource identifier.
  375.              * Copy it.
  376.              * Deprecated.  Here for compatibility only.
  377.              */
  378.             $id $result;
  379.             $got_string = false;
  380.         }
  381.  
  382.         if (!is_a($id'mysqli_result')) {
  383.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  384.         }
  385.  
  386.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  387.             if ($db->options['field_case'== CASE_LOWER{
  388.                 $case_func 'strtolower';
  389.             else {
  390.                 $case_func 'strtoupper';
  391.             }
  392.         else {
  393.             $case_func 'strval';
  394.         }
  395.  
  396.         $count @mysqli_num_fields($id);
  397.         $res   = array();
  398.  
  399.         if ($mode{
  400.             $res['num_fields'$count;
  401.         }
  402.  
  403.         $db->loadModule('Datatype'nulltrue);
  404.         for ($i = 0; $i $count$i++{
  405.             $tmp @mysqli_fetch_field($id);
  406.  
  407.             $flags '';
  408.             foreach ($this->flags as $const => $means{
  409.                 if ($tmp->flags $const{
  410.                     $flags.= $means ' ';
  411.                 }
  412.             }
  413.             if ($tmp->def{
  414.                 $flags.= 'default_' rawurlencode($tmp->def);
  415.             }
  416.             $flags trim($flags);
  417.  
  418.             $res[$i= array(
  419.                 'table' => $case_func($tmp->table),
  420.                 'name'  => $case_func($tmp->name),
  421.                 'type'  => isset($this->types[$tmp->type])
  422.                                     ? $this->types[$tmp->type]
  423.                                     : 'unknown',
  424.                 'length'   => $tmp->max_length,
  425.                 'flags' => $flags,
  426.             );
  427.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  428.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  429.             if ($mode MDB2_TABLEINFO_ORDER{
  430.                 $res['order'][$res[$i]['name']] $i;
  431.             }
  432.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  433.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  434.             }
  435.         }
  436.  
  437.         // free the result only if we were called on a table
  438.         if ($got_string{
  439.             @mysqli_free_result($id);
  440.         }
  441.         return $res;
  442.     }
  443. }
  444. ?>

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