From b108491135cff0dfa9e24f56a33eb1ebea262b9b Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 3 May 2025 16:06:32 +0200 Subject: [PATCH] Continued implementing the conditions --- include/sqlgen/dynamic/Condition.hpp | 10 ++-- include/sqlgen/transpilation/Condition.hpp | 30 ++++++++++ include/sqlgen/transpilation/Value.hpp | 13 ++++ include/sqlgen/transpilation/conditions.hpp | 56 +++++++++++++++++ include/sqlgen/transpilation/to_condition.hpp | 60 +++++++++++++++++++ 5 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 include/sqlgen/transpilation/Condition.hpp create mode 100644 include/sqlgen/transpilation/Value.hpp create mode 100644 include/sqlgen/transpilation/conditions.hpp create mode 100644 include/sqlgen/transpilation/to_condition.hpp diff --git a/include/sqlgen/dynamic/Condition.hpp b/include/sqlgen/dynamic/Condition.hpp index dd1c948..eb8fa7b 100644 --- a/include/sqlgen/dynamic/Condition.hpp +++ b/include/sqlgen/dynamic/Condition.hpp @@ -11,8 +11,8 @@ namespace sqlgen::dynamic { struct Condition { struct And { - Ref op1; - Ref op2; + Ref cond1; + Ref cond2; }; struct Equals { @@ -46,15 +46,15 @@ struct Condition { }; struct Or { - Ref op1; - Ref op2; + Ref cond1; + Ref cond2; }; using ReflectionType = rfl::TaggedUnion<"what", And, Equals, GreaterEqual, GreaterThan, NotEquals, LesserEqual, LesserThan, Or>; - ReflectionType reflection() const { return val; } + const ReflectionType& reflection() const { return val; } ReflectionType val; }; diff --git a/include/sqlgen/transpilation/Condition.hpp b/include/sqlgen/transpilation/Condition.hpp new file mode 100644 index 0000000..69f5e7b --- /dev/null +++ b/include/sqlgen/transpilation/Condition.hpp @@ -0,0 +1,30 @@ +#ifndef SQLGEN_TRANSPILATION_CONDITION_HPP_ +#define SQLGEN_TRANSPILATION_CONDITION_HPP_ + +#include "conditions.hpp" + +namespace sqlgen::transpilation { + +template +struct Condition { + using ConditionType = _ConditionType; + ConditionType condition; +}; + +template +auto operator&&(const Condition& _lhs, const Condition& _rhs) { + return Condition>{ + .condition = conditions::And{.lhs = _lhs.condition, + .rhs = _rhs.condition}}; +} + +template +auto operator||(const Condition& _lhs, const Condition& _rhs) { + return Condition>{ + .condition = + conditions::Or{.lhs = _lhs.condition, .rhs = _rhs.condition}}; +} + +} // namespace sqlgen::transpilation + +#endif diff --git a/include/sqlgen/transpilation/Value.hpp b/include/sqlgen/transpilation/Value.hpp new file mode 100644 index 0000000..de49d0d --- /dev/null +++ b/include/sqlgen/transpilation/Value.hpp @@ -0,0 +1,13 @@ +#ifndef SQLGEN_TRANSPILATION_VALUE_HPP_ +#define SQLGEN_TRANSPILATION_VALUE_HPP_ + +namespace sqlgen::transpilation { + +template +struct Value { + T val; +}; + +} // namespace sqlgen::transpilation + +#endif diff --git a/include/sqlgen/transpilation/conditions.hpp b/include/sqlgen/transpilation/conditions.hpp new file mode 100644 index 0000000..c6c202e --- /dev/null +++ b/include/sqlgen/transpilation/conditions.hpp @@ -0,0 +1,56 @@ +#ifndef SQLGEN_TRANSPILATION_CONDITIONS_HPP_ +#define SQLGEN_TRANSPILATION_CONDITIONS_HPP_ + +namespace sqlgen::transpilation::conditions { + +template +struct And { + CondType1 cond1; + CondType2 cond2; +}; + +template +struct Equals { + OpType1 op1; + OpType2 op2; +}; + +template +struct GreaterEqual { + OpType1 op1; + OpType2 op2; +}; + +template +struct GreaterThan { + OpType1 op1; + OpType2 op2; +}; + +template +struct NotEquals { + OpType1 op1; + OpType2 op2; +}; + +template +struct LesserEqual { + OpType1 op1; + OpType2 op2; +}; + +template +struct LesserThan { + OpType1 op1; + OpType2 op2; +}; + +template +struct Or { + CondType1 cond1; + CondType2 cond2; +}; + +} // namespace sqlgen::transpilation::conditions + +#endif diff --git a/include/sqlgen/transpilation/to_condition.hpp b/include/sqlgen/transpilation/to_condition.hpp new file mode 100644 index 0000000..88c2096 --- /dev/null +++ b/include/sqlgen/transpilation/to_condition.hpp @@ -0,0 +1,60 @@ +#ifndef SQLGEN_TRANSPILATION_TO_CONDITION_HPP_ +#define SQLGEN_TRANSPILATION_TO_CONDITION_HPP_ + +#include +#include + +#include "../Ref.hpp" +#include "../Result.hpp" +#include "../dynamic/Condition.hpp" +#include "Condition.hpp" +#include "conditions.hpp" + +namespace sqlgen::transpilation { + +template +struct ToCondition; + +template +struct ToCondition> { + dynamic::Condition operator()(const Condition& _cond) const { + return ToCondition>{}(_cond.condition); + } +}; + +template +struct ToCondition> { + dynamic::Condition operator()( + const conditions::And& _cond) const { + return dynamic::Condition{ + .val = dynamic::Condition::And{ + .cond1 = Ref::make( + ToCondition>{}(_cond.lhs)), + .cond2 = Ref::make( + ToCondition>{}(_cond.rhs)), + }}; + } +}; + +template +struct ToCondition> { + dynamic::Condition operator()( + const conditions::Or& _cond) const { + return dynamic::Condition{ + .val = dynamic::Condition::Or{ + .cond1 = Ref::make( + ToCondition>{}(_cond.lhs)), + .cond2 = Ref::make( + ToCondition>{}(_cond.rhs)), + }}; + } +}; + +template +dynamic::Condition to_condition(const ConditionType& _cond) { + return ToCondition>{}(_cond); +} + +} // namespace sqlgen::transpilation + +#endif