Make some more DB fixes and fix red error color.

This commit is contained in:
Martin Rotter 2016-04-14 12:21:38 +02:00
parent ec4eb2e561
commit 755eda6eff
5 changed files with 49 additions and 36 deletions

View file

@ -179,20 +179,20 @@ bool DatabaseQueries::purgeRecycleBin(QSqlDatabase db) {
return q.exec(); return q.exec();
} }
QMap<int,int> DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, QMap<int,QPair<int,int> > DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok) { bool including_total_counts, bool *ok) {
QMap<int,int> counts; QMap<int, QPair<int,int> > counts;
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
if (including_total_counts) { if (including_total_counts) {
q.prepare("SELECT feed, count(*) FROM Messages " q.prepare("SELECT feed, sum((is_read + 1) % 2), count(*) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;"); "GROUP BY feed;");
} }
else { else {
q.prepare("SELECT feed, count(*) FROM Messages " q.prepare("SELECT feed, sum((is_read + 1) % 2) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 0 AND account_id = :account_id " "WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;"); "GROUP BY feed;");
} }
@ -202,9 +202,16 @@ QMap<int,int> DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int
if (q.exec()) { if (q.exec()) {
while (q.next()) { while (q.next()) {
int feed_id = q.value(0).toInt(); int feed_id = q.value(0).toInt();
int new_count = q.value(1).toInt(); int unread_count = q.value(1).toInt();
counts.insert(feed_id, new_count); if (including_total_counts) {
int total_count = q.value(2).toInt();
counts.insert(feed_id, QPair<int,int>(unread_count, total_count));
}
else {
counts.insert(feed_id, QPair<int,int>(unread_count, 0));
}
} }
if (ok != NULL) { if (ok != NULL) {
@ -228,12 +235,12 @@ QMap<int, QPair<int,int> > DatabaseQueries::getMessageCountsForAccount(QSqlDatab
if (including_total_counts) { if (including_total_counts) {
q.prepare("SELECT feed, sum((is_read + 1) % 2), count(*) FROM Messages " q.prepare("SELECT feed, sum((is_read + 1) % 2), count(*) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;"); "GROUP BY feed;");
} }
else { else {
q.prepare("SELECT feed, sum((is_read + 1) % 2) FROM Messages " q.prepare("SELECT feed, sum((is_read + 1) % 2) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;"); "GROUP BY feed;");
} }

View file

@ -41,7 +41,7 @@ class DatabaseQueries {
static bool purgeReadMessages(QSqlDatabase db); static bool purgeReadMessages(QSqlDatabase db);
static bool purgeOldMessages(QSqlDatabase db, int older_than_days); static bool purgeOldMessages(QSqlDatabase db, int older_than_days);
static bool purgeRecycleBin(QSqlDatabase db); static bool purgeRecycleBin(QSqlDatabase db);
static QMap<int,int> getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, static QMap<int,QPair<int,int> > getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok = NULL); bool including_total_counts, bool *ok = NULL);
static QMap<int,QPair<int,int> > getMessageCountsForAccount(QSqlDatabase db, int account_id, static QMap<int,QPair<int,int> > getMessageCountsForAccount(QSqlDatabase db, int account_id,
bool including_total_counts, bool *ok = NULL); bool including_total_counts, bool *ok = NULL);

View file

@ -48,24 +48,18 @@ void Category::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
bool ok; bool ok;
QMap<int,QPair<int,int> > counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(),
if (including_total_count) {
QMap<int,int> counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(),
including_total_count, &ok); including_total_count, &ok);
if (ok) { if (ok) {
foreach (Feed *feed, feeds) { foreach (Feed *feed, feeds) {
feed->setCountOfAllMessages(counts.value(feed->customId())); if (counts.contains(feed->customId())) {
} feed->setCountOfUnreadMessages(counts.value(feed->customId()).first);
}
}
QMap<int,int> counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(), if (including_total_count) {
false, &ok); feed->setCountOfAllMessages(counts.value(feed->customId()).second);
}
if (ok) { }
foreach (Feed *feed, feeds) {
feed->setCountOfUnreadMessages(counts.value(feed->customId()));
} }
} }
} }

View file

@ -47,6 +47,8 @@ QVariant Feed::data(int column, int role) const {
return QColor(Qt::blue); return QColor(Qt::blue);
case Error: case Error:
case ParsingError:
case OtherError:
return QColor(Qt::red); return QColor(Qt::red);
default: default:
@ -84,8 +86,14 @@ void Feed::setCountOfUnreadMessages(int count_unread_messages) {
int Feed::update() { int Feed::update() {
QList<Message> msgs = obtainNewMessages(); QList<Message> msgs = obtainNewMessages();
if (msgs.size() > 0) {
return updateMessages(msgs); return updateMessages(msgs);
} }
else {
return 0;
}
}
void Feed::setAutoUpdateInitialInterval(int auto_update_interval) { void Feed::setAutoUpdateInitialInterval(int auto_update_interval) {
// If new initial auto-update interval is set, then // If new initial auto-update interval is set, then

View file

@ -95,7 +95,9 @@ void ServiceRoot::updateCounts(bool including_total_count) {
bool ok; bool ok;
QMap<int,QPair<int,int> > counts = DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok); QMap<int,QPair<int,int> > counts = DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok);
if (ok) {
foreach (Feed *feed, feeds) { foreach (Feed *feed, feeds) {
if (counts.contains(feed->customId())) {
feed->setCountOfUnreadMessages(counts.value(feed->customId()).first); feed->setCountOfUnreadMessages(counts.value(feed->customId()).first);
if (including_total_count) { if (including_total_count) {
@ -103,6 +105,8 @@ void ServiceRoot::updateCounts(bool including_total_count) {
} }
} }
} }
}
}
void ServiceRoot::completelyRemoveAllData() { void ServiceRoot::completelyRemoveAllData() {
// Purge old data from SQL and clean all model items. // Purge old data from SQL and clean all model items.