From 6429a239da8754f59cb025051eeead5d88a358c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Sat, 8 Oct 2005 20:10:01 +0000 Subject: [PATCH] Parse query before inserting LIMIT to know where to place it. --- ChangeLog | 2 ++ libraries/common.lib.php | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8f0116097..ffaf8c665 100755 --- a/ChangeLog +++ b/ChangeLog @@ -68,6 +68,8 @@ $Source$ string at the end. * import.php: Reenable LIMIT appending (bug #1318624). * sql.php: Fix appending LIMIT when there are comments. + * libraries/common.lib.php: Parse query before inserting LIMIT to know + where to place it. 2005-10-07 Marc Delisle * libraries/check_user_privileges.lib.php: bug #1313821, dbname containing a diff --git a/libraries/common.lib.php b/libraries/common.lib.php index 46116070a..038708ebf 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -1847,6 +1847,16 @@ if (typeof(document.getElementById) != 'undefined' $query_base = $local_query; } + // Parse SQL if needed + if (isset($GLOBALS['parsed_sql']) && $query_base == $GLOBALS['parsed_sql']['raw']) { + $parsed_sql = $GLOBALS['parsed_sql']; + } else { + $parsed_sql = PMA_SQP_parse($query_base); + } + + // Analyze it + $analyzed_display_query = PMA_SQP_analyze($parsed_sql); + // Here we append the LIMIT added for navigation, to // enable its display. Adding it higher in the code // to $local_query would create a problem when @@ -1857,11 +1867,11 @@ if (typeof(document.getElementById) != 'undefined' // FIXME: what would be the best to do when someone // hits Refresh: use the current LIMITs ? - // TODO: use the parser instead of preg_match() - - if (preg_match('@^SELECT[[:space:]]+@i', $query_base) + if (isset($analyzed_display_query[0]['queryflags']['select_from']) && isset($GLOBALS['sql_limit_to_append'])) { - $query_base .= $GLOBALS['sql_limit_to_append']; + $query_base = $analyzed_display_query[0]['section_before_limit'] . "\n" . $GLOBALS['sql_limit_to_append'] . $analyzed_display_query[0]['section_after_limit']; + // Need to reparse query + $parsed_sql = PMA_SQP_parse($query_base); } if (!empty($GLOBALS['show_as_php'])) { @@ -1869,12 +1879,6 @@ if (typeof(document.getElementById) != 'undefined' } else if (!empty($GLOBALS['validatequery'])) { $query_base = PMA_validateSQL($query_base); } else { - // avoid reparsing query: - if (isset($GLOBALS['parsed_sql']) && $query_base == $GLOBALS['parsed_sql']['raw']) { - $parsed_sql = $GLOBALS['parsed_sql']; - } else { - $parsed_sql = PMA_SQP_parse($query_base); - } $query_base = PMA_formatSql($parsed_sql, $query_base); }