diff --git a/CMakeLists.txt b/CMakeLists.txt index e25a919b..e55f8197 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (C) 2018-2022 QuasarApp. +# Copyright (C) 2018-2023 QuasarApp. # Distributed under the lgplv3 software license, see the accompanying # Everyone is permitted to copy and distribute verbatim copies # of this license document, but changing it is not allowed. @@ -33,8 +33,9 @@ else() endif() # Use only for android debug builds with debugging from usb. option(HEART_STATIC_SSL "This option enable or disabled static link ssl libraryes" OFF) -option(HEART_DEPRECATED_API "This option enable or disabled deprecated command api" OFF) -option(CMAKE_SHARE "This option enable or disabled ssl functions of nodes" OFF) +option(HEART_PRINT_PACKAGES "This option enable or disabled log of add incoming network packages" OFF) +option(HEART_PRINT_SQL_QUERIES "This option enable or disabled log of all sql queries" OFF) + option(BUILD_SHARED_LIBS "Enable or disable shared libraryes" OFF) if (WIN32) diff --git a/HeartTests/CMakeLists.txt b/HeartTests/CMakeLists.txt index 60892a72..4f83d9cc 100644 --- a/HeartTests/CMakeLists.txt +++ b/HeartTests/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (C) 2018-2022 QuasarApp. +# Copyright (C) 2018-2023 QuasarApp. # Distributed under the lgplv3 software license, see the accompanying # Everyone is permitted to copy and distribute verbatim copies # of this license document, but changing it is not allowed. diff --git a/HeartTests/tst_testprotockol.cpp b/HeartTests/tst_testprotockol.cpp index 9f5ca975..7e3820aa 100644 --- a/HeartTests/tst_testprotockol.cpp +++ b/HeartTests/tst_testprotockol.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/abstractnodetest.cpp b/HeartTests/units/abstractnodetest.cpp index 6a689431..7853401a 100644 --- a/HeartTests/units/abstractnodetest.cpp +++ b/HeartTests/units/abstractnodetest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/abstractnodetest.h b/HeartTests/units/abstractnodetest.h index 61d1f366..5f00b1b7 100644 --- a/HeartTests/units/abstractnodetest.h +++ b/HeartTests/units/abstractnodetest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/bigdatatest.cpp b/HeartTests/units/bigdatatest.cpp index e7e4ff08..7dadf62b 100644 --- a/HeartTests/units/bigdatatest.cpp +++ b/HeartTests/units/bigdatatest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/bigdatatest.h b/HeartTests/units/bigdatatest.h index bca3710a..a36b5c04 100644 --- a/HeartTests/units/bigdatatest.h +++ b/HeartTests/units/bigdatatest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/ecdsaauthtest.cpp b/HeartTests/units/ecdsaauthtest.cpp index 76c748ba..b88fcfe8 100644 --- a/HeartTests/units/ecdsaauthtest.cpp +++ b/HeartTests/units/ecdsaauthtest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 QuasarApp. + * Copyright (C) 2022-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/ecdsaauthtest.h b/HeartTests/units/ecdsaauthtest.h index 9101ed5d..4eaf62d3 100644 --- a/HeartTests/units/ecdsaauthtest.h +++ b/HeartTests/units/ecdsaauthtest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 QuasarApp. + * Copyright (C) 2022-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/keystoragetest.h b/HeartTests/units/keystoragetest.h index 2625bdb5..bb3d6fa9 100644 --- a/HeartTests/units/keystoragetest.h +++ b/HeartTests/units/keystoragetest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/shedullertest.cpp b/HeartTests/units/shedullertest.cpp index 1f8ab572..2b2e58a6 100644 --- a/HeartTests/units/shedullertest.cpp +++ b/HeartTests/units/shedullertest.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/HeartTests/units/shedullertest.h b/HeartTests/units/shedullertest.h index 038564df..35c58988 100644 --- a/HeartTests/units/shedullertest.h +++ b/HeartTests/units/shedullertest.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/HeartTests/units/test.cpp b/HeartTests/units/test.cpp index 29fcfa04..57d5de8f 100644 --- a/HeartTests/units/test.cpp +++ b/HeartTests/units/test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/test.h b/HeartTests/units/test.h index 3d711719..3fb2e529 100644 --- a/HeartTests/units/test.h +++ b/HeartTests/units/test.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/testutils.cpp b/HeartTests/units/testutils.cpp index 2b819584..66af55c3 100644 --- a/HeartTests/units/testutils.cpp +++ b/HeartTests/units/testutils.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/testutils.h b/HeartTests/units/testutils.h index 4ad1ef62..16075f38 100644 --- a/HeartTests/units/testutils.h +++ b/HeartTests/units/testutils.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/HeartTests/units/upgradedatabasetest.cpp b/HeartTests/units/upgradedatabasetest.cpp index b6ca7ffe..e0b7e0c6 100644 --- a/HeartTests/units/upgradedatabasetest.cpp +++ b/HeartTests/units/upgradedatabasetest.cpp @@ -22,7 +22,7 @@ class UpgradableDatabase: public QH::DataBase { bool checkVersion(int version) { QSqlQuery query; - if (!db()->doQuery("SELECT * FROM DataBaseAttributes WHERE name='version'", true, &query)){ + if (!db()->doQuery("SELECT * FROM DataBaseAttributes WHERE name='version'", {}, true, &query)){ return false; }; @@ -44,7 +44,7 @@ class UpgradableDatabase: public QH::DataBase { [](const QH::iObjectProvider* database) -> bool { QSqlQuery query; - if (!database->doQuery("select * from DataBaseAttributes", true, &query)){ + if (!database->doQuery("select * from DataBaseAttributes", {}, true, &query)){ return false; }; @@ -57,7 +57,7 @@ class UpgradableDatabase: public QH::DataBase { 2, // to version [](const QH::iObjectProvider* database) -> bool { QSqlQuery query; - if (!database->doQuery("select * from DataBaseAttributes", true, &query)){ + if (!database->doQuery("select * from DataBaseAttributes", {}, true, &query)){ return false; }; @@ -70,7 +70,7 @@ class UpgradableDatabase: public QH::DataBase { 3, // to version [](const QH::iObjectProvider* database) -> bool { QSqlQuery query; - if (!database->doQuery("select * from DataBaseAttributes", true, &query)){ + if (!database->doQuery("select * from DataBaseAttributes", {}, true, &query)){ return false; }; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f1de354e..78d1d351 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (C) 2018-2022 QuasarApp. +# Copyright (C) 2018-2023 QuasarApp. # Distributed under the lgplv3 software license, see the accompanying # Everyone is permitted to copy and distribute verbatim copies # of this license document, but changing it is not allowed. @@ -17,8 +17,12 @@ set(CMAKE_AUTORCC ON) add_definitions(-DHEART_LIBRARY) -if (HEART_DEPRECATED_API) - add_definitions(-DHEART_DEPRECATED_API) +if (HEART_PRINT_SQL_QUERIES) + add_definitions(-DHEART_PRINT_SQL_QUERIES) +endif() + +if (HEART_PRINT_PACKAGES) + add_definitions(-DHEART_PRINT_PACKAGES) endif() set(SLL_DEFINE "WITHOUT_SSL") @@ -41,13 +45,6 @@ file(GLOB SOURCE_CPP "public/hcryptoFeatures/*.cpp" "public/hcryptoFeatures/*.h" "public/hcryptoFeatures/*.qrc" ) -if (NOT HEART_DEPRECATED_API) - file(GLOB SOURCE_CPP_DEPRECATED - "private/*_old.cpp" "private/*_old.h" - ) - list(REMOVE_ITEM SOURCE_CPP ${SOURCE_CPP_DEPRECATED}) -endif() - set(PUBLIC_INCUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set(PUBLIC_INCUDE_DIR ${PUBLIC_INCUDE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/public") set(PUBLIC_INCUDE_DIR ${PUBLIC_INCUDE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/public/packages") diff --git a/src/heart.cpp b/src/heart.cpp index 3e23877a..078d4efc 100644 --- a/src/heart.cpp +++ b/src/heart.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/heart.h b/src/heart.h index 69a98854..4c4f4fdf 100644 --- a/src/heart.h +++ b/src/heart.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/heart_global.h.in b/src/heart_global.h.in index 9ee55d49..8fae3062 100644 --- a/src/heart_global.h.in +++ b/src/heart_global.h.in @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/abstractnodeparser.cpp b/src/private/abstractnodeparser.cpp index 6e05cf44..baa18721 100644 --- a/src/private/abstractnodeparser.cpp +++ b/src/private/abstractnodeparser.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2022-2022 QuasarApp. +//# Copyright (C) 2022-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/private/abstractnodeparser.h b/src/private/abstractnodeparser.h index 08cebf2e..d58c5cc3 100644 --- a/src/private/abstractnodeparser.h +++ b/src/private/abstractnodeparser.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2022-2022 QuasarApp. +//# Copyright (C) 2022-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/private/abstractnodeparser_old.cpp b/src/private/abstractnodeparser_old.cpp deleted file mode 100644 index 83203d0b..00000000 --- a/src/private/abstractnodeparser_old.cpp +++ /dev/null @@ -1,86 +0,0 @@ -//# -//# Copyright (C) 2022-2022 QuasarApp. -//# Distributed under the lgplv3 software license, see the accompanying -//# Everyone is permitted to copy and distribute verbatim copies -//# of this license document, but changing it is not allowed. -//# - -#include "abstractnodeparser_old.h" -#include "params.h" -#include "abstractnode.h" -#include "qaglobalutils.h" - -#include -#include -#include -#include - -namespace QH { - -AbstractNodeParserOld::AbstractNodeParserOld(AbstractNode* parentNode): iParser(parentNode) { - debug_assert(parentNode, "Node object can't be null!"); - - registerPackageTypeOld(); - registerPackageTypeOld(); - registerPackageTypeOld(); -} - -AbstractNodeParserOld::~AbstractNodeParserOld() { -} - -ParserResult AbstractNodeParserOld::parsePackage(const QSharedPointer &pkg, - const Header &pkgHeader, - AbstractNodeInfo *sender) { - auto nodePtr = node(); - if (!nodePtr) { - return ParserResult::NotProcessed; - } - - if (!(sender)) { - QuasarAppUtils::Params::log("sender socket is not valid!", - QuasarAppUtils::Error); - return ParserResult::Error; - } - - if (!pkg->isValid()) { - QuasarAppUtils::Params::log("incomming package is not valid!", - QuasarAppUtils::Error); - nodePtr->changeTrust(sender->networkAddress(), CRITICAL_ERROOR); - return ParserResult::Error; - } - - if (PKG::Ping::commandOld() == pkg->cmd()) { - auto cmd = pkg.staticCast(); - if (!cmd->ansver()) { - cmd->setAnsver(true); - nodePtr->sendData(cmd.data(), sender, &pkgHeader); - } - - emit sigPingReceived(cmd); - - return ParserResult::Processed; - } else if (PKG::BadRequest::commandOld() == pkg->cmd()) { - auto cmd = static_cast(pkg.data()); - - emit nodePtr->requestError(cmd->errCode(), cmd->err()); - - return ParserResult::Processed; - - } else if (PKG::CloseConnection::commandOld() == pkg->cmd()) { - if (sender->isLocal()) { - nodePtr->removeNode(sender->networkAddress()); - } - return ParserResult::Processed; - } - - return ParserResult::NotProcessed; -} - -int AbstractNodeParserOld::version() const { - return 0; -} - -QString AbstractNodeParserOld::parserId() const { - return "HeartLibAbstractAPI"; -} -} diff --git a/src/private/abstractnodeparser_old.h b/src/private/abstractnodeparser_old.h deleted file mode 100644 index 96afd7da..00000000 --- a/src/private/abstractnodeparser_old.h +++ /dev/null @@ -1,53 +0,0 @@ -//# -//# Copyright (C) 2022-2022 QuasarApp. -//# Distributed under the lgplv3 software license, see the accompanying -//# Everyone is permitted to copy and distribute verbatim copies -//# of this license document, but changing it is not allowed. -//# - -#ifndef ABSTRACTNODEPARSER_OLD_H -#define ABSTRACTNODEPARSER_OLD_H - -#include -#include - -namespace QH { - -/** - * @brief The AbstractNodeParserOld class is main parser of the abstract level of the hear lib. - * @note This class some as AbstractNodeParser - */ -class AbstractNodeParserOld: public iParser -{ - Q_OBJECT -public: - template - /** - * @brief registerPackageTypeOld This method register package type T. - * This is need to prepare pacakge for parsing in the parsePackage method. - */ - void registerPackageTypeOld() { - _registeredTypes[T::commandOld()] = [](){ - return new T(); - }; - }; - - AbstractNodeParserOld(AbstractNode *parentNode); - ~AbstractNodeParserOld() override; - ParserResult parsePackage(const QSharedPointer &pkg, - const Header &pkgHeader, - AbstractNodeInfo *sender) override; - int version() const override; - QString parserId() const override; - -signals: - - /** - * @brief sigPingReceived This method emited - * @param ping this is received ping object. - */ - void sigPingReceived(const QSharedPointer &ping); - -}; -} -#endif diff --git a/src/private/apiversion.cpp b/src/private/apiversion.cpp index baab2b73..f29c3398 100644 --- a/src/private/apiversion.cpp +++ b/src/private/apiversion.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2020-2022 QuasarApp. +//# Copyright (C) 2020-2023 QuasarApp. //# Distributed under the GPLv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/private/apiversion.h b/src/private/apiversion.h index c8fb3622..dd43df0e 100644 --- a/src/private/apiversion.h +++ b/src/private/apiversion.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2020-2022 QuasarApp. +//# Copyright (C) 2020-2023 QuasarApp. //# Distributed under the GPLv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. @@ -30,8 +30,6 @@ class APIVersion: public QH::PKG::AbstractData static QString commandText(){return "PROTOCKOL_VERSION_COMMAND";} unsigned short cmd() const override {return APIVersion::command();} - unsigned short cmdOld() const override {return APIVersion::command();} - QString cmdString() const override {return APIVersion::commandText();} const VersionData &version() const; diff --git a/src/private/bigdataparser.cpp b/src/private/bigdataparser.cpp index 040b1296..3a32c708 100644 --- a/src/private/bigdataparser.cpp +++ b/src/private/bigdataparser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 QuasarApp. + * Copyright (C) 2022-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -95,6 +95,8 @@ bool BigDataParser::newPackage(const QSharedPointer &header, request.setCurrentPart(0); request.setPackageId(header->packageId()); + QuasarAppUtils::Params::log("Receive BigData Header:" + header->toString()); + return node()->sendData(&request, sender, &hdr); } @@ -110,6 +112,11 @@ bool BigDataParser::processPart(const QSharedPointer &part, auto& localPool = _pool[part->packageId()]; + QuasarAppUtils::Params::log(QString("Process Part of %0: part %1/%2"). + arg(part->packageId()). + arg(part->getPakckageNumber()). + arg(localPool.chaindata.size() - 1)); + localPool.chaindata[part->getPakckageNumber()] = part; auto sendRequest = [sender, &hdr, this](int part, unsigned int id){ diff --git a/src/private/bigdataparser.h b/src/private/bigdataparser.h index 3956249b..3964e026 100644 --- a/src/private/bigdataparser.h +++ b/src/private/bigdataparser.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/bigdataparser_old.cpp b/src/private/bigdataparser_old.cpp deleted file mode 100644 index 9627b50b..00000000 --- a/src/private/bigdataparser_old.cpp +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright (C) 2022-2022 QuasarApp. - * Distributed under the lgplv3 software license, see the accompanying - * Everyone is permitted to copy and distribute verbatim copies - * of this license document, but changing it is not allowed. -*/ - - -#include "bigdataparser_old.h" -#include "bigdataheader.h" -#include "bigdatapart.h" - -#include -#include -#include -#include -#include - -#define TIMEOUT_INTERVAL 30000 - -namespace QH { - -BigDataParserOld::BigDataParserOld(AbstractNode* parentNode): iParser(parentNode) { - - registerPackageTypeOld(); - registerPackageTypeOld(); - registerPackageTypeOld(); - registerPackageTypeOld(); - -} - -ParserResult BigDataParserOld::parsePackage(const QSharedPointer &pkg, - const Header &pkgHeader, - AbstractNodeInfo *sender) { - - - auto result = commandHandlerOld(this, - &BigDataParserOld::processRequest, - pkg, sender, pkgHeader); - if (result != QH::ParserResult::NotProcessed) { - return result; - } - - result = commandHandlerOld(this, - &BigDataParserOld::processBigDataWraper, - pkg, sender, pkgHeader); - if (result != QH::ParserResult::NotProcessed) { - return result; - } - - result = commandHandlerOld(this, - &BigDataParserOld::newPackage, - pkg, sender, pkgHeader); - if (result != QH::ParserResult::NotProcessed) { - return result; - } - - result = commandHandlerOld(this, - &BigDataParserOld::processPart, - pkg, sender, pkgHeader); - if (result != QH::ParserResult::NotProcessed) { - return result; - } - - return ParserResult::NotProcessed; -} - -int BigDataParserOld::version() const { - return 0; -} - -QString BigDataParserOld::parserId() const { - return "HeartBigDataAPI"; -} - -void QH::BigDataParserOld::insertNewBigData(const QSharedPointer &header) { - if (!_pool.contains(header->packageId())) { - QVector> _array; - _array.resize(header->getPackagesCount()); - _pool[header->packageId()] = {header, _array, static_cast(time(0))}; - } - - checkOutDatedPacakges(header->packageId()); -} - -bool BigDataParserOld::newPackage(const QSharedPointer &header, - AbstractNodeInfo *sender, - const Header & hdr) { - - if (!header->isValid()) - return false; - - insertNewBigData(header); - - PKG::BigDataRequest request; - request.setCurrentPart(0); - request.setPackageId(header->packageId()); - - return node()->sendData(&request, sender, &hdr); -} - -bool BigDataParserOld::processPart(const QSharedPointer &part, - AbstractNodeInfo *sender, - const Header & hdr) { - - if (!_pool.contains(part->packageId())) { - return false; - } - - checkOutDatedPacakges(part->packageId()); - - auto& localPool = _pool[part->packageId()]; - - localPool.chaindata[part->getPakckageNumber()] = part; - - auto sendRequest = [sender, &hdr, this](int part, unsigned int id){ - - PKG::BigDataRequest request; - request.setCurrentPart(part); - request.setPackageId(id); - - return node()->sendData(&request, sender, &hdr); - }; - - if (part->getPakckageNumber() + 1 < localPool.chaindata.size()) { - return sendRequest(part->getPakckageNumber() + 1, - part->packageId()); - - } - - auto package = node()->genPackage(localPool.header->getCommand(), - sender); - if (!package) - return false; - - QByteArray packageRawData; - for (int idx = 0; idx < localPool.chaindata.size(); ++idx) { - if (localPool.chaindata[idx]) { - packageRawData += localPool.chaindata[idx]->data(); - } else { - return sendRequest(idx, part->packageId()); - } - } - - package->fromBytes(packageRawData); - - if (node()->parsePackage(package, hdr, sender) == ParserResult::Error) { - return false; - } - - _pool.remove(part->packageId()); - - return true; -} - -bool BigDataParserOld::processRequest(const QSharedPointer &request, - AbstractNodeInfo *sender, - const Header &pkgHeader) { - - - unsigned int id = request->packageId(); - - if (!_pool.contains(id)) { - QuasarAppUtils::Params::log("requested data is missing!"); - return false; - } - - checkOutDatedPacakges(id); - - const auto &localPool = _pool[id]; - if (request->currentPart() >= localPool.chaindata.size()) { - return false; - } - - bool fLast = localPool.chaindata.size() - 1 == request->currentPart(); - - const auto &data = localPool.chaindata[request->currentPart()]; - if (!node()->sendData(data.data(), sender, &pkgHeader)) { - return false; - } - - if (fLast) { - _pool.remove(id); - } - - return true; -} - -bool BigDataParserOld::processBigDataWraper(const QSharedPointer &request, - AbstractNodeInfo *sender, - const Header &pkgHeader) { - return sendBigDataPackage(request->data(), sender, &pkgHeader); -} - -bool BigDataParserOld::sendBigDataPackage(const PKG::AbstractData *data, - const AbstractNodeInfo *sender, - const QH::Header *pkgHeader) { - - unsigned int sizeLimit = Package::maximumSize() - sizeof (PKG::BigDataPart); - auto rawData = data->toBytes(); - - auto hdr = QSharedPointer::create(); - hdr->setPackagesCount(std::ceil(rawData.size() / static_cast(sizeLimit))); - hdr->setPackageId(rand()); - hdr->setCommand(data->cmd()); - - insertNewBigData(hdr); - - for (int i = 0; i < hdr->getPackagesCount(); ++i) { - auto part = QSharedPointer::create(); - part->setPackageId(hdr->packageId()); - part->setPakckageNumber(i); - part->setData(rawData.mid(i * sizeLimit, sizeLimit)); - - _pool[hdr->packageId()].chaindata[i] = part; - } - - - if (!node()->sendData(hdr.data(), sender, pkgHeader)) { - return false; - } - - return true; -} - -void BigDataParserOld::checkOutDatedPacakges(unsigned int currentProcessedId) { - int utx = time(0); - - if (_pool.contains(currentProcessedId)) { - _pool[currentProcessedId].lastUpdate = utx; - } - - for (auto key = _pool.keyBegin(); key != _pool.keyEnd(); ++key) { - if (utx - _pool[*key].lastUpdate > TIMEOUT_INTERVAL) { - _pool.remove(*key); - } - } -} - -} diff --git a/src/private/bigdataparser_old.h b/src/private/bigdataparser_old.h deleted file mode 100644 index 91f05147..00000000 --- a/src/private/bigdataparser_old.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2021-2022 QuasarApp. - * Distributed under the lgplv3 software license, see the accompanying - * Everyone is permitted to copy and distribute verbatim copies - * of this license document, but changing it is not allowed. -*/ - - -#ifndef BIGDATAPARSER_OLD_H -#define BIGDATAPARSER_OLD_H - -#include - - -namespace QH { - -namespace PKG { -class BigDataHeader; -class BigDataPart; -class BigDataRequest; -class BigDataWraper; -} - -class AbstractNode; -class AbstractNodeInfo; - -/** - * @brief The BigDataParserOld class is main manager for control big data packages. - * @note This is some a BigDataParser - */ -class BigDataParserOld final: public iParser -{ - struct PoolData { - QSharedPointer header; - QVector> chaindata; - int lastUpdate = time(0); - }; -public: - template - /** - * @brief registerPackageType This method register package type T. - * This is need to prepare pacakge for parsing in the parsePackage method. - */ - void registerPackageTypeOld() { - _registeredTypes[T::commandOld()] = [](){ - return new T(); - }; - }; - - template - - inline ParserResult commandHandlerOld(HandlerType handlerObject, HandlerMethod method, - const QSharedPointer &pkg, - QH::AbstractNodeInfo *sender, - const QH::Header &pkgHeader) { - - if (PackageClass::commandOld() == pkg->cmd()) { - auto data = pkg.staticCast(); - - if (!data->isValid()) { - return QH::ParserResult::Error; - } - - if(!(handlerObject->*method)(data, sender, pkgHeader)) { - return QH::ParserResult::Error; - } - - return QH::ParserResult::Processed; - } - - return QH::ParserResult::NotProcessed; - } - - BigDataParserOld(AbstractNode* parentNode); - - ParserResult parsePackage(const QSharedPointer &pkg, - const Header &pkgHeader, - AbstractNodeInfo *sender) override; - int version() const override; - QString parserId() const override; - -protected: - - /** - * @brief newPackage This method process first header packge of the big data. - * @param header This is header package. - * @param pkgHeader This is header of an incomming package. - * @param sender This is socket object of a sender that send this package. - * @return true if packge processed successful else false - */ - bool newPackage(const QSharedPointer &header, - AbstractNodeInfo * sender, - const Header & pkgHeader); - - /** - * @brief processPart This method process part of package - * @param part This is pacakge part. - * @param pkgHeader This is header of an incomming package. - * @param sender This is socket object of a sender that send this package. - * @return true if packge processed successful else false - */ - bool processPart(const QSharedPointer& part, - AbstractNodeInfo *sender, - const QH::Header &pkgHeader); - - /** - * @brief finishPart This metho process last package of big data transaction. - * @param request this is shared pointer to last part of big data transaction. - * @param pkgHeader This is header of an incomming package. - * @param sender This is socket object of a sender that send this package. - * @return true if pacakge parsed successful else false. - */ - bool processRequest(const QSharedPointer& request, - QH::AbstractNodeInfo *sender, - const QH::Header &pkgHeader); - - /** - * @brief sendBigDataPackage This method separate big pacakge and sent only heder ot serve. - * @param data This is package that will be sent to remote node. - * @param sender This is request object. - * @param pkgHeader requested header. - * @return true if package sent successful - */ - bool processBigDataWraper(const QSharedPointer &request, - AbstractNodeInfo *sender, - const Header &pkgHeader); - - -private: - - /** - * @brief sendBigDataPackage This method separate big pacakge and sent only heder ot serve. - * @param data This is package that will be sent to remote node. - * @param sender This is request object. - * @param pkgHeader requested header. - * @return true if package sent successful - */ - bool sendBigDataPackage(const PKG::AbstractData *data, - const QH::AbstractNodeInfo *sender, - const Header *pkgHeader); - - void insertNewBigData(const QSharedPointer &header); - void checkOutDatedPacakges(unsigned int currentProcessedId); - - QHash _pool; - -}; -} -#endif // BIGDATAPARSER_OLD_H diff --git a/src/private/datasender.cpp b/src/private/datasender.cpp index ba17e0d5..11571bd2 100644 --- a/src/private/datasender.cpp +++ b/src/private/datasender.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/datasender.h b/src/private/datasender.h index aabea0e8..44b011f2 100644 --- a/src/private/datasender.h +++ b/src/private/datasender.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/packadata.cpp b/src/private/packadata.cpp index f87b504f..22eef285 100644 --- a/src/private/packadata.cpp +++ b/src/private/packadata.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/packadata.h b/src/private/packadata.h index 27f97aa3..4f0fcda1 100644 --- a/src/private/packadata.h +++ b/src/private/packadata.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/receivedata.cpp b/src/private/receivedata.cpp index 0afda27f..8e05a01b 100644 --- a/src/private/receivedata.cpp +++ b/src/private/receivedata.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/receivedata.h b/src/private/receivedata.h index f66c9db0..0acbb27d 100644 --- a/src/private/receivedata.h +++ b/src/private/receivedata.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/sslsocket.cpp b/src/private/sslsocket.cpp index da8fefec..6a03941f 100644 --- a/src/private/sslsocket.cpp +++ b/src/private/sslsocket.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/sslsocket.h b/src/private/sslsocket.h index b69c263e..3fca5567 100644 --- a/src/private/sslsocket.h +++ b/src/private/sslsocket.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/taskscheduler.cpp b/src/private/taskscheduler.cpp index b0eee500..fb31898f 100644 --- a/src/private/taskscheduler.cpp +++ b/src/private/taskscheduler.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/private/taskscheduler.h b/src/private/taskscheduler.h index 87e9510c..718c56d4 100644 --- a/src/private/taskscheduler.h +++ b/src/private/taskscheduler.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/private/tcpsocket.cpp b/src/private/tcpsocket.cpp index 760d56bd..dafef888 100644 --- a/src/private/tcpsocket.cpp +++ b/src/private/tcpsocket.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/tcpsocket.h b/src/private/tcpsocket.h index 789e421b..f893599a 100644 --- a/src/private/tcpsocket.h +++ b/src/private/tcpsocket.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/private/versionisreceived.cpp b/src/private/versionisreceived.cpp index a298cf72..fd5c05c6 100644 --- a/src/private/versionisreceived.cpp +++ b/src/private/versionisreceived.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2020-2022 QuasarApp. +//# Copyright (C) 2020-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/private/versionisreceived.h b/src/private/versionisreceived.h index b0c18e86..a2f58c8e 100644 --- a/src/private/versionisreceived.h +++ b/src/private/versionisreceived.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2020-2022 QuasarApp. +//# Copyright (C) 2020-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. @@ -26,7 +26,6 @@ class VersionIsReceived: public QH::PKG::AbstractData static unsigned short command(){return PROTOCKOL_VERSION_RECEIVED_COMMAND;} static QString commandText(){return "PROTOCKOL_VERSION_RECEIVED_COMMAND";} unsigned short cmd() const override {return VersionIsReceived::command();} - unsigned short cmdOld() const override {return VersionIsReceived::command();} QString cmdString() const override {return VersionIsReceived::commandText();} diff --git a/src/public/abstracterrorcodes.cpp b/src/public/abstracterrorcodes.cpp index fbc8293f..4d2e0e3e 100644 --- a/src/public/abstracterrorcodes.cpp +++ b/src/public/abstracterrorcodes.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 QuasarApp. + * Copyright (C) 2020-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/abstracterrorcodes.h b/src/public/abstracterrorcodes.h index bb5e79b3..4bcfb99c 100644 --- a/src/public/abstracterrorcodes.h +++ b/src/public/abstracterrorcodes.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 QuasarApp. + * Copyright (C) 2020-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/abstractkey.cpp b/src/public/abstractkey.cpp index 9970092b..cb69ec97 100644 --- a/src/public/abstractkey.cpp +++ b/src/public/abstractkey.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/abstractkey.h b/src/public/abstractkey.h index 4fa793d5..239f8e0c 100644 --- a/src/public/abstractkey.h +++ b/src/public/abstractkey.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/abstractnode.cpp b/src/public/abstractnode.cpp index 98bcf56a..b0e05905 100644 --- a/src/public/abstractnode.cpp +++ b/src/public/abstractnode.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -48,11 +48,6 @@ #include #include -#ifdef HEART_DEPRECATED_API -#include "bigdataparser_old.h" -#include "abstractnodeparser_old.h" -#endif - namespace QH { using namespace PKG; @@ -73,18 +68,15 @@ AbstractNode::AbstractNode( QObject *ptr): _tasksheduller = new TaskScheduler(); _apiVersionParser = new APIVersionParser(this); -#ifdef HEART_DEPRECATED_API - addApiParser(); - auto abstractNodeParserOld = addApiParserNative(); - connect(abstractNodeParserOld.data(), &AbstractNodeParserOld::sigPingReceived, - this, &AbstractNode::receivePing, Qt::DirectConnection); -#endif addApiParser(); auto abstractNodeParser = addApiParserNative(); connect(abstractNodeParser.data(), &AbstractNodeParser::sigPingReceived, this, &AbstractNode::receivePing, Qt::DirectConnection); + connect(_apiVersionParser, &APIVersionParser::sigNoLongerSupport, + this, &AbstractNode::sigNoLongerSupport, Qt::DirectConnection); + qRegisterMetaType>(); #ifdef USE_HEART_SSL qRegisterMetaType>(); @@ -140,6 +132,20 @@ bool AbstractNode::run(const QString &addres, unsigned short port) { return true; } +QSharedPointer AbstractNode::selectParser(unsigned short cmd, + AbstractNodeInfo *sender) const { + return _apiVersionParser->selectParser(cmd, sender); +} + +QSharedPointer AbstractNode::selectParser(const QString &type, + AbstractNodeInfo *sender) const { + return _apiVersionParser->selectParser(type, sender); +} + +QSharedPointer AbstractNode::selectParser(const QString &type, int version) const { + return _apiVersionParser->selectParser(type, version); +} + void AbstractNode::stop() { close(); @@ -606,8 +612,24 @@ void AbstractNode::handleSslErrorOcurred(SslSocket *scket, #endif +bool AbstractNode::fCloseConnectionAfterBadRequest() const { + return _closeConnectionAfterBadRequest; +} + +void AbstractNode::setCloseConnectionAfterBadRequest(bool newCloseConnectionAfterBadRequest) { + _closeConnectionAfterBadRequest = newCloseConnectionAfterBadRequest; +} + +bool AbstractNode::fSendBadRequestErrors() const { + return _sendBadRequestErrors; +} + +void AbstractNode::setSendBadRequestErrors(bool val) { + _sendBadRequestErrors = val; +} + const QSharedPointer & -AbstractNode::addApiParser(const QSharedPointer &parserObject) { +AbstractNode::addApiParserImpl(const QSharedPointer &parserObject) { return _apiVersionParser->addApiParser(parserObject); } @@ -733,26 +755,6 @@ unsigned int AbstractNode::sendData(const PKG::AbstractData *resp, return 0; } -#ifdef HEART_DEPRECATED_API - bool fOld = node->version().value("HeartLibAbstractAPI").max() <= 0 && - resp->cmd() != PROTOCKOL_VERSION_RECEIVED_COMMAND && - resp->cmd() != PROTOCKOL_VERSION_COMMAND; - Package pkg; - bool convert = false; - if (req && req->isValid()) { - if (fOld) { - convert = resp->toPackageOld(pkg, req->hash); - } else { - convert = resp->toPackage(pkg, req->hash); - } - } else { - if (fOld) { - convert = resp->toPackageOld(pkg); - } else { - convert = resp->toPackage(pkg); - } - } -#else Package pkg; bool convert = false; if (req && req->isValid()) { @@ -760,7 +762,6 @@ unsigned int AbstractNode::sendData(const PKG::AbstractData *resp, } else { convert = resp->toPackage(pkg); } -#endif if (!convert) { @@ -808,14 +809,21 @@ void AbstractNode::badRequest(const HostAddress &address, const Header &req, return; } - auto bad = BadRequest(err); - if (!sendData(&bad, address, &req)) { - return; - } + auto node = getInfoPtr(address); + if (!isBanned(node)) { + auto bad = BadRequest(err); + if (!sendData(&bad, address, &req)) { + return; + } - QuasarAppUtils::Params::log("Bad request sendet to adderess: " + - address.toString(), - QuasarAppUtils::Info); + QuasarAppUtils::Params::log("Bad request sendet to adderess: " + + address.toString(), + QuasarAppUtils::Info); + + if (fCloseConnectionAfterBadRequest()) { + removeNode(node); + } + } } WorkState AbstractNode::getWorkState() const { @@ -1045,9 +1053,7 @@ void AbstractNode::avelableBytes(AbstractNodeInfo *sender) { newWork(pkg, sender, id); pkg.reset(); hdrArray.clear(); - } - - if (static_cast(pkg.data.size()) > pkg.hdr.size) { + } else if (static_cast(pkg.data.size()) >= pkg.hdr.size) { QuasarAppUtils::Params::log("Invalid Package received." + pkg.toString(), QuasarAppUtils::Warning); pkg.reset(); @@ -1172,6 +1178,14 @@ void AbstractNode::nodeAddedSucessful(AbstractNodeInfo *) { } +bool AbstractNode::addApiParser(const QSharedPointer &parser) { + + if (!parser || parser->node() != this) + return false; + + return !addApiParserImpl(parser).isNull(); +} + void AbstractNode::receivePing(const QSharedPointer &) {}; bool AbstractNode::sheduleTask(const QSharedPointer &task) { @@ -1200,21 +1214,39 @@ void AbstractNode::newWork(const Package &pkg, AbstractNodeInfo *sender, ParserResult parseResult = parsePackage(data, pkg.hdr, sender); +#ifdef HEART_PRINT_PACKAGES + QuasarAppUtils::Params::log(QString("Package received! %0").arg(data->toString()), QuasarAppUtils::Info); +#endif + if (parseResult != ParserResult::Processed) { auto message = QString("Package not parsed! %0 \nresult: %1. \n%2"). arg(pkg.toString(), iParser::pareseResultToString(parseResult), data->toString()); - QuasarAppUtils::Params::log(message, QuasarAppUtils::Warning); + QuasarAppUtils::Params::log(message, QuasarAppUtils::Info); if (parseResult == ParserResult::Error) { - changeTrust(id, REQUEST_ERROR); + + if (fSendBadRequestErrors()) { + badRequest(id, pkg.hdr, ErrorData{1, "Your send the invalid request."}, REQUEST_ERROR); + } else { + changeTrust(id, REQUEST_ERROR); + } } if (parseResult == ParserResult::NotProcessed) { - changeTrust(id, NOTSUPPORT_ERROR); + if (fSendBadRequestErrors()) { + badRequest(id, pkg.hdr, ErrorData{2, "Your send the not supported command."}, NOTSUPPORT_ERROR); + } else { + changeTrust(id, NOTSUPPORT_ERROR); + } + } +#ifdef QT_DEBUG + QuasarAppUtils::Params::log(_apiVersionParser->toString(), QuasarAppUtils::Info); +#endif + return false; } @@ -1370,4 +1402,4 @@ QThread *AbstractNode::mainThreadID() { return thread; } -} +} // namespace QH diff --git a/src/public/abstractnode.h b/src/public/abstractnode.h index 71757e94..8e8f7081 100644 --- a/src/public/abstractnode.h +++ b/src/public/abstractnode.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -153,6 +153,32 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete */ virtual bool run(const QString& addres, unsigned short port); + /** + * @brief selectParser This method select parser by command and sender. + * @param cmd this is command that need to parse. + * @param sender this is node that sent this command. + * @return parser for the @a cmd command + */ + QSharedPointer selectParser(unsigned short cmd, + AbstractNodeInfo *sender) const; + + /** + * @brief selectParser This method select parser by command and sender. + * @param type this is parser type. + * @param version this is node that sent this command. + * @return parser for the @a cmd command + */ + QSharedPointer selectParser(const QString& type, + int version) const; + + /** + * @brief selectParser This method select parser by command and sender. + * @param type this is parser type. + * @param sender this is node that sent this command. + * @return parser for the @a cmd command + */ + QSharedPointer selectParser(const QString& type, + AbstractNodeInfo *sender) const; /** * @brief stop - Stopped this node and close all network connections. @@ -350,6 +376,17 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete */ int sheduledTaskCount() const; + /** + * @brief badRequest This method is send data about error of request. + * @param address This is addrees of receiver. + * @param req This is header of incomming request. + * @param err This is message and code of error. For more information see the ErrorData struct. + * @param diff This is difference of current trust (currenTrus += diff). + * By default diff equals REQUEST_ERROR + */ + virtual void badRequest(const HostAddress &address, const Header &req, + const PKG::ErrorData& err, qint8 diff = REQUEST_ERROR); + #ifdef USE_HEART_SSL /** @@ -360,6 +397,37 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete const QList &ignoreSslErrors() const; #endif + /** + * @brief addApiParser This method add new Api parser for this node. + * @param parserObject This is bew api parser. + * @return added parser. + */ + bool addApiParser(const QSharedPointer& parser); + + /** + * @brief fSendBadRequestErrors This property enable or disable sending feedback to connected node when them sent wrong packages. + * @return true if the sending feedbacks is enabled and false if disabled. + */ + bool fSendBadRequestErrors() const; + + /** + * @brief setSendBadRequestErrors This method enable or disable the fSendBadRequestErrors property. + * @param value This is new value of the sendBadRequestErrors property. + */ + void setSendBadRequestErrors(bool value); + + /** + * @brief fCloseConnectionAfterBadRequest This propery enable or disable droping connection after badRequests. By default it is true. + * @return true if the clossing connection is enabled and false if disabled. + */ + bool fCloseConnectionAfterBadRequest() const; + + /** + * @brief setSendBadRequestErrors This method enable or disable the fcloseConnectionAfterBadRequest property. + * @param value This is new value of the fcloseConnectionAfterBadRequest property. + */ + void setCloseConnectionAfterBadRequest(bool value); + signals: /** * @brief requestError This signal emited when client or node received from remoute server or node the BadRequest package. @@ -368,6 +436,14 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete */ void requestError(unsigned char code, QString msg); + /** + * @brief sigNoLongerSupport is some as a APIVersionParser::sigNoLongerSupport. This signal just retronslate this signal as direct connection. + * @param ApiKey see the APIVersionParser::sigNoLongerSupport description + * @param version see the APIVersionParser::sigNoLongerSupport description + * @see APIVersionParser::sigNoLongerSupport + */ + void sigNoLongerSupport(const QString& ApiKey, unsigned short version); + protected: @@ -434,17 +510,6 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete */ virtual bool sendPackage(const Package &pkg, QAbstractSocket *target) const; - /** - * @brief badRequest This method is send data about error of request. - * @param address This is addrees of receiver. - * @param req This is header of incomming request. - * @param err This is message and code of error. For more information see the ErrorData struct. - * @param diff This is difference of current trust (currenTrus += diff). - * By default diff equals REQUEST_ERROR - */ - virtual void badRequest(const HostAddress &address, const Header &req, - const PKG::ErrorData& err, qint8 diff = REQUEST_ERROR); - /** * @brief getWorkStateString This method generate string about work state of server. * @return string of work state. @@ -595,7 +660,7 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete */ template QSharedPointer addApiParserNative(Args&&... arg) { - return addApiParser(QSharedPointer::create(this, std::forward(arg)...)).template staticCast(); + return addApiParserImpl(QSharedPointer::create(this, std::forward(arg)...)).template staticCast(); } /** @@ -606,7 +671,7 @@ class HEARTSHARED_EXPORT AbstractNode : public QTcpServer, public SoftDelete */ template const QSharedPointer & addApiParser(Args&&... arg) { - return addApiParser(QSharedPointer::create(this, std::forward(arg)...)); + return addApiParserImpl(QSharedPointer::create(this, std::forward(arg)...)); } protected slots: @@ -686,11 +751,11 @@ private slots: private: /** - * @brief addApiParser This method add new Api parser for this node. + * @brief addApiParserImpl This method add new Api parser for this node. * @param parserObject This is bew api parser. * @return added parser. */ - const QSharedPointer & addApiParser(const QSharedPointer& parserObject); + const QSharedPointer & addApiParserImpl(const QSharedPointer& parserObject); // iParser interface ParserResult parsePackage(const QSharedPointer &pkg, @@ -750,9 +815,13 @@ private slots: TaskScheduler *_tasksheduller = nullptr; APIVersionParser *_apiVersionParser = nullptr; - QHash>> _connectActions; + QHash>> _connectActions; QSet*> _workers; + bool _sendBadRequestErrors = true; + bool _closeConnectionAfterBadRequest = false; mutable QMutex _connectionsMutex; mutable QMutex _confirmNodeMutex; diff --git a/src/public/abstractnodeinfo.cpp b/src/public/abstractnodeinfo.cpp index 37a7880d..fda1eb38 100644 --- a/src/public/abstractnodeinfo.cpp +++ b/src/public/abstractnodeinfo.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -199,7 +199,7 @@ int AbstractNodeInfo::trust() const { void AbstractNodeInfo::setTrust(int trust) { _trust = trust; - if (isBanned()) { + if (isBanned() && _sct) { QuasarAppUtils::Params::log(QString("The node %0 is banned!"). arg(_sct->peerAddress().toString())); @@ -221,17 +221,42 @@ void AbstractNodeInfo::reset() { setTrust(static_cast(TrustNode::Default)); setStatus(NodeCoonectionStatus::NotConnected); setIsLocal(false); + + QMutexLocker lock(&_parsersListMutex); _parsersMap.clear(); } QSharedPointer AbstractNodeInfo::getParser(unsigned short cmd) { + QMutexLocker lock(&_parsersListMutex); + return _parsersMap.value(cmd, nullptr); } +QSharedPointer AbstractNodeInfo::getParser(const QString &type) { + QMutexLocker lock(&_parsersListMutex); + + return _parsersKeysMap.value(type, nullptr); +} + void QH::AbstractNodeInfo::addParser(unsigned short cmd, QSharedPointer parser) { + if (!parser) + return; + + QMutexLocker lock(&_parsersListMutex); + _parsersMap[cmd] = parser; + _parsersKeysMap[parser->parserId()] = parser; +} + +void AbstractNodeInfo::addParser(QSharedPointer parser) { + if (!parser) + return; + + QMutexLocker lock(&_parsersListMutex); + + _parsersKeysMap[parser->parserId()] = parser; } uint qHash(NodeCoonectionStatus status) { diff --git a/src/public/abstractnodeinfo.h b/src/public/abstractnodeinfo.h index e0503284..015be850 100644 --- a/src/public/abstractnodeinfo.h +++ b/src/public/abstractnodeinfo.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -12,6 +12,7 @@ #include "distversion.h" #include "heart_global.h" #include "iparser.h" +#include #include @@ -236,14 +237,27 @@ class HEARTSHARED_EXPORT AbstractNodeInfo: public QObject */ QSharedPointer getParser(unsigned short cmd); + /** + * @brief getParser This method return parser of choosed command. + * @param type This is type of api parser. + * @return parser by type. + */ + QSharedPointer getParser(const QString& type); + /** * @brief addParser This method add to cache new parser for command . - * @param cmd - * @param parser + * @param cmd This is any command that support this parser. + * @param parser This is added parser. * @note All parsers will be removed after reconnect of this node. */ void addParser(unsigned short cmd, QSharedPointer parser); + /** + * @brief addParser This method add to cache new parser for command . + * @param parser This is added parser. + * @note All parsers will be removed after reconnect of this node. + */ + void addParser(QSharedPointer parser); public slots: /** @@ -318,6 +332,8 @@ public slots: bool _isLocal = false; QHash> _parsersMap; + QHash> _parsersKeysMap; + QMutex _parsersListMutex; VersionData _version; bool _fVersionReceived = false; diff --git a/src/public/abstracttask.cpp b/src/public/abstracttask.cpp index 6d59b092..6e4ea1ae 100644 --- a/src/public/abstracttask.cpp +++ b/src/public/abstracttask.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/abstracttask.h b/src/public/abstracttask.h index e33c2aa1..c29c30f6 100644 --- a/src/public/abstracttask.h +++ b/src/public/abstracttask.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/accesstoken.cpp b/src/public/accesstoken.cpp index ae867124..768a7c96 100644 --- a/src/public/accesstoken.cpp +++ b/src/public/accesstoken.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/accesstoken.h b/src/public/accesstoken.h index 33548360..761b6f9d 100644 --- a/src/public/accesstoken.h +++ b/src/public/accesstoken.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/apiversionparser.cpp b/src/public/apiversionparser.cpp index 802150d6..53200f7f 100644 --- a/src/public/apiversionparser.cpp +++ b/src/public/apiversionparser.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2022-2022 QuasarApp. +//# Copyright (C) 2022-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. @@ -67,6 +67,19 @@ QString APIVersionParser::parserId() const { return "APIVersionParser"; } +QString APIVersionParser::toString() const { + QString message = " supports parsers:\n"; + message += iParser::toString(); + + for (const auto& parser: _apiParsers) { + for (auto it = parser.begin(); it != parser.end(); ++it) { + message += "ver: " + QString::number(it.key()) + " " + it.value()->toString(); + } + } + + return message; +} + QSharedPointer APIVersionParser::searchPackage(unsigned short cmd, AbstractNodeInfo *sender) const { @@ -106,9 +119,57 @@ APIVersionParser::getSelectedApiParser(const QString &apiKey, return selectParser(node->version()).value(apiKey, nullptr); } +bool QH::APIVersionParser::commandsValidation(const QSharedPointer &parserObject) { +#ifdef QT_DEBUG + auto types = QSet{parserObject->registeredTypes().keyBegin(), + parserObject->registeredTypes().keyEnd()}; + int typesSize = types.size(); + + for (const auto &parsersMap : qAsConst(_apiParsers)) { + for (const auto &parser: parsersMap) { + if (parser->parserId() == parserObject->parserId()) { + continue; + } + + auto localTypes = QSet{parser->registeredTypes().keyBegin(), + parser->registeredTypes().keyEnd()}; + types -= localTypes; + + if (types.size() != typesSize) { + auto err = QString("Parser object can't be added. " + "Because commands of the %0_v%1 parser " + "already registered in the %2_v%3 parser. " + "Use the iParser::initSupportedCommands method to " + "fix this issue. " + "See our documentation for get more inforamtion. " + "https://quasarapp.ddns.net:3031/docs/QuasarApp/Heart/latest/classQH_1_1iParser.html"). + arg(parserObject->parserId()). + arg(parserObject->version()). + arg(parser->parserId()). + arg(parser->version()); + + QuasarAppUtils::Params::log(err, QuasarAppUtils::Error); + + return false; + } + } + } +#endif + + return true; +} + const QSharedPointer& APIVersionParser::addApiParser(const QSharedPointer &parserObject) { debug_assert(parserObject, "Parser object should not be nullptr"); + parserObject->initSupportedCommands(); + + if (!commandsValidation(parserObject)) { + debug_assert(false, "Parser object can't be added. " + "Because its commands already registered " + "in the another parsers."); + } + _apiParsers[parserObject->parserId()][parserObject->version()] = parserObject; return _apiParsers[parserObject->parserId()][parserObject->version()]; } @@ -135,7 +196,7 @@ unsigned int APIVersionParser::parsersTypedCount() const { } QSharedPointer APIVersionParser::selectParser(unsigned short cmd, - AbstractNodeInfo *sender) { + AbstractNodeInfo *sender) const{ auto parser = sender->getParser(cmd); if (!parser) { parser = selectParserImpl(cmd, sender); @@ -144,8 +205,34 @@ QSharedPointer APIVersionParser::selectParser(unsigned short cmd, return parser; } +QSharedPointer APIVersionParser::selectParser(const QString &parserKey, + AbstractNodeInfo *sender) const { + if (!sender) + return nullptr; + + auto parser = sender->getParser(parserKey); + if (!parser) { + parser = selectParserImpl(parserKey, sender); + } + + return parser; +} + +QSharedPointer APIVersionParser::selectParser(const QString &parserKey, + unsigned short version) const { + + DistVersion versionData; + versionData.setMin(version); + versionData.setMax(version); + + VersionData versions; + versions.insert(parserKey, versionData); + + return selectParser(versions).value(parserKey); +} + QSharedPointer APIVersionParser::selectParserImpl(unsigned short cmd, - AbstractNodeInfo *sender) { + AbstractNodeInfo *sender) const{ auto version = sender->version(); const auto availableParser = selectParser(version); for (const auto& parser: availableParser) { @@ -158,6 +245,14 @@ QSharedPointer APIVersionParser::selectParserImpl(unsigned short cmd, return nullptr; } +QSharedPointer APIVersionParser::selectParserImpl(const QString &key, + AbstractNodeInfo *sender) const{ + auto version = sender->version(); + auto parser = selectParser(version).value(key); + sender->addParser(parser); + + return parser; +} unsigned short APIVersionParser::maximumApiVersion(const QString &apiKey) const { diff --git a/src/public/apiversionparser.h b/src/public/apiversionparser.h index 9cc860b6..3ff2012e 100644 --- a/src/public/apiversionparser.h +++ b/src/public/apiversionparser.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2022-2022 QuasarApp. +//# Copyright (C) 2022-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. @@ -34,6 +34,12 @@ class APIVersionParser: public iParser int version() const override; QString parserId() const override; + /** + * @brief toString This method show all supported commands and them names. + * @return list of the supported commands as a message + */ + QString toString() const override; + /** * @brief searchPackage This method search package recursive in all registered pararsers. Searching will be in compatibility versions. * Before search methd choose compatibly verson. @@ -82,7 +88,25 @@ class APIVersionParser: public iParser * @return parser for the @a cmd command */ QSharedPointer selectParser(unsigned short cmd, - AbstractNodeInfo *sender); + AbstractNodeInfo *sender) const; + + /** + * @brief selectParser This method select parser by command and sender. + * @param parserKey this is key of the parser type.. + * @param version this is needed version. + * @return parser for the @a cmd command + */ + QSharedPointer selectParser(const QString& parserKey, + unsigned short version) const; + + /** + * @brief selectParser This method select parser by command and sender. + * @param parserKey this is key of the parser type.. + * @param sender this is node that sent this command. + * @return parser for the @a cmd command + */ + QSharedPointer selectParser(const QString& parserKey, + AbstractNodeInfo *sender) const; /** * @brief maximumApiVersion This method return maximum supported api version of this node. @@ -117,7 +141,10 @@ class APIVersionParser: public iParser private: QSharedPointer - selectParserImpl(unsigned short cmd, AbstractNodeInfo *sender); + selectParserImpl(unsigned short cmd, AbstractNodeInfo *sender) const; + + QSharedPointer + selectParserImpl(const QString& key, AbstractNodeInfo *sender) const; bool processAppVersion(const QSharedPointer &message, AbstractNodeInfo *sender, @@ -128,6 +155,9 @@ class APIVersionParser: public iParser QHash>> _apiParsers; + // This is internal check of registered commands. + // works only in debug. + bool commandsValidation(const QSharedPointer &parserObject); }; } #endif // APIVERSIONPARSER_H diff --git a/src/public/async.cpp b/src/public/async.cpp index 1befbdd8..923176e5 100644 --- a/src/public/async.cpp +++ b/src/public/async.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/async.h b/src/public/async.h index 3f94bc22..e0c0f124 100644 --- a/src/public/async.h +++ b/src/public/async.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/asynclauncher.cpp b/src/public/asynclauncher.cpp index a5a5ec98..70cb76f0 100644 --- a/src/public/asynclauncher.cpp +++ b/src/public/asynclauncher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/asynclauncher.h b/src/public/asynclauncher.h index 5ad204b2..728868f1 100644 --- a/src/public/asynclauncher.h +++ b/src/public/asynclauncher.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/asyncsqldbwriter.cpp b/src/public/asyncsqldbwriter.cpp index 2fb8d33c..7246c088 100644 --- a/src/public/asyncsqldbwriter.cpp +++ b/src/public/asyncsqldbwriter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/asyncsqldbwriter.h b/src/public/asyncsqldbwriter.h index 496fc508..2602c39a 100644 --- a/src/public/asyncsqldbwriter.h +++ b/src/public/asyncsqldbwriter.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/atomicmetatypes.h b/src/public/atomicmetatypes.h index bf7453d6..403b9eca 100644 --- a/src/public/atomicmetatypes.h +++ b/src/public/atomicmetatypes.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/config.h b/src/public/config.h index bd60a9cc..ab6b4a23 100644 --- a/src/public/config.h +++ b/src/public/config.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/database.cpp b/src/public/database.cpp index 7432d6fe..d220d3bf 100644 --- a/src/public/database.cpp +++ b/src/public/database.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -192,7 +192,7 @@ bool DataBase::upgradeDataBase() { int currentVersion = 0; - bool fsupportUpgrade = db()->doQuery("SELECT COUNT(*) FROM DataBaseAttributes", true); + bool fsupportUpgrade = db()->doQuery("SELECT COUNT(*) FROM DataBaseAttributes", {}, true); if (!fsupportUpgrade) { @@ -244,7 +244,7 @@ bool DataBase::upgradeDataBase() { DbAddress{"DataBaseAttributes", "version"}, "value", currentVersion, "name"); - if (!_db->insertIfExistsUpdateObject(updateVersionRequest, true)) { + if (!_db->replaceObject(updateVersionRequest, true)) { QuasarAppUtils::Params::log("Failed to update version attribute", QuasarAppUtils::Error); return false; diff --git a/src/public/database.h b/src/public/database.h index 1f883f3c..900bc622 100644 --- a/src/public/database.h +++ b/src/public/database.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -18,7 +18,6 @@ namespace QH { namespace PKG { class WebSocket; class ISubscribableData; - } class ISqlDB; @@ -32,6 +31,9 @@ class AbstractNodeInfo; * @brief The DataBase class is DataBase base implementation. * This implementation contains methods for work with database. * DataBaseNode is thread save class. + * + * @note before use the database you need to run it. Use The DataBase::run method for this. If you need to stop database, run the DataBase::stop method. + * @see DBObject * @see DataBaseNode */ diff --git a/src/public/dbaddress.cpp b/src/public/dbaddress.cpp index b870d022..35133ed8 100644 --- a/src/public/dbaddress.cpp +++ b/src/public/dbaddress.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/dbaddress.h b/src/public/dbaddress.h index e6ecb80c..f1db7a88 100644 --- a/src/public/dbaddress.h +++ b/src/public/dbaddress.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/dbaddresskey.cpp b/src/public/dbaddresskey.cpp index 8f67f4fd..1d02f150 100644 --- a/src/public/dbaddresskey.cpp +++ b/src/public/dbaddresskey.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/dbaddresskey.h b/src/public/dbaddresskey.h index 29b582a7..09075146 100644 --- a/src/public/dbaddresskey.h +++ b/src/public/dbaddresskey.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/dbpatch.cpp b/src/public/dbpatch.cpp index 655c4745..602b8a89 100644 --- a/src/public/dbpatch.cpp +++ b/src/public/dbpatch.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/dbpatch.h b/src/public/dbpatch.h index ba516a0e..828f9728 100644 --- a/src/public/dbpatch.h +++ b/src/public/dbpatch.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -32,7 +32,7 @@ struct DBPatch { unsigned short versionTo = 0; /// This is lymbda function with action that will upgrade data base to new versio. - std::function action; + std::function action; /** * @brief isValid This method check this oject to valid. diff --git a/src/public/distversion.cpp b/src/public/distversion.cpp index bd72d9da..005d6f50 100644 --- a/src/public/distversion.cpp +++ b/src/public/distversion.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/distversion.h b/src/public/distversion.h index 6c8d3f95..1942933b 100644 --- a/src/public/distversion.h +++ b/src/public/distversion.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcrypto/ecdsassl11.cpp b/src/public/hcrypto/ecdsassl11.cpp index 1813c6d9..5400a428 100644 --- a/src/public/hcrypto/ecdsassl11.cpp +++ b/src/public/hcrypto/ecdsassl11.cpp @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the GPLv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/hcrypto/ecdsassl11.h b/src/public/hcrypto/ecdsassl11.h index 0c099526..8c06cb8a 100644 --- a/src/public/hcrypto/ecdsassl11.h +++ b/src/public/hcrypto/ecdsassl11.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the GPLv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/hcrypto/icrypto.cpp b/src/public/hcrypto/icrypto.cpp index 81c01ced..4a2003ce 100644 --- a/src/public/hcrypto/icrypto.cpp +++ b/src/public/hcrypto/icrypto.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcrypto/icrypto.h b/src/public/hcrypto/icrypto.h index 686a30b5..36f656dd 100644 --- a/src/public/hcrypto/icrypto.h +++ b/src/public/hcrypto/icrypto.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcryptoFeatures/asynckeysauth.h b/src/public/hcryptoFeatures/asynckeysauth.h index 05627edb..9e4e59cd 100644 --- a/src/public/hcryptoFeatures/asynckeysauth.h +++ b/src/public/hcryptoFeatures/asynckeysauth.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcryptoFeatures/authecdsa.h b/src/public/hcryptoFeatures/authecdsa.h index 73252d1a..2f909cf8 100644 --- a/src/public/hcryptoFeatures/authecdsa.h +++ b/src/public/hcryptoFeatures/authecdsa.h @@ -1,5 +1,5 @@ //# -//# Copyright (C) 2021-2022 QuasarApp. +//# Copyright (C) 2021-2023 QuasarApp. //# Distributed under the GPLv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/hcryptoFeatures/ecdsasigner.h b/src/public/hcryptoFeatures/ecdsasigner.h index c2a328a8..d4cab7ac 100644 --- a/src/public/hcryptoFeatures/ecdsasigner.h +++ b/src/public/hcryptoFeatures/ecdsasigner.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcryptoFeatures/isignerdelegate.cpp b/src/public/hcryptoFeatures/isignerdelegate.cpp index f6a08446..78100031 100644 --- a/src/public/hcryptoFeatures/isignerdelegate.cpp +++ b/src/public/hcryptoFeatures/isignerdelegate.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcryptoFeatures/isignerdelegate.h b/src/public/hcryptoFeatures/isignerdelegate.h index 83c1054f..13e28821 100644 --- a/src/public/hcryptoFeatures/isignerdelegate.h +++ b/src/public/hcryptoFeatures/isignerdelegate.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hcryptoFeatures/signer.h b/src/public/hcryptoFeatures/signer.h index 82e5164f..8cd92bd1 100644 --- a/src/public/hcryptoFeatures/signer.h +++ b/src/public/hcryptoFeatures/signer.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/header.cpp b/src/public/header.cpp index 2ac42f35..7140c82b 100644 --- a/src/public/header.cpp +++ b/src/public/header.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/header.h b/src/public/header.h index 3ded12ef..49427377 100644 --- a/src/public/header.h +++ b/src/public/header.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hostaddress.cpp b/src/public/hostaddress.cpp index 280f4ab9..14e6ffc6 100644 --- a/src/public/hostaddress.cpp +++ b/src/public/hostaddress.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/hostaddress.h b/src/public/hostaddress.h index 25ce1bfa..03e84a31 100644 --- a/src/public/hostaddress.h +++ b/src/public/hostaddress.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/iobjectprovider.cpp b/src/public/iobjectprovider.cpp index ff44e2cb..b7ca7ede 100644 --- a/src/public/iobjectprovider.cpp +++ b/src/public/iobjectprovider.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -27,33 +27,12 @@ QSharedPointer iObjectProvider::getObjectRaw( if (list.size() > 1) { QuasarAppUtils::Params::log("getObject method returned more than one object," - " the first object was selected as the result, all the rest were lost.", + " the first object was selected as the result," + " all the rest were lost.", QuasarAppUtils::Warning); } return list.first(); } -bool QH::iObjectProvider::insertIfExistsUpdateObject(const QSharedPointer &saveObject, bool wait) { - - bool restore = saveObject->printError(); - - if (restore) { - saveObject->setPrintError(false); - } - if(!insertObject(saveObject, wait)) { - - if (restore) { - saveObject->setPrintError(true); - } - return updateObject(saveObject, wait); - } - - if (restore) { - saveObject->setPrintError(true); - } - return true; - -} - } diff --git a/src/public/iobjectprovider.h b/src/public/iobjectprovider.h index 46bd60a0..4416eed9 100644 --- a/src/public/iobjectprovider.h +++ b/src/public/iobjectprovider.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -75,8 +75,17 @@ class HEARTSHARED_EXPORT iObjectProvider QList> &result) = 0; /** - * @brief updateObject This method execute a update method of the saveObject and save all changes into database. + * @brief replaceObject This method execute a replace method of the saveObject and insert or save if not exists, all changes into database. * @note This method update object in the database only. If you try update not exists object then this method return false. + * @param saveObject This is object for updating. + * @param wait This arguments force current thread wait for the function finishing. + * @return true if objects is updated successful else false. + */ + virtual bool replaceObject(const QSharedPointer& saveObject, bool wait) = 0; + + /** + * @brief updateObject This method execute a update method of the saveObject and save all changes into database. + * @note This method update object in the database only. * @warning This method do not guarantee that return false if The updated object is not exist. * @param saveObject This is object for updating. * @param wait This arguments force current thread wait for the function finishing. @@ -109,25 +118,16 @@ class HEARTSHARED_EXPORT iObjectProvider */ virtual void setSQLSources(const QStringList& list) = 0; - /** - * @brief insertIfExistsUpdateObject This method try to insert object to database If object alredy exists in database then object will be updated. - * @param saveObject This is saved object pointer. - * @param wait This arguments force current thread wait for the function finishing. - * @return true if object is updated or inserted successful else false. - * @see iObjectProvider::insertObject - * @see iObjectProvider::updateObject - */ - bool insertIfExistsUpdateObject(const QSharedPointer& saveObject, - bool wait = true); - /** * @brief doQuery This method execute a @a query in this database. * @param query This is query that will be executed. + * @param bindValues This is values that need to bind before excute query. * @param result This is query result value. * @warning The result works onlt on await mode. Set the @a wait param to true. * @return true if the query finished successful */ - virtual bool doQuery(const QString& query, bool wait = false, QSqlQuery* result = nullptr) const = 0; + virtual bool doQuery(const QString& query, const QVariantMap& bindValues = {}, + bool wait = false, QSqlQuery* result = nullptr) const = 0; /** * @brief doSql This method execute a @a query in this database. diff --git a/src/public/iparser.cpp b/src/public/iparser.cpp index 9c6db1eb..b9412c96 100644 --- a/src/public/iparser.cpp +++ b/src/public/iparser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 QuasarApp. + * Copyright (C) 2022-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -61,4 +61,16 @@ unsigned int iParser::sendData(const PKG::AbstractData *resp, } +void iParser::initSupportedCommands() {} + +QString iParser::toString() const { + QString message = parserId() + " supports next commands:\n"; + + for (auto it = _registeredTypes.keyBegin(); it != _registeredTypes.keyEnd(); ++it) { + message += genPackage(*it)->cmdString() + " - " + QString::number(*it) + "\n"; + } + + return message; +} + } diff --git a/src/public/iparser.h b/src/public/iparser.h index 730584a4..b6ee64bf 100644 --- a/src/public/iparser.h +++ b/src/public/iparser.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 QuasarApp. + * Copyright (C) 2022-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -44,7 +44,7 @@ enum class ParserResult { * @see AbstractNode::parsePackage * @see AbstractNode */ -class HEARTSHARED_EXPORT iParser: public QObject +class HEARTSHARED_EXPORT iParser: public QObject, public QuasarAppUtils::iHRO { Q_OBJECT public: @@ -54,6 +54,7 @@ class HEARTSHARED_EXPORT iParser: public QObject /** * @brief registerPackageType This method register package type T. * This is need to prepare pacakge for parsing in the parsePackage method. + * @see initSupportedCommands */ void registerPackageType() { _registeredTypes[T::command()] = [](){ @@ -190,7 +191,7 @@ class HEARTSHARED_EXPORT iParser: public QObject * @see AbstractNode::registerPackageType * @see Header::command */ - virtual QSharedPointer genPackage(unsigned short cmd) const ; + QSharedPointer genPackage(unsigned short cmd) const; /** * @brief checkCommand This method check command are if registered type or not. @@ -205,6 +206,13 @@ class HEARTSHARED_EXPORT iParser: public QObject */ virtual QString parserId() const = 0; + /** + * @brief initSupportedCommands This method will be invoked before add a parser into parser's storage. Use this method to register your command for this parser object. By default, this method does nothing, You still can register your command in the class constructor. But if you use inheritance between your APIs versions to you must use this method, because your constructors both all your commands, this broken API selector of your node. + * @see registerPackageType + */ + virtual void initSupportedCommands(); + + QString toString() const override; protected: AbstractNode *node() const; @@ -236,6 +244,7 @@ class HEARTSHARED_EXPORT iParser: public QObject friend class BigDataParserOld; friend class AbstractNodeParserOld; + friend class AbstractNode; }; diff --git a/src/public/isqldb.cpp b/src/public/isqldb.cpp index 4e28e8e0..2f995956 100644 --- a/src/public/isqldb.cpp +++ b/src/public/isqldb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -98,7 +98,26 @@ bool ISqlDB::insertObjectP(const QSharedPointer &saveObject, } return _writer && _writer->isValid() && - _writer->insertObject(saveObject, wait); + _writer->insertObject(saveObject, wait); +} + +bool ISqlDB::replaceObjectP(const QSharedPointer &saveObject, bool wait) { + if (updateCache(saveObject)) { + + if (getMode() == SqlDBCasheWriteMode::Force) { + + return _writer && _writer->isValid() && + _writer->replaceObject(saveObject, wait); + } + + pushToQueue(saveObject, CacheAction::Update); + globalUpdateDataBase(getMode()); + + return true; + } + + return _writer && _writer->isValid() && + _writer->replaceObject(saveObject, wait); } qint64 ISqlDB::getLastUpdateTime() const { @@ -211,14 +230,28 @@ bool ISqlDB::insertObject(const QSharedPointer &saveObject, bool wait) return true; } -bool ISqlDB::doQuery(const QString &query, bool wait, - QSqlQuery *result) const { +bool ISqlDB::replaceObject(const QSharedPointer &saveObject, bool wait) { + if (!saveObject || !saveObject->isValid()) { + return false; + } + + if (!replaceObjectP(saveObject, wait)) { + return false; + } + + emit sigItemChanged(saveObject); + + return true; +} + +bool ISqlDB::doQuery(const QString &query, const QVariantMap& toBind, + bool wait, QSqlQuery *result) const { if (!_writer) { return false; } - return _writer->doQuery(query, wait, result); + return _writer->doQuery(query, toBind, wait, result); } bool ISqlDB::doSql(const QString &sqlFile, bool wait) const { diff --git a/src/public/isqldb.h b/src/public/isqldb.h index 6964d596..ea41ad96 100644 --- a/src/public/isqldb.h +++ b/src/public/isqldb.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -105,7 +105,11 @@ class HEARTSHARED_EXPORT ISqlDB: public QObject, public iObjectProvider, public bool insertObject(const QSharedPointer& saveObject, bool wait = false) override; - bool doQuery(const QString &query, bool wait = false, QSqlQuery* result = nullptr) const override; + bool replaceObject(const QSharedPointer& saveObject, + bool wait = false) override; + + bool doQuery(const QString &query, const QVariantMap& bindValues, + bool wait = false, QSqlQuery* result = nullptr) const override; bool doSql(const QString &sqlFile, bool wait) const override; @@ -232,6 +236,8 @@ class HEARTSHARED_EXPORT ISqlDB: public QObject, public iObjectProvider, public bool wait = false); bool insertObjectP(const QSharedPointer& saveObject, bool wait = false); + bool replaceObjectP(const QSharedPointer& saveObject, + bool wait = false); qint64 lastUpdateTime = 0; qint64 updateInterval = DEFAULT_UPDATE_INTERVAL; diff --git a/src/public/itoken.h b/src/public/itoken.h index 621a45a4..b5d02222 100644 --- a/src/public/itoken.h +++ b/src/public/itoken.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/package.cpp b/src/public/package.cpp index ca8c2c04..710ead3f 100644 --- a/src/public/package.cpp +++ b/src/public/package.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -27,11 +27,10 @@ bool Package::isValid() const { if (hdr.size > maximumSize()) return false; -#ifdef HEART_DEPRECATED_API - return calcHash() == hdr.hash || calcHashOld() == hdr.hash; -#else - return calcHash() == hdr.hash; -#endif + if (calcHash() == hdr.hash) + return true; + + return calcHashOld() == hdr.hash; } void Package::reset() { @@ -47,7 +46,7 @@ QString Package::toString() const { unsigned int Package::calcHash() const { auto tmp = data + QByteArray::number(hdr.command); - return common::Hash32(tmp.constData(), tmp.size()); + return qa_common::hash32(tmp.constData(), tmp.size()); } unsigned int Package::calcHashOld() const { diff --git a/src/public/package.h b/src/public/package.h index 19fe2d5d..3f8c8be0 100644 --- a/src/public/package.h +++ b/src/public/package.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packagemanager.cpp b/src/public/packagemanager.cpp index a43990ef..fcb00897 100644 --- a/src/public/packagemanager.cpp +++ b/src/public/packagemanager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packagemanager.h b/src/public/packagemanager.h index c0942990..d881f824 100644 --- a/src/public/packagemanager.h +++ b/src/public/packagemanager.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/abstractdata.cpp b/src/public/packages/abstractdata.cpp index 2dcf0d86..16094622 100644 --- a/src/public/packages/abstractdata.cpp +++ b/src/public/packages/abstractdata.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -38,29 +38,13 @@ bool AbstractData::toPackage(Package &package, package.hdr.command = cmd(); package.hdr.triggerHash = triggerHash; package.hdr.size = package.data.size(); - package.hdr.hash = package.calcHash(); - return package.isValid(); -} - -bool AbstractData::toPackageOld(Package &package, unsigned int triggerHash) const { - if (!checkCmd()) { - QuasarAppUtils::Params::log("You try send pacakge without QH_PACKAGE macross. Please add QH_PACKAGE macros to this class.", - QuasarAppUtils::Error); - return false; - } - - if (!isValid()) { - return false; + if (isOldPackage()) { + package.hdr.hash = package.calcHashOld(); + } else { + package.hdr.hash = package.calcHash(); } - package.data = toBytes(); - - package.hdr.command = cmdOld(); - package.hdr.triggerHash = triggerHash; - package.hdr.size = package.data.size(); - package.hdr.hash = package.calcHashOld(); - return package.isValid(); } @@ -74,8 +58,8 @@ bool AbstractData::isValid() const { } QString AbstractData::toString() const { - return QString("Object: type:%0, command:%1"). - arg(cmdString()). + return QString("Type: %0, command: %1"). + arg(cmdString()). arg(cmd()); } @@ -87,6 +71,10 @@ AbstractData::~AbstractData() { } +bool QH::PKG::AbstractData::isOldPackage() const { + return false; +} + } } diff --git a/src/public/packages/abstractdata.h b/src/public/packages/abstractdata.h index 0ce092af..64e0dd14 100644 --- a/src/public/packages/abstractdata.h +++ b/src/public/packages/abstractdata.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -28,11 +28,12 @@ */ #define QH_PACKAGE(X, S) \ public: \ - static unsigned short commandOld(){return qHash(QString(S)) % 0xFFFF;} \ - static unsigned short command(){return common::Hash16(S, strlen(S));} \ + static unsigned short command(){\ + QByteArray ba = QString(S).toLocal8Bit();\ + return qa_common::hash16(ba.data(), ba.size());\ + } \ static QString commandText(){return S;} \ unsigned short cmd() const override {return X::command();} \ - unsigned short cmdOld() const override {return X::commandOld();} \ QString cmdString() const override {return X::commandText();} \ protected: \ unsigned int localCode() const override {return typeid(X).hash_code();} \ @@ -108,23 +109,6 @@ class HEARTSHARED_EXPORT AbstractData : public StreamBase, public QuasarAppUtils */ bool toPackage(Package &package, unsigned int triggerHash = 0) const; - /** - * @brief toPackageOld This method convert this class object to the package. - * For more info see Package class. - * @param package This is return value of Package class. - * @param triggerHash This is hash of the package the current class is responding to. - * @return True if convert to package finished successful. - */ - bool toPackageOld(Package &package, unsigned int triggerHash = 0) const; - - /** - * @brief cmdOld - This is command of this object, (for generate cmd use macross QH_PACKAGE) - * @note Use the QH_PACKAGE macross for implement this method. - * @return global command of package. - * @see QH_PACKAGE - */ - virtual unsigned short cmdOld() const = 0; - /** * @brief isValid This method check current object to valid. * @return True if class isValid. @@ -180,6 +164,11 @@ class HEARTSHARED_EXPORT AbstractData : public StreamBase, public QuasarAppUtils */ virtual unsigned int localCode() const = 0; + /** + * @brief isOldPackage This method mark package as a old, old pacakges use the Package::calcHashOld method for calculation hash sum of packages. + * @return true if the pacakge is old. + */ + virtual bool isOldPackage() const; private: /** * @brief checkCmd This method check QH_PACKAGE macross. diff --git a/src/public/packages/badrequest.cpp b/src/public/packages/badrequest.cpp index 00d6be07..7872265d 100644 --- a/src/public/packages/badrequest.cpp +++ b/src/public/packages/badrequest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/badrequest.h b/src/public/packages/badrequest.h index 98b8e8e3..36ab5230 100644 --- a/src/public/packages/badrequest.h +++ b/src/public/packages/badrequest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatabase.cpp b/src/public/packages/bigdatabase.cpp index 92091a7c..ade92968 100644 --- a/src/public/packages/bigdatabase.cpp +++ b/src/public/packages/bigdatabase.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatabase.h b/src/public/packages/bigdatabase.h index d1c023e8..3f296cca 100644 --- a/src/public/packages/bigdatabase.h +++ b/src/public/packages/bigdatabase.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdataheader.cpp b/src/public/packages/bigdataheader.cpp index 10722798..ac0c46b0 100644 --- a/src/public/packages/bigdataheader.cpp +++ b/src/public/packages/bigdataheader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdataheader.h b/src/public/packages/bigdataheader.h index 0bfc1568..d3b5aca3 100644 --- a/src/public/packages/bigdataheader.h +++ b/src/public/packages/bigdataheader.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatapart.cpp b/src/public/packages/bigdatapart.cpp index 1646efb9..b44699eb 100644 --- a/src/public/packages/bigdatapart.cpp +++ b/src/public/packages/bigdatapart.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatapart.h b/src/public/packages/bigdatapart.h index 78b16ddc..5e465598 100644 --- a/src/public/packages/bigdatapart.h +++ b/src/public/packages/bigdatapart.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatarequest.cpp b/src/public/packages/bigdatarequest.cpp index 1de4740f..7e089a96 100644 --- a/src/public/packages/bigdatarequest.cpp +++ b/src/public/packages/bigdatarequest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatarequest.h b/src/public/packages/bigdatarequest.h index 0b8896c6..633d12b3 100644 --- a/src/public/packages/bigdatarequest.h +++ b/src/public/packages/bigdatarequest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 QuasarApp. + * Copyright (C) 2021-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatawraper.cpp b/src/public/packages/bigdatawraper.cpp index 1c39f74a..c12c0681 100644 --- a/src/public/packages/bigdatawraper.cpp +++ b/src/public/packages/bigdatawraper.cpp @@ -1,6 +1,6 @@ //# -//# Copyright (C) 2022-2022 QuasarApp. +//# Copyright (C) 2022-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/packages/bigdatawraper.h b/src/public/packages/bigdatawraper.h index 3cae04dd..685a5671 100644 --- a/src/public/packages/bigdatawraper.h +++ b/src/public/packages/bigdatawraper.h @@ -1,6 +1,6 @@ //# -//# Copyright (C) 2022-2022 QuasarApp. +//# Copyright (C) 2022-2023 QuasarApp. //# Distributed under the lgplv3 software license, see the accompanying //# Everyone is permitted to copy and distribute verbatim copies //# of this license document, but changing it is not allowed. diff --git a/src/public/packages/cacheddbobjectsrequest.cpp b/src/public/packages/cacheddbobjectsrequest.cpp index 78551843..98e0ee39 100644 --- a/src/public/packages/cacheddbobjectsrequest.cpp +++ b/src/public/packages/cacheddbobjectsrequest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/cacheddbobjectsrequest.h b/src/public/packages/cacheddbobjectsrequest.h index bda42de5..7b0ee263 100644 --- a/src/public/packages/cacheddbobjectsrequest.h +++ b/src/public/packages/cacheddbobjectsrequest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/closeconnection.cpp b/src/public/packages/closeconnection.cpp index 1a2f17c7..3fac4393 100644 --- a/src/public/packages/closeconnection.cpp +++ b/src/public/packages/closeconnection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/closeconnection.h b/src/public/packages/closeconnection.h index 6177b303..79d33b01 100644 --- a/src/public/packages/closeconnection.h +++ b/src/public/packages/closeconnection.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/customdbrequest.cpp b/src/public/packages/customdbrequest.cpp index 713ca2bd..cef5a35a 100644 --- a/src/public/packages/customdbrequest.cpp +++ b/src/public/packages/customdbrequest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/customdbrequest.h b/src/public/packages/customdbrequest.h index d3741a68..fc0fe75f 100644 --- a/src/public/packages/customdbrequest.h +++ b/src/public/packages/customdbrequest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/datapack.cpp b/src/public/packages/datapack.cpp index c8dd19f3..bc84a302 100644 --- a/src/public/packages/datapack.cpp +++ b/src/public/packages/datapack.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/datapack.h b/src/public/packages/datapack.h index 29622083..2c96f1f2 100644 --- a/src/public/packages/datapack.h +++ b/src/public/packages/datapack.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -29,6 +29,14 @@ class DataPack final: public AbstractData setPackData(newPackData); } + /** + * @brief size This method return of the items count of this pack. + * @return size of the packs. + */ + unsigned int size() const { + return _packData.size(); + } + /** * @brief packData This method return source list of the elements. * @return source list diff --git a/src/public/packages/dbobject.cpp b/src/public/packages/dbobject.cpp index cba83484..fa004947 100644 --- a/src/public/packages/dbobject.cpp +++ b/src/public/packages/dbobject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -30,10 +30,26 @@ DBObject::~DBObject() { PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const { auto map = variantMap().keys(); + QString queryString = "SELECT " + map.join(",") + " FROM %0 "; + queryString = queryString.arg(table()); - QString queryString = "SELECT " + map.join(",") + " FROM %0 " + getWhereBlock(); - queryString = queryString.arg(table()); + auto [conditionQueryString, conditionBindingMap] = condition(); + + if (conditionQueryString.size()) { + + queryString += " WHERE " + conditionQueryString; + if (!q.prepare(queryString)) { + return PrepareResult::Fail; + } + + for (auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) { + q.bindValue(it.key(), it.value()); + } + + return PrepareResult::Success; + } + if (!q.prepare(queryString)) { return PrepareResult::Fail; @@ -42,7 +58,7 @@ PrepareResult DBObject::prepareSelectQuery(QSqlQuery &q) const { return PrepareResult::Success; } -PrepareResult DBObject::prepareInsertQuery(QSqlQuery &q) const { +PrepareResult DBObject::prepareInsertQuery(QSqlQuery &q, bool replace) const { DBVariantMap map = variantMap(); @@ -53,7 +69,9 @@ PrepareResult DBObject::prepareInsertQuery(QSqlQuery &q) const { return PrepareResult::Fail; } - QString queryString = "INSERT INTO %0(%1) VALUES (%2) "; + QString queryString = (replace)? + "REPLACE INTO %0(%1) VALUES (%2) " : + "INSERT INTO %0(%1) VALUES (%2) "; queryString = queryString.arg(table()); @@ -105,7 +123,15 @@ PrepareResult DBObject::prepareUpdateQuery(QSqlQuery &q) const { return PrepareResult::Fail; } - QString queryString = "UPDATE %0 SET %1 WHERE " + condition(); + auto [conditionQueryString, conditionBindingMap] = condition(); + + if (conditionQueryString.isEmpty()) { + QuasarAppUtils::Params::log("The object soue not have condition for update object.", + QuasarAppUtils::Error); + return PrepareResult::Fail; + } + + QString queryString = "UPDATE %0 SET %1 WHERE " + conditionQueryString; queryString = queryString.arg(table()); QString tableUpdateValues = ""; @@ -142,6 +168,21 @@ PrepareResult DBObject::prepareUpdateQuery(QSqlQuery &q) const { q.bindValue(":" + it.key(), it.value().value); } + for (auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) { +#ifdef QT_DEBUG + if (bool(map.value(it.key()).type & MemberType::Update)) { + QuasarAppUtils::Params::log(QString("Bad object configuration: " + "The %0 field using in the condition and has MemberType::Update configuration." + " All condition fields should not use the MemberType::Update. \n %1"). + arg(it.key(), toString()), + QuasarAppUtils::Warning); + } +#endif + q.bindValue(it.key(), it.value()); + } + + + return PrepareResult::Success; } @@ -160,72 +201,30 @@ bool DBObject::isBundle() const { return false; } -QString DBObject::condition() const { - - // prepare key value to string condition - auto prepareCondition = [](const QString& key, const QString &val){ - return key + "= '" + val + "'"; - }; - - auto byteArrayWarning = [](){ - QuasarAppUtils::Params::log("You try generate a condition from the raw bytes array." - " This operation can not be brok the sql request." - " Use the QString or int type for values of condition." - " If you want to bytes array in condition then override the DBObject::condition method.", - QuasarAppUtils::Warning); - }; - - QString errorString = "WRONG OBJECT"; +std::pair> DBObject::condition() const { // if object have a primaryKey then return primary key auto primaryVal = primaryValue(); - if (primaryVal.size()) { - return prepareCondition(primaryKey(), primaryVal); + if (!primaryVal.isNull()) { + return {QString("%0 = :%0").arg(primaryKey()), + {{QString(":%0").arg(primaryKey()), {primaryVal}}}}; } - auto map = variantMap(); - - // check all objects fields - for (auto it = map.begin(); it != map.end(); ++it) { - // if field is unique then - if (bool(it.value().type & MemberType::Unique)) { -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - - QVariant::Type type = it.value().value.type(); - - bool typeisString = type == QVariant::String; - bool typeisArray = type == QVariant::ByteArray; - -#else - - int type = it.value().value.metaType().id(); - - bool typeisString = type == QMetaType::QString; - bool typeisArray = type == QMetaType::QByteArray; - -#endif - - // if field is string then check size. - if (typeisString) { - QString val = it.value().value.toString(); - if (val.size()) { - return prepareCondition(it.key(), val); - } - } else if (typeisArray) { - byteArrayWarning(); - continue; - } else if (it.value().value.isValid()) { - return prepareCondition(it.key(), it.value().value.toString()); - } - } - } QuasarAppUtils::Params::log("Fail to generate condition for object: " + toString() + ". Object do not have valid unique fields or valid database address.", QuasarAppUtils::Error); - return errorString; + return {}; +} + +QString DBObject::primaryKey() const { + return ""; +} + +QVariant DBObject::primaryValue() const { + return {}; } bool DBObject::isInsertPrimaryKey() const { @@ -241,25 +240,6 @@ QString DBObject::toString() const { QString(" %0").arg(dbAddress().toString()); } -QString DBObject::getWhereBlock() const { - auto con = condition(); - - if (!con.size()) - return ""; - - QString whereBlock = "WHERE " + con; - - return whereBlock; -} - -bool DBObject::printError() const { - return _printError; -} - -void DBObject::setPrintError(bool newPrintError) { - _printError = newPrintError; -} - QDataStream &DBObject::fromStream(QDataStream &stream) { QuasarAppUtils::Params::log("This object not support stream operator." " Please Override the fromStream method for this object. " + toString(), @@ -278,15 +258,29 @@ QDataStream &DBObject::toStream(QDataStream &stream) const { PrepareResult DBObject::prepareRemoveQuery(QSqlQuery &q) const { - QString queryString = "DELETE FROM %0 " + getWhereBlock(); - + QString queryString = "DELETE FROM %0 "; queryString = queryString.arg(table()); - if (!q.prepare(queryString)) { - return PrepareResult::Fail; + auto [conditionQueryString, conditionBindingMap] = condition(); + + if (conditionQueryString.size()) { + + queryString += " WHERE " + conditionQueryString; + if (!q.prepare(queryString)) { + return PrepareResult::Fail; + } + + for (auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) { + q.bindValue(it.key(), it.value()); + } + + return PrepareResult::Success; } - return PrepareResult::Success; + QuasarAppUtils::Params::log("This object doues not have condition for remove." + toString(), + QuasarAppUtils::Error); + + return PrepareResult::Fail; } DBVariantMap DBObject::variantMap() const { @@ -302,7 +296,7 @@ bool DBObject::isValid() const { return false; if (isInsertPrimaryKey()) { - return primaryValue().size(); + return primaryValue().isValid(); } return table().size(); diff --git a/src/public/packages/dbobject.h b/src/public/packages/dbobject.h index a0f5a7d7..e4a27996 100644 --- a/src/public/packages/dbobject.h +++ b/src/public/packages/dbobject.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -171,6 +171,9 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData * Override this method for insert item into database. * By Default This method prepare a insert query using the data that returned from the variantMap method. * + * @param q Thuis is query object. + * @param replace This option disable or enable replacing of exists objects. + * * Default insert query have a next template: * \code{sql} * INSERT INTO %0(%1) VALUES (%3) @@ -183,7 +186,7 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData * * Example of overriding: * \code{cpp} - * PrepareResult ExampleObject::prepareInsertQuery(QSqlQuery &q) const { + * PrepareResult ExampleObject::prepareInsertQuery(QSqlQuery &q, replace) const { DBVariantMap map = variantMap(); @@ -225,7 +228,7 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData * * @note If you want disable this method, just override it and return the PrepareResult::Disabled value. */ - virtual PrepareResult prepareInsertQuery(QSqlQuery& q) const; + virtual PrepareResult prepareInsertQuery(QSqlQuery& q, bool replace) const; /** * @brief prepareUpdateQuery this method should be prepare a insert data query. @@ -366,20 +369,6 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData */ virtual QString table() const = 0; - /** - * @brief printError This method return status of printing error messages for sql quries. by default this propertye is enabled. - * @return true if printing error messages is enabled else false. - * @see DBObject::setPrintError - */ - bool printError() const; - - /** - * @brief setPrintError This method sets new value for printError propertye. - * @param newPrintError This is new value for printError propertye - * @see DBObject::printError - */ - void setPrintError(bool newPrintError); - protected: QDataStream &fromStream(QDataStream &stream) override; @@ -390,18 +379,19 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData * This method using on default implementation of DBObject::prepareSelectQuery and DBObject::prepareRemoveQuery methods. * The default implementation generate when block by map for more information see the variantMap nethod. * Override this method for customize your select or delete query. - * @return condition string. + * @return condition string and qvariant map of the values for binding (bindingKey:bindValue). * @note This operation can not be block the sql request. Use the QString or int type for values of condition. If you want to bytes array in condition then override the DBObject::condition method. * * Example of overriding: * \code{cpp} * QString DBObject::condition() const { - return {"id = '" + getId().toRaw() + "'"}; + return {{"id = :id"}, + {{{":id"}, {getId()}}}}; } * \endcode */ - virtual QString condition() const; + virtual std::pair> condition() const; /** * @brief primaryKey This method must be return the name of primary key of this object table. @@ -409,7 +399,7 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData * @note If you returned empty value then this method can not be prepare insert update and delete querys. * @return The primary key name. */ - virtual QString primaryKey() const = 0; + virtual QString primaryKey() const; /** * @brief primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then return invalid value. @@ -417,7 +407,7 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData * @note If you alredy override the condition method then You can return empty string because this method using in generate default condition only. * @see DBObject::condition. */ - virtual QString primaryValue() const = 0; + virtual QVariant primaryValue() const; /** * @brief isInsertPrimaryKey This method check primaryKeys type. @@ -426,9 +416,6 @@ class HEARTSHARED_EXPORT DBObject : public AbstractData */ bool isInsertPrimaryKey() const; -private: - QString getWhereBlock() const; - bool _printError = true; }; } } diff --git a/src/public/packages/dbobjectset.cpp b/src/public/packages/dbobjectset.cpp index 22b7daae..7a7e5e0b 100644 --- a/src/public/packages/dbobjectset.cpp +++ b/src/public/packages/dbobjectset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -12,7 +12,7 @@ DBObjectSet::DBObjectSet(const QString table) { _table = table; } -PrepareResult DBObjectSet::prepareInsertQuery(QSqlQuery &) const { +PrepareResult DBObjectSet::prepareInsertQuery(QSqlQuery &, bool ) const { return PrepareResult::Disabled; } diff --git a/src/public/packages/dbobjectset.h b/src/public/packages/dbobjectset.h index 6613fb64..57b53289 100644 --- a/src/public/packages/dbobjectset.h +++ b/src/public/packages/dbobjectset.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -27,7 +27,7 @@ class HEARTSHARED_EXPORT DBObjectSet: public DBObject public: DBObjectSet(const QString table); - PrepareResult prepareInsertQuery(QSqlQuery &) const override final; + PrepareResult prepareInsertQuery(QSqlQuery &q, bool replace) const override final; PrepareResult prepareRemoveQuery(QSqlQuery &q) const override final; PrepareResult prepareSelectQuery(QSqlQuery &q) const override final; PrepareResult prepareUpdateQuery(QSqlQuery &q) const override final; diff --git a/src/public/packages/dbobjectsrequest.cpp b/src/public/packages/dbobjectsrequest.cpp index df2e54a0..32c7fec5 100644 --- a/src/public/packages/dbobjectsrequest.cpp +++ b/src/public/packages/dbobjectsrequest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/dbobjectsrequest.h b/src/public/packages/dbobjectsrequest.h index 9ada7311..08012f9c 100644 --- a/src/public/packages/dbobjectsrequest.h +++ b/src/public/packages/dbobjectsrequest.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -47,23 +47,18 @@ class DBObjectsRequest final: public DBObjectSet * @note If you want to get all elements from table then skip @a conditions argument or set it to empty string value. * @param table This is name of database table. * @param conditions This is string with conditions for create sql query. If you want to get all elemts just ignore this argument. + * @param valuesToBind This is map of the value for the reques. * @see DBObjectsRequest::setConditions */ DBObjectsRequest(const QString& table, - const QString& conditions = "" ): + const QString& conditions = "", + const QVariantMap& valuesToBind = {}): DBObjectSet (table) { _conditions = conditions; + _condirionValues = valuesToBind; }; - QString primaryValue() const override { - return ""; - } - - QString primaryKey() const override { - return ""; - } - void clear() override { _data.clear(); }; @@ -115,8 +110,8 @@ class DBObjectsRequest final: public DBObjectSet protected: - QString condition() const override { - return _conditions; + std::pair > condition() const override { + return {_conditions, _condirionValues}; } DBObject *createDBObject() const override { @@ -127,6 +122,7 @@ class DBObjectsRequest final: public DBObjectSet private: QString _conditions; + QVariantMap _condirionValues; }; diff --git a/src/public/packages/dbobjectsrequestwithStream.cpp b/src/public/packages/dbobjectsrequestwithStream.cpp index df2e54a0..32c7fec5 100644 --- a/src/public/packages/dbobjectsrequestwithStream.cpp +++ b/src/public/packages/dbobjectsrequestwithStream.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/dbobjectsrequestwithStream.h b/src/public/packages/dbobjectsrequestwithStream.h index 7f42a4ab..06d4031e 100644 --- a/src/public/packages/dbobjectsrequestwithStream.h +++ b/src/public/packages/dbobjectsrequestwithStream.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/deleteobject.cpp b/src/public/packages/deleteobject.cpp index 2ab9441c..f77ae9c8 100644 --- a/src/public/packages/deleteobject.cpp +++ b/src/public/packages/deleteobject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -12,6 +12,11 @@ DeleteObject::DeleteObject() { } +DeleteObject::DeleteObject(const DbAddress &address, const QString &primaryKey) { + _primaryKey = primaryKey; + _address = address; +} + DeleteObject::DeleteObject(const Package &pkg): DeleteObject() { fromBytes(pkg.toBytes()); } @@ -41,6 +46,7 @@ QDataStream &DeleteObject::fromStream(QDataStream &stream) { stream >> _address; stream >> _token; + stream >> _primaryKey; return stream; } @@ -48,10 +54,15 @@ QDataStream &DeleteObject::fromStream(QDataStream &stream) { QDataStream &DeleteObject::toStream(QDataStream &stream) const { stream << _address; stream << _token; + stream << _primaryKey; return stream; } +void DeleteObject::setPrimaryKey(const QString &newPrimaryKey) { + _primaryKey = newPrimaryKey; +} + bool DeleteObject::isCached() const { return false; } @@ -69,11 +80,11 @@ void DeleteObject::setAddress(const DbAddress &newAddress) { } QString DeleteObject::primaryKey() const { - return "id"; + return _primaryKey; } -QString DeleteObject::primaryValue() const { - return _address.id().toString(); +QVariant DeleteObject::primaryValue() const { + return _address.id(); } } diff --git a/src/public/packages/deleteobject.h b/src/public/packages/deleteobject.h index 3fdf6090..9138e542 100644 --- a/src/public/packages/deleteobject.h +++ b/src/public/packages/deleteobject.h @@ -1,7 +1,7 @@ #ifndef DELETEOBJECT_H #define DELETEOBJECT_H /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -16,6 +16,12 @@ namespace PKG { /** * @brief The DeleteObject class is request for update object with dbId; + * + * Example of use + * + * @code{cpp} + * QH::PKG::DeleteObject rquest({"UsersTable", "userName"}, "name"); + * @endcode */ class HEARTSHARED_EXPORT DeleteObject: public DBObject, public IToken { @@ -23,6 +29,14 @@ class HEARTSHARED_EXPORT DeleteObject: public DBObject, public IToken public: DeleteObject(); + + /** + * @brief DeleteObject This is main constructor for request, you can remove any db object if you know them value of the primary key. + * @param address This is defautl addres of the ovject {table, id}. Where id is value of the primart key. + * @param primaryKey This is primary key name - filed of the table. By default using **id** + */ + DeleteObject(const DbAddress& address, const QString& primaryKey); + DeleteObject(const Package& pkg); DBObject *createDBObject() const override; @@ -48,15 +62,21 @@ class HEARTSHARED_EXPORT DeleteObject: public DBObject, public IToken */ void setAddress(const DbAddress &newAddress); + /** + * @brief setPrimaryKey This meethod sets new value of the primary key. + * @param newPrimaryKey This is new value of the primary key + */ + void setPrimaryKey(const QString &newPrimaryKey); + protected: QString primaryKey() const override; - QString primaryValue() const override; + QVariant primaryValue() const override; QDataStream &fromStream(QDataStream &stream) override; QDataStream &toStream(QDataStream &stream) const override; private: - + QString _primaryKey = "id"; DbAddress _address; AccessToken _token; diff --git a/src/public/packages/getmaxintegerid.cpp b/src/public/packages/getmaxintegerid.cpp index 3939abe1..cb685850 100644 --- a/src/public/packages/getmaxintegerid.cpp +++ b/src/public/packages/getmaxintegerid.cpp @@ -36,7 +36,6 @@ PrepareResult GetMaxIntegerId::prepareSelectQuery(QSqlQuery &q) const { bool GetMaxIntegerId::fromSqlRecord(const QSqlRecord &q) { _value = q.value(0).toInt(); - return true; } @@ -48,13 +47,5 @@ QString GetMaxIntegerId::table() const { return _table; } -QString GetMaxIntegerId::primaryKey() const { - return ""; -} - -QString GetMaxIntegerId::primaryValue() const { - return ""; -} - } } diff --git a/src/public/packages/getmaxintegerid.h b/src/public/packages/getmaxintegerid.h index c53864a8..c5a5c1f9 100644 --- a/src/public/packages/getmaxintegerid.h +++ b/src/public/packages/getmaxintegerid.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -41,10 +41,6 @@ class HEARTSHARED_EXPORT GetMaxIntegerId: public DBObject bool isCached() const override; QString table() const override; -protected: - QString primaryKey() const override; - QString primaryValue() const override; - private: QString _table; QString _field; diff --git a/src/public/packages/getsinglevalue.cpp b/src/public/packages/getsinglevalue.cpp index f944be54..58182a2b 100644 --- a/src/public/packages/getsinglevalue.cpp +++ b/src/public/packages/getsinglevalue.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -19,9 +19,9 @@ GetSingleValue::GetSingleValue(const DbAddress& address, const QString& primaryKey) { _table = address.table(); - _id = address.id().toString(); + _primaryValue = address.id(); _field = field; - _key = primaryKey; + _primaryKey = primaryKey; } QVariant GetSingleValue::value() const { @@ -33,14 +33,15 @@ DBObject *GetSingleValue::createDBObject() const { } PrepareResult GetSingleValue::prepareSelectQuery(QSqlQuery &q) const { - QString queryString = "SELECT %0 FROM %1 WHERE %2='%3'"; - - queryString = queryString.arg(_field, table(), _key, _id); + QString queryString = "SELECT %0 FROM %1 WHERE %2=:%2"; + queryString = queryString.arg(_field, table(), _primaryKey); if (!q.prepare(queryString)) { return PrepareResult::Fail; } + q.bindValue(":" + _primaryKey, _primaryValue); + return PrepareResult::Success; } @@ -59,11 +60,11 @@ QString GetSingleValue::table() const { } QString GetSingleValue::primaryKey() const { - return _id; + return _primaryKey; } -QString GetSingleValue::primaryValue() const { - return _field; +QVariant GetSingleValue::primaryValue() const { + return _primaryValue; } } diff --git a/src/public/packages/getsinglevalue.h b/src/public/packages/getsinglevalue.h index a8a6f1e2..f8e8c9f6 100644 --- a/src/public/packages/getsinglevalue.h +++ b/src/public/packages/getsinglevalue.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -63,13 +63,13 @@ class HEARTSHARED_EXPORT GetSingleValue final: public DBObject protected: QString primaryKey() const override; - QString primaryValue() const override; + QVariant primaryValue() const override; private: - QString _id; - QString _field; + QString _primaryKey; + QVariant _primaryValue; QString _table; QVariant _value; - QString _key; + QString _field; }; } diff --git a/src/public/packages/ping.cpp b/src/public/packages/ping.cpp index f38bce55..308e9bb3 100644 --- a/src/public/packages/ping.cpp +++ b/src/public/packages/ping.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/ping.h b/src/public/packages/ping.h index 1c8c26cb..8bac4e43 100644 --- a/src/public/packages/ping.h +++ b/src/public/packages/ping.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/packages/setsinglevalue.cpp b/src/public/packages/setsinglevalue.cpp index fe8bb904..60f08937 100644 --- a/src/public/packages/setsinglevalue.cpp +++ b/src/public/packages/setsinglevalue.cpp @@ -1,12 +1,12 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. */ #include "setsinglevalue.h" -#include "quasarapp.h" +#include "params.h" #include #include @@ -20,7 +20,7 @@ SetSingleValue::SetSingleValue(const DbAddress& address, const QString& field, const QVariant& value, const QString &primaryKey) { - _id = address.id().toString(); + _primaryValue = address.id(); _table = address.table(); _field = field; _value = value; @@ -32,9 +32,9 @@ DBObject *SetSingleValue::createDBObject() const { } PrepareResult SetSingleValue::prepareUpdateQuery(QSqlQuery &q) const { - QString queryString = "UPDATE %0 SET %1=:%1 WHERE %2='%3'"; + QString queryString = "UPDATE %0 SET %1=:%1 WHERE %2=:%2"; - queryString = queryString.arg(table(), _field, primaryKey(), _id); + queryString = queryString.arg(table(), _field, primaryKey()); if (!q.prepare(queryString)) { @@ -44,12 +44,16 @@ PrepareResult SetSingleValue::prepareUpdateQuery(QSqlQuery &q) const { } q.bindValue(":" + _field, _value); + q.bindValue(":" + _primaryKey, _primaryValue); return PrepareResult::Success; } -PrepareResult SetSingleValue::prepareInsertQuery(QSqlQuery &q) const { - QString queryString = "INSERT INTO %0 (%1, %2) VALUES (:%1, :%2)"; +PrepareResult SetSingleValue::prepareInsertQuery(QSqlQuery &q, bool replace) const { + + QString queryString = (replace)? + "REPLACE INTO %0 (%1, %2) VALUES (:%1, :%2)": + "INSERT INTO %0 (%1, %2) VALUES (:%1, :%2)"; queryString = queryString.arg(table(), primaryKey(), _field); @@ -60,7 +64,7 @@ PrepareResult SetSingleValue::prepareInsertQuery(QSqlQuery &q) const { return PrepareResult::Fail; } - q.bindValue(":" + primaryKey(), primaryValue()); + q.bindValue(":" + _primaryKey, _primaryValue); q.bindValue(":" + _field, _value); return PrepareResult::Success; @@ -82,8 +86,8 @@ QString SetSingleValue::primaryKey() const { return _primaryKey; } -QString SetSingleValue::primaryValue() const { - return _id; +QVariant SetSingleValue::primaryValue() const { + return _primaryValue; } } } diff --git a/src/public/packages/setsinglevalue.h b/src/public/packages/setsinglevalue.h index fd319f06..1e092197 100644 --- a/src/public/packages/setsinglevalue.h +++ b/src/public/packages/setsinglevalue.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -49,7 +49,7 @@ class HEARTSHARED_EXPORT SetSingleValue final: public DBObject DBObject *createDBObject() const override; PrepareResult prepareUpdateQuery(QSqlQuery &q) const override; - PrepareResult prepareInsertQuery(QSqlQuery &q) const override; + PrepareResult prepareInsertQuery(QSqlQuery &q, bool replace) const override; bool fromSqlRecord(const QSqlRecord &q) override; bool isCached() const override; @@ -59,14 +59,14 @@ class HEARTSHARED_EXPORT SetSingleValue final: public DBObject protected: QString primaryKey() const override; - QString primaryValue() const override; + QVariant primaryValue() const override; private: - QString _id; - QString _table; - QString _field; QString _primaryKey; + QVariant _primaryValue; + QString _table; QVariant _value; + QString _field; }; } diff --git a/src/public/request.cpp b/src/public/request.cpp index 7cb89c04..54ce09f0 100644 --- a/src/public/request.cpp +++ b/src/public/request.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/request.h b/src/public/request.h index 16886632..621ab387 100644 --- a/src/public/request.h +++ b/src/public/request.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/softdelete.cpp b/src/public/softdelete.cpp index ae60a57a..ecbd9758 100644 --- a/src/public/softdelete.cpp +++ b/src/public/softdelete.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/softdelete.h b/src/public/softdelete.h index 26123e9d..96ca2aa6 100644 --- a/src/public/softdelete.h +++ b/src/public/softdelete.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/sqldb.cpp b/src/public/sqldb.cpp index 04a4ab51..04d2fbaa 100644 --- a/src/public/sqldb.cpp +++ b/src/public/sqldb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/sqldb.h b/src/public/sqldb.h index 77b6c272..f1a34f8a 100644 --- a/src/public/sqldb.h +++ b/src/public/sqldb.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/sqldbwriter.cpp b/src/public/sqldbwriter.cpp index 1b44c466..ff776f66 100644 --- a/src/public/sqldbwriter.cpp +++ b/src/public/sqldbwriter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -131,17 +131,35 @@ bool SqlDBWriter::initDbPrivate(const QVariantMap ¶ms) { return initSuccessful; } -bool SqlDBWriter::doQueryPrivate(const QString &query, QSqlQuery* result) const { +bool SqlDBWriter::doQueryPrivate(const QString &query, const QVariantMap &bindValues, QSqlQuery* result) const { if (!db()) { return false; } QSqlQuery q(*db()); - if (!q.exec(query)) { - QuasarAppUtils::Params::log("request error : " + q.lastError().text(), - QuasarAppUtils::Error); - return false; + if (bindValues.size()) { + if (!q.prepare(query)) { + QuasarAppUtils::Params::log("request error : " + q.lastError().text(), + QuasarAppUtils::Error); + return false; + } + + for (auto it = bindValues.begin(); it != bindValues.end(); ++it) { + q.bindValue(it.key(), it.value()); + } + + if (!q.exec()) { + QuasarAppUtils::Params::log("request error : " + q.lastError().text(), + QuasarAppUtils::Error); + return false; + } + } else { + if (!q.exec(query)) { + QuasarAppUtils::Params::log("request error : " + q.lastError().text(), + QuasarAppUtils::Error); + return false; + } } if (result) { @@ -303,6 +321,14 @@ bool SqlDBWriter::insertObject(const QSharedPointer &ptr, bool wait) { } +bool SqlDBWriter::replaceObject(const QSharedPointer &ptr, bool wait) { + Async::Job job = [this, ptr]() { + return replaceQuery(ptr); + }; + + return asyncLauncher(job, wait); +} + void SqlDBWriter::setSQLSources(const QStringList &list) { _SQLSources = list; } @@ -339,21 +365,40 @@ bool SqlDBWriter::insertQuery(const QSharedPointer &ptr) const { QSqlQuery q(*db()); auto prepare = [ptr](QSqlQuery&q) { - return ptr->prepareInsertQuery(q); + return ptr->prepareInsertQuery(q, false); }; auto cb = [](){return true;}; - return workWithQuery(q, prepare, cb, ptr->printError()); + return workWithQuery(q, prepare, cb); } -bool SqlDBWriter::doQuery(const QString &query, +bool SqlDBWriter::replaceQuery(const QSharedPointer &ptr) const { + if (!ptr) + return false; + + if (!db()) { + return false; + } + + QSqlQuery q(*db()); + + auto prepare = [ptr](QSqlQuery&q) { + return ptr->prepareInsertQuery(q, true); + }; + + auto cb = [](){return true;}; + + return workWithQuery(q, prepare, cb); +} + +bool SqlDBWriter::doQuery(const QString &query, const QVariantMap &bindValues, bool wait, QSqlQuery* result) const { wait = result || wait; - Async::Job job = [this, query, result]() { - return doQueryPrivate(query, result); + Async::Job job = [this, query, bindValues, result]() { + return doQueryPrivate(query, bindValues, result); }; return asyncLauncher(job, wait); @@ -422,7 +467,7 @@ bool SqlDBWriter::selectQuery(const DBObject& requestObject, return result.size(); }; - return workWithQuery(q, prepare, cb, requestObject.printError()); + return workWithQuery(q, prepare, cb); } bool SqlDBWriter::deleteQuery(const QSharedPointer &deleteObject) const { @@ -440,7 +485,7 @@ bool SqlDBWriter::deleteQuery(const QSharedPointer &deleteObject) cons }; - return workWithQuery(q, prepare, cb, deleteObject->printError()); + return workWithQuery(q, prepare, cb); } bool SqlDBWriter::updateQuery(const QSharedPointer &ptr) const { @@ -455,18 +500,14 @@ bool SqlDBWriter::updateQuery(const QSharedPointer &ptr) const { auto cb = [](){return true;}; - return workWithQuery(q, prepare, cb, ptr->printError()); + return workWithQuery(q, prepare, cb); } bool SqlDBWriter::workWithQuery(QSqlQuery &q, const std::function< PrepareResult (QSqlQuery &)> &prepareFunc, - const std::function &cb, - bool printErrors) const { + const std::function &cb) const { - auto printError = [printErrors](const QSqlQuery &q) { - - if (!printErrors) - return ; + auto printError = [](const QSqlQuery &q) { QuasarAppUtils::Params::log("prepare sql error: " + q.executedQuery(), QuasarAppUtils::Debug); @@ -485,6 +526,13 @@ bool SqlDBWriter::workWithQuery(QSqlQuery &q, return false; } +#ifdef HEART_PRINT_SQL_QUERIES + QuasarAppUtils::Params::log(QString("Query executed successfull into %0\n" + "query: %1"). + arg(_db->databaseName(), q.executedQuery()), + QuasarAppUtils::Debug); +#endif + return cb(); } case PrepareResult::Disabled: { diff --git a/src/public/sqldbwriter.h b/src/public/sqldbwriter.h index 713fd64f..f1af6688 100644 --- a/src/public/sqldbwriter.h +++ b/src/public/sqldbwriter.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. @@ -68,8 +68,10 @@ class HEARTSHARED_EXPORT SqlDBWriter : public Async, public iObjectProvider bool updateObject(const QSharedPointer &ptr, bool wait = false) override; bool deleteObject(const QSharedPointer &ptr, bool wait = false) override; bool insertObject(const QSharedPointer &ptr, bool wait = false) override; + bool replaceObject(const QSharedPointer &ptr, bool wait = false) override; + void setSQLSources(const QStringList &list) override; - bool doQuery(const QString& query, bool wait = false, QSqlQuery *result = nullptr) const override; + bool doQuery(const QString& query, const QVariantMap& bindValues = {}, bool wait = false, QSqlQuery *result = nullptr) const override; bool doSql(const QString &sqlFile, bool wait) const override; /** @@ -112,7 +114,13 @@ class HEARTSHARED_EXPORT SqlDBWriter : public Async, public iObjectProvider */ virtual bool insertQuery(const QSharedPointer& insertObject) const; - + /** + * @brief replaceQuery This method prepare the replce object query. + * @param insertObject This is strong pointer of object for generate the insert query. + * @return true if query generated successful. + * @note This method generate query for replace objects in the database. + */ + virtual bool replaceQuery(const QSharedPointer& insertObject) const; protected slots: @@ -198,13 +206,11 @@ protected slots: * @param q - query object with a request. * @param prepareFunc - function with prepare data for query. * @param cb - call after success exec and prepare steps. - * @param printErrors This propertye enabled or disabled printing error messages. By default is enabled. * @return true if all steps finished successful. */ bool workWithQuery(QSqlQuery &q, const std::function< PKG::PrepareResult (QSqlQuery &)> &prepareFunc, - const std::function& cb, - bool printErrors = true) const; + const std::function& cb) const; bool exec(QSqlQuery *sq, const QString &sqlFile) const; @@ -221,7 +227,9 @@ protected slots: * @param result This is pointer to result value. * @return true if query finished successfull */ - bool doQueryPrivate(const QString& query, QSqlQuery *result) const; + bool doQueryPrivate(const QString& query, + const QVariantMap &bindValues, + QSqlQuery *result) const; bool doSqlPrivate(const QString &sqlFile) const; diff --git a/src/public/sqlitedbcache.cpp b/src/public/sqlitedbcache.cpp index f4270195..1a3a87f6 100644 --- a/src/public/sqlitedbcache.cpp +++ b/src/public/sqlitedbcache.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/sqlitedbcache.h b/src/public/sqlitedbcache.h index 0b757f9e..a3ba97c0 100644 --- a/src/public/sqlitedbcache.h +++ b/src/public/sqlitedbcache.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/streambase.cpp b/src/public/streambase.cpp index eea17560..56afc5e3 100644 --- a/src/public/streambase.cpp +++ b/src/public/streambase.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/streambase.h b/src/public/streambase.h index 2f209e93..3eb3531e 100644 --- a/src/public/streambase.h +++ b/src/public/streambase.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/workstate.cpp b/src/public/workstate.cpp index b143aaf7..bc34bb04 100644 --- a/src/public/workstate.cpp +++ b/src/public/workstate.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/src/public/workstate.h b/src/public/workstate.h index ac44f257..df6f60ed 100644 --- a/src/public/workstate.h +++ b/src/public/workstate.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 QuasarApp. + * Copyright (C) 2018-2023 QuasarApp. * Distributed under the lgplv3 software license, see the accompanying * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. diff --git a/submodules/QuasarAppLib b/submodules/QuasarAppLib index afb0ef5f..7b4c10be 160000 --- a/submodules/QuasarAppLib +++ b/submodules/QuasarAppLib @@ -1 +1 @@ -Subproject commit afb0ef5fd83d5a00900c721b5d8cdcb1fa32b92a +Subproject commit 7b4c10be77ddc522c0430471b8dbb2acd70e6cbf diff --git a/submodules/crc b/submodules/crc index 04393056..3a3ab094 160000 --- a/submodules/crc +++ b/submodules/crc @@ -1 +1 @@ -Subproject commit 043930569667c3749fc7b7b62000d120848afa5a +Subproject commit 3a3ab09461bd345be6dbe999bb823c0f34d80f61