mirror of https://github.com/KDE/kasts.git
only save each entry to the database once
This commit is contained in:
parent
552ad82816
commit
0d1c0104db
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue