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

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