DB refactoring #2.

This commit is contained in:
Martin Rotter 2015-12-21 07:06:00 +01:00
parent dd1d7be8b0
commit c0ac2d0ddb
3 changed files with 50 additions and 63 deletions

View File

@ -157,13 +157,7 @@ bool StandardServiceRoot::markFeedsReadUnread(QList<Feed*> items, ReadStatus rea
query_read_msg.bindValue(QSL(":read"), read == RootItem::Read ? 1 : 0);
if (!query_read_msg.exec()) {
qDebug("Query execution for feeds read change failed.");
db_handle.rollback();
}
// Commit changes.
if (db_handle.commit()) {
if (query_read_msg.exec()) {
// Messages are switched, now inform model about need to reload data.
QList<RootItem*> itemss;
@ -177,7 +171,7 @@ bool StandardServiceRoot::markFeedsReadUnread(QList<Feed*> items, ReadStatus rea
return true;
}
else {
return db_handle.rollback();
return false;
}
}
@ -187,27 +181,17 @@ bool StandardServiceRoot::cleanFeeds(QList<Feed*> items, bool clean_read_only) {
query_delete_msg.setForwardOnly(true);
if (clean_read_only) {
if (!query_delete_msg.prepare(QString("UPDATE Messages SET is_deleted = :deleted "
"WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 1;").arg(textualFeedIds(items).join(QSL(", "))))) {
qWarning("Query preparation failed for feeds clearing.");
return false;
}
query_delete_msg.prepare(QString("UPDATE Messages SET is_deleted = :deleted "
"WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 1;").arg(textualFeedIds(items).join(QSL(", "))));
}
else {
if (!query_delete_msg.prepare(QString("UPDATE Messages SET is_deleted = :deleted "
"WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0;").arg(textualFeedIds(items).join(QSL(", "))))) {
qWarning("Query preparation failed for feeds clearing.");
return false;
}
query_delete_msg.prepare(QString("UPDATE Messages SET is_deleted = :deleted "
"WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0;").arg(textualFeedIds(items).join(QSL(", "))));
}
query_delete_msg.bindValue(QSL(":deleted"), 1);
if (!query_delete_msg.exec()) {
qDebug("Query execution for feeds clearing failed.");
return false;
}
else {
if (query_delete_msg.exec()) {
// Messages are cleared, now inform model about need to reload data.
QList<RootItem*> itemss;
@ -223,6 +207,11 @@ bool StandardServiceRoot::cleanFeeds(QList<Feed*> items, bool clean_read_only) {
requestReloadMessageList(true);
return true;
}
else {
QString aa = query_delete_msg.lastError().text();
return false;
}
}
void StandardServiceRoot::loadFromDatabase(){
@ -233,8 +222,10 @@ void StandardServiceRoot::loadFromDatabase(){
// Obtain data for categories from the database.
QSqlQuery query_categories(database);
query_categories.setForwardOnly(true);
query_categories.prepare(QSL("SELECT * FROM Categories WHERE account_id = :account_id;"));
query_categories.bindValue(QSL(":account_id"), accountId());
if (!query_categories.exec(QString("SELECT * FROM Categories WHERE account_id = %1;").arg(accountId())) || query_categories.lastError().isValid()) {
if (!query_categories.exec()) {
qFatal("Query for obtaining categories failed. Error message: '%s'.",
qPrintable(query_categories.lastError().text()));
}
@ -250,8 +241,10 @@ void StandardServiceRoot::loadFromDatabase(){
// All categories are now loaded.
QSqlQuery query_feeds(database);
query_feeds.setForwardOnly(true);
query_feeds.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
query_feeds.bindValue(QSL(":account_id"), accountId());
if (!query_feeds.exec(QString("SELECT * FROM Feeds WHERE account_id = %1;").arg(accountId())) || query_feeds.lastError().isValid()) {
if (!query_feeds.exec()) {
qFatal("Query for obtaining feeds failed. Error message: '%s'.",
qPrintable(query_feeds.lastError().text()));
}

View File

@ -103,15 +103,23 @@ void TtRssFeed::updateCounts(bool including_total_count) {
query_all.setForwardOnly(true);
if (including_total_count) {
if (query_all.exec(QString("SELECT count(*) FROM Messages WHERE feed = '%1' AND is_deleted = 0 AND account_id = %2;").arg(QString::number(customId()),
QString::number(serviceRoot()->accountId()))) && query_all.next()) {
query_all.prepare("SELECT count(*) FROM Messages "
"WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;");
query_all.bindValue(QSL(":feed"), customId());
query_all.bindValue(QSL(":account_id"), serviceRoot()->accountId());
if (query_all.exec() && query_all.next()) {
m_totalCount = query_all.value(0).toInt();
}
}
query_all.prepare("SELECT count(*) FROM Messages "
"WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 0 AND account_id = :account_id;");
query_all.bindValue(QSL(":feed"), customId());
query_all.bindValue(QSL(":account_id"), serviceRoot()->accountId());
// Obtain count of unread messages.
if (query_all.exec(QString("SELECT count(*) FROM Messages WHERE feed = '%1' AND is_deleted = 0 AND is_read = 0 AND account_id = %2;").arg(QString::number(customId()),
QString::number(serviceRoot()->accountId()))) && query_all.next()) {
if (query_all.exec() && query_all.next()) {
int new_unread_count = query_all.value(0).toInt();
if (status() == NewMessages && new_unread_count < m_unreadCount) {
@ -331,7 +339,6 @@ int TtRssFeed::updateMessages(const QList<Message> &messages) {
}
query_update.finish();
qDebug("Updating message '%s' in DB.", qPrintable(message.m_title));
}
}
@ -354,14 +361,12 @@ int TtRssFeed::updateMessages(const QList<Message> &messages) {
}
query_insert.finish();
qDebug("Adding new message '%s' to DB.", qPrintable(message.m_title));
}
}
if (!database.commit()) {
database.rollback();
qDebug("Transaction commit for message downloader failed.");
}
else {

View File

@ -74,14 +74,19 @@ bool TtRssServiceRoot::editViaGui() {
bool TtRssServiceRoot::deleteViaGui() {
QSqlDatabase connection = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
QSqlQuery query(connection);
query.setForwardOnly(true);
query.prepare(QSL("DELETE FROM TtRssAccounts WHERE id = :id;"));
query.bindValue(QSL(":id"), accountId());
// Remove extra entry in "Tiny Tiny RSS accounts list" and then delete
// all the categories/feeds and messages.
if (!QSqlQuery(connection).exec(QString("DELETE FROM TtRssAccounts WHERE id = %1;").arg(accountId()))) {
return false;
if (query.exec()) {
return ServiceRoot::deleteViaGui();
}
else {
return ServiceRoot::deleteViaGui();
return false;
}
}
@ -332,32 +337,14 @@ QStringList TtRssServiceRoot::customIDSOfMessagesForItem(RootItem *item) {
bool TtRssServiceRoot::markFeedsReadUnread(QList<Feed*> items, RootItem::ReadStatus read) {
QSqlDatabase db_handle = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
if (!db_handle.transaction()) {
qWarning("Starting transaction for feeds read change.");
return false;
}
QSqlQuery query_read_msg(db_handle);
query_read_msg.setForwardOnly(true);
if (!query_read_msg.prepare(QString("UPDATE Messages SET is_read = :read "
"WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0;").arg(textualFeedIds(items).join(QSL(", "))))) {
qWarning("Query preparation failed for feeds read change.");
db_handle.rollback();
return false;
}
query_read_msg.prepare(QString("UPDATE Messages SET is_read = :read "
"WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0;").arg(textualFeedIds(items).join(QSL(", "))));
query_read_msg.bindValue(QSL(":read"), read == RootItem::Read ? 1 : 0);
if (!query_read_msg.exec()) {
qDebug("Query execution for feeds read change failed.");
db_handle.rollback();
}
// Commit changes.
if (db_handle.commit()) {
if (query_read_msg.exec()) {
QList<RootItem*> itemss;
foreach (Feed *feed, items) {
@ -370,7 +357,7 @@ bool TtRssServiceRoot::markFeedsReadUnread(QList<Feed*> items, RootItem::ReadSta
return true;
}
else {
return db_handle.rollback();
return false;
}
}
@ -497,10 +484,11 @@ void TtRssServiceRoot::loadFromDatabase() {
// Obtain data for categories from the database.
QSqlQuery query_categories(database);
query_categories.setForwardOnly(true);
query_categories.prepare(QSL("SELECT * FROM Categories WHERE account_id = :account_id;"));
query_categories.bindValue(QSL(":account_id"), accountId());
if (!query_categories.exec(QString("SELECT * FROM Categories WHERE account_id = %1;").arg(accountId())) || query_categories.lastError().isValid()) {
qFatal("Query for obtaining categories failed. Error message: '%s'.",
qPrintable(query_categories.lastError().text()));
if (!query_categories.exec()) {
qFatal("Query for obtaining categories failed. Error message: '%s'.", qPrintable(query_categories.lastError().text()));
}
while (query_categories.next()) {
@ -514,10 +502,11 @@ void TtRssServiceRoot::loadFromDatabase() {
// All categories are now loaded.
QSqlQuery query_feeds(database);
query_feeds.setForwardOnly(true);
query_feeds.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
query_feeds.bindValue(QSL(":account_id"), accountId());
if (!query_feeds.exec(QString("SELECT * FROM Feeds WHERE account_id = %1;").arg(accountId())) || query_feeds.lastError().isValid()) {
qFatal("Query for obtaining feeds failed. Error message: '%s'.",
qPrintable(query_feeds.lastError().text()));
if (!query_feeds.exec()) {
qFatal("Query for obtaining feeds failed. Error message: '%s'.", qPrintable(query_feeds.lastError().text()));
}
while (query_feeds.next()) {