mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-05-11 08:59:41 +00:00
Merge pull request #67 from QuasarApp/some_fixes
Adde new temlate methods for make easyly get access to default database objects
This commit is contained in:
commit
8b8e20afa1
@ -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();
|
||||
|
@ -125,7 +125,7 @@ bool QH::APIVersionParser::commandsValidation(const QSharedPointer<iParser> &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;
|
||||
|
@ -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<PKG::SetSingleValue>::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<PKG::SetSingleValue>::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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,9 @@
|
||||
#ifndef QH_DATABASE_H
|
||||
#define QH_DATABASE_H
|
||||
|
||||
#include "dbobjectsrequest.h"
|
||||
#include "dbpatch.h"
|
||||
#include "isqldb.h"
|
||||
#include <dbobject.h>
|
||||
#include <hostaddress.h>
|
||||
|
||||
@ -97,6 +99,21 @@ public:
|
||||
*/
|
||||
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:
|
||||
|
||||
/**
|
||||
@ -293,6 +310,127 @@ protected:
|
||||
*/
|
||||
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.
|
||||
* @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<Object>, or nullptr if the object is not found.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @code{cpp}
|
||||
* return getById<MenuItem>(id, &MenuItem::setId);
|
||||
* @endcode
|
||||
*/
|
||||
template <class Object, class Id, class Setter>
|
||||
QSharedPointer<Object> getById(const Id& id, Setter setter, bool ifNotExistsCreate = false) {
|
||||
if (auto&& database = db()) {
|
||||
auto&& request = QSharedPointer<Object>::create();
|
||||
(*request.*setter)(id);
|
||||
|
||||
if (auto&& result = database->getObject(*request)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (ifNotExistsCreate)
|
||||
return 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<Role>(userId, &Role::setUserId);
|
||||
* @endcode
|
||||
*/
|
||||
template <class Object, class Id, class Setter>
|
||||
bool deleteById(const Id& id, Setter setter) {
|
||||
if (auto&& database = db()) {
|
||||
auto&& request = QSharedPointer<Object>::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<Role>());
|
||||
* @endcode
|
||||
*/
|
||||
template <class Object>
|
||||
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<Object>.
|
||||
* If no objects are found, an empty list is returned.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @code{cpp}
|
||||
auto&& data = getAll<User>("Users");
|
||||
* @endcode
|
||||
*/
|
||||
template<class Object>
|
||||
QList<QSharedPointer<Object>> getAll(const QString& table, const QString& condition = "") {
|
||||
QH::PKG::DBObjectsRequest<Object> request(table, condition);
|
||||
|
||||
auto&& response = db()->getObject(request);
|
||||
if (!response) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return response->data();
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief workWithSubscribe This method work with subscribe commnads.
|
||||
|
@ -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;
|
||||
|
@ -21,7 +21,7 @@ PackageManager::PackageManager()
|
||||
}
|
||||
|
||||
PackageManager::~PackageManager() {
|
||||
for (const auto& data : qAsConst(_parseResults)) {
|
||||
for (const auto& data : std::as_const(_parseResults)) {
|
||||
delete data;
|
||||
}
|
||||
|
||||
|
@ -134,7 +134,7 @@ protected:
|
||||
QDataStream &toStream(QDataStream &stream) const override {
|
||||
stream << static_cast<int>(_packData.size());
|
||||
|
||||
for (const auto &data: qAsConst(_packData)) {
|
||||
for (const auto &data: std::as_const(_packData)) {
|
||||
stream << *data;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 90a4284c56856f0cbdb1a7af151d575a468c59eb
|
||||
Subproject commit 334e209ff4ba23bf50927c6bbeb8993fe13f9f03
|
Loading…
x
Reference in New Issue
Block a user