diff --git a/cmake/Modules/TinySources.cmake b/cmake/Modules/TinySources.cmake index 1b5cbabb5..32f76ed65 100644 --- a/cmake/Modules/TinySources.cmake +++ b/cmake/Modules/TinySources.cmake @@ -93,6 +93,7 @@ function(tinyorm_sources out_headers out_sources) types/statementscounter.hpp utils/container.hpp utils/fs.hpp + utils/helpers.hpp utils/query.hpp utils/thread.hpp utils/type.hpp diff --git a/include/include.pri b/include/include.pri index bb0dcaf3a..851b3b2a1 100644 --- a/include/include.pri +++ b/include/include.pri @@ -88,6 +88,7 @@ headersList += \ $$PWD/orm/types/statementscounter.hpp \ $$PWD/orm/utils/container.hpp \ $$PWD/orm/utils/fs.hpp \ + $$PWD/orm/utils/helpers.hpp \ $$PWD/orm/utils/query.hpp \ $$PWD/orm/utils/thread.hpp \ $$PWD/orm/utils/type.hpp \ diff --git a/include/orm/schema/schemabuilder.hpp b/include/orm/schema/schemabuilder.hpp index a8b528bee..33c53b7e1 100644 --- a/include/orm/schema/schemabuilder.hpp +++ b/include/orm/schema/schemabuilder.hpp @@ -11,6 +11,7 @@ TINY_SYSTEM_HEADER #include "orm/macros/export.hpp" // Include the blueprint here so a user doesn't have to (it can be forward declared) #include "orm/schema/blueprint.hpp" +#include "orm/utils/helpers.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -30,6 +31,9 @@ namespace Grammars { Q_DISABLE_COPY(SchemaBuilder) + /*! Alias for the helper utils. */ + using Helpers = Orm::Utils::Helpers; + public: /*! Constructor. */ explicit SchemaBuilder(DatabaseConnection &connection); diff --git a/include/orm/tiny/tinybuilder.hpp b/include/orm/tiny/tinybuilder.hpp index 8de850ae2..f6dd3e5b2 100644 --- a/include/orm/tiny/tinybuilder.hpp +++ b/include/orm/tiny/tinybuilder.hpp @@ -15,6 +15,7 @@ TINY_SYSTEM_HEADER #include "orm/tiny/concerns/queriesrelationships.hpp" #include "orm/tiny/exceptions/modelnotfounderror.hpp" #include "orm/tiny/tinybuilderproxies.hpp" +#include "orm/utils/helpers.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -34,6 +35,8 @@ namespace Orm::Tiny /*! Alias for the attribute utils. */ using AttributeUtils = Orm::Tiny::Utils::Attribute; + /*! Alias for the helper utils. */ + using Helpers = Orm::Utils::Helpers; /*! Alias for the type utils. */ using TypeUtils = Orm::Utils::Type; @@ -365,13 +368,13 @@ namespace Orm::Tiny if (auto instance = this->where(attributes).first(); instance) return *instance; - auto newInstance = - newModelInstance(AttributeUtils::joinAttributesForFirstOr( - attributes, values, m_model.getKeyName())); - - newInstance.save(); - - return newInstance; + return Helpers::tap( + newModelInstance(AttributeUtils::joinAttributesForFirstOr( + attributes, values, m_model.getKeyName())), + [](auto &newInstance) + { + newInstance.save(); + }); } template diff --git a/include/orm/utils/helpers.hpp b/include/orm/utils/helpers.hpp new file mode 100644 index 000000000..dd4a26e16 --- /dev/null +++ b/include/orm/utils/helpers.hpp @@ -0,0 +1,52 @@ +#pragma once +#ifndef ORM_UTILS_HELPERS_HPP +#define ORM_UTILS_HELPERS_HPP + +#include "orm/macros/systemheader.hpp" +TINY_SYSTEM_HEADER + +#include + +#include + +#include "orm/macros/commonnamespace.hpp" + +TINYORM_BEGIN_COMMON_NAMESPACE + +namespace Orm::Utils +{ + + /*! Helpers library class. */ + class Helpers + { + Q_DISABLE_COPY(Helpers) + + public: + /*! Deleted default constructor, this is a pure library class. */ + Helpers() = delete; + /*! Deleted destructor. */ + ~Helpers() = delete; + + /*! Call the given callback with the given value then return the value. */ + template + static T &&tap(std::remove_reference_t &&value, + std::function &&callback = nullptr) noexcept; + }; + + /* public */ + + template + T &&Helpers::tap(std::remove_reference_t &&value, + std::function &&callback) noexcept + { + if (callback) + std::invoke(callback, value); + + return std::move(value); + } + +} // namespace Orm::Utils + +TINYORM_END_COMMON_NAMESPACE + +#endif // ORM_UTILS_HELPERS_HPP diff --git a/src/orm/schema/schemabuilder.cpp b/src/orm/schema/schemabuilder.cpp index 5258ebe01..47f71f4d7 100644 --- a/src/orm/schema/schemabuilder.cpp +++ b/src/orm/schema/schemabuilder.cpp @@ -32,13 +32,14 @@ QSqlQuery SchemaBuilder::dropDatabaseIfExists(const QString &/*unused*/) const void SchemaBuilder::create(const QString &table, const std::function &callback) const { - auto blueprint = createBlueprint(table); - - blueprint.create(); - - std::invoke(callback, blueprint); + build(Helpers::tap( + createBlueprint(table), + [&callback](auto &blueprint) + { + blueprint.create(); - build(std::move(blueprint)); + std::invoke(callback, blueprint); + })); } void SchemaBuilder::table(const QString &table, @@ -49,30 +50,26 @@ void SchemaBuilder::table(const QString &table, void SchemaBuilder::drop(const QString &table) const { - // CUR schema, create tap helper silverqx - auto blueprint = createBlueprint(table); - - blueprint.drop(); - - build(std::move(blueprint)); + build(Helpers::tap(createBlueprint(table), [](auto &blueprint) + { + blueprint.drop(); + })); } void SchemaBuilder::dropIfExists(const QString &table) const { - auto blueprint = createBlueprint(table); - - blueprint.dropIfExists(); - - build(std::move(blueprint)); + build(Helpers::tap(createBlueprint(table), [](auto &blueprint) + { + blueprint.dropIfExists(); + })); } void SchemaBuilder::rename(const QString &from, const QString &to) const { - auto blueprint = createBlueprint(from); - - blueprint.rename(to); - - build(std::move(blueprint)); + build(Helpers::tap(createBlueprint(from), [&to](auto &blueprint) + { + blueprint.rename(to); + })); } void SchemaBuilder::dropColumns(const QString &table,