Fix removing comments from SQL when there are quoted strings

In the code for removing comments from SQL statements we have to make
sure to only match the '--' characters when they are not inside a quoted
string or identifier. This works fine and as expected for single quotes.
However, for double quotes it doesn't. This is fixed by this commit.

See issue #1270.
This commit is contained in:
Martin Kleusberg
2018-01-05 16:45:50 +01:00
parent 7d2931baa2
commit eae0730e3e

View File

@@ -678,16 +678,16 @@ void SqliteTableModel::removeCommentsFromQuery(QString& query) {
// deal with end-of-line comments
{
/* The regular expression for removing end of line comments works like this:
* ^((?:(?:[^'-]|-(?!-))*|(?:'[^']*'))*)(--.*)$
* ^ $ # anchor beginning and end of string so we use it all
* ( )( ) # two separate capture groups for code and comment
* --.* # comment starts with -- and consumes everything afterwards
* (?: | )* # code is none or many strings alternating with non-strings
* (?:'[^']*') # a string is a quote, followed by none or more non-quotes, followed by a quote
* (?:[^'-]|-(?!-))* # non-string is a sequence of characters which aren't quotes or hyphens,
* OR if they are hyphens then they can't be followed immediately by another hyphen
* ^((?:(?:[^'-]|-(?!-))*|(?:(?P<quote>['"])[^(?P=quote)]*(?P=quote)))*)(--.*)$
* ^ $ # anchor beginning and end of string so we use it all
* ( )( ) # two separate capture groups for code and comment
* --.* # comment starts with -- and consumes everything afterwards
* (?: | )* # code is none or many strings alternating with non-strings
* (?:(?P<quote>['"])[^(?P=quote)]*(?P=quote)) # a string is a quote, followed by none or more non-quotes, followed by a quote
* (?:[^'-]|-(?!-))* # non-string is a sequence of characters which aren't quotes or hyphens,
* OR if they are hyphens then they can't be followed immediately by another hyphen
*/
QRegExp rxSQL("^((?:(?:[^'-]|-(?!-))*|(?:'[^']*'))*)(--[^\\r\\n]*)([\\r\\n]*)(.*)$"); // set up regex to find end-of-line comment
QRegExp rxSQL("^((?:(?:[^'-]|-(?!-))*|(?:'[^']*'))(?:(?P<quote>['\"])[^(?P=quote)]*(?P=quote))*)(--[^\\r\\n]*)([\\r\\n]*)(.*)$"); // set up regex to find end-of-line comment
QString result;
while(query.size() != 0)