parse_analyze.inc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. *
  13. */
  14. $GLOBALS['unparsed_sql'] = $sql_query;
  15. $parsed_sql = PMA_SQP_parse($sql_query);
  16. $analyzed_sql = PMA_SQP_analyze($parsed_sql);
  17. // for bug 780516: now that we use case insensitive preg_match
  18. // or flags from the analyser, do not put back the reformatted query
  19. // into $sql_query, to make this kind of query work without
  20. // capitalizing keywords:
  21. //
  22. // CREATE TABLE SG_Persons (
  23. // id int(10) unsigned NOT NULL auto_increment,
  24. // first varchar(64) NOT NULL default '',
  25. // PRIMARY KEY (`id`)
  26. // )
  27. // Fills some variables from the analysed SQL
  28. // A table has to be created, renamed, dropped:
  29. // the navigation panel should be reloaded
  30. $reload = isset($analyzed_sql[0]['queryflags']['reload']);
  31. // check for drop database
  32. $drop_database = isset($analyzed_sql[0]['queryflags']['drop_database']);
  33. // for the presence of EXPLAIN
  34. $is_explain = isset($analyzed_sql[0]['queryflags']['is_explain']);
  35. // for the presence of DELETE
  36. $is_delete = isset($analyzed_sql[0]['queryflags']['is_delete']);
  37. // for the presence of UPDATE, DELETE or INSERT|LOAD DATA|REPLACE
  38. $is_affected = isset($analyzed_sql[0]['queryflags']['is_affected']);
  39. // for the presence of REPLACE
  40. $is_replace = isset($analyzed_sql[0]['queryflags']['is_replace']);
  41. // for the presence of INSERT
  42. $is_insert = isset($analyzed_sql[0]['queryflags']['is_insert']);
  43. // for the presence of CHECK|ANALYZE|REPAIR|OPTIMIZE TABLE
  44. $is_maint = isset($analyzed_sql[0]['queryflags']['is_maint']);
  45. // for the presence of SHOW
  46. $is_show = isset($analyzed_sql[0]['queryflags']['is_show']);
  47. // for the presence of PROCEDURE ANALYSE
  48. $is_analyse = isset($analyzed_sql[0]['queryflags']['is_analyse']);
  49. // for the presence of INTO OUTFILE
  50. $is_export = isset($analyzed_sql[0]['queryflags']['is_export']);
  51. // for the presence of GROUP BY|HAVING|SELECT DISTINCT
  52. $is_group = isset($analyzed_sql[0]['queryflags']['is_group']);
  53. // for the presence of SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND
  54. $is_func = isset($analyzed_sql[0]['queryflags']['is_func']);
  55. // for the presence of SELECT COUNT
  56. $is_count = isset($analyzed_sql[0]['queryflags']['is_count']);
  57. // check for a real SELECT ... FROM
  58. $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
  59. // the query contains a subquery
  60. $is_subquery = isset($analyzed_sql[0]['queryflags']['is_subquery']);
  61. // check for CALL
  62. // Since multiple query execution is anyway handled,
  63. // ignore the WHERE clause of the first sql statement
  64. // which might contain a phrase like 'call '
  65. if (isset($analyzed_sql[0]['queryflags']['is_procedure'])
  66. && empty($analyzed_sql[0]['where_clause'])
  67. ) {
  68. $is_procedure = true;
  69. } else {
  70. $is_procedure = false;
  71. }
  72. // aggregates all the results into one array
  73. $analyzed_sql_results = array(
  74. "parsed_sql" => $parsed_sql,
  75. "analyzed_sql" => $analyzed_sql,
  76. "reload" => $reload,
  77. "drop_database" => $drop_database,
  78. "is_explain" => $is_explain,
  79. "is_delete" => $is_delete,
  80. "is_affected" => $is_affected,
  81. "is_replace" => $is_replace,
  82. "is_insert" => $is_insert,
  83. "is_maint" => $is_maint,
  84. "is_show" => $is_show,
  85. "is_analyse" => $is_analyse,
  86. "is_export" => $is_export,
  87. "is_group" => $is_group,
  88. "is_func" => $is_func,
  89. "is_count" => $is_count,
  90. "is_select" => $is_select,
  91. "is_procedure" => $is_procedure,
  92. "is_subquery" => $is_subquery
  93. );
  94. // If the query is a Select, extract the db and table names and modify
  95. // $db and $table, to have correct page headers, links and left frame.
  96. // db and table name may be enclosed with backquotes, db is optionnal,
  97. // query may contain aliases.
  98. /**
  99. * @todo if there are more than one table name in the Select:
  100. * - do not extract the first table name
  101. * - do not show a table name in the page header
  102. * - do not display the sub-pages links)
  103. */
  104. if ($is_select) {
  105. $prev_db = $db;
  106. if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
  107. $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
  108. }
  109. if (isset($analyzed_sql[0]['table_ref'][0]['db'])
  110. && strlen($analyzed_sql[0]['table_ref'][0]['db'])
  111. ) {
  112. $db = $analyzed_sql[0]['table_ref'][0]['db'];
  113. } else {
  114. $db = $prev_db;
  115. }
  116. // Don't change reload, if we already decided to reload in import
  117. if (empty($reload) && empty($GLOBALS['is_ajax_request'])) {
  118. $reload = ($db == $prev_db) ? 0 : 1;
  119. }
  120. }
  121. ?>