1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

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.
This commit is contained in:
David Rosca 2018-04-11 17:39:46 +02:00
parent 8a5c69b8a1
commit e61352c70c
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
2 changed files with 47 additions and 56 deletions

View File

@ -82,54 +82,56 @@ void History::addHistoryEntry(const QUrl &url, QString title)
title = tr("Empty Page"); title = tr("Empty Page");
} }
QSqlQuery query(SqlDatabase::instance()->database()); auto job = new SqlQueryJob(QSL("SELECT id, count, date, title FROM history WHERE url=?"), this);
query.prepare("SELECT id, count, date, title FROM history WHERE url=?"); job->addBindValue(url);
query.bindValue(0, url); connect(job, &SqlQueryJob::finished, this, [=]() {
query.exec(); if (job->records().isEmpty()) {
if (!query.next()) { auto job = new SqlQueryJob(QSL("INSERT INTO history (count, date, url, title) VALUES (1,?,?,?)"), this);
query.prepare("INSERT INTO history (count, date, url, title) VALUES (1,?,?,?)"); job->addBindValue(QDateTime::currentMSecsSinceEpoch());
query.bindValue(0, QDateTime::currentMSecsSinceEpoch()); job->addBindValue(url);
query.bindValue(1, url); job->addBindValue(title);
query.bindValue(2, title); connect(job, &SqlQueryJob::finished, this, [=]() {
query.exec(); 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(); auto job = new SqlQueryJob(QSL("UPDATE history SET count = count + 1, date=?, title=? WHERE url=?"), this);
HistoryEntry entry; job->addBindValue(QDateTime::currentMSecsSinceEpoch());
entry.id = id; job->addBindValue(title);
entry.count = 1; job->addBindValue(url);
entry.date = QDateTime::currentDateTime(); connect(job, &SqlQueryJob::finished, this, [=]() {
entry.url = url; HistoryEntry before;
entry.urlString = url.toEncoded(); before.id = id;
entry.title = title; before.count = count;
emit historyEntryAdded(entry); before.date = date;
} before.url = url;
else { before.urlString = url.toEncoded();
int id = query.value(0).toInt(); before.title = oldTitle;
int count = query.value(1).toInt();
QDateTime date = QDateTime::fromMSecsSinceEpoch(query.value(2).toLongLong());
QString oldTitle = query.value(3).toString();
query.prepare("UPDATE history SET count = count + 1, date=?, title=? WHERE url=?"); HistoryEntry after = before;
query.bindValue(0, QDateTime::currentMSecsSinceEpoch()); after.count = count + 1;
query.bindValue(1, title); after.date = QDateTime::currentDateTime();
query.bindValue(2, url); after.title = title;
query.exec();
HistoryEntry before; emit historyEntryEdited(before, after);
before.id = id; });
before.count = count; job->start();
before.date = date; }
before.url = url; });
before.urlString = url.toEncoded(); job->start();
before.title = oldTitle;
HistoryEntry after = before;
after.count = count + 1;
after.date = QDateTime::currentDateTime();
after.title = title;
emit historyEntryEdited(before, after);
}
} }
// DeleteHistoryEntry // DeleteHistoryEntry
@ -212,15 +214,6 @@ QList<int> History::indexesFromTimeRange(qint64 start, qint64 end)
return list; 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<HistoryEntry> History::mostVisited(int count) QVector<HistoryEntry> History::mostVisited(int count)
{ {
QVector<HistoryEntry> list; QVector<HistoryEntry> list;

View File

@ -58,8 +58,6 @@ public:
QList<int> indexesFromTimeRange(qint64 start, qint64 end); QList<int> indexesFromTimeRange(qint64 start, qint64 end);
bool urlIsStored(const QString &url);
QVector<HistoryEntry> mostVisited(int count); QVector<HistoryEntry> mostVisited(int count);
void clearHistory(); void clearHistory();