some code cleanups

This commit is contained in:
Martin Rotter 2020-06-23 13:55:00 +02:00
parent 5139d3ae69
commit 59e4dc528e
7 changed files with 43 additions and 75 deletions

View File

@ -1425,41 +1425,6 @@ QList<ServiceRoot*> 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<ServiceRoot*> DatabaseQueries::getGmailAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery query(db);
QList<ServiceRoot*> roots;

View File

@ -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<typename T>
static Assignment getCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
template<typename T>
@ -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<ServiceRoot*> getStandardAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static Assignment getStandardCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
template<typename T>
static void fillFeedData(T* feed, const QSqlRecord& sql_record);
@ -174,6 +176,41 @@ inline void DatabaseQueries::fillFeedData(StandardFeed* feed, const QSqlRecord&
}
}
template<typename T>
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<typename T>
Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;

View File

@ -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<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<GmailFeed>(database, accountId());
// All data are now obtained, lets create the hierarchy.

View File

@ -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<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<InoreaderFeed>(database, accountId());
// All data are now obtained, lets create the hierarchy.

View File

@ -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<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<OwnCloudFeed>(database, accountId());
// All data are now obtained, lets create the hierarchy.

View File

@ -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<StandardCategory>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<StandardFeed>(database, accountId());
// All data are now obtained, lets create the hierarchy.

View File

@ -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<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<TtRssFeed>(database, accountId());
// All data are now obtained, lets create the hierarchy.