[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libraries/ -> database_interface.lib.php (source)

   1  <?php
   2  /* $Id: database_interface.lib.php 10356 2007-05-08 20:39:33Z cybot_tm $ */
   3  // vim: expandtab sw=4 ts=4 sts=4:
   4  
   5  /**
   6   * Common Option Constants For DBI Functions
   7   */
   8  // PMA_DBI_try_query()
   9  define('PMA_DBI_QUERY_STORE',       1);  // Force STORE_RESULT method, ignored by classic MySQL.
  10  define('PMA_DBI_QUERY_UNBUFFERED',  2);  // Do not read whole query
  11  // PMA_DBI_get_variable()
  12  define('PMA_DBI_GETVAR_SESSION', 1);
  13  define('PMA_DBI_GETVAR_GLOBAL', 2);
  14  
  15  /**
  16   * Loads the mysql extensions if it is not loaded yet
  17   *
  18   * @param   string  $extension  mysql extension to load
  19   */
  20  function PMA_DBI_checkAndLoadMysqlExtension( $extension = 'mysql' ) {
  21      if ( ! function_exists( $extension . '_connect' ) ) {
  22          PMA_dl( $extension );
  23          // check whether mysql is available
  24          if ( ! function_exists( $extension . '_connect' ) ) {
  25              return false;
  26          }
  27      }
  28  
  29      return true;
  30  }
  31  
  32  
  33  /**
  34   * check for requested extension
  35   */
  36  if ( ! PMA_DBI_checkAndLoadMysqlExtension( $GLOBALS['cfg']['Server']['extension'] ) ) {
  37  
  38      // if it fails try alternative extension ...
  39      // and display an error ...
  40  
  41      /**
  42       * @todo 2.7.1: add different messages for alternativ extension
  43       * and complete fail (no alternativ extension too)
  44       */
  45      $GLOBALS['PMA_errors'][] =
  46          sprintf( PMA_sanitize( $GLOBALS['strCantLoad'] ),
  47              $GLOBALS['cfg']['Server']['extension'] )
  48          .' - <a href="./Documentation.html#faqmysql" target="documentation">'
  49          .$GLOBALS['strDocu'] . '</a>';
  50  
  51      if ( $GLOBALS['cfg']['Server']['extension'] === 'mysql' ) {
  52          $alternativ_extension = 'mysqli';
  53      } else {
  54          $alternativ_extension = 'mysql';
  55      }
  56  
  57      if ( ! PMA_DBI_checkAndLoadMysqlExtension( $alternativ_extension ) ) {
  58          // if alternativ fails too ...
  59          header( 'Location: error.php'
  60                  . '?lang='  . urlencode( $available_languages[$lang][2] )
  61                  . '&dir='   . urlencode( $text_dir )
  62                  . '&type='  . urlencode( $strError )
  63                  . '&error=' . urlencode(
  64                      sprintf( $GLOBALS['strCantLoad'],
  65                          $GLOBALS['cfg']['Server']['extension'] )
  66                      .' - [a@./Documentation.html#faqmysql@documentation]'
  67                      .$GLOBALS['strDocu'] . '[/a]' )
  68                  . '&' . SID
  69                   );
  70          exit();
  71      }
  72  
  73      $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
  74      unset( $alternativ_extension );
  75  }
  76  
  77  /**
  78   * Including The DBI Plugin
  79   */
  80  require_once('./libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php');
  81  
  82  /**
  83   * Common Functions
  84   */
  85  function PMA_DBI_query($query, $link = null, $options = 0) {
  86      $res = PMA_DBI_try_query($query, $link, $options)
  87          or PMA_mysqlDie(PMA_DBI_getError($link), $query);
  88      return $res;
  89  }
  90  
  91  /**
  92   * converts charset of a mysql message, usally coming from mysql_error(),
  93   * into PMA charset, usally UTF-8
  94   * uses language to charset mapping from mysql/share/errmsg.txt
  95   * and charset names to ISO charset from information_schema.CHARACTER_SETS
  96   *
  97   * @uses    $GLOBALS['cfg']['IconvExtraParams']
  98   * @uses    $GLOBALS['charset']     as target charset
  99   * @uses    PMA_DBI_fetch_value()   to get server_language
 100   * @uses    preg_match()            to filter server_language
 101   * @uses    in_array()
 102   * @uses    function_exists()       to check for a convert function
 103   * @uses    iconv()                 to convert message
 104   * @uses    libiconv()              to convert message
 105   * @uses    recode_string()         to convert message
 106   * @uses    mb_convert_encoding()   to convert message
 107   * @param   string  $message
 108   * @return  string  $message
 109   */
 110  function PMA_DBI_convert_message( $message ) {
 111      // latin always last!
 112      $encodings = array(
 113          'japanese'      => 'EUC-JP', //'ujis',
 114          'japanese-sjis' => 'Shift-JIS', //'sjis',
 115          'korean'        => 'EUC-KR', //'euckr',
 116          'russian'       => 'KOI8-R', //'koi8r',
 117          'ukrainian'     => 'KOI8-U', //'koi8u',
 118          'greek'         => 'ISO-8859-7', //'greek',
 119          'serbian'       => 'CP1250', //'cp1250',
 120          'estonian'      => 'ISO-8859-13', //'latin7',
 121          'slovak'        => 'ISO-8859-2', //'latin2',
 122          'czech'         => 'ISO-8859-2', //'latin2',
 123          'hungarian'     => 'ISO-8859-2', //'latin2',
 124          'polish'        => 'ISO-8859-2', //'latin2',
 125          'romanian'      => 'ISO-8859-2', //'latin2',
 126          'spanish'       => 'CP1252', //'latin1',
 127          'swedish'       => 'CP1252', //'latin1',
 128          'italian'       => 'CP1252', //'latin1',
 129          'norwegian-ny'  => 'CP1252', //'latin1',
 130          'norwegian'     => 'CP1252', //'latin1',
 131          'portuguese'    => 'CP1252', //'latin1',
 132          'danish'        => 'CP1252', //'latin1',
 133          'dutch'         => 'CP1252', //'latin1',
 134          'english'       => 'CP1252', //'latin1',
 135          'french'        => 'CP1252', //'latin1',
 136          'german'        => 'CP1252', //'latin1',
 137      );
 138  
 139      if ( $server_language = PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'language\';', 0, 1 ) ) {
 140          $found = array();
 141          if ( preg_match( '&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found )) {
 142              $server_language = $found[1];
 143          }
 144      }
 145  
 146      if ( ! empty( $server_language ) && isset( $encodings[$server_language] ) ) {
 147          if ( function_exists( 'iconv' ) ) {
 148              if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
 149                  require_once ('./libraries/iconv_wrapper.lib.php');
 150                  $message = PMA_aix_iconv_wrapper( $encodings[$server_language],
 151                      $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
 152              } else {
 153                  $message = iconv( $encodings[$server_language],
 154                      $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
 155              }
 156          } elseif ( function_exists( 'recode_string' ) ) {
 157              $message = recode_string( $encodings[$server_language] . '..'  . $GLOBALS['charset'],
 158                  $message );
 159          } elseif ( function_exists( 'libiconv' ) ) {
 160              $message = libiconv( $encodings[$server_language], $GLOBALS['charset'], $message );
 161          } elseif ( function_exists( 'mb_convert_encoding' ) ) {
 162              // do not try unsupported charsets
 163              if ( ! in_array( $server_language, array( 'ukrainian', 'greek', 'serbian' ) ) ) {
 164                  $message = mb_convert_encoding( $message, $GLOBALS['charset'],
 165                      $encodings[$server_language] );
 166              }
 167          }
 168      } else {
 169          /**
 170           * @todo lang not found, try all, what TODO ?
 171           */
 172      }
 173  
 174      return $message;
 175  }
 176  
 177  /**
 178   * returns array with table names for given db
 179   *
 180   * @param   string  $database   name of database
 181   * @param   mixed   $link       mysql link resource|object
 182   * @return  array   tables names
 183   */
 184  function PMA_DBI_get_tables($database, $link = null)
 185  {
 186      return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
 187          null, 0, $link, PMA_DBI_QUERY_STORE);
 188  }
 189  
 190  /**
 191   * returns array of all tables in given db or dbs
 192   * this function expects unqoted names:
 193   * RIGHT: my_database
 194   * WRONG: `my_database`
 195   * WRONG: my\_database
 196   * if $tbl_is_group is true, $table is used as filter for table names
 197   * if $tbl_is_group is 'comment, $table is used as filter for table comments
 198   *
 199   * <code>
 200   * PMA_DBI_get_tables_full( 'my_database' );
 201   * PMA_DBI_get_tables_full( 'my_database', 'my_table' ) );
 202   * PMA_DBI_get_tables_full( 'my_database', 'my_tables_', true ) );
 203   * PMA_DBI_get_tables_full( 'my_database', 'my_tables_', 'comment' ) );
 204   * </code>
 205   *
 206   * @uses    PMA_MYSQL_INT_VERSION
 207   * @uses    PMA_DBI_fetch_result()
 208   * @uses    PMA_escape_mysql_wildcards()
 209   * @uses    PMA_backquote()
 210   * @uses    is_array()
 211   * @uses    addslashes()
 212   * @uses    strpos()
 213   * @uses    strtoupper()
 214   * @param   string          $databases      database
 215   * @param   string          $table          table
 216   * @param   boolean|string  $tbl_is_group   $table is a table group
 217   * @param   resource        $link           mysql link
 218   * @return  array           list of tables in given db(s)
 219   */
 220  function PMA_DBI_get_tables_full($database, $table = false,
 221      $tbl_is_group = false, $link = null)
 222  {
 223      // prepare and check parameters
 224      if ( ! is_array($database) ) {
 225          $databases = array(addslashes($database));
 226      } else {
 227          $databases = array_map('addslashes', $database);
 228      }
 229  
 230      $tables = array();
 231  
 232      if ( PMA_MYSQL_INT_VERSION >= 50002 ) {
 233          // get table information from information_schema
 234          if ( $table ) {
 235              if ( true === $tbl_is_group ) {
 236                  $sql_where_table = 'AND `TABLE_NAME` LIKE \''
 237                      . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
 238              } elseif ( 'comment' === $tbl_is_group ) {
 239                  $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
 240                      . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
 241              } else {
 242                  $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
 243              }
 244          } else {
 245              $sql_where_table = '';
 246          }
 247  
 248          // for PMA bc:
 249          // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
 250          //
 251          // on non-Windows servers,
 252          // added BINARY in the WHERE clause to force a case sensitive
 253          // comparison (if we are looking for the db Aa we don't want
 254          // to find the db aa)
 255          $sql = '
 256               SELECT *,
 257                      `TABLE_SCHEMA`       AS `Db`,
 258                      `TABLE_NAME`         AS `Name`,
 259                      `ENGINE`             AS `Engine`,
 260                      `ENGINE`             AS `Type`,
 261                      `VERSION`            AS `Version`,
 262                      `ROW_FORMAT`         AS `Row_format`,
 263                      `TABLE_ROWS`         AS `Rows`,
 264                      `AVG_ROW_LENGTH`     AS `Avg_row_length`,
 265                      `DATA_LENGTH`        AS `Data_length`,
 266                      `MAX_DATA_LENGTH`    AS `Max_data_length`,
 267                      `INDEX_LENGTH`       AS `Index_length`,
 268                      `DATA_FREE`          AS `Data_free`,
 269                      `AUTO_INCREMENT`     AS `Auto_increment`,
 270                      `CREATE_TIME`        AS `Create_time`,
 271                      `UPDATE_TIME`        AS `Update_time`,
 272                      `CHECK_TIME`         AS `Check_time`,
 273                      `TABLE_COLLATION`    AS `Collation`,
 274                      `CHECKSUM`           AS `Checksum`,
 275                      `CREATE_OPTIONS`     AS `Create_options`,
 276                      `TABLE_COMMENT`      AS `Comment`
 277                 FROM `information_schema`.`TABLES`
 278                WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $databases) . '\')
 279                  ' . $sql_where_table;
 280  
 281          $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
 282              null, $link);
 283          unset( $sql_where_table, $sql );
 284      }
 285      // If permissions are wrong on even one database directory,
 286      // information_schema does not return any table info for any database
 287      // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
 288      if ( PMA_MYSQL_INT_VERSION < 50002 || empty($tables)) {
 289          foreach ( $databases as $each_database ) {
 290              if ( true === $tbl_is_group ) {
 291                  $sql = 'SHOW TABLE STATUS FROM '
 292                      . PMA_backquote($each_database)
 293                      .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
 294              } else {
 295                  $sql = 'SHOW TABLE STATUS FROM '
 296                      . PMA_backquote($each_database) . ';';
 297              }
 298              $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
 299              foreach ( $each_tables as $table_name => $each_table ) {
 300                  if ( 'comment' === $tbl_is_group
 301                    && 0 === strpos($each_table['Comment'], $table) )
 302                  {
 303                      // remove table from list
 304                      unset( $each_tables[$table_name] );
 305                      continue;
 306                  }
 307  
 308                  if ( ! isset( $each_tables[$table_name]['Type'] )
 309                    && isset( $each_tables[$table_name]['Engine'] ) ) {
 310                      // pma BC, same parts of PMA still uses 'Type'
 311                      $each_tables[$table_name]['Type']
 312                          =& $each_tables[$table_name]['Engine'];
 313                  } elseif ( ! isset( $each_tables[$table_name]['Engine'] )
 314                    && isset( $each_tables[$table_name]['Type'] ) ) {
 315                      // old MySQL reports Type, newer MySQL reports Engine
 316                      $each_tables[$table_name]['Engine']
 317                          =& $each_tables[$table_name]['Type'];
 318                  }
 319  
 320                  // MySQL forward compatibility
 321                  // so pma could use this array as if every server is of version >5.0
 322                  $each_tables[$table_name]['TABLE_SCHEMA']      = $each_database;
 323                  $each_tables[$table_name]['TABLE_NAME']        =& $each_tables[$table_name]['Name'];
 324                  $each_tables[$table_name]['ENGINE']            =& $each_tables[$table_name]['Engine'];
 325                  $each_tables[$table_name]['VERSION']           =& $each_tables[$table_name]['Version'];
 326                  $each_tables[$table_name]['ROW_FORMAT']        =& $each_tables[$table_name]['Row_format'];
 327                  $each_tables[$table_name]['TABLE_ROWS']        =& $each_tables[$table_name]['Rows'];
 328                  $each_tables[$table_name]['AVG_ROW_LENGTH']    =& $each_tables[$table_name]['Avg_row_length'];
 329                  $each_tables[$table_name]['DATA_LENGTH']       =& $each_tables[$table_name]['Data_length'];
 330                  $each_tables[$table_name]['MAX_DATA_LENGTH']   =& $each_tables[$table_name]['Max_data_length'];
 331                  $each_tables[$table_name]['INDEX_LENGTH']      =& $each_tables[$table_name]['Index_length'];
 332                  $each_tables[$table_name]['DATA_FREE']         =& $each_tables[$table_name]['Data_free'];
 333                  $each_tables[$table_name]['AUTO_INCREMENT']    =& $each_tables[$table_name]['Auto_increment'];
 334                  $each_tables[$table_name]['CREATE_TIME']       =& $each_tables[$table_name]['Create_time'];
 335                  $each_tables[$table_name]['UPDATE_TIME']       =& $each_tables[$table_name]['Update_time'];
 336                  $each_tables[$table_name]['CHECK_TIME']        =& $each_tables[$table_name]['Check_time'];
 337                  $each_tables[$table_name]['TABLE_COLLATION']   =& $each_tables[$table_name]['Collation'];
 338                  $each_tables[$table_name]['CHECKSUM']          =& $each_tables[$table_name]['Checksum'];
 339                  $each_tables[$table_name]['CREATE_OPTIONS']    =& $each_tables[$table_name]['Create_options'];
 340                  $each_tables[$table_name]['TABLE_COMMENT']     =& $each_tables[$table_name]['Comment'];
 341  
 342                  if ( strtoupper( $each_tables[$table_name]['Comment'] ) === 'VIEW' ) {
 343                      $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
 344                  } else {
 345                      /**
 346                       * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
 347                       */
 348                      $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
 349                  }
 350              }
 351  
 352              $tables[$each_database] = $each_tables;
 353          }
 354      }
 355  
 356      if ( $GLOBALS['cfg']['NaturalOrder'] ) {
 357          foreach ( $tables as $key => $val ) {
 358              uksort($tables[$key], 'strnatcasecmp');
 359          }
 360      }
 361  
 362      if (! is_array($database)) {
 363          if (isset($tables[$database])) {
 364              return $tables[$database];
 365          } elseif (isset($tables[strtolower($database)])) {
 366              // on windows with lower_case_table_names = 1
 367              // MySQL returns
 368              // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
 369              // but information_schema.TABLES gives `test`
 370              // bug #1436171
 371              // sf.net/tracker/?func=detail&aid=1436171&group_id=23067&atid=377408
 372              return $tables[strtolower($database)];
 373          } else {
 374              return $tables;
 375          }
 376      } else {
 377          return $tables;
 378      }
 379  }
 380  
 381  /**
 382   * returns array with databases containing extended infos about them
 383   *
 384   * @todo    move into PMA_List_Database?
 385   * @param   string      $databases      database
 386   * @param   boolean     $force_stats    retrieve stats also for MySQL < 5
 387   * @param   resource    $link           mysql link
 388   * @param   string      $sort_by        collumn to order by
 389   * @param   string      $sort_order     ASC or DESC
 390   * @param   integer     $limit_offset   starting offset for LIMIT
 391   * @param   bool|int    $limit_count    row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
 392   * @return  array       $databases
 393   */
 394  function PMA_DBI_get_databases_full($database = null, $force_stats = false,
 395      $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
 396      $limit_offset = 0, $limit_count = false)
 397  {
 398      $sort_order = strtoupper($sort_order);
 399  
 400      if (true === $limit_count) {
 401          $limit_count = $GLOBALS['cfg']['MaxDbList'];
 402      }
 403  
 404      // initialize to avoid errors when there are no databases
 405      $databases = array();
 406  
 407      $apply_limit_and_order_manual = true;
 408  
 409      if (PMA_MYSQL_INT_VERSION >= 50002) {
 410          /**
 411           * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
 412           * cause MySQL does not support natural ordering, we have to do it afterward
 413           */
 414          if ($GLOBALS['cfg']['NaturalOrder']) {
 415              $limit = '';
 416          } else {
 417              if ($limit_count) {
 418                  $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
 419              }
 420  
 421              $apply_limit_and_order_manual = false;
 422          }
 423  
 424          // get table information from information_schema
 425          if ( $database ) {
 426              $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
 427                  . addslashes( $database ) . '\'';
 428          } else {
 429              $sql_where_schema = '';
 430          }
 431  
 432          // for PMA bc:
 433          // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
 434          $sql = '
 435               SELECT `information_schema`.`SCHEMATA`.*';
 436          if ($force_stats) {
 437              $sql .= ',
 438                      COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
 439                          AS `SCHEMA_TABLES`,
 440                      SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
 441                          AS `SCHEMA_TABLE_ROWS`,
 442                      SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
 443                          AS `SCHEMA_DATA_LENGTH`,
 444                      SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
 445                          AS `SCHEMA_MAX_DATA_LENGTH`,
 446                      SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
 447                          AS `SCHEMA_INDEX_LENGTH`,
 448                      SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
 449                        + `information_schema`.`TABLES`.`INDEX_LENGTH`)
 450                          AS `SCHEMA_LENGTH`,
 451                      SUM(`information_schema`.`TABLES`.`DATA_FREE`)
 452                          AS `SCHEMA_DATA_FREE`';
 453          }
 454          $sql .= '
 455                 FROM `information_schema`.`SCHEMATA`';
 456          if ($force_stats) {
 457              $sql .= '
 458            LEFT JOIN `information_schema`.`TABLES`
 459                   ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
 460                    = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
 461          }
 462          $sql .= '
 463                ' . $sql_where_schema . '
 464             GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
 465             ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
 466             . $limit;
 467          $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
 468  
 469          $mysql_error = PMA_DBI_getError($link);
 470          if (! count($databases) && $GLOBALS['errno']) {
 471              PMA_mysqlDie($mysql_error, $sql);
 472          }
 473  
 474          // display only databases also in official database list
 475          // f.e. to apply hide_db and only_db
 476          $drops = array_diff(array_keys($databases), $GLOBALS['PMA_List_Database']->items);
 477          if (count($drops)) {
 478              foreach ($drops as $drop) {
 479                  unset($databases[$drop]);
 480              }
 481              unset($drop);
 482          }
 483          unset($sql_where_schema, $sql, $drops);
 484      } else {
 485          foreach ($GLOBALS['PMA_List_Database']->items as $database_name) {
 486              // MySQL forward compatibility
 487              // so pma could use this array as if every server is of version >5.0
 488              $databases[$database_name]['SCHEMA_NAME']      = $database_name;
 489  
 490              if ( $force_stats ) {
 491                  require_once  'mysql_charsets.lib.php';
 492  
 493                  $databases[$database_name]['DEFAULT_COLLATION_NAME']
 494                      = PMA_getDbCollation( $database_name );
 495  
 496                  // get additonal info about tables
 497                  $databases[$database_name]['SCHEMA_TABLES']          = 0;
 498                  $databases[$database_name]['SCHEMA_TABLE_ROWS']      = 0;
 499                  $databases[$database_name]['SCHEMA_DATA_LENGTH']     = 0;
 500                  $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
 501                  $databases[$database_name]['SCHEMA_INDEX_LENGTH']    = 0;
 502                  $databases[$database_name]['SCHEMA_LENGTH']          = 0;
 503                  $databases[$database_name]['SCHEMA_DATA_FREE']       = 0;
 504  
 505                  $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote( $database_name ) . ';');
 506                  while ( $row = PMA_DBI_fetch_assoc( $res ) ) {
 507                      $databases[$database_name]['SCHEMA_TABLES']++;
 508                      $databases[$database_name]['SCHEMA_TABLE_ROWS']
 509                          += $row['Rows'];
 510                      $databases[$database_name]['SCHEMA_DATA_LENGTH']
 511                          += $row['Data_length'];
 512                      $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
 513                          += $row['Max_data_length'];
 514                      $databases[$database_name]['SCHEMA_INDEX_LENGTH']
 515                          += $row['Index_length'];
 516                      $databases[$database_name]['SCHEMA_DATA_FREE']
 517                          += $row['Data_free'];
 518                      $databases[$database_name]['SCHEMA_LENGTH']
 519                          += $row['Data_length'] + $row['Index_length'];
 520                  }
 521                  PMA_DBI_free_result( $res );
 522                  unset( $res );
 523              }
 524          }
 525      }
 526  
 527      /**
 528       * apply limit and order manually now
 529       * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
 530       */
 531      if ($apply_limit_and_order_manual) {
 532  
 533          /**
 534           * first apply ordering
 535           */
 536          if ($GLOBALS['cfg']['NaturalOrder']) {
 537              $sorter = 'strnatcasecmp';
 538          } else {
 539              $sorter = 'strcasecmp';
 540          }
 541  
 542          // produces f.e.:
 543          // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
 544          $sort_function = '
 545              return ' . ($sort_order == 'ASC' ? 1 : -1) . ' * ' . $sorter . '($a["' . $sort_by . '"], $b["' . $sort_by . '"]);
 546          ';
 547  
 548          usort($databases, create_function('$a, $b', $sort_function));
 549  
 550          /**
 551           * now apply limit
 552           */
 553          if ($limit_count) {
 554              $databases = array_slice($databases, $limit_offset, $limit_count);
 555          }
 556      }
 557  
 558      return $databases;
 559  }
 560  
 561  /**
 562   * returns detailed array with all columns for given table in database,
 563   * or all tables/databases
 564   *
 565   * @param   string  $database   name of database
 566   * @param   string  $table      name of table to retrieve columns from
 567   * @param   string  $column     name of specific column
 568   * @param   mixed   $link       mysql link resource
 569   */
 570  function PMA_DBI_get_columns_full($database = null, $table = null,
 571      $column = null, $link = null)
 572  {
 573      $columns = array();
 574  
 575      if ( PMA_MYSQL_INT_VERSION >= 50002 ) {
 576          $sql_wheres = array();
 577          $array_keys = array();
 578  
 579          // get columns information from information_schema
 580          if ( null !== $database ) {
 581              $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
 582          } else {
 583              $array_keys[] = 'TABLE_SCHEMA';
 584          }
 585          if (  null !== $table ) {
 586              $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
 587          } else {
 588              $array_keys[] = 'TABLE_NAME';
 589          }
 590          if (  null !== $column ) {
 591              $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
 592          } else {
 593              $array_keys[] = 'COLUMN_NAME';
 594          }
 595  
 596          // for PMA bc:
 597          // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
 598          $sql = '
 599               SELECT *,
 600                      `COLUMN_NAME`       AS `Field`,
 601                      `COLUMN_TYPE`       AS `Type`,
 602                      `COLLATION_NAME`    AS `Collation`,
 603                      `IS_NULLABLE`       AS `Null`,
 604                      `COLUMN_KEY`        AS `Key`,
 605                      `COLUMN_DEFAULT`    AS `Default`,
 606                      `EXTRA`             AS `Extra`,
 607                      `PRIVILEGES`        AS `Privileges`,
 608                      `COLUMN_COMMENT`    AS `Comment`
 609                 FROM `information_schema`.`COLUMNS`';
 610          if ( count($sql_wheres) ) {
 611              $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
 612          }
 613  
 614          $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
 615          unset( $sql_wheres, $sql );
 616      } else {
 617          if ( null === $database ) {
 618              foreach ($GLOBALS['PMA_List_Database']->items as $database) {
 619                  $columns[$database] = PMA_DBI_get_columns_full($database, null,
 620                      null, $link);
 621              }
 622              return $columns;
 623          } elseif ( null === $table ) {
 624              $tables = PMA_DBI_get_tables($database);
 625              foreach ( $tables as $table ) {
 626                  $columns[$table] = PMA_DBI_get_columns_full(
 627                      $database, $table, null, $link);
 628              }
 629              return $columns;
 630          }
 631  
 632          $sql = 'SHOW FULL COLUMNS FROM '
 633              . PMA_backquote($database) . '.' . PMA_backquote($table);
 634          if ( null !== $column ) {
 635              $sql .= " LIKE '" . $column . "'";
 636          }
 637  
 638          $columns = PMA_DBI_fetch_result( $sql, 'Field', null, $link );
 639  
 640          $ordinal_position = 1;
 641          foreach ( $columns as $column_name => $each_column ) {
 642  
 643              // MySQL forward compatibility
 644              // so pma could use this array as if every server is of version >5.0
 645              $columns[$column_name]['COLUMN_NAME']                 =& $columns[$column_name]['Field'];
 646              $columns[$column_name]['COLUMN_TYPE']                 =& $columns[$column_name]['Type'];
 647              $columns[$column_name]['COLLATION_NAME']              =& $columns[$column_name]['Collation'];
 648              $columns[$column_name]['IS_NULLABLE']                 =& $columns[$column_name]['Null'];
 649              $columns[$column_name]['COLUMN_KEY']                  =& $columns[$column_name]['Key'];
 650              $columns[$column_name]['COLUMN_DEFAULT']              =& $columns[$column_name]['Default'];
 651              $columns[$column_name]['EXTRA']                       =& $columns[$column_name]['Extra'];
 652              $columns[$column_name]['PRIVILEGES']                  =& $columns[$column_name]['Privileges'];
 653              $columns[$column_name]['COLUMN_COMMENT']              =& $columns[$column_name]['Comment'];
 654  
 655              $columns[$column_name]['TABLE_CATALOG']               = null;
 656              $columns[$column_name]['TABLE_SCHEMA']                = $database;
 657              $columns[$column_name]['TABLE_NAME']                  = $table;
 658              $columns[$column_name]['ORDINAL_POSITION']            = $ordinal_position;
 659              $columns[$column_name]['DATA_TYPE']                   =
 660                  substr($columns[$column_name]['COLUMN_TYPE'], 0,
 661                      strpos($columns[$column_name]['COLUMN_TYPE'], '('));
 662              /**
 663               * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
 664               */
 665              $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH']    = null;
 666              /**
 667               * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
 668               */
 669              $columns[$column_name]['CHARACTER_OCTET_LENGTH']      = null;
 670              $columns[$column_name]['NUMERIC_PRECISION']           = null;
 671              $columns[$column_name]['NUMERIC_SCALE']               = null;
 672              $columns[$column_name]['CHARACTER_SET_NAME']          =
 673                  substr($columns[$column_name]['COLLATION_NAME'], 0,
 674                      strpos($columns[$column_name]['COLLATION_NAME'], '_'));
 675  
 676              $ordinal_position++;
 677          }
 678  
 679          if ( null !== $column ) {
 680              reset($columns);
 681              $columns = current($columns);
 682          }
 683      }
 684  
 685      return $columns;
 686  }
 687  
 688  /**
 689   * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
 690   *
 691   * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
 692   * @param   string  $database   name of database
 693   * @param   string  $table      name of table to retrieve columns from
 694   * @param   mixed   $link       mysql link resource
 695   * @return  array   column info
 696   */
 697  function PMA_DBI_get_fields($database, $table, $link = null)
 698  {
 699      // here we use a try_query because when coming from
 700      // tbl_create + tbl_properties.inc.php, the table does not exist
 701      $fields = PMA_DBI_fetch_result(
 702          'SHOW FULL COLUMNS
 703          FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
 704          null, null, $link);
 705      if ( ! is_array($fields) || count($fields) < 1 ) {
 706          return false;
 707      }
 708      return $fields;
 709  }
 710  
 711  /**
 712   * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
 713   *
 714   * @param   string  $database   name of database
 715   * @param   string  $table      name of table to retrieve columns from
 716   * @param   boolean $full       wether to return full info or only column names
 717   * @param   mixed   $link       mysql link resource
 718   * @return  array   column names
 719   */
 720  function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
 721  {
 722      $fields = PMA_DBI_fetch_result(
 723          'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
 724          FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
 725          'Fields', ($full ? null : 'Fields'), $link);
 726      if ( ! is_array($fields) || count($fields) < 1 ) {
 727          return false;
 728      }
 729      return $fields;
 730  }
 731  
 732  /**
 733   * returns value of given mysql server variable
 734   *
 735   * @param   string  $var    mysql server variable name
 736   * @param   int     $type   PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
 737   * @param   mixed   $link   mysql link resource|object
 738   * @return  mixed   value for mysql server variable
 739   */
 740  function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
 741  {
 742      if ($link === null) {
 743          if (isset($GLOBALS['userlink'])) {
 744              $link = $GLOBALS['userlink'];
 745          } else {
 746              return false;
 747          }
 748      }
 749      if (PMA_MYSQL_INT_VERSION < 40002) {
 750          $type = 0;
 751      }
 752      switch ($type) {
 753          case PMA_DBI_GETVAR_SESSION:
 754              $modifier = ' SESSION';
 755              break;
 756          case PMA_DBI_GETVAR_GLOBAL:
 757              $modifier = ' GLOBAL';
 758              break;
 759          default:
 760              $modifier = '';
 761      }
 762      return PMA_DBI_fetch_value(
 763          'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
 764  }
 765  
 766  /**
 767   * @uses    ./libraries/charset_conversion.lib.php
 768   * @uses    PMA_DBI_QUERY_STORE
 769   * @uses    PMA_REMOVED_NON_UTF_8
 770   * @uses    PMA_MYSQL_INT_VERSION
 771   * @uses    PMA_MYSQL_STR_VERSION
 772   * @uses    PMA_DBI_GETVAR_SESSION
 773   * @uses    PMA_DBI_fetch_value()
 774   * @uses    PMA_DBI_query()
 775   * @uses    PMA_DBI_get_variable()
 776   * @uses    $GLOBALS['collation_connection']
 777   * @uses    $GLOBALS['charset_connection']
 778   * @uses    $GLOBALS['available_languages']
 779   * @uses    $GLOBALS['mysql_charset_map']
 780   * @uses    $GLOBALS['charset']
 781   * @uses    $GLOBALS['lang']
 782   * @uses    $GLOBALS['cfg']['Lang']
 783   * @uses    $GLOBALS['cfg']['ColumnTypes']
 784   * @uses    defined()
 785   * @uses    explode()
 786   * @uses    sprintf()
 787   * @uses    intval()
 788   * @uses    define()
 789   * @uses    defined()
 790   * @uses    substr()
 791   * @uses    count()
 792   * @param   mixed   $link   mysql link resource|object
 793   * @param   boolean $is_controluser
 794   */
 795  function PMA_DBI_postConnect($link, $is_controluser = false)
 796  {
 797      if (!defined('PMA_MYSQL_INT_VERSION')) {
 798          $mysql_version = PMA_DBI_fetch_value(
 799              'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
 800          if ( $mysql_version ) {
 801              $match = explode('.', $mysql_version);
 802              define('PMA_MYSQL_INT_VERSION',
 803                  (int) sprintf('%d%02d%02d', $match[0], $match[1],
 804                          intval($match[2])));
 805              define('PMA_MYSQL_STR_VERSION', $mysql_version);
 806              unset($mysql_version, $match);
 807          } else {
 808              define('PMA_MYSQL_INT_VERSION', 32332);
 809              define('PMA_MYSQL_STR_VERSION', '3.23.32');
 810          }
 811      }
 812  
 813      if (!defined('PMA_ENGINE_KEYWORD')) {
 814          if (PMA_MYSQL_INT_VERSION >= 40102) {
 815              define('PMA_ENGINE_KEYWORD','ENGINE');
 816          } else {
 817              define('PMA_ENGINE_KEYWORD','TYPE');
 818          }
 819      }
 820  
 821      if (PMA_MYSQL_INT_VERSION >= 40100) {
 822  
 823          // If $lang is defined and we are on MySQL >= 4.1.x,
 824          // we auto-switch the lang to its UTF-8 version (if it exists and user
 825          // didn't force language)
 826          if ( !empty($GLOBALS['lang'])
 827            && (substr($GLOBALS['lang'], -5) != 'utf-8')
 828            && !isset($GLOBALS['cfg']['Lang']) ) {
 829              $lang_utf_8_version =
 830                  substr($GLOBALS['lang'], 0, strpos($GLOBALS['lang'], '-'))
 831                  . '-utf-8';
 832              if (!empty($GLOBALS['available_languages'][$lang_utf_8_version])) {
 833                  $GLOBALS['lang'] = $lang_utf_8_version;
 834                  $GLOBALS['charset'] = 'utf-8';
 835                  define('PMA_LANG_RELOAD', 1);
 836              }
 837          }
 838  
 839          // and we remove the non-UTF-8 choices to avoid confusion
 840          if (!defined('PMA_REMOVED_NON_UTF_8')) {
 841              foreach ( $GLOBALS['available_languages'] as $each_lang => $dummy ) {
 842                  if ( substr($each_lang, -5) != 'utf-8' ) {
 843                      unset( $GLOBALS['available_languages'][$each_lang] );
 844                  }
 845              }
 846              define('PMA_REMOVED_NON_UTF_8', 1);
 847          }
 848  
 849          $mysql_charset = $GLOBALS['mysql_charset_map'][$GLOBALS['charset']];
 850          if ( $is_controluser
 851            || empty($GLOBALS['collation_connection'])
 852            || (strpos($GLOBALS['collation_connection'], '_')
 853                  ? substr($GLOBALS['collation_connection'], 0, strpos($GLOBALS['collation_connection'], '_'))
 854                  : $GLOBALS['collation_connection']) == $mysql_charset) {
 855  
 856              PMA_DBI_query('SET NAMES ' . $mysql_charset . ';', $link,
 857                  PMA_DBI_QUERY_STORE);
 858          } else {
 859              PMA_DBI_query('SET CHARACTER SET ' . $mysql_charset . ';', $link,
 860                  PMA_DBI_QUERY_STORE);
 861          }
 862          if (!empty($GLOBALS['collation_connection'])) {
 863              PMA_DBI_query('SET collation_connection = \'' . $GLOBALS['collation_connection'] . '\';',
 864                  $link, PMA_DBI_QUERY_STORE);
 865          }
 866          if (!$is_controluser) {
 867              $GLOBALS['collation_connection'] = PMA_DBI_get_variable('collation_connection',
 868                  PMA_DBI_GETVAR_SESSION, $link);
 869              $GLOBALS['charset_connection']   = PMA_DBI_get_variable('character_set_connection',
 870                  PMA_DBI_GETVAR_SESSION, $link);
 871          }
 872  
 873          // Add some field types to the list, this needs to be done once per session!
 874          if (!in_array('BINARY', $GLOBALS['cfg']['ColumnTypes']))
 875              $GLOBALS['cfg']['ColumnTypes'][] = 'BINARY';
 876          if (!in_array('VARBINARY', $GLOBALS['cfg']['ColumnTypes']))
 877              $GLOBALS['cfg']['ColumnTypes'][] = 'VARBINARY';
 878  
 879      } else {
 880          require_once ('./libraries/charset_conversion.lib.php');
 881      }
 882  }
 883  
 884  /**
 885   * returns a single value from the given result or query,
 886   * if the query or the result has more than one row or field
 887   * the first field of the first row is returned
 888   *
 889   * <code>
 890   * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
 891   * $user_name = PMA_DBI_fetch_value( $sql );
 892   * // produces
 893   * // $user_name = 'John Doe'
 894   * </code>
 895   *
 896   * @uses    is_string()
 897   * @uses    is_int()
 898   * @uses    PMA_DBI_try_query()
 899   * @uses    PMA_DBI_num_rows()
 900   * @uses    PMA_DBI_fetch_row()
 901   * @uses    PMA_DBI_fetch_assoc()
 902   * @uses    PMA_DBI_free_result()
 903   * @param   string|mysql_result $result query or mysql result
 904   * @param   integer             $row_number row to fetch the value from,
 905   *                                      starting at 0, with 0 beeing default
 906   * @param   integer|string      $field  field to fetch the value from,
 907   *                                      starting at 0, with 0 beeing default
 908   * @param   resource            $link   mysql link
 909   * @param   mixed               $options
 910   * @return  mixed               value of first field in first row from result
 911   *                              or false if not found
 912   */
 913  function PMA_DBI_fetch_value( $result, $row_number = 0, $field = 0, $link = null, $options = 0 ) {
 914      $value = false;
 915  
 916      if ( is_string( $result ) ) {
 917          $result = PMA_DBI_try_query( $result, $link, $options | PMA_DBI_QUERY_STORE );
 918      }
 919  
 920      // return false if result is empty or false
 921      // or requested row is larger than rows in result
 922      if ( PMA_DBI_num_rows( $result ) < ( $row_number + 1 ) ) {
 923          return $value;
 924      }
 925  
 926      // if $field is an integer use non associative mysql fetch function
 927      if ( is_int( $field ) ) {
 928          $fetch_function = 'PMA_DBI_fetch_row';
 929      } else {
 930          $fetch_function = 'PMA_DBI_fetch_assoc';
 931      }
 932  
 933      // get requested row
 934      for ( $i = 0; $i <= $row_number; $i++ ) {
 935          $row = $fetch_function( $result );
 936      }
 937      PMA_DBI_free_result( $result );
 938  
 939      // return requested field
 940      if ( isset( $row[$field] ) ) {
 941          $value = $row[$field];
 942      }
 943      unset( $row );
 944  
 945      return $value;
 946  }
 947  
 948  /**
 949   * returns only the first row from the result
 950   *
 951   * <code>
 952   * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
 953   * $user = PMA_DBI_fetch_single_row( $sql );
 954   * // produces
 955   * // $user = array( 'id' => 123, 'name' => 'John Doe' )
 956   * </code>
 957   *
 958   * @uses    is_string()
 959   * @uses    PMA_DBI_try_query()
 960   * @uses    PMA_DBI_num_rows()
 961   * @uses    PMA_DBI_fetch_row()
 962   * @uses    PMA_DBI_fetch_assoc()
 963   * @uses    PMA_DBI_fetch_array()
 964   * @uses    PMA_DBI_free_result()
 965   * @param   string|mysql_result $result query or mysql result
 966   * @param   string              $type   NUM|ASSOC|BOTH
 967   *                                      returned array should either numeric
 968   *                                      associativ or booth
 969   * @param   resource            $link   mysql link
 970   * @param   mixed               $options
 971   * @return  array|boolean       first row from result
 972   *                              or false if result is empty
 973   */
 974  function PMA_DBI_fetch_single_row( $result, $type = 'ASSOC', $link = null, $options = 0 ) {
 975      if ( is_string( $result ) ) {
 976          $result = PMA_DBI_try_query( $result, $link, $options | PMA_DBI_QUERY_STORE );
 977      }
 978  
 979      // return null if result is empty or false
 980      if ( ! PMA_DBI_num_rows( $result ) ) {
 981          return false;
 982      }
 983  
 984      switch ( $type ) {
 985          case 'NUM' :
 986              $fetch_function = 'PMA_DBI_fetch_row';
 987              break;
 988          case 'ASSOC' :
 989              $fetch_function = 'PMA_DBI_fetch_assoc';
 990              break;
 991          case 'BOTH' :
 992          default :
 993              $fetch_function = 'PMA_DBI_fetch_array';
 994              break;
 995      }
 996  
 997      $row = $fetch_function( $result );
 998      PMA_DBI_free_result( $result );
 999      return $row;
1000  }
1001  
1002  /**
1003   * returns all rows in the resultset in one array
1004   *
1005   * <code>
1006   * $sql = 'SELECT * FROM `user`';
1007   * $users = PMA_DBI_fetch_result( $sql );
1008   * // produces
1009   * // $users[] = array( 'id' => 123, 'name' => 'John Doe' )
1010   *
1011   * $sql = 'SELECT `id`, `name` FROM `user`';
1012   * $users = PMA_DBI_fetch_result( $sql, 'id' );
1013   * // produces
1014   * // $users['123'] = array( 'id' => 123, 'name' => 'John Doe' )
1015   *
1016   * $sql = 'SELECT `id`, `name` FROM `user`';
1017   * $users = PMA_DBI_fetch_result( $sql, 0 );
1018   * // produces
1019   * // $users['123'] = array( 0 => 123, 1 => 'John Doe' )
1020   *
1021   * $sql = 'SELECT `id`, `name` FROM `user`';
1022   * $users = PMA_DBI_fetch_result( $sql, 'id', 'name' );
1023   * // or
1024   * $users = PMA_DBI_fetch_result( $sql, 0, 1 );
1025   * // produces
1026   * // $users['123'] = 'John Doe'
1027   *
1028   * $sql = 'SELECT `name` FROM `user`';
1029   * $users = PMA_DBI_fetch_result( $sql );
1030   * // produces
1031   * // $users[] = 'John Doe'
1032   * </code>
1033   *
1034   * @uses    is_string()
1035   * @uses    is_int()
1036   * @uses    PMA_DBI_try_query()
1037   * @uses    PMA_DBI_num_rows()
1038   * @uses    PMA_DBI_num_fields()
1039   * @uses    PMA_DBI_fetch_row()
1040   * @uses    PMA_DBI_fetch_assoc()
1041   * @uses    PMA_DBI_free_result()
1042   * @param   string|mysql_result $result query or mysql result
1043   * @param   string|integer      $key    field-name or offset
1044   *                                      used as key for array
1045   * @param   string|integer      $value  value-name or offset
1046   *                                      used as value for array
1047   * @param   resource            $link   mysql link
1048   * @param   mixed               $options
1049   * @return  array               resultrows or values indexed by $key
1050   */
1051  function PMA_DBI_fetch_result( $result, $key = null, $value = null,
1052      $link = null, $options = 0 )
1053  {
1054      $resultrows = array();
1055  
1056      if ( is_string($result) ) {
1057          $result = PMA_DBI_try_query($result, $link, $options);
1058      }
1059  
1060      // return empty array if result is empty or false
1061      if ( ! $result ) {
1062          return $resultrows;
1063      }
1064  
1065      $fetch_function = 'PMA_DBI_fetch_assoc';
1066  
1067      // no nested array if only one field is in result
1068      if ( null === $key && 1 === PMA_DBI_num_fields($result) ) {
1069          $value = 0;
1070          $fetch_function = 'PMA_DBI_fetch_row';
1071      }
1072  
1073      // if $key is an integer use non associative mysql fetch function
1074      if ( is_int($key) ) {
1075          $fetch_function = 'PMA_DBI_fetch_row';
1076      }
1077  
1078      if ( null === $key && null === $value ) {
1079          while ( $row = $fetch_function($result) ) {
1080              $resultrows[] = $row;
1081          }
1082      } elseif ( null === $key ) {
1083          while ( $row = $fetch_function($result) ) {
1084              $resultrows[] = $row[$value];
1085          }
1086      } elseif ( null === $value ) {
1087          if ( is_array($key) ) {
1088              while ( $row = $fetch_function($result) ) {
1089                  $result_target =& $resultrows;
1090                  foreach ( $key as $key_index ) {
1091                      if ( ! isset( $result_target[$row[$key_index]] ) ) {
1092                          $result_target[$row[$key_index]] = array();
1093                      }
1094                      $result_target =& $result_target[$row[$key_index]];
1095                  }
1096                  $result_target = $row;
1097              }
1098          } else {
1099              while ( $row = $fetch_function($result) ) {
1100                  $resultrows[$row[$key]] = $row;
1101              }
1102          }
1103      } else {
1104          if ( is_array($key) ) {
1105              while ( $row = $fetch_function($result) ) {
1106                  $result_target =& $resultrows;
1107                  foreach ( $key as $key_index ) {
1108                      if ( ! isset( $result_target[$row[$key_index]] ) ) {
1109                          $result_target[$row[$key_index]] = array();
1110                      }
1111                      $result_target =& $result_target[$row[$key_index]];
1112                  }
1113                  $result_target = $row[$value];
1114              }
1115          } else {
1116              while ( $row = $fetch_function($result) ) {
1117                  $resultrows[$row[$key]] = $row[$value];
1118              }
1119          }
1120      }
1121  
1122      PMA_DBI_free_result($result);
1123      return $resultrows;
1124  }
1125  
1126  /**
1127   * return default table engine for given database
1128   *
1129   * @return  string  default table engine
1130   */
1131  function PMA_DBI_get_default_engine()
1132  {
1133      if ( PMA_MYSQL_INT_VERSION > 50002 ) {
1134          return PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'storage_engine\';', 0, 1 );
1135      } else {
1136          return PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'table_type\';', 0, 1 );
1137      }
1138  }
1139  
1140  /**
1141   * Get supported SQL compatibility modes
1142   *
1143   * @return  array   supported SQL compatibility modes
1144   */
1145  function PMA_DBI_getCompatibilities()
1146  {
1147      if (PMA_MYSQL_INT_VERSION < 40100) {
1148          return array();
1149      }
1150      $compats = array('NONE');
1151      if (PMA_MYSQL_INT_VERSION >= 40101) {
1152          $compats[] = 'ANSI';
1153          $compats[] = 'DB2';
1154          $compats[] = 'MAXDB';
1155          $compats[] = 'MYSQL323';
1156          $compats[] = 'MYSQL40';
1157          $compats[] = 'MSSQL';
1158          $compats[] = 'ORACLE';
1159          // removed; in MySQL 5.0.33, this produces exports that
1160          // can't be read by POSTGRESQL (see our bug #1596328)
1161          //$compats[] = 'POSTGRESQL';
1162          if (PMA_MYSQL_INT_VERSION >= 50002) {
1163              $compats[] = 'TRADITIONAL';
1164          }
1165      }
1166      return $compats;
1167  }
1168  
1169  /**
1170   * returns warnings for last query
1171   * 
1172   * @uses    $GLOBALS['userlink']
1173   * @uses    PMA_DBI_fetch_result()
1174   * @param   resource mysql link  $link   mysql link resource
1175   * @return  array   warnings
1176   */
1177  function PMA_DBI_get_warnings($link = null)
1178  {
1179      if (PMA_MYSQL_INT_VERSION < 40100) {
1180          return array();
1181      }
1182      
1183      if (empty($link)) {
1184          if (isset($GLOBALS['userlink'])) {
1185              $link = $GLOBALS['userlink'];
1186          } else {
1187              return array();
1188          }
1189      }
1190      
1191      return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1192  }
1193  
1194  /**
1195   * returns true (int > 0) if current user is superuser
1196   * otherwise 0
1197   *
1198   * @return integer  $is_superuser
1199   */
1200  function PMA_isSuperuser() {
1201      return PMA_DBI_try_query( 'SELECT COUNT(*) FROM mysql.user',
1202          $GLOBALS['userlink'], PMA_DBI_QUERY_STORE );
1203  }
1204  
1205  
1206  /**
1207   * returns an array of PROCEDURE or FUNCTION names for a db
1208   *
1209   * @uses    PMA_DBI_free_result()
1210   * @param   string              $db     db name
1211   * @param   string              $which  PROCEDURE | FUNCTION
1212   * @param   resource            $link   mysql link
1213   *
1214   * @return  array   the procedure names or function names
1215   */
1216  function PMA_DBI_get_procedures_or_functions($db, $which, $link = null) {
1217  
1218      $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1219      $result = array();
1220      foreach ($shows as $one_show) {
1221          if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1222              $result[] = $one_show['Name'];
1223          }
1224      }
1225      return($result);
1226  }
1227  
1228  /**
1229   * returns the definition of a specific PROCEDURE or FUNCTION
1230   *
1231   * @uses    PMA_DBI_fetch_value()
1232   * @param   string              $db     db name
1233   * @param   string              $which  PROCEDURE | FUNCTION
1234   * @param   string              $proc_or_function_name  the procedure name or function name
1235   * @param   resource            $link   mysql link
1236   *
1237   * @return  string              the procedure's or function's definition
1238   */
1239  function PMA_DBI_get_procedure_or_function_def($db, $which, $proc_or_function_name, $link = null) {
1240  
1241      $returned_field = array('PROCEDURE' => 'Create Procedure', 'FUNCTION' => 'Create Function');
1242      $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($proc_or_function_name);
1243      return(PMA_DBI_fetch_value( $query, 0, $returned_field[$which]));
1244  }
1245  ?>


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics