use bindValue for the log value filter

This commit is contained in:
Michael Zanetti 2018-05-24 14:23:47 +02:00
parent 80b444a62a
commit 23316942ce
3 changed files with 24 additions and 9 deletions

View File

@ -187,15 +187,23 @@ LogEngine::~LogEngine()
QList<LogEntry> LogEngine::logEntries(const LogFilter &filter) const QList<LogEntry> LogEngine::logEntries(const LogFilter &filter) const
{ {
QList<LogEntry> results; QList<LogEntry> results;
QSqlQuery query; QSqlQuery query(m_db);
QString queryCall = "SELECT * FROM entries ORDER BY timestamp;"; QString queryString;
if (filter.isEmpty()) { if (filter.isEmpty()) {
query = m_db.exec(queryCall); queryString = "SELECT * FROM entries ORDER BY timestamp;";
} else { } else {
queryCall = QString("SELECT * FROM entries WHERE %1 ORDER BY timestamp;").arg(filter.queryString()); queryString = QString("SELECT * FROM entries WHERE %1 ORDER BY timestamp;").arg(filter.queryString());
query = m_db.exec(queryCall);
} }
// qCDebug(dcLogEngine()) << "Preparing query:" << queryString;
query.prepare(queryString);
foreach (const QString &value, filter.values()) {
query.addBindValue(LogValueTool::serializeValue(value));
qCDebug(dcLogEngine()) << "Binding value to query:" << LogValueTool::serializeValue(value);
}
query.exec();
if (m_db.lastError().isValid()) { if (m_db.lastError().isValid()) {
qCWarning(dcLogEngine) << "Error fetching log entries. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText(); qCWarning(dcLogEngine) << "Error fetching log entries. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText();
@ -215,7 +223,7 @@ QList<LogEntry> LogEngine::logEntries(const LogFilter &filter) const
entry.setActive(query.value("active").toBool()); entry.setActive(query.value("active").toBool());
results.append(entry); results.append(entry);
} }
qCDebug(dcLogEngine) << "Fetched" << results.count() << "entries for db query:" << queryCall; // qCDebug(dcLogEngine) << "Fetched" << results.count() << "entries for db query:" << query.executedQuery();
return results; return results;
} }

View File

@ -341,11 +341,11 @@ QString LogFilter::createValuesString() const
QString query; QString query;
if (!m_values.isEmpty()) { if (!m_values.isEmpty()) {
if (m_values.count() == 1) { if (m_values.count() == 1) {
query.append(QString("value = '%1' ").arg(m_values.first())); query.append("value = ? ");
} else { } else {
query.append("( "); query.append("( ");
foreach (const QString &value, m_values) { foreach (const QString &value, m_values) {
query.append(QString("value = '%1' ").arg(value)); query.append("value = ? ");
if (value != m_values.last()) if (value != m_values.last())
query.append("OR "); query.append("OR ");
} }

View File

@ -69,17 +69,24 @@ void TestLoggingDirect::benchmarkDB_data() {
void TestLoggingDirect::benchmarkDB() void TestLoggingDirect::benchmarkDB()
{ {
if (qgetenv("WITH_BENCHMARK").isEmpty()) {
QSKIP("Skipping benchmark tests: export WITH_BENCHMARK=1 to enable it.");
}
QFETCH(int, prefill); QFETCH(int, prefill);
QFETCH(int, maxSize); QFETCH(int, maxSize);
// setting max log entries to "prefill" to trim it down to what this test needs. // setting max log entries to "prefill" to trim it down to what this test needs.
int overflow = 10; int overflow = 10;
qDebug() << "Flushing DB for test";
engine->setMaxLogEntries(prefill, overflow); engine->setMaxLogEntries(prefill, overflow);
engine->setMaxLogEntries(maxSize, overflow); engine->setMaxLogEntries(maxSize, overflow);
qDebug() << "DB has" << engine->logEntries().count() << "entries";
qDebug() << "Prefilling DB for test";
for (int i = engine->logEntries().count(); i < prefill; i++) { for (int i = engine->logEntries().count(); i < prefill; i++) {
engine->logSystemEvent(QDateTime::currentDateTime(), true); engine->logSystemEvent(QDateTime::currentDateTime(), true);
} }
qDebug() << "DB has" << engine->logEntries().count() << "entries";
qDebug() << "Starting benchmark with" << engine->logEntries().count() << "entries in the db"; qDebug() << "Starting benchmark with" << engine->logEntries().count() << "entries in the db";
QBENCHMARK { QBENCHMARK {