From eabb9727cb2efd2a28bf31bf26541aecf8b0713b Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 7 Apr 2016 11:39:50 +0200 Subject: [PATCH] SQL refactoring. --- src/miscellaneous/databasequeries.cpp | 344 ++++++++++++++---- src/miscellaneous/databasequeries.h | 11 + .../owncloud/owncloudserviceentrypoint.cpp | 28 +- src/services/owncloud/owncloudserviceroot.cpp | 112 +----- 4 files changed, 298 insertions(+), 197 deletions(-) diff --git a/src/miscellaneous/databasequeries.cpp b/src/miscellaneous/databasequeries.cpp index fb4701034..d64472041 100644 --- a/src/miscellaneous/databasequeries.cpp +++ b/src/miscellaneous/databasequeries.cpp @@ -19,6 +19,11 @@ #include "services/abstract/category.h" #include "services/abstract/feed.h" +#include "services/owncloud/owncloudserviceroot.h" +#include "services/owncloud/owncloudcategory.h" +#include "services/owncloud/owncloudfeed.h" +#include "services/owncloud/network/owncloudnetworkfactory.h" +#include "miscellaneous/textfactory.h" #include "miscellaneous/application.h" #include "miscellaneous/iconfactory.h" @@ -52,15 +57,15 @@ bool DatabaseQueries::markMessageImportant(QSqlDatabase db, int id, RootItem::Im } bool DatabaseQueries::markFeedsReadUnread(QSqlDatabase db, const QStringList &ids, int account_id, RootItem::ReadStatus read) { - QSqlQuery query_read_msg(db); - query_read_msg.setForwardOnly(true); - query_read_msg.prepare(QString("UPDATE Messages SET is_read = :read " - "WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;").arg(ids.join(QSL(", ")))); + QSqlQuery q(db); + q.setForwardOnly(true); + q.prepare(QString("UPDATE Messages SET is_read = :read " + "WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;").arg(ids.join(QSL(", ")))); - query_read_msg.bindValue(QSL(":read"), read == RootItem::Read ? 1 : 0); - query_read_msg.bindValue(QSL(":account_id"), account_id); + q.bindValue(QSL(":read"), read == RootItem::Read ? 1 : 0); + q.bindValue(QSL(":account_id"), account_id); - return query_read_msg.exec(); + return q.exec(); } bool DatabaseQueries::markBinReadUnread(QSqlDatabase db, int account_id, RootItem::ReadStatus read) { @@ -142,7 +147,7 @@ bool DatabaseQueries::purgeReadMessages(QSqlDatabase db) { } bool DatabaseQueries::purgeOldMessages(QSqlDatabase db, int older_than_days) { - QSqlQuery q = QSqlQuery(db); + QSqlQuery q(db); const qint64 since_epoch = QDateTime::currentDateTimeUtc().addDays(-older_than_days).toMSecsSinceEpoch(); q.setForwardOnly(true); @@ -156,16 +161,16 @@ bool DatabaseQueries::purgeOldMessages(QSqlDatabase db, int older_than_days) { } bool DatabaseQueries::purgeRecycleBin(QSqlDatabase db) { - QSqlQuery query = QSqlQuery(db); + QSqlQuery q(db); - query.setForwardOnly(true); - query.prepare(QSL("DELETE FROM Messages WHERE is_important = :is_important AND is_deleted = :is_deleted;")); - query.bindValue(QSL(":is_deleted"), 1); + q.setForwardOnly(true); + q.prepare(QSL("DELETE FROM Messages WHERE is_important = :is_important AND is_deleted = :is_deleted;")); + q.bindValue(QSL(":is_deleted"), 1); // Remove only messages which are NOT starred. - query.bindValue(QSL(":is_important"), 0); + q.bindValue(QSL(":is_important"), 0); - return query.exec(); + return q.exec(); } QMap DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, @@ -211,27 +216,27 @@ QMap DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int int DatabaseQueries::getMessageCountsForFeed(QSqlDatabase db, int feed_custom_id, int account_id, bool including_total_counts, bool *ok) { - QSqlQuery query_all(db); - query_all.setForwardOnly(true); + QSqlQuery q(db); + q.setForwardOnly(true); if (including_total_counts) { - query_all.prepare("SELECT count(*) FROM Messages " - "WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;"); + q.prepare("SELECT count(*) FROM Messages " + "WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;"); } else { - 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;"); + q.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"), feed_custom_id); - query_all.bindValue(QSL(":account_id"), account_id); + q.bindValue(QSL(":feed"), feed_custom_id); + q.bindValue(QSL(":account_id"), account_id); - if (query_all.exec() && query_all.next()) { + if (q.exec() && q.next()) { if (ok != NULL) { *ok = true; } - return query_all.value(0).toInt(); + return q.value(0).toInt(); } else { if (ok != NULL) { @@ -243,26 +248,26 @@ int DatabaseQueries::getMessageCountsForFeed(QSqlDatabase db, int feed_custom_id } int DatabaseQueries::getMessageCountsForBin(QSqlDatabase db, int account_id, bool including_total_counts, bool *ok) { - QSqlQuery query_all(db); - query_all.setForwardOnly(true); + QSqlQuery q(db); + q.setForwardOnly(true); if (including_total_counts) { - query_all.prepare("SELECT count(*) FROM Messages " - "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"); + q.prepare("SELECT count(*) FROM Messages " + "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"); } else { - query_all.prepare("SELECT count(*) FROM Messages " - "WHERE is_read = 0 AND is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"); + q.prepare("SELECT count(*) FROM Messages " + "WHERE is_read = 0 AND is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"); } - query_all.bindValue(QSL(":account_id"), account_id); + q.bindValue(QSL(":account_id"), account_id); - if (query_all.exec() && query_all.next()) { + if (q.exec() && q.next()) { if (ok != NULL) { *ok = true; } - return query_all.value(0).toInt(); + return q.value(0).toInt(); } else { if (ok != NULL) { @@ -560,20 +565,20 @@ int DatabaseQueries::updateMessages(QSqlDatabase db, } bool DatabaseQueries::cleanMessagesFromBin(QSqlDatabase db, bool clear_only_read, int account_id) { - QSqlQuery query_empty_bin(db); - query_empty_bin.setForwardOnly(true); + QSqlQuery q(db); + q.setForwardOnly(true); if (clear_only_read) { - query_empty_bin.prepare("UPDATE Messages SET is_pdeleted = 1 " - "WHERE is_read = 1 AND is_deleted = 1 AND account_id = :account_id;"); + q.prepare("UPDATE Messages SET is_pdeleted = 1 " + "WHERE is_read = 1 AND is_deleted = 1 AND account_id = :account_id;"); } else { - query_empty_bin.prepare(QSL("UPDATE Messages SET is_pdeleted = 1 WHERE is_deleted = 1 AND account_id = :account_id;")); + q.prepare(QSL("UPDATE Messages SET is_pdeleted = 1 WHERE is_deleted = 1 AND account_id = :account_id;")); } - query_empty_bin.bindValue(QSL(":account_id"), account_id); + q.bindValue(QSL(":account_id"), account_id); - return query_empty_bin.exec(); + return q.exec(); } bool DatabaseQueries::deleteAccount(QSqlDatabase db, int account_id) { @@ -604,25 +609,25 @@ bool DatabaseQueries::deleteAccount(QSqlDatabase db, int account_id) { bool DatabaseQueries::deleteAccountData(QSqlDatabase db, int account_id, bool delete_messages_too) { bool result = true; - QSqlQuery query(db); - query.setForwardOnly(true); + QSqlQuery q(db); + q.setForwardOnly(true); if (delete_messages_too) { - query.prepare(QSL("DELETE FROM Messages WHERE account_id = :account_id;")); - query.bindValue(QSL(":account_id"), account_id); + q.prepare(QSL("DELETE FROM Messages WHERE account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); - result &= query.exec(); + result &= q.exec(); } - query.prepare(QSL("DELETE FROM Feeds WHERE account_id = :account_id;")); - query.bindValue(QSL(":account_id"), account_id); + q.prepare(QSL("DELETE FROM Feeds WHERE account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); - result &= query.exec(); + result &= q.exec(); - query.prepare(QSL("DELETE FROM Categories WHERE account_id = :account_id;")); - query.bindValue(QSL(":account_id"), account_id); + q.prepare(QSL("DELETE FROM Categories WHERE account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); - result &= query.exec(); + result &= q.exec(); return result; } @@ -720,68 +725,255 @@ bool DatabaseQueries::storeAccountTree(QSqlDatabase db, RootItem *tree_root, int } QStringList DatabaseQueries::customIdsOfMessagesFromAccount(QSqlDatabase db, int account_id, bool *ok) { - QSqlQuery query(db); + QSqlQuery q(db); QStringList ids; - query.setForwardOnly(true); - query.prepare(QSL("SELECT custom_id FROM Messages WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); - query.bindValue(QSL(":account_id"), account_id); + q.setForwardOnly(true); + q.prepare(QSL("SELECT custom_id FROM Messages WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); if (ok != NULL) { - *ok = query.exec(); + *ok = q.exec(); } else { - query.exec(); + q.exec(); } - while (query.next()) { - ids.append(query.value(0).toString()); + while (q.next()) { + ids.append(q.value(0).toString()); } return ids; } QStringList DatabaseQueries::customIdsOfMessagesFromBin(QSqlDatabase db, int account_id, bool *ok) { - QSqlQuery query(db); + QSqlQuery q(db); QStringList ids; - query.setForwardOnly(true); - query.prepare(QSL("SELECT custom_id FROM Messages WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")); - query.bindValue(QSL(":account_id"), account_id); + q.setForwardOnly(true); + q.prepare(QSL("SELECT custom_id FROM Messages WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); if (ok != NULL) { - *ok = query.exec(); + *ok = q.exec(); } else { - query.exec(); + q.exec(); } - while (query.next()) { - ids.append(query.value(0).toString()); + while (q.next()) { + ids.append(q.value(0).toString()); } return ids; } QStringList DatabaseQueries::customIdsOfMessagesFromFeed(QSqlDatabase db, int feed_custom_id, int account_id, bool *ok) { - QSqlQuery query(db); + QSqlQuery q(db); QStringList ids; - query.setForwardOnly(true); - query.prepare(QSL("SELECT custom_id FROM Messages WHERE is_deleted = 0 AND is_pdeleted = 0 AND feed = :feed AND account_id = :account_id;")); - query.bindValue(QSL(":account_id"), account_id); - query.bindValue(QSL(":feed"), feed_custom_id); + q.setForwardOnly(true); + q.prepare(QSL("SELECT custom_id FROM Messages WHERE is_deleted = 0 AND is_pdeleted = 0 AND feed = :feed AND account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); + q.bindValue(QSL(":feed"), feed_custom_id); if (ok != NULL) { - *ok = query.exec(); + *ok = q.exec(); } else { - query.exec(); + q.exec(); } - while (query.next()) { - ids.append(query.value(0).toString()); + while (q.next()) { + ids.append(q.value(0).toString()); } return ids; } +QList DatabaseQueries::getOwnCloudAccounts(QSqlDatabase db, bool *ok) { + QSqlQuery query(db); + QList roots; + + if (query.exec("SELECT * FROM OwnCloudAccounts;")) { + while (query.next()) { + OwnCloudServiceRoot *root = new OwnCloudServiceRoot(); + root->setId(query.value(0).toInt()); + root->setAccountId(query.value(0).toInt()); + root->network()->setAuthUsername(query.value(1).toString()); + root->network()->setAuthPassword(TextFactory::decrypt(query.value(2).toString())); + root->network()->setUrl(query.value(3).toString()); + root->network()->setForceServerSideUpdate(query.value(4).toBool()); + + root->updateTitle(); + roots.append(root); + } + + if (ok != NULL) { + *ok = true; + } + } + else { + qWarning("OwnCloud: Getting list of activated accounts failed: '%s'.", qPrintable(query.lastError().text())); + + if (ok != NULL) { + *ok = false; + } + } + + return roots; +} + +bool DatabaseQueries::deleteOwnCloudAccount(QSqlDatabase db, int account_id) { + QSqlQuery q(db); + + q.setForwardOnly(true); + q.prepare(QSL("DELETE FROM OwnCloudAccounts WHERE id = :id;")); + q.bindValue(QSL(":id"), account_id); + + return q.exec(); +} + +bool DatabaseQueries::overwriteOwnCloudAccount(QSqlDatabase db, const QString &username, const QString &password, + const QString &url, bool force_server_side_feed_update, int account_id) { + QSqlQuery query(db); + + query.prepare("UPDATE OwnCloudAccounts " + "SET username = :username, password = :password, url = :url, force_update = :force_update " + "WHERE id = :id;"); + query.bindValue(QSL(":username"), username); + query.bindValue(QSL(":password"), TextFactory::encrypt(password)); + query.bindValue(QSL(":url"), url); + query.bindValue(QSL(":force_update"), force_server_side_feed_update ? 1 : 0); + query.bindValue(QSL(":id"), account_id); + + if (query.exec()) { + return true; + } + else { + qWarning("ownCloud: Updating account failed: '%s'.", qPrintable(query.lastError().text())); + return false; + } +} + +bool DatabaseQueries::createOwnCloudAccount(QSqlDatabase db, int id_to_assign, const QString &username, + const QString &password, const QString &url, + bool force_server_side_feed_update) { + QSqlQuery q(db); + + q.prepare("INSERT INTO OwnCloudAccounts (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)); + q.bindValue(QSL(":url"), url); + q.bindValue(QSL(":force_update"), force_server_side_feed_update ? 1 : 0); + + if (q.exec()) { + return true; + } + else { + qWarning("ownCloud: Inserting of new account failed: '%s'.", qPrintable(q.lastError().text())); + return false; + } +} + +int DatabaseQueries::createAccount(QSqlDatabase db, const QString &code, bool *ok) { + QSqlQuery q(db); + + // First obtain the ID, which can be assigned to this new account. + if (!q.exec("SELECT max(id) FROM Accounts;") || !q.next()) { + qWarning("Getting max ID from Accounts table failed: '%s'.", qPrintable(q.lastError().text())); + + if (ok != NULL) { + *ok = false; + } + + return 0; + } + + int id_to_assign = q.value(0).toInt() + 1; + + q.prepare(QSL("INSERT INTO Accounts (id, type) VALUES (:id, :type);")); + q.bindValue(QSL(":id"), id_to_assign); + q.bindValue(QSL(":type"), code); + + if (q.exec()) { + if (ok != NULL) { + *ok = true; + } + + return id_to_assign; + } + else { + if (ok != NULL) { + *ok = false; + } + + qWarning("Inserting of new account failed: '%s'.", qPrintable(q.lastError().text())); + return 0; + } +} + +Assignment DatabaseQueries::getOwnCloudCategories(QSqlDatabase db, int account_id, bool *ok) { + Assignment categories; + + // Obtain data for categories from the database. + QSqlQuery q(db); + q.setForwardOnly(true); + q.prepare(QSL("SELECT * FROM Categories WHERE account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); + + if (!q.exec()) { + qFatal("ownCloud: Query for obtaining categories failed. Error message: '%s'.", qPrintable(q.lastError().text())); + + if (ok != NULL) { + *ok = false; + } + } + + while (q.next()) { + AssignmentItem pair; + pair.first = q.value(CAT_DB_PARENT_ID_INDEX).toInt(); + pair.second = new OwnCloudCategory(q.record()); + + categories << pair; + } + + if (ok != NULL) { + *ok = true; + } + + return categories; +} + +Assignment DatabaseQueries::getOwnCloudFeeds(QSqlDatabase db, int account_id, bool *ok) { + Assignment feeds; + + QSqlQuery q(db); + q.setForwardOnly(true); + q.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;")); + q.bindValue(QSL(":account_id"), account_id); + + if (!q.exec()) { + qFatal("ownCloud: Query for obtaining feeds failed. Error message: '%s'.", qPrintable(q.lastError().text())); + + if (ok != NULL) { + *ok = false; + } + } + + while (q.next()) { + AssignmentItem pair; + pair.first = q.value(FDS_DB_CATEGORY_INDEX).toInt(); + pair.second = new OwnCloudFeed(q.record()); + + feeds << pair; + } + + if (ok != NULL) { + *ok = true; + } + + return feeds; +} + DatabaseQueries::DatabaseQueries() { } diff --git a/src/miscellaneous/databasequeries.h b/src/miscellaneous/databasequeries.h index 36941d7e6..ffbb8bc6e 100644 --- a/src/miscellaneous/databasequeries.h +++ b/src/miscellaneous/databasequeries.h @@ -20,6 +20,8 @@ #include "services/abstract/rootitem.h" +#include "services/abstract/serviceroot.h" + #include @@ -57,6 +59,15 @@ class DatabaseQueries { static QStringList customIdsOfMessagesFromAccount(QSqlDatabase db, int account_id, bool *ok = NULL); static QStringList customIdsOfMessagesFromBin(QSqlDatabase db, int account_id, bool *ok = NULL); static QStringList customIdsOfMessagesFromFeed(QSqlDatabase db, int feed_custom_id, int account_id, bool *ok = NULL); + static QList getOwnCloudAccounts(QSqlDatabase db, bool *ok = NULL); + static bool deleteOwnCloudAccount(QSqlDatabase db, int account_id); + static bool overwriteOwnCloudAccount(QSqlDatabase db, const QString &username, const QString &password, + const QString &url, bool force_server_side_feed_update, int account_id); + static bool createOwnCloudAccount(QSqlDatabase db, int id_to_assign, const QString &username, const QString &password, + const QString &url, bool force_server_side_feed_update); + static int createAccount(QSqlDatabase db, const QString &code, 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); private: explicit DatabaseQueries(); diff --git a/src/services/owncloud/owncloudserviceentrypoint.cpp b/src/services/owncloud/owncloudserviceentrypoint.cpp index 93479acba..64e4efe90 100755 --- a/src/services/owncloud/owncloudserviceentrypoint.cpp +++ b/src/services/owncloud/owncloudserviceentrypoint.cpp @@ -20,15 +20,11 @@ #include "definitions/definitions.h" #include "miscellaneous/application.h" #include "miscellaneous/iconfactory.h" +#include "miscellaneous/databasequeries.h" #include "services/owncloud/definitions.h" #include "services/owncloud/owncloudserviceroot.h" -#include "services/owncloud/network/owncloudnetworkfactory.h" #include "services/owncloud/gui/formeditowncloudaccount.h" #include "gui/dialogs/formmain.h" -#include "miscellaneous/textfactory.h" - -#include -#include OwnCloudServiceEntryPoint::OwnCloudServiceEntryPoint() { @@ -45,28 +41,8 @@ ServiceRoot *OwnCloudServiceEntryPoint::createNewRoot() const { QList OwnCloudServiceEntryPoint::initializeSubtree() const { // Check DB if standard account is enabled. QSqlDatabase database = qApp->database()->connection(QSL("OwnCloudServiceEntryPoint"), DatabaseFactory::FromSettings); - QSqlQuery query(database); - QList roots; - if (query.exec("SELECT * FROM OwnCloudAccounts;")) { - while (query.next()) { - OwnCloudServiceRoot *root = new OwnCloudServiceRoot(); - root->setId(query.value(0).toInt()); - root->setAccountId(query.value(0).toInt()); - root->network()->setAuthUsername(query.value(1).toString()); - root->network()->setAuthPassword(TextFactory::decrypt(query.value(2).toString())); - root->network()->setUrl(query.value(3).toString()); - root->network()->setForceServerSideUpdate(query.value(4).toBool()); - - root->updateTitle(); - roots.append(root); - } - } - else { - qWarning("OwnCloud: Getting list of activated accounts failed: '%s'.", qPrintable(query.lastError().text())); - } - - return roots; + return DatabaseQueries::getOwnCloudAccounts(database); } bool OwnCloudServiceEntryPoint::isSingleInstanceService() const { diff --git a/src/services/owncloud/owncloudserviceroot.cpp b/src/services/owncloud/owncloudserviceroot.cpp index 053187ff8..f982d7f90 100755 --- a/src/services/owncloud/owncloudserviceroot.cpp +++ b/src/services/owncloud/owncloudserviceroot.cpp @@ -18,7 +18,7 @@ #include "services/owncloud/owncloudserviceroot.h" #include "definitions/definitions.h" -#include "miscellaneous/databasefactory.h" +#include "miscellaneous/databasequeries.h" #include "miscellaneous/application.h" #include "miscellaneous/textfactory.h" #include "miscellaneous/iconfactory.h" @@ -30,9 +30,6 @@ #include "services/owncloud/network/owncloudnetworkfactory.h" #include "services/owncloud/gui/formeditowncloudaccount.h" -#include -#include - OwnCloudServiceRoot::OwnCloudServiceRoot(RootItem *parent) : ServiceRoot(parent), m_recycleBin(new OwnCloudRecycleBin(this)), @@ -60,14 +57,9 @@ bool OwnCloudServiceRoot::editViaGui() { } bool OwnCloudServiceRoot::deleteViaGui() { - QSqlDatabase connection = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - QSqlQuery query(connection); + QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - query.setForwardOnly(true); - query.prepare(QSL("DELETE FROM OwnCloudAccounts WHERE id = :id;")); - query.bindValue(QSL(":id"), accountId()); - - if (query.exec()) { + if (DatabaseQueries::deleteOwnCloudAccount(database, accountId())) { return ServiceRoot::deleteViaGui(); } else { @@ -179,62 +171,28 @@ void OwnCloudServiceRoot::saveAccountDataToDatabase() { if (accountId() != NO_PARENT_CATEGORY) { // We are overwritting previously saved data. QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - QSqlQuery query(database); - query.prepare("UPDATE OwnCloudAccounts " - "SET username = :username, password = :password, url = :url, force_update = :force_update " - "WHERE id = :id;"); - query.bindValue(QSL(":username"), m_network->authUsername()); - query.bindValue(QSL(":password"), TextFactory::encrypt(m_network->authPassword())); - query.bindValue(QSL(":url"), m_network->url()); - query.bindValue(QSL(":force_update"), (int) m_network->forceServerSideUpdate()); - query.bindValue(QSL(":id"), accountId()); - - if (query.exec()) { + if (DatabaseQueries::overwriteOwnCloudAccount(database, m_network->authUsername(), + m_network->authPassword(), m_network->url(), + m_network->forceServerSideUpdate(), accountId())) { updateTitle(); itemChanged(QList() << this); } - else { - qWarning("ownCloud: Updating account failed: '%s'.", qPrintable(query.lastError().text())); - } } else { // We are probably saving newly added account. QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - QSqlQuery query(database); - - // First obtain the ID, which can be assigned to this new account. - if (!query.exec("SELECT max(id) FROM Accounts;") || !query.next()) { - qWarning("ownCloud: Getting max ID from Accounts table failed: '%s'.", qPrintable(query.lastError().text())); - return; - } - - int id_to_assign = query.value(0).toInt() + 1; - bool saved = true; - - query.prepare(QSL("INSERT INTO Accounts (id, type) VALUES (:id, :type);")); - query.bindValue(QSL(":id"), id_to_assign); - query.bindValue(QSL(":type"), code()); - - saved &= query.exec(); - - query.prepare("INSERT INTO OwnCloudAccounts (id, username, password, url, force_update) " - "VALUES (:id, :username, :password, :url, :force_update);"); - query.bindValue(QSL(":id"), id_to_assign); - query.bindValue(QSL(":username"), m_network->authUsername()); - query.bindValue(QSL(":password"), TextFactory::encrypt(m_network->authPassword())); - query.bindValue(QSL(":url"), m_network->url()); - query.bindValue(QSL(":force_update"), (int) m_network->forceServerSideUpdate()); - - saved &= query.exec(); + bool saved; + int id_to_assign = DatabaseQueries::createAccount(database, code(), &saved); if (saved) { - setId(id_to_assign); - setAccountId(id_to_assign); - updateTitle(); - } - else { - qWarning("ownCloud: Saving of new account failed: '%s'.", qPrintable(query.lastError().text())); + if (DatabaseQueries::createOwnCloudAccount(database, id_to_assign, m_network->authUsername(), + m_network->authPassword(), m_network->url(), + m_network->forceServerSideUpdate())) { + setId(id_to_assign); + setAccountId(id_to_assign); + updateTitle(); + } } } } @@ -258,44 +216,8 @@ RootItem *OwnCloudServiceRoot::obtainNewTreeForSyncIn() const { void OwnCloudServiceRoot::loadFromDatabase() { QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - Assignment categories; - Assignment feeds; - - // 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()) { - qFatal("Query for obtaining categories failed. Error message: '%s'.", qPrintable(query_categories.lastError().text())); - } - - while (query_categories.next()) { - AssignmentItem pair; - pair.first = query_categories.value(CAT_DB_PARENT_ID_INDEX).toInt(); - pair.second = new OwnCloudCategory(query_categories.record()); - - categories << pair; - } - - // 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()) { - qFatal("Query for obtaining feeds failed. Error message: '%s'.", qPrintable(query_feeds.lastError().text())); - } - - while (query_feeds.next()) { - AssignmentItem pair; - pair.first = query_feeds.value(FDS_DB_CATEGORY_INDEX).toInt(); - pair.second = new OwnCloudFeed(query_feeds.record()); - - feeds << pair; - } + Assignment categories = DatabaseQueries::getOwnCloudCategories(database, accountId()); + Assignment feeds = DatabaseQueries::getOwnCloudFeeds(database, accountId()); // All data are now obtained, lets create the hierarchy. assembleCategories(categories);