mirror of
https://github.com/martinrotter/rssguard.git
synced 2025-01-01 02:48:05 +01:00
SQL refactoring.
This commit is contained in:
parent
0b2cb88bf6
commit
04179a378d
@ -859,7 +859,7 @@ bool DatabaseQueries::createOwnCloudAccount(QSqlDatabase db, int id_to_assign, c
|
||||
QSqlQuery q(db);
|
||||
|
||||
q.prepare("INSERT INTO OwnCloudAccounts (id, username, password, url, force_update) "
|
||||
"VALUES (:id, :username, :password, :url, :force_update);");
|
||||
"VALUES (:id, :username, :password, :url, :force_update);");
|
||||
q.bindValue(QSL(":id"), id_to_assign);
|
||||
q.bindValue(QSL(":username"), username);
|
||||
q.bindValue(QSL(":password"), TextFactory::encrypt(password));
|
||||
@ -976,24 +976,95 @@ Assignment DatabaseQueries::getOwnCloudFeeds(QSqlDatabase db, int account_id, bo
|
||||
}
|
||||
|
||||
bool DatabaseQueries::deleteFeed(QSqlDatabase db, int feed_custom_id, int account_id) {
|
||||
QSqlQuery query_remove(db);
|
||||
query_remove.setForwardOnly(true);
|
||||
QSqlQuery q(db);
|
||||
q.setForwardOnly(true);
|
||||
|
||||
// Remove all messages from this feed.
|
||||
query_remove.prepare(QSL("DELETE FROM Messages WHERE feed = :feed AND account_id = :account_id;"));
|
||||
query_remove.bindValue(QSL(":feed"), feed_custom_id);
|
||||
query_remove.bindValue(QSL(":account_id"), account_id);
|
||||
q.prepare(QSL("DELETE FROM Messages WHERE feed = :feed AND account_id = :account_id;"));
|
||||
q.bindValue(QSL(":feed"), feed_custom_id);
|
||||
q.bindValue(QSL(":account_id"), account_id);
|
||||
|
||||
if (!query_remove.exec()) {
|
||||
if (!q.exec()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove feed itself.
|
||||
query_remove.prepare(QSL("DELETE FROM Feeds WHERE custom_id = :feed AND account_id = :account_id;"));
|
||||
query_remove.bindValue(QSL(":feed"), feed_custom_id);
|
||||
query_remove.bindValue(QSL(":account_id"), account_id);
|
||||
q.prepare(QSL("DELETE FROM Feeds WHERE custom_id = :feed AND account_id = :account_id;"));
|
||||
q.bindValue(QSL(":feed"), feed_custom_id);
|
||||
q.bindValue(QSL(":account_id"), account_id);
|
||||
|
||||
return query_remove.exec();
|
||||
return q.exec();
|
||||
}
|
||||
|
||||
bool DatabaseQueries::deleteCategory(QSqlDatabase db, int id) {
|
||||
QSqlQuery q(db);
|
||||
|
||||
// Remove this category from database.
|
||||
q.setForwardOnly(true);
|
||||
q.prepare(QSL("DELETE FROM Categories WHERE id = :category;"));
|
||||
q.bindValue(QSL(":category"), id);
|
||||
|
||||
return q.exec();
|
||||
}
|
||||
|
||||
int DatabaseQueries::addCategory(QSqlDatabase db, int parent_id, int account_id, const QString &title,
|
||||
const QString &description, QDateTime creation_date, const QIcon &icon,
|
||||
bool *ok) {
|
||||
QSqlQuery q(db);
|
||||
|
||||
q.setForwardOnly(true);
|
||||
q.prepare("INSERT INTO Categories "
|
||||
"(parent_id, title, description, date_created, icon, account_id) "
|
||||
"VALUES (:parent_id, :title, :description, :date_created, :icon, :account_id);");
|
||||
q.bindValue(QSL(":parent_id"), parent_id);
|
||||
q.bindValue(QSL(":title"), title);
|
||||
q.bindValue(QSL(":description"), description);
|
||||
q.bindValue(QSL(":date_created"), creation_date.toMSecsSinceEpoch());
|
||||
q.bindValue(QSL(":icon"), qApp->icons()->toByteArray(icon));
|
||||
q.bindValue(QSL(":account_id"), account_id);
|
||||
|
||||
if (!q.exec()) {
|
||||
qDebug("Failed to add category to database: '%s'.", qPrintable(q.lastError().text()));
|
||||
|
||||
if (ok != NULL) {
|
||||
*ok = false;
|
||||
}
|
||||
|
||||
// Query failed.
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
if (ok != NULL) {
|
||||
*ok = true;
|
||||
}
|
||||
|
||||
int new_id = q.lastInsertId().toInt();
|
||||
|
||||
// Now set custom ID in the DB.
|
||||
q.prepare(QSL("UPDATE Categories SET custom_id = :custom_id WHERE id = :id;"));
|
||||
q.bindValue(QSL(":custom_id"), QString::number(new_id));
|
||||
q.bindValue(QSL(":id"), new_id);
|
||||
q.exec();
|
||||
|
||||
return new_id;
|
||||
}
|
||||
}
|
||||
|
||||
bool DatabaseQueries::editCategory(QSqlDatabase db, int parent_id, int category_id,
|
||||
const QString &title, const QString &description, const QIcon &icon) {
|
||||
QSqlQuery q(db);
|
||||
|
||||
q.setForwardOnly(true);
|
||||
q.prepare("UPDATE Categories "
|
||||
"SET title = :title, description = :description, icon = :icon, parent_id = :parent_id "
|
||||
"WHERE id = :id;");
|
||||
q.bindValue(QSL(":title"), title);
|
||||
q.bindValue(QSL(":description"), description);
|
||||
q.bindValue(QSL(":icon"), qApp->icons()->toByteArray(icon));
|
||||
q.bindValue(QSL(":parent_id"), parent_id);
|
||||
q.bindValue(QSL(":id"), category_id);
|
||||
|
||||
return q.exec();
|
||||
}
|
||||
|
||||
DatabaseQueries::DatabaseQueries() {
|
||||
|
@ -69,6 +69,11 @@ class DatabaseQueries {
|
||||
static Assignment getOwnCloudCategories(QSqlDatabase db, int account_id, bool *ok = NULL);
|
||||
static Assignment getOwnCloudFeeds(QSqlDatabase db, int account_id, bool *ok = NULL);
|
||||
static bool deleteFeed(QSqlDatabase db, int feed_custom_id, int account_id);
|
||||
static bool deleteCategory(QSqlDatabase db, int id);
|
||||
static int addCategory(QSqlDatabase db, int parent_id, int account_id, const QString &title,
|
||||
const QString &description, QDateTime creation_date, const QIcon &icon, bool *ok = NULL);
|
||||
static bool editCategory(QSqlDatabase db, int parent_id, int category_id,
|
||||
const QString &title, const QString &description, const QIcon &icon);
|
||||
|
||||
private:
|
||||
explicit DatabaseQueries();
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include "services/standard/standardcategory.h"
|
||||
|
||||
#include "definitions/definitions.h"
|
||||
#include "miscellaneous/databasefactory.h"
|
||||
#include "miscellaneous/databasequeries.h"
|
||||
#include "miscellaneous/textfactory.h"
|
||||
#include "miscellaneous/settings.h"
|
||||
#include "miscellaneous/iconfactory.h"
|
||||
@ -30,9 +30,6 @@
|
||||
#include "services/standard/standardserviceroot.h"
|
||||
#include "services/standard/standardfeed.h"
|
||||
|
||||
#include <QVariant>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include <QPointer>
|
||||
|
||||
|
||||
@ -142,14 +139,8 @@ bool StandardCategory::removeItself() {
|
||||
if (children_removed) {
|
||||
// Children are removed, remove this standard category too.
|
||||
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
||||
QSqlQuery query_remove(database);
|
||||
|
||||
// Remove this category from database.
|
||||
query_remove.setForwardOnly(true);
|
||||
query_remove.prepare(QSL("DELETE FROM Categories WHERE id = :category;"));
|
||||
query_remove.bindValue(QSL(":category"), id());
|
||||
|
||||
return query_remove.exec();
|
||||
return DatabaseQueries::deleteCategory(database, id());
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
@ -159,66 +150,39 @@ bool StandardCategory::removeItself() {
|
||||
bool StandardCategory::addItself(RootItem *parent) {
|
||||
// Now, add category to persistent storage.
|
||||
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
||||
QSqlQuery query_add(database);
|
||||
int new_id = DatabaseQueries::addCategory(database, parent->id(), parent->getParentServiceRoot()->accountId(),
|
||||
title(), description(), creationDate(), icon());
|
||||
|
||||
query_add.setForwardOnly(true);
|
||||
query_add.prepare("INSERT INTO Categories "
|
||||
"(parent_id, title, description, date_created, icon, account_id) "
|
||||
"VALUES (:parent_id, :title, :description, :date_created, :icon, :account_id);");
|
||||
query_add.bindValue(QSL(":parent_id"), parent->id());
|
||||
query_add.bindValue(QSL(":title"), title());
|
||||
query_add.bindValue(QSL(":description"), description());
|
||||
query_add.bindValue(QSL(":date_created"), creationDate().toMSecsSinceEpoch());
|
||||
query_add.bindValue(QSL(":icon"), qApp->icons()->toByteArray(icon()));
|
||||
query_add.bindValue(QSL(":account_id"), parent->getParentServiceRoot()->accountId());
|
||||
|
||||
if (!query_add.exec()) {
|
||||
qDebug("Failed to add category to database: '%s'.", qPrintable(query_add.lastError().text()));
|
||||
|
||||
// Query failed.
|
||||
if (new_id <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
setId(query_add.lastInsertId().toInt());
|
||||
setCustomId(id());
|
||||
|
||||
// Now set custom ID in the DB.
|
||||
query_add.prepare(QSL("UPDATE Categories SET custom_id = :custom_id WHERE id = :id;"));
|
||||
query_add.bindValue(QSL(":custom_id"), QString::number(customId()));
|
||||
query_add.bindValue(QSL(":id"), id());
|
||||
query_add.exec();
|
||||
|
||||
return true;
|
||||
else {
|
||||
setId(new_id);
|
||||
setCustomId(new_id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool StandardCategory::editItself(StandardCategory *new_category_data) {
|
||||
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
||||
QSqlQuery query_update_category(database);
|
||||
|
||||
StandardCategory *original_category = this;
|
||||
RootItem *new_parent = new_category_data->parent();
|
||||
|
||||
query_update_category.setForwardOnly(true);
|
||||
query_update_category.prepare("UPDATE Categories "
|
||||
"SET title = :title, description = :description, icon = :icon, parent_id = :parent_id "
|
||||
"WHERE id = :id;");
|
||||
query_update_category.bindValue(QSL(":title"), new_category_data->title());
|
||||
query_update_category.bindValue(QSL(":description"), new_category_data->description());
|
||||
query_update_category.bindValue(QSL(":icon"), qApp->icons()->toByteArray(new_category_data->icon()));
|
||||
query_update_category.bindValue(QSL(":parent_id"), new_parent->id());
|
||||
query_update_category.bindValue(QSL(":id"), original_category->id());
|
||||
if (DatabaseQueries::editCategory(database, new_parent->id(), original_category->id(),
|
||||
new_category_data->title(), new_category_data->description(),
|
||||
new_category_data->icon())) {
|
||||
// Setup new model data for the original item.
|
||||
original_category->setDescription(new_category_data->description());
|
||||
original_category->setIcon(new_category_data->icon());
|
||||
original_category->setTitle(new_category_data->title());
|
||||
|
||||
if (!query_update_category.exec()) {
|
||||
// Persistent storage update failed, no way to continue now.
|
||||
// Editing is done.
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup new model data for the original item.
|
||||
original_category->setDescription(new_category_data->description());
|
||||
original_category->setIcon(new_category_data->icon());
|
||||
original_category->setTitle(new_category_data->title());
|
||||
|
||||
// Editing is done.
|
||||
return true;
|
||||
}
|
||||
|
||||
StandardCategory::StandardCategory(const QSqlRecord &record) : Category(NULL) {
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include "definitions/definitions.h"
|
||||
#include "core/parsingfactory.h"
|
||||
#include "core/feedsmodel.h"
|
||||
#include "miscellaneous/databasefactory.h"
|
||||
#include "miscellaneous/databasequeries.h"
|
||||
#include "miscellaneous/textfactory.h"
|
||||
#include "miscellaneous/settings.h"
|
||||
#include "miscellaneous/iconfactory.h"
|
||||
@ -33,9 +33,6 @@
|
||||
#include "services/standard/standardserviceroot.h"
|
||||
#include "services/standard/gui/formstandardfeeddetails.h"
|
||||
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include <QVariant>
|
||||
#include <QTextCodec>
|
||||
#include <QPointer>
|
||||
@ -377,23 +374,8 @@ bool StandardFeed::performDragDropChange(RootItem *target_item) {
|
||||
|
||||
bool StandardFeed::removeItself() {
|
||||
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
||||
QSqlQuery query_remove(database);
|
||||
|
||||
query_remove.setForwardOnly(true);
|
||||
|
||||
// Remove all messages from this standard feed.
|
||||
query_remove.prepare(QSL("DELETE FROM Messages WHERE feed = :feed;"));
|
||||
query_remove.bindValue(QSL(":feed"), customId());
|
||||
|
||||
if (!query_remove.exec()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove feed itself.
|
||||
query_remove.prepare(QSL("DELETE FROM Feeds WHERE id = :feed;"));
|
||||
query_remove.bindValue(QSL(":feed"), id());
|
||||
|
||||
return query_remove.exec();
|
||||
return DatabaseQueries::deleteFeed(database, customId(), getParentServiceRoot()->accountId());
|
||||
}
|
||||
|
||||
bool StandardFeed::addItself(RootItem *parent) {
|
||||
|
Loading…
Reference in New Issue
Block a user