SQL refactoring.

This commit is contained in:
Martin Rotter 2016-04-11 14:57:47 +02:00
parent 0b2cb88bf6
commit 04179a378d
4 changed files with 111 additions and 89 deletions

View File

@ -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) { bool DatabaseQueries::deleteFeed(QSqlDatabase db, int feed_custom_id, int account_id) {
QSqlQuery query_remove(db); QSqlQuery q(db);
query_remove.setForwardOnly(true); q.setForwardOnly(true);
// Remove all messages from this feed. // Remove all messages from this feed.
query_remove.prepare(QSL("DELETE FROM Messages WHERE feed = :feed AND account_id = :account_id;")); q.prepare(QSL("DELETE FROM Messages WHERE feed = :feed AND account_id = :account_id;"));
query_remove.bindValue(QSL(":feed"), feed_custom_id); q.bindValue(QSL(":feed"), feed_custom_id);
query_remove.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
if (!query_remove.exec()) { if (!q.exec()) {
return false; return false;
} }
// Remove feed itself. // Remove feed itself.
query_remove.prepare(QSL("DELETE FROM Feeds WHERE custom_id = :feed AND account_id = :account_id;")); q.prepare(QSL("DELETE FROM Feeds WHERE custom_id = :feed AND account_id = :account_id;"));
query_remove.bindValue(QSL(":feed"), feed_custom_id); q.bindValue(QSL(":feed"), feed_custom_id);
query_remove.bindValue(QSL(":account_id"), account_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() { DatabaseQueries::DatabaseQueries() {

View File

@ -69,6 +69,11 @@ class DatabaseQueries {
static Assignment getOwnCloudCategories(QSqlDatabase db, int account_id, bool *ok = NULL); static Assignment getOwnCloudCategories(QSqlDatabase db, int account_id, bool *ok = NULL);
static Assignment getOwnCloudFeeds(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 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: private:
explicit DatabaseQueries(); explicit DatabaseQueries();

View File

@ -18,7 +18,7 @@
#include "services/standard/standardcategory.h" #include "services/standard/standardcategory.h"
#include "definitions/definitions.h" #include "definitions/definitions.h"
#include "miscellaneous/databasefactory.h" #include "miscellaneous/databasequeries.h"
#include "miscellaneous/textfactory.h" #include "miscellaneous/textfactory.h"
#include "miscellaneous/settings.h" #include "miscellaneous/settings.h"
#include "miscellaneous/iconfactory.h" #include "miscellaneous/iconfactory.h"
@ -30,9 +30,6 @@
#include "services/standard/standardserviceroot.h" #include "services/standard/standardserviceroot.h"
#include "services/standard/standardfeed.h" #include "services/standard/standardfeed.h"
#include <QVariant>
#include <QSqlQuery>
#include <QSqlError>
#include <QPointer> #include <QPointer>
@ -142,14 +139,8 @@ bool StandardCategory::removeItself() {
if (children_removed) { if (children_removed) {
// Children are removed, remove this standard category too. // Children are removed, remove this standard category too.
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
QSqlQuery query_remove(database);
// Remove this category from database. return DatabaseQueries::deleteCategory(database, id());
query_remove.setForwardOnly(true);
query_remove.prepare(QSL("DELETE FROM Categories WHERE id = :category;"));
query_remove.bindValue(QSL(":category"), id());
return query_remove.exec();
} }
else { else {
return false; return false;
@ -159,59 +150,28 @@ bool StandardCategory::removeItself() {
bool StandardCategory::addItself(RootItem *parent) { bool StandardCategory::addItself(RootItem *parent) {
// Now, add category to persistent storage. // Now, add category to persistent storage.
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); 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); if (new_id <= 0) {
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.
return false; return false;
} }
else {
setId(query_add.lastInsertId().toInt()); setId(new_id);
setCustomId(id()); setCustomId(new_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; return true;
}
} }
bool StandardCategory::editItself(StandardCategory *new_category_data) { bool StandardCategory::editItself(StandardCategory *new_category_data) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
QSqlQuery query_update_category(database);
StandardCategory *original_category = this; StandardCategory *original_category = this;
RootItem *new_parent = new_category_data->parent(); RootItem *new_parent = new_category_data->parent();
query_update_category.setForwardOnly(true); if (DatabaseQueries::editCategory(database, new_parent->id(), original_category->id(),
query_update_category.prepare("UPDATE Categories " new_category_data->title(), new_category_data->description(),
"SET title = :title, description = :description, icon = :icon, parent_id = :parent_id " new_category_data->icon())) {
"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 (!query_update_category.exec()) {
// Persistent storage update failed, no way to continue now.
return false;
}
// Setup new model data for the original item. // Setup new model data for the original item.
original_category->setDescription(new_category_data->description()); original_category->setDescription(new_category_data->description());
original_category->setIcon(new_category_data->icon()); original_category->setIcon(new_category_data->icon());
@ -219,6 +179,10 @@ bool StandardCategory::editItself(StandardCategory *new_category_data) {
// Editing is done. // Editing is done.
return true; return true;
}
else {
return false;
}
} }
StandardCategory::StandardCategory(const QSqlRecord &record) : Category(NULL) { StandardCategory::StandardCategory(const QSqlRecord &record) : Category(NULL) {

View File

@ -20,7 +20,7 @@
#include "definitions/definitions.h" #include "definitions/definitions.h"
#include "core/parsingfactory.h" #include "core/parsingfactory.h"
#include "core/feedsmodel.h" #include "core/feedsmodel.h"
#include "miscellaneous/databasefactory.h" #include "miscellaneous/databasequeries.h"
#include "miscellaneous/textfactory.h" #include "miscellaneous/textfactory.h"
#include "miscellaneous/settings.h" #include "miscellaneous/settings.h"
#include "miscellaneous/iconfactory.h" #include "miscellaneous/iconfactory.h"
@ -33,9 +33,6 @@
#include "services/standard/standardserviceroot.h" #include "services/standard/standardserviceroot.h"
#include "services/standard/gui/formstandardfeeddetails.h" #include "services/standard/gui/formstandardfeeddetails.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant> #include <QVariant>
#include <QTextCodec> #include <QTextCodec>
#include <QPointer> #include <QPointer>
@ -377,23 +374,8 @@ bool StandardFeed::performDragDropChange(RootItem *target_item) {
bool StandardFeed::removeItself() { bool StandardFeed::removeItself() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
QSqlQuery query_remove(database);
query_remove.setForwardOnly(true); return DatabaseQueries::deleteFeed(database, customId(), getParentServiceRoot()->accountId());
// 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();
} }
bool StandardFeed::addItself(RootItem *parent) { bool StandardFeed::addItself(RootItem *parent) {