Skip to content

Commit

Permalink
added tap helper
Browse files Browse the repository at this point in the history
It calls the given callback with the given value then return the value.

Also used it on few places as a proof that it works, the old code
looks cleaner though because it's flat.
  • Loading branch information
silverqx committed Jun 2, 2022
1 parent 51016a5 commit 67dfa12
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 29 deletions.
1 change: 1 addition & 0 deletions cmake/Modules/TinySources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/include.pri
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 4 additions & 0 deletions include/orm/schema/schemabuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions include/orm/tiny/tinybuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;

Expand Down Expand Up @@ -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<Model>(
newModelInstance(AttributeUtils::joinAttributesForFirstOr(
attributes, values, m_model.getKeyName())),
[](auto &newInstance)
{
newInstance.save();
});
}

template<typename Model>
Expand Down
52 changes: 52 additions & 0 deletions include/orm/utils/helpers.hpp
Original file line number Diff line number Diff line change
@@ -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 <QtGlobal>

#include <functional>

#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<typename T>
static T &&tap(std::remove_reference_t<T> &&value,
std::function<void(T &)> &&callback = nullptr) noexcept;
};

/* public */

template<typename T>
T &&Helpers::tap(std::remove_reference_t<T> &&value,
std::function<void(T &)> &&callback) noexcept
{
if (callback)
std::invoke(callback, value);

return std::move(value);
}

} // namespace Orm::Utils

TINYORM_END_COMMON_NAMESPACE

#endif // ORM_UTILS_HELPERS_HPP
41 changes: 19 additions & 22 deletions src/orm/schema/schemabuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ QSqlQuery SchemaBuilder::dropDatabaseIfExists(const QString &/*unused*/) const
void SchemaBuilder::create(const QString &table,
const std::function<void(Blueprint &)> &callback) const
{
auto blueprint = createBlueprint(table);

blueprint.create();

std::invoke(callback, blueprint);
build(Helpers::tap<Blueprint>(
createBlueprint(table),
[&callback](auto &blueprint)
{
blueprint.create();

build(std::move(blueprint));
std::invoke(callback, blueprint);
}));
}

void SchemaBuilder::table(const QString &table,
Expand All @@ -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<Blueprint>(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<Blueprint>(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<Blueprint>(createBlueprint(from), [&to](auto &blueprint)
{
blueprint.rename(to);
}));
}

void SchemaBuilder::dropColumns(const QString &table,
Expand Down

0 comments on commit 67dfa12

Please sign in to comment.