Skip to content

Commit

Permalink
Merge pull request #67 from QuasarApp/some_fixes
Browse files Browse the repository at this point in the history
Adde new temlate methods for make easyly get access to default database objects
  • Loading branch information
EndrII authored Oct 30, 2023
2 parents 214b248 + bfe8b75 commit 8b8e20a
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/public/abstractnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ AbstractNode::~AbstractNode() {
_senderThread->quit();
_senderThread->wait();

for (auto it: qAsConst(_receiveData)) {
for (auto it: std::as_const(_receiveData)) {
delete it;
}

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/public/apiversionparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 32 additions & 13 deletions src/public/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

}

138 changes: 138 additions & 0 deletions src/public/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -97,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:

/**
Expand Down Expand Up @@ -293,6 +310,127 @@ 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.
* @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.
Expand Down
4 changes: 2 additions & 2 deletions src/public/isqldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/public/packagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ PackageManager::PackageManager()
}

PackageManager::~PackageManager() {
for (const auto& data : qAsConst(_parseResults)) {
for (const auto& data : std::as_const(_parseResults)) {
delete data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/public/packages/datapack.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class DataPack final: public AbstractData
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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/public/sqldbwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bool SqlDBWriter::initDbPrivate(const QVariantMap &params) {
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;
Expand Down
2 changes: 1 addition & 1 deletion submodules/QuasarAppLib

0 comments on commit 8b8e20a

Please sign in to comment.