Line data Source code
1 : #include "ReloadNewsOperation.h"
2 : #include "../utilities/FangLogging.h"
3 :
4 : #include <QSqlQuery>
5 : #include <QSqlError>
6 :
7 4 : ReloadNewsOperation::ReloadNewsOperation(OperationManager *parent, FeedItem* feedItem, const QList<qint64>& ids) :
8 : DBOperation(parent),
9 4 : feedItem(feedItem),
10 4 : idsToReload(ids),
11 4 : reloadedItems()
12 : {
13 4 : }
14 :
15 4 : void ReloadNewsOperation::execute()
16 : {
17 4 : if (idsToReload.isEmpty()) {
18 2 : qCDebug(logOperation) << "ReloadNewsOperation: No IDs to reload";
19 1 : return;
20 : }
21 :
22 : // Build the IN clause for the query
23 3 : QStringList placeholders;
24 11 : for (int i = 0; i < idsToReload.size(); ++i) {
25 8 : placeholders << QString(":id%1").arg(i);
26 : }
27 :
28 3 : QString queryString = QString(
29 : "SELECT id, feed_id, title, author, summary, content, timestamp, url, pinned, media_image_url "
30 : "FROM NewsItemTable "
31 : "WHERE id IN (%1) "
32 : "ORDER BY timestamp ASC, id ASC"
33 3 : ).arg(placeholders.join(", "));
34 :
35 3 : QSqlQuery query(db());
36 3 : query.prepare(queryString);
37 :
38 : // Bind the ID values
39 11 : for (int i = 0; i < idsToReload.size(); ++i) {
40 8 : query.bindValue(QString(":id%1").arg(i), idsToReload.at(i));
41 : }
42 :
43 3 : if (!query.exec()) {
44 0 : qCWarning(logOperation) << "ReloadNewsOperation: Query failed:" << query.lastError().text();
45 0 : return;
46 : }
47 :
48 : // Create NewsItem objects from results
49 8 : while (query.next()) {
50 : NewsItem* newsItem = new NewsItem(
51 : feedItem,
52 5 : query.value("id").toLongLong(),
53 10 : query.value("feed_id").toLongLong(),
54 10 : query.value("title").toString(),
55 10 : query.value("author").toString(),
56 10 : query.value("summary").toString(),
57 10 : query.value("content").toString(),
58 10 : QDateTime::fromMSecsSinceEpoch(query.value("timestamp").toLongLong()),
59 10 : query.value("url").toString(),
60 5 : query.value("pinned").toBool(),
61 10 : query.value("media_image_url").toString()
62 35 : );
63 :
64 5 : reloadedItems.append(newsItem);
65 : }
66 :
67 6 : qCDebug(logOperation) << "ReloadNewsOperation: Reloaded" << reloadedItems.size()
68 3 : << "items for feed" << feedItem->getDbID();
69 3 : }
|