Canonicalized queries remove non-executable comments

The comments which do not alter the functionality of a query are now removed
from canonicalized queries.

Also fixed missing semicolon in the comment removal regex and added tests
for comment removal.
This commit is contained in:
Markus Makela 2016-01-11 12:25:33 +02:00
parent 8ee110efa8
commit 54ca1ab2de
4 changed files with 32 additions and 3 deletions

View File

@ -29,3 +29,10 @@ add_test(NAME Internal-CanonicalQueryAlter COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/c
${CMAKE_CURRENT_BINARY_DIR}/alter.output
${CMAKE_CURRENT_SOURCE_DIR}/alter.expected
$<TARGET_FILE:canonizer>)
add_test(NAME Internal-CanonicalQueryComment COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/canontest.sh
${CMAKE_CURRENT_BINARY_DIR}/test.log
${CMAKE_CURRENT_SOURCE_DIR}/comment.sql
${CMAKE_CURRENT_BINARY_DIR}/comment.output
${CMAKE_CURRENT_SOURCE_DIR}/comment.expected
$<TARGET_FILE:canonizer>)

View File

@ -0,0 +1,11 @@
select ?;
select ?;
select ?;
select /*! ? + */ ?;
select /*!? ? + */ ?;
select /*!? ? + */ ?;
SELECT ? ;
SELECT ? /*! +? */;
SELECT ? /*!? +? */;
SELECT ? /*M! +? */;
SELECT ? /*M!? +? */;

View File

@ -0,0 +1,11 @@
select 1;-- comment after statement
select 1;# comment after statement
select /* inline comment */ 1;
select /*! 1 + */ 1;
select /*!300000 1 + */ 1;
select /*!300000 1 + */ 1;
SELECT 2 /* +1 */;
SELECT 1 /*! +1 */;
SELECT 1 /*!50101 +1 */;
SELECT 2 /*M! +1 */;
SELECT 2 /*M!50101 +1 */;

View File

@ -2034,13 +2034,13 @@ void skygw_file_close(
#define BUFFER_GROWTH_RATE 1.2
static pcre2_code* remove_comments_re = NULL;
static const PCRE2_SPTR remove_comments_pattern = (PCRE2_SPTR)
"(?:`[^`]*`\\K)|(?:#.*|--[[:space]].*)";
"(?:`[^`]*`\\K)|(\\/[*](?!(M?!)).*?[*]\\/)|(?:#.*|--[[:space:]].*)";
/**
* Remove SQL comments from the end of a string
*
* The inline comments are not removed due to the fact that they can alter the
* behavior of the query.
* The inline executable comments are not removed due to the fact that they can
* alter the behavior of the query.
* @param src Pointer to the string to modify.
* @param srcsize Pointer to a size_t variable which holds the length of the string to
* be modified.