mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user