ParseAnalyze.php 2.5 KB

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