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-2006 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.39 2006/05/14 05:51:45 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 MySQLi 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.         $query = "SHOW COLUMNS FROM $table LIKE ".$db->quote($field_name);
  135.         $columns $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  136.         if (PEAR::isError($columns)) {
  137.             return $columns;
  138.         }
  139.         foreach ($columns as $column{
  140.             $column array_change_key_case($columnCASE_LOWER);
  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$unsigned$fixed$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 (!is_null($unsigned)) {
  179.                         $definition[$key]['unsigned'$unsigned;
  180.                     }
  181.                     if (!is_null($fixed)) {
  182.                         $definition[$key]['fixed'$fixed;
  183.                     }
  184.                     if ($default !== false{
  185.                         $definition[$key]['default'$default;
  186.                     }
  187.                     if ($autoincrement !== false{
  188.                         $definition[$key]['autoincrement'$autoincrement;
  189.                     }
  190.                 }
  191.                 return $definition;
  192.             }
  193.         }
  194.  
  195.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  196.             'getTableFieldDefinition: it was not specified an existing table column');
  197.     }
  198.  
  199.     // }}}
  200.     // {{{ getTableIndexDefinition()
  201.  
  202.     /**
  203.      * Get the stucture of an index into an array
  204.      *
  205.      * @param string    $table      name of table that should be used in method
  206.      * @param string    $index_name name of index that should be used in method
  207.      * @return mixed data array on success, a MDB2 error on failure
  208.      * @access public
  209.      */
  210.     function getTableIndexDefinition($table$index_name)
  211.     {
  212.         $db =$this->getDBInstance();
  213.         if (PEAR::isError($db)) {
  214.             return $db;
  215.         }
  216.  
  217.         $index_name $db->getIndexName($index_name);
  218.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  219.         $result $db->query($query);
  220.         if (PEAR::isError($result)) {
  221.             return $result;
  222.         }
  223.         $definition = array();
  224.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  225.             $row array_change_key_case($rowCASE_LOWER);
  226.             $key_name $row['key_name'];
  227.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  228.                 if ($db->options['field_case'== CASE_LOWER{
  229.                     $key_name strtolower($key_name);
  230.                 else {
  231.                     $key_name strtoupper($key_name);
  232.                 }
  233.             }
  234.             if ($index_name == $key_name{
  235.                 if (!$row['non_unique']{
  236.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  237.                         'getTableIndexDefinition: it was not specified an existing table index');
  238.                 }
  239.                 $column_name $row['column_name'];
  240.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  241.                     if ($db->options['field_case'== CASE_LOWER{
  242.                         $column_name strtolower($column_name);
  243.                     else {
  244.                         $column_name strtoupper($column_name);
  245.                     }
  246.                 }
  247.                 $definition['fields'][$column_name= array();
  248.                 if (array_key_exists('collation'$row)) {
  249.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  250.                         ? 'ascending' 'descending');
  251.                 }
  252.             }
  253.         }
  254.         $result->free();
  255.         if (!array_key_exists('fields'$definition)) {
  256.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  257.                 'getTableIndexDefinition: it was not specified an existing table index');
  258.         }
  259.         return $definition;
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ getTableConstraintDefinition()
  264.  
  265.     /**
  266.      * Get the stucture of a constraint into an array
  267.      *
  268.      * @param string    $table      name of table that should be used in method
  269.      * @param string    $index_name name of index that should be used in method
  270.      * @return mixed data array on success, a MDB2 error on failure
  271.      * @access public
  272.      */
  273.     function getTableConstraintDefinition($table$index_name)
  274.     {
  275.         $db =$this->getDBInstance();
  276.         if (PEAR::isError($db)) {
  277.             return $db;
  278.         }
  279.  
  280.         if (strtolower($index_name!= 'primary'{
  281.             $index_name $db->getIndexName($index_name);
  282.         }
  283.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  284.         $result $db->query($query);
  285.         if (PEAR::isError($result)) {
  286.             return $result;
  287.         }
  288.         $definition = array();
  289.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  290.             $row array_change_key_case($rowCASE_LOWER);
  291.             $key_name $row['key_name'];
  292.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  293.                 if ($db->options['field_case'== CASE_LOWER{
  294.                     $key_name strtolower($key_name);
  295.                 else {
  296.                     $key_name strtoupper($key_name);
  297.                 }
  298.             }
  299.             if ($index_name == $key_name{
  300.                 if ($row['non_unique']{
  301.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  302.                         'getTableConstraintDefinition: it was not specified an existing table constraint');
  303.                 }
  304.                 if ($row['key_name'== 'PRIMARY'{
  305.                     $definition['primary'= true;
  306.                 else {
  307.                     $definition['unique'= true;
  308.                 }
  309.                 $column_name $row['column_name'];
  310.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  311.                     if ($db->options['field_case'== CASE_LOWER{
  312.                         $column_name strtolower($column_name);
  313.                     else {
  314.                         $column_name strtoupper($column_name);
  315.                     }
  316.                 }
  317.                 $definition['fields'][$column_name= array();
  318.                 if (array_key_exists('collation'$row)) {
  319.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  320.                         ? 'ascending' 'descending');
  321.                 }
  322.             }
  323.         }
  324.         $result->free();
  325.         if (!array_key_exists('fields'$definition)) {
  326.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  327.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  328.         }
  329.         return $definition;
  330.     }
  331.  
  332.     // }}}
  333.     // {{{ tableInfo()
  334.  
  335.     /**
  336.      * Returns information about a table or a result set
  337.      *
  338.      * @param object|string $result  MDB2_result object from a query or a
  339.      *                                  string containing the name of a table.
  340.      *                                  While this also accepts a query result
  341.      *                                  resource identifier, this behavior is
  342.      *                                  deprecated.
  343.      * @param int            $mode    a valid tableInfo mode
  344.      *
  345.      * @return array  an associative array with the information requested.
  346.      *                  A MDB2_Error object on failure.
  347.      *
  348.      * @see MDB2_Driver_Common::setOption()
  349.      */
  350.     function tableInfo($result$mode = null)
  351.     {
  352.         $db =$this->getDBInstance();
  353.         if (PEAR::isError($db)) {
  354.             return $db;
  355.         }
  356.  
  357.         if (is_string($result)) {
  358.             /*
  359.              * Probably received a table name.
  360.              * Create a result resource identifier.
  361.              */
  362.             $query 'SELECT * FROM '.$db->quoteIdentifier($result).' LIMIT 0';
  363.             $id =$db->_doQuery($queryfalse);
  364.             if (PEAR::isError($id)) {
  365.                 return $id;
  366.             }
  367.             $got_string = true;
  368.         elseif (MDB2::isResultCommon($result)) {
  369.             /*
  370.              * Probably received a result object.
  371.              * Extract the result resource identifier.
  372.              */
  373.             $id $result->getResource();
  374.             $got_string = false;
  375.         else {
  376.             /*
  377.              * Probably received a result resource identifier.
  378.              * Copy it.
  379.              * Deprecated.  Here for compatibility only.
  380.              */
  381.             $id $result;
  382.             $got_string = false;
  383.         }
  384.  
  385.         if (!is_a($id'mysqli_result')) {
  386.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  387.         }
  388.  
  389.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  390.             if ($db->options['field_case'== CASE_LOWER{
  391.                 $case_func 'strtolower';
  392.             else {
  393.                 $case_func 'strtoupper';
  394.             }
  395.         else {
  396.             $case_func 'strval';
  397.         }
  398.  
  399.         $count @mysqli_num_fields($id);
  400.         $res   = array();
  401.  
  402.         if ($mode{
  403.             $res['num_fields'$count;
  404.         }
  405.  
  406.         $db->loadModule('Datatype'nulltrue);
  407.         for ($i = 0; $i $count$i++{
  408.             $tmp @mysqli_fetch_field($id);
  409.  
  410.             $flags '';
  411.             foreach ($this->flags as $const => $means{
  412.                 if ($tmp->flags $const{
  413.                     $flags.= $means ' ';
  414.                 }
  415.             }
  416.             if ($tmp->def{
  417.                 $flags.= 'default_' rawurlencode($tmp->def);
  418.             }
  419.             $flags trim($flags);
  420.  
  421.             $res[$i= array(
  422.                 'table'  => $case_func($tmp->table),
  423.                 'name'   => $case_func($tmp->name),
  424.                 'type'   => isset($this->types[$tmp->type])
  425.                                     ? $this->types[$tmp->type]
  426.                                     : 'unknown',
  427.                 // http://bugs.php.net/?id=36579
  428.                 'length' => $tmp->length,
  429.                 'flags'  => $flags,
  430.             );
  431.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  432.             if (PEAR::isError($mdb2type_info)) {
  433.                return $mdb2type_info;
  434.             }
  435.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  436.             if ($mode MDB2_TABLEINFO_ORDER{
  437.                 $res['order'][$res[$i]['name']] $i;
  438.             }
  439.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  440.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  441.             }
  442.         }
  443.  
  444.         // free the result only if we were called on a table
  445.         if ($got_string{
  446.             @mysqli_free_result($id);
  447.         }
  448.         return $res;
  449.     }
  450. }
  451. ?>

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