diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3d201404..07dbd199 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -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; diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index a655f475..ce802fa3 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -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& fieldsTypes) diff --git a/src/sqlitetablemodel.h b/src/sqlitetablemodel.h index abb18779..445f58a0 100644 --- a/src/sqlitetablemodel.h +++ b/src/sqlitetablemodel.h @@ -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& fieldsTypes); int getQueryRowCount();