Start refactoring all sql queries into separate data interaction class.

This commit is contained in:
Martin Rotter 2016-04-04 13:52:07 +02:00
parent 743512c614
commit 8646e8c852
6 changed files with 79 additions and 17 deletions

View File

@ -35,7 +35,6 @@
#include <QThread>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QPair>
#include <QStack>

View File

@ -24,6 +24,7 @@
#include "miscellaneous/iconfactory.h"
#include "gui/dialogs/formmain.h"
#include "services/abstract/serviceroot.h"
#include "miscellaneous/databasequeries.h"
#include <QSqlRecord>
#include <QSqlError>
@ -258,18 +259,7 @@ bool MessagesModel::setMessageRead(int row_index, RootItem::ReadStatus read) {
return false;
}
QSqlQuery query_read_msg(database());
query_read_msg.setForwardOnly(true);
if (!query_read_msg.prepare(QSL("UPDATE Messages SET is_read = :read WHERE id = :id;"))) {
qWarning("Query preparation failed for message read change.");
return false;
}
query_read_msg.bindValue(QSL(":id"), message.m_id);
query_read_msg.bindValue(QSL(":read"), (int) read);
if (query_read_msg.exec()) {
if (DatabaseQueries::markMessageReadUnread(database(), message.m_id, read)) {
return m_selectedItem->getParentServiceRoot()->onAfterSetMessagesRead(m_selectedItem, QList<Message>() << message, read);
}
else {

View File

@ -0,0 +1,39 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2016 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "miscellaneous/databasequeries.h"
#include <QVariant>
bool DatabaseQueries::markMessageReadUnread(QSqlDatabase db, int id, RootItem::ReadStatus read) {
QSqlQuery query_read_msg(db);
query_read_msg.setForwardOnly(true);
if (!query_read_msg.prepare(QSL("UPDATE Messages SET is_read = :read WHERE id = :id;"))) {
qWarning("Query preparation failed for message read change.");
return false;
}
query_read_msg.bindValue(QSL(":id"), id);
query_read_msg.bindValue(QSL(":read"), (int) read);
return query_read_msg.exec();
}
DatabaseQueries::DatabaseQueries() {
}

View File

@ -0,0 +1,35 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2016 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef DATABASEQUERIES_H
#define DATABASEQUERIES_H
#include "services/abstract/rootitem.h"
#include <QSqlQuery>
class DatabaseQueries {
public:
static bool markMessageReadUnread(QSqlDatabase db, int id, RootItem::ReadStatus read);
private:
explicit DatabaseQueries();
};
#endif // DATABASEQUERIES_H

View File

@ -383,7 +383,7 @@ bool StandardFeed::removeItself() {
// Remove all messages from this standard feed.
query_remove.prepare(QSL("DELETE FROM Messages WHERE feed = :feed;"));
query_remove.bindValue(QSL(":feed"), id());
query_remove.bindValue(QSL(":feed"), customId());
if (!query_remove.exec()) {
return false;

View File

@ -213,14 +213,13 @@ bool TtRssFeed::removeItself() {
}
// Remove feed itself.
query_remove.prepare(QSL("DELETE FROM Feeds WHERE custom_id = :feed;"));
query_remove.bindValue(QSL(":feed"), customId());
query_remove.prepare(QSL("DELETE FROM Feeds WHERE id = :feed;"));
query_remove.bindValue(QSL(":feed"), id());
return query_remove.exec();
}
else {
qWarning("TT-RSS: Unsubscribing from feed failed, received JSON: '%s'", qPrintable(response.toString()));
return false;
}
}