ParseAnalyze.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Parse and analyse a SQL query
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\SqlParser\Utils\Query;
  12. /**
  13. * PhpMyAdmin\ParseAnalyze class
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class ParseAnalyze
  18. {
  19. /**
  20. * Calls the parser on a query
  21. *
  22. * @param string $sql_query the query to parse
  23. * @param string $db the current database
  24. *
  25. * @return array
  26. *
  27. * @access public
  28. */
  29. public static function sqlQuery($sql_query, $db)
  30. {
  31. global $reload;
  32. // @todo: move to returned results (also in all the calling chain)
  33. $GLOBALS['unparsed_sql'] = $sql_query;
  34. // Get details about the SQL query.
  35. $analyzed_sql_results = Query::getAll($sql_query);
  36. extract($analyzed_sql_results);
  37. $table = '';
  38. // If the targeted table (and database) are different than the ones that is
  39. // currently browsed, edit `$db` and `$table` to match them so other elements
  40. // (page headers, links, navigation panel) can be updated properly.
  41. if (! empty($analyzed_sql_results['select_tables'])) {
  42. // Previous table and database name is stored to check if it changed.
  43. $prev_db = $db;
  44. if (count($analyzed_sql_results['select_tables']) > 1) {
  45. /**
  46. * @todo if there are more than one table name in the Select:
  47. * - do not extract the first table name
  48. * - do not show a table name in the page header
  49. * - do not display the sub-pages links)
  50. */
  51. $table = '';
  52. } else {
  53. $table = $analyzed_sql_results['select_tables'][0][0];
  54. if (! empty($analyzed_sql_results['select_tables'][0][1])) {
  55. $db = $analyzed_sql_results['select_tables'][0][1];
  56. }
  57. }
  58. // There is no point checking if a reload is required if we already decided
  59. // to reload. Also, no reload is required for AJAX requests.
  60. $response = Response::getInstance();
  61. if (empty($reload) && ! $response->isAjax()) {
  62. // NOTE: Database names are case-insensitive.
  63. $reload = strcasecmp($db, $prev_db) != 0;
  64. }
  65. // Updating the array.
  66. $analyzed_sql_results['reload'] = $reload;
  67. }
  68. return [
  69. $analyzed_sql_results,
  70. $db,
  71. $table,
  72. ];
  73. }
  74. }