only save each entry to the database once

This commit is contained in:
Tobias Fella 2020-03-19 00:40:57 +01:00
parent 552ad82816
commit 0d1c0104db
No known key found for this signature in database
GPG Key ID: E55EDAB3CA5D9925
3 changed files with 12 additions and 7 deletions

View File

@ -51,7 +51,7 @@ bool Database::migrate() {
bool Database::migrateTo1() {
QSqlQuery query(QSqlDatabase::database());
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Feeds (name TEXT, url TEXT);")));
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Entries (feed TEXT, id TEXT UNIQUE, title TEXT, content TEXT);")));
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Entries (feed TEXT, id TEXT UNIQUE, title TEXT, content TEXT, created INTEGER, updated INTEGER);")));
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Authors (id TEXT, name TEXT, uri TEXT, email TEXT);")));
TRUE_OR_RETURN(execute(QStringLiteral("PRAGMA user_version = 1;")));
return true;

View File

@ -80,10 +80,10 @@ void EntryListModel::update() {
beginResetModel();
QSqlQuery query;
if(m_feed.compare("all") == 0) {
query.prepare(QStringLiteral("SELECT id, title, content FROM Entries;"));
query.prepare(QStringLiteral("SELECT id, title, content FROM Entries ORDER BY updated DESC;"));
}
else {
query.prepare(QStringLiteral("SELECT id, title, content FROM Entries WHERE feed=:feed;"));
query.prepare(QStringLiteral("SELECT id, title, content FROM Entries WHERE feed=:feed ORDER BY updated DESC;"));
query.bindValue(QStringLiteral(":feed"), m_feed);
}
Database::instance().execute(query);

View File

@ -43,21 +43,26 @@ void Fetcher::fetch(QUrl url)
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString());
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
QSqlQuery query;
for (const auto &entry : feed->items()) {
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content);"));
query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries WHERE id=:id;"));
query.bindValue(QStringLiteral(":id"), entry->id());
Database::instance().execute(query);
query.next();
if(query.value(0).toInt() != 0) continue;
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated);"));
query.bindValue(QStringLiteral(":feed"), url.toString());
query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":title"), entry->title());
query.bindValue(QStringLiteral(":created"), static_cast<int>(entry->datePublished()));
query.bindValue(QStringLiteral(":updated"), static_cast<int>(entry->dateUpdated()));
if(!entry->content().isEmpty())
query.bindValue(QStringLiteral(":content"), entry->content());
else
query.bindValue(QStringLiteral(":content"), entry->description());
Database::instance().execute(query);
for (const auto &author : entry->authors()) {
query = QSqlQuery(db);
query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:id, :name, :uri, :email);"));
query.bindValue(QStringLiteral(":id"), entry->id());
query.bindValue(QStringLiteral(":name"), author->name());