ParseAnalyze.php 2.4 KB

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