From 59e4dc528ef225a8955f361567ba98e0883a15f3 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Tue, 23 Jun 2020 13:55:00 +0200 Subject: [PATCH] some code cleanups --- .../miscellaneous/databasequeries.cpp | 69 ------------------- .../miscellaneous/databasequeries.h | 39 ++++++++++- .../services/gmail/gmailserviceroot.cpp | 2 +- .../inoreader/inoreaderserviceroot.cpp | 2 +- .../services/owncloud/owncloudserviceroot.cpp | 2 +- .../services/standard/standardserviceroot.cpp | 2 +- .../services/tt-rss/ttrssserviceroot.cpp | 2 +- 7 files changed, 43 insertions(+), 75 deletions(-) diff --git a/src/librssguard/miscellaneous/databasequeries.cpp b/src/librssguard/miscellaneous/databasequeries.cpp index ded7e5cab..c02bb9bbe 100755 --- a/src/librssguard/miscellaneous/databasequeries.cpp +++ b/src/librssguard/miscellaneous/databasequeries.cpp @@ -1425,41 +1425,6 @@ QList DatabaseQueries::getStandardAccounts(const QSqlDatabase& db, return roots; } -Assignment DatabaseQueries::getStandardCategories(const QSqlDatabase& db, int account_id, bool* ok) { - Assignment categories; - - // Obtain data for categories from the database. - QSqlQuery q(db); - - q.setForwardOnly(true); - q.prepare(QSL("SELECT * FROM Categories WHERE account_id = :account_id;")); - q.bindValue(QSL(":account_id"), account_id); - - if (!q.exec()) { - qFatal("Query for obtaining categories failed. Error message: '%s'.", - qPrintable(q.lastError().text())); - - if (ok != nullptr) { - *ok = false; - } - } - else { - if (ok != nullptr) { - *ok = true; - } - } - - while (q.next()) { - AssignmentItem pair; - - pair.first = q.value(CAT_DB_PARENT_ID_INDEX).toInt(); - pair.second = new StandardCategory(q.record()); - categories << pair; - } - - return categories; -} - bool DatabaseQueries::deleteTtRssAccount(const QSqlDatabase& db, int account_id) { QSqlQuery q(db); @@ -1528,40 +1493,6 @@ bool DatabaseQueries::createTtRssAccount(const QSqlDatabase& db, int id_to_assig } } -Assignment DatabaseQueries::getCategories(const QSqlDatabase& db, int account_id, bool* ok) { - Assignment categories; - - // Obtain data for categories from the database. - QSqlQuery query_categories(db); - - query_categories.setForwardOnly(true); - query_categories.prepare(QSL("SELECT * FROM Categories WHERE account_id = :account_id;")); - query_categories.bindValue(QSL(":account_id"), account_id); - - if (!query_categories.exec()) { - qFatal("Query for obtaining categories failed. Error message: '%s'.", qPrintable(query_categories.lastError().text())); - - if (ok != nullptr) { - *ok = false; - } - } - else { - if (ok != nullptr) { - *ok = true; - } - } - - while (query_categories.next()) { - AssignmentItem pair; - - pair.first = query_categories.value(CAT_DB_PARENT_ID_INDEX).toInt(); - pair.second = new Category(query_categories.record()); - categories << pair; - } - - return categories; -} - QList DatabaseQueries::getGmailAccounts(const QSqlDatabase& db, bool* ok) { QSqlQuery query(db); QList roots; diff --git a/src/librssguard/miscellaneous/databasequeries.h b/src/librssguard/miscellaneous/databasequeries.h index e74f501c2..173661402 100644 --- a/src/librssguard/miscellaneous/databasequeries.h +++ b/src/librssguard/miscellaneous/databasequeries.h @@ -5,6 +5,7 @@ #include "services/abstract/rootitem.h" +#include "services/abstract/category.h" #include "services/abstract/serviceroot.h" #include "services/standard/standardfeed.h" @@ -75,6 +76,8 @@ class DatabaseQueries { static bool storeAccountTree(const QSqlDatabase& db, RootItem* tree_root, int account_id); static bool editBaseFeed(const QSqlDatabase& db, int feed_id, Feed::AutoUpdateType auto_update_type, int auto_update_interval); + + template static Assignment getCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr); template @@ -99,7 +102,6 @@ class DatabaseQueries { const QString& username, const QString& password, Feed::AutoUpdateType auto_update_type, int auto_update_interval, StandardFeed::Type feed_format); static QList getStandardAccounts(const QSqlDatabase& db, bool* ok = nullptr); - static Assignment getStandardCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr); template static void fillFeedData(T* feed, const QSqlRecord& sql_record); @@ -174,6 +176,41 @@ inline void DatabaseQueries::fillFeedData(StandardFeed* feed, const QSqlRecord& } } +template +Assignment DatabaseQueries::getCategories(const QSqlDatabase& db, int account_id, bool* ok) { + Assignment categories; + + // Obtain data for categories from the database. + QSqlQuery query_categories(db); + + query_categories.setForwardOnly(true); + query_categories.prepare(QSL("SELECT * FROM Categories WHERE account_id = :account_id;")); + query_categories.bindValue(QSL(":account_id"), account_id); + + if (!query_categories.exec()) { + qFatal("Query for obtaining categories failed. Error message: '%s'.", qPrintable(query_categories.lastError().text())); + + if (ok != nullptr) { + *ok = false; + } + } + else { + if (ok != nullptr) { + *ok = true; + } + } + + while (query_categories.next()) { + AssignmentItem pair; + + pair.first = query_categories.value(CAT_DB_PARENT_ID_INDEX).toInt(); + pair.second = new T(query_categories.record()); + categories << pair; + } + + return categories; +} + template Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db, int account_id, bool* ok) { Assignment feeds; diff --git a/src/librssguard/services/gmail/gmailserviceroot.cpp b/src/librssguard/services/gmail/gmailserviceroot.cpp index de6c16b3e..7c2c33e8c 100644 --- a/src/librssguard/services/gmail/gmailserviceroot.cpp +++ b/src/librssguard/services/gmail/gmailserviceroot.cpp @@ -58,7 +58,7 @@ void GmailServiceRoot::writeNewEmail() { void GmailServiceRoot::loadFromDatabase() { QSqlDatabase database = qApp->database()->connection(metaObject()->className()); - Assignment categories = DatabaseQueries::getCategories(database, accountId()); + Assignment categories = DatabaseQueries::getCategories(database, accountId()); Assignment feeds = DatabaseQueries::getFeeds(database, accountId()); // All data are now obtained, lets create the hierarchy. diff --git a/src/librssguard/services/inoreader/inoreaderserviceroot.cpp b/src/librssguard/services/inoreader/inoreaderserviceroot.cpp index 47957d3c6..9ae5b3243 100644 --- a/src/librssguard/services/inoreader/inoreaderserviceroot.cpp +++ b/src/librssguard/services/inoreader/inoreaderserviceroot.cpp @@ -50,7 +50,7 @@ void InoreaderServiceRoot::updateTitle() { void InoreaderServiceRoot::loadFromDatabase() { QSqlDatabase database = qApp->database()->connection(metaObject()->className()); - Assignment categories = DatabaseQueries::getCategories(database, accountId()); + Assignment categories = DatabaseQueries::getCategories(database, accountId()); Assignment feeds = DatabaseQueries::getFeeds(database, accountId()); // All data are now obtained, lets create the hierarchy. diff --git a/src/librssguard/services/owncloud/owncloudserviceroot.cpp b/src/librssguard/services/owncloud/owncloudserviceroot.cpp index 821fcc93b..07e5f3067 100644 --- a/src/librssguard/services/owncloud/owncloudserviceroot.cpp +++ b/src/librssguard/services/owncloud/owncloudserviceroot.cpp @@ -195,7 +195,7 @@ RootItem* OwnCloudServiceRoot::obtainNewTreeForSyncIn() const { void OwnCloudServiceRoot::loadFromDatabase() { QSqlDatabase database = qApp->database()->connection(metaObject()->className()); - Assignment categories = DatabaseQueries::getCategories(database, accountId()); + Assignment categories = DatabaseQueries::getCategories(database, accountId()); Assignment feeds = DatabaseQueries::getFeeds(database, accountId()); // All data are now obtained, lets create the hierarchy. diff --git a/src/librssguard/services/standard/standardserviceroot.cpp b/src/librssguard/services/standard/standardserviceroot.cpp index be899840b..4043ba1c5 100644 --- a/src/librssguard/services/standard/standardserviceroot.cpp +++ b/src/librssguard/services/standard/standardserviceroot.cpp @@ -132,7 +132,7 @@ Qt::ItemFlags StandardServiceRoot::additionalFlags() const { void StandardServiceRoot::loadFromDatabase() { QSqlDatabase database = qApp->database()->connection(metaObject()->className()); - Assignment categories = DatabaseQueries::getStandardCategories(database, accountId()); + Assignment categories = DatabaseQueries::getCategories(database, accountId()); Assignment feeds = DatabaseQueries::getFeeds(database, accountId()); // All data are now obtained, lets create the hierarchy. diff --git a/src/librssguard/services/tt-rss/ttrssserviceroot.cpp b/src/librssguard/services/tt-rss/ttrssserviceroot.cpp index a1de18358..2c5c47d36 100644 --- a/src/librssguard/services/tt-rss/ttrssserviceroot.cpp +++ b/src/librssguard/services/tt-rss/ttrssserviceroot.cpp @@ -206,7 +206,7 @@ void TtRssServiceRoot::saveAccountDataToDatabase() { void TtRssServiceRoot::loadFromDatabase() { QSqlDatabase database = qApp->database()->connection(metaObject()->className()); - Assignment categories = DatabaseQueries::getCategories(database, accountId()); + Assignment categories = DatabaseQueries::getCategories(database, accountId()); Assignment feeds = DatabaseQueries::getFeeds(database, accountId()); // All data are now obtained, lets create the hierarchy.