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

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