Simplify dependencies and remove unneeded dependencies from tests

This moves the code to remove comments from SQL statements from the
SqliteTableModel class to Data.cpp making it a free function. This
removes some dependencies from the SqliteTableModel class with all its
dependencies.

Also simplify the CMakeLists.txt file for the tests by removing all the
dependencies which are not really required.
This commit is contained in:
Martin Kleusberg
2021-08-22 10:31:31 +02:00
parent 302ade704c
commit a302128d2e
7 changed files with 74 additions and 160 deletions
+57
View File
@@ -4,6 +4,7 @@
#include <QDateTime>
#include <QImageReader>
#include <QLocale>
#include <QRegExp>
#include <QTextCodec>
#include <QFile>
@@ -175,3 +176,59 @@ QString isoDateTimeStringToLocalDateTimeString(const QString& date_string)
{
return QLocale::system().toString(QDateTime::fromString(date_string, Qt::ISODate).toLocalTime(), QLocale::ShortFormat);
}
void removeCommentsFromQuery(QString& query)
{
// Store the current size so we can easily check later if the string has been changed
int oldSize = query.size();
// This implements a simple state machine to strip the query from comments
QChar quote;
for(int i=0;i<query.size();i++)
{
// Are we in quote state?
if(quote.isNull())
{
// We are currently not in quote state
// So are we starting a quote?
if((query.at(i) == '\'' || query.at(i) == '\"' || query.at(i) == '[') && (i == 0 || query.at(i-1) != '\\'))
{
// Quoted text is beginning. Switch to the quote state
quote = query.at(i);
} else if(query.at(i) == '-' && i+1 < query.size() && query.at(i+1) == '-') {
// This is an end of line comment. Remove anything till the end of the line or the end of the string if this is the last line
int pos_next_line_break = query.indexOf('\n', i);
if(pos_next_line_break == -1)
query = query.left(i);
else
query.remove(i, pos_next_line_break - i); // The \n is left in intentionally
} else if(query.at(i) == '/' && i+1 < query.size() && query.at(i+1) == '*') {
// This is a block comment. Remove anything till the end of the block or the end of the string if the block is not closed
int pos_end_comment = query.indexOf("*/", i);
if(pos_end_comment == -1)
query = query.left(i);
else
query.remove(i, pos_end_comment - i + 2); // Add 2 to include the */
}
} else {
// We are currently in quote state
// If this is the closing quote character, switch back to normal state
if((query.at(i) == quote) && (i == 0 || query.at(i-1) != '\\'))
quote = 0;
}
}
query = query.trimmed();
if (oldSize != query.size()) {
// Remove multiple line breaks that might have been created by deleting comments till the end of the line but not including the line break
query.replace(QRegExp("\\n+"), "\n");
// Also remove any remaining whitespace at the end of each line
query.replace(QRegExp("[ \t]+\n"), "\n");
}
}
+3
View File
@@ -37,4 +37,7 @@ QString humanReadableSize(unsigned long byteCount);
QString isoDateTimeStringToLocalDateTimeString(const QString& date_string);
// Helper function for removing all comments from a SQL query
void removeCommentsFromQuery(QString& query);
#endif
+2 -2
View File
@@ -1,7 +1,7 @@
#include "RunSql.h"
#include "sqlite.h"
#include "sqlitedb.h"
#include "sqlitetablemodel.h"
#include "Data.h"
#include <chrono>
#include <QApplication>
@@ -86,7 +86,7 @@ bool RunSql::executeNextStatement()
// Remove trailing comments so we don't get fooled by some trailing text at the end of the stream.
// Otherwise we'll pass them to SQLite and its execution will trigger a savepoint that wouldn't be
// reverted.
SqliteTableModel::removeCommentsFromQuery(qtail);
removeCommentsFromQuery(qtail);
if (qtail.isEmpty())
return false;
-56
View File
@@ -717,62 +717,6 @@ void SqliteTableModel::updateAndRunQuery()
emit layoutChanged();
}
void SqliteTableModel::removeCommentsFromQuery(QString& query)
{
// Store the current size so we can easily check later if the string has been changed
int oldSize = query.size();
// This implements a simple state machine to strip the query from comments
QChar quote;
for(int i=0;i<query.size();i++)
{
// Are we in quote state?
if(quote.isNull())
{
// We are currently not in quote state
// So are we starting a quote?
if((query.at(i) == '\'' || query.at(i) == '\"' || query.at(i) == '[') && (i == 0 || query.at(i-1) != '\\'))
{
// Quoted text is beginning. Switch to the quote state
quote = query.at(i);
} else if(query.at(i) == '-' && i+1 < query.size() && query.at(i+1) == '-') {
// This is an end of line comment. Remove anything till the end of the line or the end of the string if this is the last line
int pos_next_line_break = query.indexOf('\n', i);
if(pos_next_line_break == -1)
query = query.left(i);
else
query.remove(i, pos_next_line_break - i); // The \n is left in intentionally
} else if(query.at(i) == '/' && i+1 < query.size() && query.at(i+1) == '*') {
// This is a block comment. Remove anything till the end of the block or the end of the string if the block is not closed
int pos_end_comment = query.indexOf("*/", i);
if(pos_end_comment == -1)
query = query.left(i);
else
query.remove(i, pos_end_comment - i + 2); // Add 2 to include the */
}
} else {
// We are currently in quote state
// If this is the closing quote character, switch back to normal state
if((query.at(i) == quote) && (i == 0 || query.at(i-1) != '\\'))
quote = 0;
}
}
query = query.trimmed();
if (oldSize != query.size()) {
// Remove multiple line breaks that might have been created by deleting comments till the end of the line but not including the line break
query.replace(QRegExp("\\n+"), "\n");
// Also remove any remaining whitespace at the end of each line
query.replace(QRegExp("[ \t]+\n"), "\n");
}
}
void SqliteTableModel::getColumnNames(const std::string& sQuery)
{
auto pDb = m_db.get(tr("retrieving list of columns"));
-3
View File
@@ -116,9 +116,6 @@ public:
// can be edited. This makes a difference for generated columns which are in (editable) tables but cannot be modified anyway.
bool isEditable(const QModelIndex& index = QModelIndex()) const;
// Helper function for removing all comments from a SQL query
static void removeCommentsFromQuery(QString& query);
// Conditional formats are of two kinds: regular conditional formats (including condition-free formats applying to any value in the
// column) and formats applying to a particular row-id and which have always precedence over the first kind and whose filter apply
// to the row-id column.
+10 -97
View File
@@ -1,25 +1,14 @@
include_directories("${CMAKE_CURRENT_BINARY_DIR}" ..)
if(NOT WIN32)
set(LPTHREAD pthread)
endif()
find_package(Qt5 REQUIRED COMPONENTS Test Widgets)
# test-sqlobjects
set(TESTSQLOBJECTS_SRC
../sqlitedb.cpp
../sqlitetablemodel.cpp
../RowLoader.cpp
../sql/sqlitetypes.cpp
../sql/Query.cpp
../sql/ObjectIdentifier.cpp
../csvparser.cpp
../Settings.cpp
testsqlobjects.cpp
../Data.cpp
../CipherSettings.cpp
../DotenvFormat.cpp
../CondFormat.cpp
../sql/parser/ParserDriver.cpp
../sql/parser/sqlite3_lexer.cpp
../sql/parser/sqlite3_parser.cpp
@@ -29,40 +18,15 @@ set(TESTSQLOBJECTS_HDR
../sql/sqlitetypes.h
../sql/Query.h
../sql/ObjectIdentifier.h
../Data.h
../sql/parser/ParserDriver.h
../sql/parser/sqlite3_lexer.h
../sql/parser/sqlite3_location.h
../sql/parser/sqlite3_parser.hpp
)
set(TESTSQLOBJECTS_MOC_HDR
../sqlitedb.h
../sqlitetablemodel.h
../Settings.h
testsqlobjects.h
../CipherSettings.h
../DotenvFormat.h
../CondFormat.h
)
if(sqlcipher)
list(APPEND TESTSQLOBJECTS_SRC ../CipherDialog.cpp)
list(APPEND TESTSQLOBJECTS_FORMS ../CipherDialog.ui)
list(APPEND TESTSQLOBJECTS_MOC_HDR ../CipherDialog.h)
endif()
QT5_WRAP_UI(TESTSQLOBJECTS_FORM_HDR ${TESTSQLOBJECTS_FORMS})
add_executable(test-sqlobjects ${TESTSQLOBJECTS_MOC} ${TESTSQLOBJECTS_HDR} ${TESTSQLOBJECTS_SRC} ${TESTSQLOBJECTS_FORM_HDR})
find_package(Qt5 REQUIRED COMPONENTS Test Widgets Gui)
target_link_libraries(test-sqlobjects Qt5::Test Qt5::Widgets Qt5::Gui)
set(QT_LIBRARIES "")
target_link_libraries(test-sqlobjects ${QT_LIBRARIES} ${LIBSQLITE})
target_link_libraries(test-sqlobjects ${LPTHREAD})
add_executable(test-sqlobjects ${TESTSQLOBJECTS_HDR} ${TESTSQLOBJECTS_SRC})
target_link_libraries(test-sqlobjects Qt5::Test)
add_test(test-sqlobjects test-sqlobjects)
# test-import
@@ -76,70 +40,24 @@ set(TESTIMPORT_MOC_HDR
TestImport.h
)
add_executable(test-import ${TESTIMPORT_MOC} ${TESTIMPORT_SRC})
find_package(Qt5 REQUIRED COMPONENTS Core)
target_link_libraries(test-import Qt5::Test Qt5::Core)
set(QT_LIBRARIES "")
target_link_libraries(test-import ${QT_LIBRARIES})
add_executable(test-import ${TESTIMPORT_MOC_HDR} ${TESTIMPORT_SRC})
target_link_libraries(test-import Qt5::Test)
add_test(test-import test-import)
# test regex
set(TESTREGEX_SRC
../sqlitedb.cpp
../sqlitetablemodel.cpp
../RowLoader.cpp
../sql/sqlitetypes.cpp
../sql/Query.cpp
../sql/ObjectIdentifier.cpp
../Settings.cpp
TestRegex.cpp
../Data.cpp
../CipherSettings.cpp
../DotenvFormat.cpp
../CondFormat.cpp
../sql/parser/ParserDriver.cpp
../sql/parser/sqlite3_lexer.cpp
../sql/parser/sqlite3_parser.cpp
)
set(TESTREGEX_HDR
../sql/sqlitetypes.h
../sql/Query.h
../sql/ObjectIdentifier.h
../Data.h
../sql/parser/ParserDriver.h
../sql/parser/sqlite3_lexer.h
../sql/parser/sqlite3_location.h
../sql/parser/sqlite3_parser.hpp
)
set(TESTREGEX_MOC_HDR
../sqlitedb.h
../sqlitetablemodel.h
../Settings.h
TestRegex.h
../CipherSettings.h
../DotenvFormat.h
../CondFormat.h
)
if(sqlcipher)
list(APPEND TESTREGEX_SRC ../CipherDialog.cpp)
list(APPEND TESTREGEX_MOC_HDR ../CipherDialog.h)
endif()
add_executable(test-regex ${TESTREGEX_MOC} ${TESTREGEX_HDR} ${TESTREGEX_SRC})
target_link_libraries(test-regex Qt5::Test Qt5::Core Qt5::Gui Qt5::Widgets)
set(QT_LIBRARIES "")
target_link_libraries(test-regex ${QT_LIBRARIES} ${LIBSQLITE})
target_link_libraries(test-regex ${LPTHREAD})
add_executable(test-regex ${TESTREGEX_HDR} ${TESTREGEX_SRC})
target_link_libraries(test-regex Qt5::Test Qt5::Widgets)
add_test(test-regex test-regex)
# test cache
@@ -148,15 +66,10 @@ set(TESTCACHE_SRC
TestRowCache.cpp
)
set(TESTCACHE_MOC_HDR
set(TESTCACHE_HDR
TestRowCache.h
)
add_executable(test-cache ${TESTCACHE_MOC} ${TESTCACHE_SRC})
target_link_libraries(test-cache Qt5::Test Qt5::Core)
set(QT_LIBRARIES "")
target_link_libraries(test-cache ${QT_LIBRARIES})
add_executable(test-cache ${TESTCACHE_HDR} ${TESTCACHE_SRC})
target_link_libraries(test-cache Qt5::Test)
add_test(test-cache test-cache)
+2 -2
View File
@@ -1,5 +1,5 @@
#include "TestRegex.h"
#include "../sqlitetablemodel.h"
#include "../Data.h"
#include <QtTest/QTest>
@@ -76,6 +76,6 @@ void TestRegex::sqlQueryComments()
QFETCH(QString, dirtyQuery);
QFETCH(QString, clearQuery);
SqliteTableModel::removeCommentsFromQuery(dirtyQuery);
removeCommentsFromQuery(dirtyQuery);
QCOMPARE(dirtyQuery, clearQuery);
}