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.57 2007/03/04 23:40:51 quipo 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.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  57.  */
  58. class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
  59. {
  60.     /**
  61.      * Array for converting MYSQLI_*_FLAG constants to text values
  62.      * @var    array 
  63.      * @access public
  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.      */
  86.     var $types = array(
  87.         MYSQLI_TYPE_DECIMAL     => 'decimal',
  88.         246                     => '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 structure 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 =$this->getDBInstance();
  127.         if (PEAR::isError($db)) {
  128.             return $db;
  129.         }
  130.  
  131.         $result $db->loadModule('Datatype'nulltrue);
  132.         if (PEAR::isError($result)) {
  133.             return $result;
  134.         }
  135.         $table $db->quoteIdentifier($tabletrue);
  136.         $query = "SHOW COLUMNS FROM $table LIKE ".$db->quote($field_name);
  137.         $columns $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  138.         if (PEAR::isError($columns)) {
  139.             return $columns;
  140.         }
  141.         foreach ($columns as $column{
  142.             $column array_change_key_case($columnCASE_LOWER);
  143.             $column['name'$column['field'];
  144.             unset($column['field']);
  145.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  146.                 if ($db->options['field_case'== CASE_LOWER{
  147.                     $column['name'strtolower($column['name']);
  148.                 else {
  149.                     $column['name'strtoupper($column['name']);
  150.                 }
  151.             else {
  152.                 $column array_change_key_case($column$db->options['field_case']);
  153.             }
  154.             if ($field_name == $column['name']{
  155.                 $mapped_datatype $db->datatype->mapNativeDatatype($column);
  156.                 if (PEAR::IsError($mapped_datatype)) {
  157.                     return $mapped_datatype;
  158.                 }
  159.                 list($types$length$unsigned$fixed$mapped_datatype;
  160.                 $notnull = false;
  161.                 if (empty($column['null']|| $column['null'!== 'YES'{
  162.                     $notnull = true;
  163.                 }
  164.                 $default = false;
  165.                 if (array_key_exists('default'$column)) {
  166.                     $default $column['default'];
  167.                     if (is_null($default&& $notnull{
  168.                         $default '';
  169.                     }
  170.                 }
  171.                 $autoincrement = false;
  172.                 if (!empty($column['extra']&& $column['extra'== 'auto_increment'{
  173.                     $autoincrement = true;
  174.                 }
  175.  
  176.                 $definition[0= array(
  177.                     'notnull' => $notnull,
  178.                     'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i''\\1'$column['type'])
  179.                 );
  180.                 if (!is_null($length)) {
  181.                     $definition[0]['length'$length;
  182.                 }
  183.                 if (!is_null($unsigned)) {
  184.                     $definition[0]['unsigned'$unsigned;
  185.                 }
  186.                 if (!is_null($fixed)) {
  187.                     $definition[0]['fixed'$fixed;
  188.                 }
  189.                 if ($default !== false{
  190.                     $definition[0]['default'$default;
  191.                 }
  192.                 if ($autoincrement !== false{
  193.                     $definition[0]['autoincrement'$autoincrement;
  194.                 }
  195.                 foreach ($types as $key => $type{
  196.                     $definition[$key$definition[0];
  197.                     if ($type == 'clob' || $type == 'blob'{
  198.                         unset($definition[$key]['default']);
  199.                     }
  200.                     $definition[$key]['type'$type;
  201.                     $definition[$key]['mdb2type'$type;
  202.                 }
  203.                 return $definition;
  204.             }
  205.         }
  206.  
  207.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  208.             'it was not specified an existing table column'__FUNCTION__);
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ getTableIndexDefinition()
  213.  
  214.     /**
  215.      * Get the structure 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 =$this->getDBInstance();
  225.         if (PEAR::isError($db)) {
  226.             return $db;
  227.         }
  228.  
  229.         $table $db->quoteIdentifier($tabletrue);
  230.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  231.         $index_name_mdb2 $db->getIndexName($index_name);
  232.         $result $db->queryRow(sprintf($query$db->quote($index_name_mdb2)));
  233.         if (!PEAR::isError($result&& !is_null($result)) {
  234.             // apply 'idxname_format' only if the query succeeded, otherwise
  235.             // fallback to the given $index_name, without transformation
  236.             $index_name $index_name_mdb2;
  237.         }
  238.         $result $db->query(sprintf($query$db->quote($index_name)));
  239.         if (PEAR::isError($result)) {
  240.             return $result;
  241.         }
  242.         $definition = array();
  243.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  244.             $row array_change_key_case($rowCASE_LOWER);
  245.             $key_name $row['key_name'];
  246.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  247.                 if ($db->options['field_case'== CASE_LOWER{
  248.                     $key_name strtolower($key_name);
  249.                 else {
  250.                     $key_name strtoupper($key_name);
  251.                 }
  252.             }
  253.             if ($index_name == $key_name{
  254.                 if (!$row['non_unique']{
  255.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  256.                         'it was not specified an existing table index'__FUNCTION__);
  257.                 }
  258.                 $column_name $row['column_name'];
  259.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  260.                     if ($db->options['field_case'== CASE_LOWER{
  261.                         $column_name strtolower($column_name);
  262.                     else {
  263.                         $column_name strtoupper($column_name);
  264.                     }
  265.                 }
  266.                 $definition['fields'][$column_name= array();
  267.                 if (!empty($row['collation'])) {
  268.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  269.                         ? 'ascending' 'descending');
  270.                 }
  271.             }
  272.         }
  273.         $result->free();
  274.         if (empty($definition['fields'])) {
  275.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  276.                 'it was not specified an existing table index'__FUNCTION__);
  277.         }
  278.         return $definition;
  279.     }
  280.  
  281.     // }}}
  282.     // {{{ getTableConstraintDefinition()
  283.  
  284.     /**
  285.      * Get the structure of a constraint into an array
  286.      *
  287.      * @param string    $table      name of table that should be used in method
  288.      * @param string    $index_name name of index that should be used in method
  289.      * @return mixed data array on success, a MDB2 error on failure
  290.      * @access public
  291.      */
  292.     function getTableConstraintDefinition($table$index_name)
  293.     {
  294.         $db =$this->getDBInstance();
  295.         if (PEAR::isError($db)) {
  296.             return $db;
  297.         }
  298.  
  299.         $table $db->quoteIdentifier($tabletrue);
  300.         $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  301.         if (strtolower($index_name!= 'primary'{
  302.             $index_name_mdb2 $db->getIndexName($index_name);
  303.             $result $db->queryRow(sprintf($query$db->quote($index_name_mdb2)));
  304.             if (!PEAR::isError($result&& !is_null($result)) {
  305.                 // apply 'idxname_format' only if the query succeeded, otherwise
  306.                 // fallback to the given $index_name, without transformation
  307.                 $index_name $index_name_mdb2;
  308.             }
  309.         }
  310.         $result $db->query(sprintf($query$db->quote($index_name)));
  311.         if (PEAR::isError($result)) {
  312.             return $result;
  313.         }
  314.         $definition = array();
  315.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  316.             $row array_change_key_case($rowCASE_LOWER);
  317.             $key_name $row['key_name'];
  318.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  319.                 if ($db->options['field_case'== CASE_LOWER{
  320.                     $key_name strtolower($key_name);
  321.                 else {
  322.                     $key_name strtoupper($key_name);
  323.                 }
  324.             }
  325.             if ($index_name == $key_name{
  326.                 if ($row['non_unique']{
  327.                     return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  328.                         'it was not specified an existing table constraint'__FUNCTION__);
  329.                 }
  330.                 if ($row['key_name'== 'PRIMARY'{
  331.                     $definition['primary'= true;
  332.                 else {
  333.                     $definition['unique'= true;
  334.                 }
  335.                 $column_name $row['column_name'];
  336.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  337.                     if ($db->options['field_case'== CASE_LOWER{
  338.                         $column_name strtolower($column_name);
  339.                     else {
  340.                         $column_name strtoupper($column_name);
  341.                     }
  342.                 }
  343.                 $definition['fields'][$column_name= array();
  344.                 if (!empty($row['collation'])) {
  345.                     $definition['fields'][$column_name]['sorting'($row['collation'== 'A'
  346.                         ? 'ascending' 'descending');
  347.                 }
  348.             }
  349.         }
  350.         $result->free();
  351.         if (empty($definition['fields'])) {
  352.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  353.                 'it was not specified an existing table constraint'__FUNCTION__);
  354.         }
  355.         return $definition;
  356.     }
  357.  
  358.     // }}}
  359.     // {{{ getTriggerDefinition()
  360.  
  361.     /**
  362.      * Get the structure of a trigger into an array
  363.      *
  364.      * EXPERIMENTAL
  365.      *
  366.      * WARNING: this function is experimental and may change the returned value
  367.      * at any time until labelled as non-experimental
  368.      *
  369.      * @param string    $trigger    name of trigger that should be used in method
  370.      * @return mixed data array on success, a MDB2 error on failure
  371.      * @access public
  372.      */
  373.     function getTriggerDefinition($trigger)
  374.     {
  375.         $db =$this->getDBInstance();
  376.         if (PEAR::isError($db)) {
  377.             return $db;
  378.         }
  379.  
  380.         $query 'SELECT trigger_name,
  381.                          event_object_table AS table_name,
  382.                          action_statement AS trigger_body,
  383.                          action_timing AS trigger_type,
  384.                          event_manipulation AS trigger_event
  385.                     FROM information_schema.triggers
  386.                    WHERE trigger_name = '$db->quote($trigger'text');
  387.         $types = array(
  388.             'trigger_name'    => 'text',
  389.             'table_name'      => 'text',
  390.             'trigger_body'    => 'text',
  391.             'trigger_type'    => 'text',
  392.             'trigger_event'   => 'text',
  393.         );
  394.         $def $db->queryRow($query$typesMDB2_FETCHMODE_ASSOC);
  395.         if (PEAR::isError($def)) {
  396.             return $def;
  397.         }
  398.         $def['trigger_comment''';
  399.         $def['trigger_enabled'= true;
  400.         return $def;
  401.     }
  402.  
  403.     // }}}
  404.     // {{{ tableInfo()
  405.  
  406.     /**
  407.      * Returns information about a table or a result set
  408.      *
  409.      * @param object|string $result  MDB2_result object from a query or a
  410.      *                                  string containing the name of a table.
  411.      *                                  While this also accepts a query result
  412.      *                                  resource identifier, this behavior is
  413.      *                                  deprecated.
  414.      * @param int            $mode    a valid tableInfo mode
  415.      *
  416.      * @return array  an associative array with the information requested.
  417.      *                  A MDB2_Error object on failure.
  418.      *
  419.      * @see MDB2_Driver_Common::setOption()
  420.      */
  421.     function tableInfo($result$mode = null)
  422.     {
  423.         if (is_string($result)) {
  424.            return parent::tableInfo($result$mode);
  425.         }
  426.  
  427.         $db =$this->getDBInstance();
  428.         if (PEAR::isError($db)) {
  429.             return $db;
  430.         }
  431.  
  432.         $resource = MDB2::isResultCommon($result$result->getResource($result;
  433.         if (!is_object($resource)) {
  434.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  435.                 'Could not generate result resource'__FUNCTION__);
  436.         }
  437.  
  438.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  439.             if ($db->options['field_case'== CASE_LOWER{
  440.                 $case_func 'strtolower';
  441.             else {
  442.                 $case_func 'strtoupper';
  443.             }
  444.         else {
  445.             $case_func 'strval';
  446.         }
  447.  
  448.         $count @mysqli_num_fields($resource);
  449.         $res = array();
  450.         if ($mode{
  451.             $res['num_fields'$count;
  452.         }
  453.  
  454.         $db->loadModule('Datatype'nulltrue);
  455.         for ($i = 0; $i $count$i++{
  456.             $tmp @mysqli_fetch_field($resource);
  457.  
  458.             $flags '';
  459.             foreach ($this->flags as $const => $means{
  460.                 if ($tmp->flags $const{
  461.                     $flags.= $means ' ';
  462.                 }
  463.             }
  464.             if ($tmp->def{
  465.                 $flags.= 'default_' rawurlencode($tmp->def);
  466.             }
  467.             $flags trim($flags);
  468.  
  469.             $res[$i= array(
  470.                 'table'  => $case_func($tmp->table),
  471.                 'name'   => $case_func($tmp->name),
  472.                 'type'   => isset($this->types[$tmp->type])
  473.                     ? $this->types[$tmp->type'unknown',
  474.                 // http://bugs.php.net/?id=36579
  475.                 'length' => $tmp->length,
  476.                 'flags'  => $flags,
  477.             );
  478.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  479.             if (PEAR::isError($mdb2type_info)) {
  480.                return $mdb2type_info;
  481.             }
  482.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  483.             if ($mode MDB2_TABLEINFO_ORDER{
  484.                 $res['order'][$res[$i]['name']] $i;
  485.             }
  486.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  487.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  488.             }
  489.         }
  490.  
  491.         return $res;
  492.     }
  493. }
  494. ?>

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