From afd2be5c5dbe13872d941541599e5e7fbbf36ddb Mon Sep 17 00:00:00 2001 From: EndrII Date: Fri, 6 Oct 2023 23:25:54 +0200 Subject: [PATCH 1/5] adde new temlate methods for make easyly get access to default data base objects --- src/public/database.h | 116 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/src/public/database.h b/src/public/database.h index 866865e..7a2e5b3 100644 --- a/src/public/database.h +++ b/src/public/database.h @@ -8,7 +8,9 @@ #ifndef QH_DATABASE_H #define QH_DATABASE_H +#include "dbobjectsrequest.h" #include "dbpatch.h" +#include "isqldb.h" #include #include @@ -293,6 +295,120 @@ class HEARTSHARED_EXPORT DataBase: public QObject */ virtual void onBeforeDBUpgrade(int currentVerion, int tergetVersion) const; + + /** + * @brief Get an object by its identifier. + * + * This method retrieves an object of type @c Object from the database using the + * given identifier and setter function. + * + * @tparam Object The type of object to retrieve. + * @tparam Id The type of the identifier. + * @tparam Setter The type of the setter function. + * @param id The identifier of the object. + * @param setter The setter function to set the identifier in the object. + * @return A pointer to an object of type QSharedPointer, or nullptr if the object is not found. + * + * Example: + * + * @code{cpp} + * return getById(id, &MenuItem::setId); + * @endcode + */ + template + QSharedPointer getById(const Id& id, Setter setter) { + if (auto&& database = db()) { + Object request; + (request.*setter)(id); + return database->getObject(request); + } + + return nullptr; + }; + + /** + * @brief Delete an object by its identifier. + * + * This method deletes an object of type @c Object from the database using the + * given identifier and setter function. + * + * @tparam Object The type of object to delete. + * @tparam Id The type of the identifier. + * @tparam Setter The type of the setter function. + * @param id The identifier of the object. + * @param setter The setter function to set the identifier in the object. + * @return true if the object is successfully deleted, false otherwise. + * + * Example: + * + * @code{cpp} + deleteById(userId, &Role::setUserId); + * @endcode + */ + template + bool deleteById(const Id& id, Setter setter) { + if (auto&& database = db()) { + auto&& request = QSharedPointer::create(); + (*request.*setter)(id); + return database->deleteObject(request); + } + + return false; + }; + + /** + * @brief Save an object in the database. + * + * This method saves an object of type @c Object in the database. + * + * @tparam Object The type of object to save. + * @param obj The object to save. + * @return true if the object is successfully saved, false otherwise. + * + * Example: + * + * @code{cpp} + saveObj(role.dynamicCast()); + * @endcode + */ + template + bool saveObj(const Object& obj) { + if (auto&& database = db()) { + return database->replaceObject(obj); + } + + return false; + }; + + /** + * @brief Get a list of all objects from a specified table. + * + * This method retrieves a list of all objects of type @c Object from the specified + * table in the database. + * + * @tparam Object The type of objects to retrieve. + * @param table The name of the table in the database. + * @return A list of pointers to objects of type QSharedPointer. + * If no objects are found, an empty list is returned. + * + * Example: + * + * @code{cpp} + auto&& data = getAll("Users"); + * @endcode + */ + template + QList> getAll(const QString& table) { + QH::PKG::DBObjectsRequest request(table); + + auto&& response = db()->getObject(request); + if (!response) { + return {}; + } + + return response->data(); + } + private: /** * @brief workWithSubscribe This method work with subscribe commnads. From 7df1b57ac2716d6c0ef22448e4d1148e1d4f4a62 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 7 Oct 2023 11:47:38 +0200 Subject: [PATCH 2/5] added new option ifNotExistsCreate --- src/public/database.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/public/database.h b/src/public/database.h index 7a2e5b3..b58c0bd 100644 --- a/src/public/database.h +++ b/src/public/database.h @@ -307,6 +307,7 @@ class HEARTSHARED_EXPORT DataBase: public QObject * @tparam Setter The type of the setter function. * @param id The identifier of the object. * @param setter The setter function to set the identifier in the object. + * @param ifNotExistsCreate - this option will create a new object if object with @a id is not existst into database. But object wil not save into database. * @return A pointer to an object of type QSharedPointer, or nullptr if the object is not found. * * Example: @@ -316,11 +317,17 @@ class HEARTSHARED_EXPORT DataBase: public QObject * @endcode */ template - QSharedPointer getById(const Id& id, Setter setter) { + QSharedPointer getById(const Id& id, Setter setter, bool ifNotExistsCreate = false) { if (auto&& database = db()) { - Object request; - (request.*setter)(id); - return database->getObject(request); + auto&& request = QSharedPointer::create(); + (*request.*setter)(id); + + if (auto&& result = database->getObject(*request)) { + return result; + } + + if (ifNotExistsCreate) + return request; } return nullptr; From 08d52c13226c9a4ecbad1d2aca213d951541fd67 Mon Sep 17 00:00:00 2001 From: EndrII Date: Thu, 12 Oct 2023 00:01:05 +0200 Subject: [PATCH 3/5] added setAttribute finctions to default database --- src/public/database.cpp | 45 +++++++++++++++++++++++++++++------------ src/public/database.h | 15 ++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/public/database.cpp b/src/public/database.cpp index 27620cd..453eb1f 100644 --- a/src/public/database.cpp +++ b/src/public/database.cpp @@ -229,11 +229,7 @@ bool DataBase::upgradeDataBase() { return false; } - PKG::GetSingleValue request({"DataBaseAttributes", "version"}, "value", "name"); - - if (auto responce = _db->getObject(request)) { - currentVersion = responce->value().toInt(); - } + currentVersion = getDBAttribute("version", 0).toInt(); if (currentVersion < _targetDBVersion) onBeforeDBUpgrade(currentVersion, _targetDBVersion); @@ -265,14 +261,7 @@ bool DataBase::upgradeDataBase() { } currentVersion = patch.versionTo; - - auto updateVersionRequest = QSharedPointer::create( - DbAddress{"DataBaseAttributes", "version"}, - "value", currentVersion, "name"); - - if (!_db->replaceObject(updateVersionRequest, true)) { - QuasarAppUtils::Params::log("Failed to update version attribute", - QuasarAppUtils::Error); + if (!setDBAttribute("version", currentVersion)) { return false; } } @@ -300,5 +289,35 @@ QVariantMap DataBase::defaultDbParams() const { {QH_DB_BACKUP_PATH, DEFAULT_DB_PATH + "/" + localNodeName() + "/BackUp"} }; } + +QVariant DataBase::getDBAttribute(const QString& key, const QVariant& defaultVal) { + if (!_db) + return defaultVal; + + PKG::GetSingleValue request({"DataBaseAttributes", key}, "value", "name"); + + if (auto&& responce = _db->getObject(request)) { + if (!responce->value().isNull()) + return responce->value(); + + } + + return defaultVal; +} + +bool DataBase::setDBAttribute(const QString& key, const QVariant& newValue) { + auto updateVersionRequest = QSharedPointer::create( + DbAddress{"DataBaseAttributes", key}, + "value", newValue, "name"); + + if (!_db->replaceObject(updateVersionRequest, true)) { + QuasarAppUtils::Params::log(QString("Failed to update %0 attribute").arg(key), + QuasarAppUtils::Error); + return false; + } + + return true; +} + } diff --git a/src/public/database.h b/src/public/database.h index b58c0bd..05ca8f6 100644 --- a/src/public/database.h +++ b/src/public/database.h @@ -99,6 +99,21 @@ class HEARTSHARED_EXPORT DataBase: public QObject */ QString dbLocation() const; + /** + * @brief getDBAttribute This method gets value from the default of QH DB table "DataBaseAttributes". + * @param key This is key of attribute + * @param defaultVal this is default value, if needed attribute is not exists. + * @return attribute value. + */ + QVariant getDBAttribute(const QString& key, const QVariant& defaultVal); + + /** + * @brief setDBAttribute This method sets value for the default of QH DB table "DataBaseAttributes". + * @param key This is key of attribute + * @param newValue this is new value + */ + bool setDBAttribute(const QString& key, const QVariant& newValue); + signals: /** From a77380f4ca968d27b25aa8b4476bc9603b932e7a Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 28 Oct 2023 11:49:28 +0200 Subject: [PATCH 4/5] fix getAll method --- src/public/database.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/database.h b/src/public/database.h index 05ca8f6..1af787d 100644 --- a/src/public/database.h +++ b/src/public/database.h @@ -420,8 +420,8 @@ class HEARTSHARED_EXPORT DataBase: public QObject * @endcode */ template - QList> getAll(const QString& table) { - QH::PKG::DBObjectsRequest request(table); + QList> getAll(const QString& table, const QString& condition = "") { + QH::PKG::DBObjectsRequest request(table, condition); auto&& response = db()->getObject(request); if (!response) { From bfe8b75e0f888b86c13c9d7c7b9fff66d908ea66 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 28 Oct 2023 21:31:51 +0200 Subject: [PATCH 5/5] fix qt 6.6 warnings --- src/public/abstractnode.cpp | 6 +++--- src/public/apiversionparser.cpp | 2 +- src/public/isqldb.cpp | 4 ++-- src/public/packagemanager.cpp | 2 +- src/public/packages/datapack.h | 2 +- src/public/sqldbwriter.cpp | 2 +- submodules/QuasarAppLib | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/public/abstractnode.cpp b/src/public/abstractnode.cpp index 1858d7b..87bad53 100644 --- a/src/public/abstractnode.cpp +++ b/src/public/abstractnode.cpp @@ -92,7 +92,7 @@ AbstractNode::~AbstractNode() { _senderThread->quit(); _senderThread->wait(); - for (auto it: qAsConst(_receiveData)) { + for (auto it: std::as_const(_receiveData)) { delete it; } @@ -147,13 +147,13 @@ void AbstractNode::stop() { close(); _connectionsMutex.lock(); - for (const auto &i : qAsConst(_connections)) { + for (const auto &i : std::as_const(_connections)) { i->disconnect(); } _connectionsMutex.unlock(); _workersMutex.lock(); - for (auto it: qAsConst(_workers)) { + for (auto it: std::as_const(_workers)) { if (!it->isFinished()) { it->cancel(); it->waitForFinished(); diff --git a/src/public/apiversionparser.cpp b/src/public/apiversionparser.cpp index 4399c13..ba1c4e1 100644 --- a/src/public/apiversionparser.cpp +++ b/src/public/apiversionparser.cpp @@ -125,7 +125,7 @@ bool QH::APIVersionParser::commandsValidation(const QSharedPointer &par parserObject->registeredTypes().keyEnd()}; int typesSize = types.size(); - for (const auto &parsersMap : qAsConst(_apiParsers)) { + for (const auto &parsersMap : std::as_const(_apiParsers)) { for (const auto &parser: parsersMap) { if (parser->parserId() == parserObject->parserId()) { continue; diff --git a/src/public/isqldb.cpp b/src/public/isqldb.cpp index 2f99595..ec5e87f 100644 --- a/src/public/isqldb.cpp +++ b/src/public/isqldb.cpp @@ -167,7 +167,7 @@ bool ISqlDB::getAllObjects(const DBObject &templateObject, return false; } - for (const auto &object: qAsConst(result)) { + for (const auto &object: std::as_const(result)) { if (object->isCached() && !insertToCache(object)) { QuasarAppUtils::Params::log("Selected object from database can not be saved into database cache. " + object->toString(), @@ -301,7 +301,7 @@ bool ISqlDB::changeObjects(const DBObject &templateObject, if (!list.size()) return false; - for (const auto& obj :qAsConst(list)) { + for (const auto& obj :std::as_const(list)) { if (!changeAction(obj)) { return false; diff --git a/src/public/packagemanager.cpp b/src/public/packagemanager.cpp index fcb0089..5c1f841 100644 --- a/src/public/packagemanager.cpp +++ b/src/public/packagemanager.cpp @@ -21,7 +21,7 @@ PackageManager::PackageManager() } PackageManager::~PackageManager() { - for (const auto& data : qAsConst(_parseResults)) { + for (const auto& data : std::as_const(_parseResults)) { delete data; } diff --git a/src/public/packages/datapack.h b/src/public/packages/datapack.h index 208983d..398609a 100644 --- a/src/public/packages/datapack.h +++ b/src/public/packages/datapack.h @@ -134,7 +134,7 @@ class DataPack final: public AbstractData QDataStream &toStream(QDataStream &stream) const override { stream << static_cast(_packData.size()); - for (const auto &data: qAsConst(_packData)) { + for (const auto &data: std::as_const(_packData)) { stream << *data; } diff --git a/src/public/sqldbwriter.cpp b/src/public/sqldbwriter.cpp index ff776f6..9055263 100644 --- a/src/public/sqldbwriter.cpp +++ b/src/public/sqldbwriter.cpp @@ -111,7 +111,7 @@ bool SqlDBWriter::initDbPrivate(const QVariantMap ¶ms) { return false; } - for (const QString& sqlFile : qAsConst(_SQLSources)) { + for (const QString& sqlFile : std::as_const(_SQLSources)) { QSqlQuery query(*_db); if (!exec(&query, sqlFile)) { return false; diff --git a/submodules/QuasarAppLib b/submodules/QuasarAppLib index 90a4284..334e209 160000 --- a/submodules/QuasarAppLib +++ b/submodules/QuasarAppLib @@ -1 +1 @@ -Subproject commit 90a4284c56856f0cbdb1a7af151d575a468c59eb +Subproject commit 334e209ff4ba23bf50927c6bbeb8993fe13f9f03