From e61352c70cacfb8bb00d3cffcd1c41f6a613eef1 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 11 Apr 2018 17:39:46 +0200 Subject: [PATCH] History: Use SqlQueryJob when adding history entry This is called every time page finishes loading and as such would block main thread during normal browsing. Also remove urlIsStored as it is not used anywhere. --- src/lib/history/history.cpp | 101 +++++++++++++++++------------------- src/lib/history/history.h | 2 - 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/src/lib/history/history.cpp b/src/lib/history/history.cpp index 2b22506d8..3bda5897c 100644 --- a/src/lib/history/history.cpp +++ b/src/lib/history/history.cpp @@ -82,54 +82,56 @@ void History::addHistoryEntry(const QUrl &url, QString title) title = tr("Empty Page"); } - QSqlQuery query(SqlDatabase::instance()->database()); - query.prepare("SELECT id, count, date, title FROM history WHERE url=?"); - query.bindValue(0, url); - query.exec(); - if (!query.next()) { - query.prepare("INSERT INTO history (count, date, url, title) VALUES (1,?,?,?)"); - query.bindValue(0, QDateTime::currentMSecsSinceEpoch()); - query.bindValue(1, url); - query.bindValue(2, title); - query.exec(); + auto job = new SqlQueryJob(QSL("SELECT id, count, date, title FROM history WHERE url=?"), this); + job->addBindValue(url); + connect(job, &SqlQueryJob::finished, this, [=]() { + if (job->records().isEmpty()) { + auto job = new SqlQueryJob(QSL("INSERT INTO history (count, date, url, title) VALUES (1,?,?,?)"), this); + job->addBindValue(QDateTime::currentMSecsSinceEpoch()); + job->addBindValue(url); + job->addBindValue(title); + connect(job, &SqlQueryJob::finished, this, [=]() { + HistoryEntry entry; + entry.id = job->lastInsertId().toInt(); + entry.count = 1; + entry.date = QDateTime::currentDateTime(); + entry.url = url; + entry.urlString = url.toEncoded(); + entry.title = title; + emit historyEntryAdded(entry); + }); + job->start(); + } else { + const auto record = job->records().at(0); + const int id = record.value(0).toInt(); + const int count = record.value(1).toInt(); + const QDateTime date = QDateTime::fromMSecsSinceEpoch(record.value(2).toLongLong()); + const QString oldTitle = record.value(3).toString(); - int id = query.lastInsertId().toInt(); - HistoryEntry entry; - entry.id = id; - entry.count = 1; - entry.date = QDateTime::currentDateTime(); - entry.url = url; - entry.urlString = url.toEncoded(); - entry.title = title; - emit historyEntryAdded(entry); - } - else { - int id = query.value(0).toInt(); - int count = query.value(1).toInt(); - QDateTime date = QDateTime::fromMSecsSinceEpoch(query.value(2).toLongLong()); - QString oldTitle = query.value(3).toString(); + auto job = new SqlQueryJob(QSL("UPDATE history SET count = count + 1, date=?, title=? WHERE url=?"), this); + job->addBindValue(QDateTime::currentMSecsSinceEpoch()); + job->addBindValue(title); + job->addBindValue(url); + connect(job, &SqlQueryJob::finished, this, [=]() { + HistoryEntry before; + before.id = id; + before.count = count; + before.date = date; + before.url = url; + before.urlString = url.toEncoded(); + before.title = oldTitle; - query.prepare("UPDATE history SET count = count + 1, date=?, title=? WHERE url=?"); - query.bindValue(0, QDateTime::currentMSecsSinceEpoch()); - query.bindValue(1, title); - query.bindValue(2, url); - query.exec(); + HistoryEntry after = before; + after.count = count + 1; + after.date = QDateTime::currentDateTime(); + after.title = title; - HistoryEntry before; - before.id = id; - before.count = count; - before.date = date; - before.url = url; - before.urlString = url.toEncoded(); - before.title = oldTitle; - - HistoryEntry after = before; - after.count = count + 1; - after.date = QDateTime::currentDateTime(); - after.title = title; - - emit historyEntryEdited(before, after); - } + emit historyEntryEdited(before, after); + }); + job->start(); + } + }); + job->start(); } // DeleteHistoryEntry @@ -212,15 +214,6 @@ QList History::indexesFromTimeRange(qint64 start, qint64 end) return list; } -bool History::urlIsStored(const QString &url) -{ - QSqlQuery query(SqlDatabase::instance()->database()); - query.prepare("SELECT id FROM history WHERE url=?"); - query.bindValue(0, url); - query.exec(); - return query.next(); -} - QVector History::mostVisited(int count) { QVector list; diff --git a/src/lib/history/history.h b/src/lib/history/history.h index b0d0171ac..ad634d2ee 100644 --- a/src/lib/history/history.h +++ b/src/lib/history/history.h @@ -58,8 +58,6 @@ public: QList indexesFromTimeRange(qint64 start, qint64 end); - bool urlIsStored(const QString &url); - QVector mostVisited(int count); void clearHistory();