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.9 2005/04/21 15:29:23 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 $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 $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.         $result $db->loadModule('Datatype');
  127.         if (PEAR::isError($result)) {
  128.             return $result;
  129.         }
  130.         if ($field_name == $db->dummy_primary_key{
  131.             return $db->raiseError(MDB2_ERRORnullnull,
  132.                 'getTableFieldDefinition: '.$db->dummy_primary_key.' is an hidden column');
  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.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  140.                 $column['field'strtolower($column['field']);
  141.             else {
  142.                 $column array_change_key_case($columnCASE_LOWER);
  143.             }
  144.             if ($field_name == $column['field']{
  145.                 list($types$length$db->datatype->mapNativeDatatype($column);
  146.                 unset($notnull);
  147.                 if (isset($column['null']&& $column['null'!= 'YES'{
  148.                     $notnull = true;
  149.                 }
  150.                 unset($default);
  151.                 if (isset($column['default'])) {
  152.                     $default $column['default'];
  153.                 }
  154.                 $definition = array();
  155.                 foreach ($types as $key => $type{
  156.                     $definition[0][$key= array('type' => $type);
  157.                     if (isset($notnull)) {
  158.                         $definition[0][$key]['notnull'= true;
  159.                     }
  160.                     if (isset($default)) {
  161.                         $definition[0][$key]['default'$default;
  162.                     }
  163.                     if (isset($length)) {
  164.                         $definition[0][$key]['length'$length;
  165.                     }
  166.                 }
  167.                 if (isset($column['extra']&& $column['extra'== 'auto_increment'{
  168.                     $implicit_sequence = array();
  169.                     $implicit_sequence['on'= array();
  170.                     $implicit_sequence['on']['table'$table;
  171.                     $implicit_sequence['on']['field'$field_name;
  172.                     $definition[1]['name'$table;
  173.                     $definition[1]['definition'$implicit_sequence;
  174.                 }
  175.                 if (isset($column['key']&& $column['key'== 'PRI'{
  176.                     // check that its not just a unique field
  177.                     $query = "SHOW INDEX FROM $table";
  178.                     $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  179.                     if (PEAR::isError($indexes)) {
  180.                         return $indexes;
  181.                     }
  182.                     $is_primary = false;
  183.                     foreach ($indexes as $index{
  184.                         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  185.                             $index['column_name'strtolower($index['column_name']);
  186.                         else {
  187.                             $index array_change_key_case($indexCASE_LOWER);
  188.                         }
  189.                         if ($index['key_name'== 'PRIMARY' && $index['column_name'== $field_name{
  190.                             $is_primary = true;
  191.                             break;
  192.                         }
  193.                     }
  194.                     if ($is_primary{
  195.                         $implicit_index = array();
  196.                         $implicit_index['unique'= true;
  197.                         $implicit_index['fields'][$field_name'';
  198.                         $definition[2]['name'$field_name;
  199.                         $definition[2]['definition'$implicit_index;
  200.                     }
  201.                 }
  202.                 return $definition;
  203.             }
  204.         }
  205.  
  206.         return $db->raiseError(MDB2_ERRORnullnull,
  207.             'getTableFieldDefinition: it was not specified an existing table column');
  208.     }
  209.  
  210.     // }}}
  211.     // {{{ getTableIndexDefinition()
  212.  
  213.     /**
  214.      * get the stucture of an index into an array
  215.      *
  216.      * @param string    $table      name of table that should be used in method
  217.      * @param string    $index_name name of index that should be used in method
  218.      * @return mixed data array on success, a MDB2 error on failure
  219.      * @access public
  220.      */
  221.     function getTableIndexDefinition($table$index_name)
  222.     {
  223.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  224.         if ($index_name == 'PRIMARY'{
  225.             return $db->raiseError(MDB2_ERRORnullnull,
  226.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  227.         }
  228.         $result $db->query("SHOW INDEX FROM $table");
  229.         if (PEAR::isError($result)) {
  230.             return $result;
  231.         }
  232.         $definition = array();
  233.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  234.             if (!($db->options['portability'MDB2_PORTABILITY_LOWERCASE)) {
  235.                 $row array_change_key_case($rowCASE_LOWER);
  236.             }
  237.             $key_name $row['key_name'];
  238.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  239.                 $key_name strtolower($key_name);
  240.             }
  241.             if ($index_name == $key_name{
  242.                 if (!$row['non_unique']{
  243.                     $definition['unique'= true;
  244.                 }
  245.                 $column_name $row['column_name'];
  246.                 if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  247.                     $column_name strtolower($column_name);
  248.                 }
  249.                 $definition['fields'][$column_name= array();
  250.                 if (isset($row['collation'])) {
  251.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  252.                         ? 'ascending' 'descending');
  253.                 }
  254.             }
  255.         }
  256.         $result->free();
  257.         if (!isset($definition['fields'])) {
  258.             return $db->raiseError(MDB2_ERRORnullnull,
  259.                 'getTableIndexDefinition: it was not specified an existing table index');
  260.         }
  261.         return $definition;
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ tableInfo()
  266.  
  267.     /**
  268.      * Returns information about a table or a result set
  269.      *
  270.      * @param object|string $result  MDB2_result object from a query or a
  271.      *                                  string containing the name of a table.
  272.      *                                  While this also accepts a query result
  273.      *                                  resource identifier, this behavior is
  274.      *                                  deprecated.
  275.      * @param int            $mode    a valid tableInfo mode
  276.      *
  277.      * @return array  an associative array with the information requested.
  278.      *                  A MDB2_Error object on failure.
  279.      *
  280.      * @see MDB2_common::setOption()
  281.      */
  282.     function tableInfo($result$mode = null)
  283.     {
  284.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  285.         if (is_string($result)) {
  286.             /*
  287.              * Probably received a table name.
  288.              * Create a result resource identifier.
  289.              */
  290.             $id $db->_doQuery("SELECT * FROM $result LIMIT 0");
  291.             if (PEAR::isError($id)) {
  292.                 return $id;
  293.             }
  294.             $got_string = true;
  295.         elseif (MDB2::isResultCommon($result)) {
  296.             /*
  297.              * Probably received a result object.
  298.              * Extract the result resource identifier.
  299.              */
  300.             $id $result->getResource();
  301.             $got_string = false;
  302.         else {
  303.             /*
  304.              * Probably received a result resource identifier.
  305.              * Copy it.
  306.              * Deprecated.  Here for compatibility only.
  307.              */
  308.             $id $result;
  309.             $got_string = false;
  310.         }
  311.  
  312.         if (!is_a($id'mysqli_result')) {
  313.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  314.         }
  315.  
  316.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  317.             $case_func 'strtolower';
  318.         else {
  319.             $case_func 'strval';
  320.         }
  321.  
  322.         $count @mysqli_num_fields($id);
  323.         $res   = array();
  324.  
  325.         if ($mode{
  326.             $res['num_fields'$count;
  327.         }
  328.  
  329.         for ($i = 0; $i $count$i++{
  330.             $tmp @mysqli_fetch_field($id);
  331.  
  332.             $flags '';
  333.             foreach ($this->flags as $const => $means{
  334.                 if ($tmp->flags $const{
  335.                     $flags .= $means ' ';
  336.                 }
  337.             }
  338.             if ($tmp->def{
  339.                 $flags .= 'default_' rawurlencode($tmp->def);
  340.             }
  341.             $flags trim($flags);
  342.  
  343.             $res[$i= array(
  344.                 'table' => $case_func($tmp->table),
  345.                 'name'  => $case_func($tmp->name),
  346.                 'type'  => isset($this->types[$tmp->type])
  347.                                     ? $this->types[$tmp->type]
  348.                                     : 'unknown',
  349.                 'len'   => $tmp->max_length,
  350.                 'flags' => $flags,
  351.             );
  352.  
  353.             if ($mode MDB2_TABLEINFO_ORDER{
  354.                 $res['order'][$res[$i]['name']] $i;
  355.             }
  356.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  357.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  358.             }
  359.         }
  360.  
  361.         // free the result only if we were called on a table
  362.         if ($got_string{
  363.             @mysqli_free_result($id);
  364.         }
  365.         return $res;
  366.     }
  367. }
  368. ?>

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