Fix for comment problems in Execute Query tab

See issue #1004.
This commit is contained in:
Jonathan Oakley
2017-08-10 20:51:30 +02:00
committed by Martin Kleusberg
parent 44d64f2a8f
commit e3a7d9aa75
3 changed files with 55 additions and 2 deletions

View File

@@ -932,6 +932,8 @@ void MainWindow::executeQuery()
sqlWidget->getEditor()->getSelection(&execution_start_line, &execution_start_index, &dummy, &dummy);
}
SqliteTableModel::removeCommentsFromQuery(query);
if (query.trimmed().isEmpty() || query.trimmed() == ";")
return;

View File

@@ -606,7 +606,56 @@ void SqliteTableModel::buildQuery()
}
void SqliteTableModel::removeCommentsFromQuery(QString& query) {
query.remove(QRegExp("\\s*--[^\\n]+"));
// first remove block comments
{
QRegExp rxSQL("^((?:(?:[^'/]|/(?![*]))*|'[^']*')*)(/[*](?:[^*]|[*](?!/))*[*]/)(.*)$"); // set up regex to find block comment
QString result;
while(query.size() != 0)
{
int pos = rxSQL.indexIn(query);
if(pos > -1)
{
result += rxSQL.cap(1) + " ";
query = rxSQL.cap(3);
} else {
result += query;
query = "";
}
}
query = result;
}
// 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
*/
QRegExp rxSQL("^((?:(?:[^'-]|-(?!-))*|(?:'[^']*'))*)(--[^\\r\\n]*)([\\r\\n]*)(.*)$"); // set up regex to find end-of-line comment
QString result;
while(query.size() != 0)
{
int pos = rxSQL.indexIn(query);
if(pos > -1)
{
result += rxSQL.cap(1) + rxSQL.cap(3);
query = rxSQL.cap(4);
} else {
result += query;
query = "";
}
}
query = result.trimmed();
}
}
QStringList SqliteTableModel::getColumns(const QString& sQuery, QVector<int>& fieldsTypes)

View File

@@ -64,6 +64,9 @@ public:
// the model is currently editable, i.e. it's running in table mode.
bool isEditable() const;
// Helper function for removing all comments from a SQL query
static void removeCommentsFromQuery(QString& query);
public slots:
void updateFilter(int column, const QString& value);
@@ -76,7 +79,6 @@ private:
void clearCache();
void buildQuery();
void removeCommentsFromQuery(QString& query);
QStringList getColumns(const QString& sQuery, QVector<int>& fieldsTypes);
int getQueryRowCount();