Continued implementing the conditions

This commit is contained in:
Dr. Patrick Urbanke
2025-05-03 16:06:32 +02:00
parent 2e63914ec7
commit b108491135
5 changed files with 164 additions and 5 deletions

View File

@@ -11,8 +11,8 @@ namespace sqlgen::dynamic {
struct Condition {
struct And {
Ref<Condition> op1;
Ref<Condition> op2;
Ref<Condition> cond1;
Ref<Condition> cond2;
};
struct Equals {
@@ -46,15 +46,15 @@ struct Condition {
};
struct Or {
Ref<Condition> op1;
Ref<Condition> op2;
Ref<Condition> cond1;
Ref<Condition> 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;
};

View File

@@ -0,0 +1,30 @@
#ifndef SQLGEN_TRANSPILATION_CONDITION_HPP_
#define SQLGEN_TRANSPILATION_CONDITION_HPP_
#include "conditions.hpp"
namespace sqlgen::transpilation {
template <class _ConditionType>
struct Condition {
using ConditionType = _ConditionType;
ConditionType condition;
};
template <class C1, class C2>
auto operator&&(const Condition<C1>& _lhs, const Condition<C2>& _rhs) {
return Condition<conditions::And<C1, C2>>{
.condition = conditions::And<C1, C2>{.lhs = _lhs.condition,
.rhs = _rhs.condition}};
}
template <class C1, class C2>
auto operator||(const Condition<C1>& _lhs, const Condition<C2>& _rhs) {
return Condition<conditions::Or<C1, C2>>{
.condition =
conditions::Or<C1, C2>{.lhs = _lhs.condition, .rhs = _rhs.condition}};
}
} // namespace sqlgen::transpilation
#endif

View File

@@ -0,0 +1,13 @@
#ifndef SQLGEN_TRANSPILATION_VALUE_HPP_
#define SQLGEN_TRANSPILATION_VALUE_HPP_
namespace sqlgen::transpilation {
template <class T>
struct Value {
T val;
};
} // namespace sqlgen::transpilation
#endif

View File

@@ -0,0 +1,56 @@
#ifndef SQLGEN_TRANSPILATION_CONDITIONS_HPP_
#define SQLGEN_TRANSPILATION_CONDITIONS_HPP_
namespace sqlgen::transpilation::conditions {
template <class CondType1, class CondType2>
struct And {
CondType1 cond1;
CondType2 cond2;
};
template <class OpType1, class OpType2>
struct Equals {
OpType1 op1;
OpType2 op2;
};
template <class OpType1, class OpType2>
struct GreaterEqual {
OpType1 op1;
OpType2 op2;
};
template <class OpType1, class OpType2>
struct GreaterThan {
OpType1 op1;
OpType2 op2;
};
template <class OpType1, class OpType2>
struct NotEquals {
OpType1 op1;
OpType2 op2;
};
template <class OpType1, class OpType2>
struct LesserEqual {
OpType1 op1;
OpType2 op2;
};
template <class OpType1, class OpType2>
struct LesserThan {
OpType1 op1;
OpType2 op2;
};
template <class CondType1, class CondType2>
struct Or {
CondType1 cond1;
CondType2 cond2;
};
} // namespace sqlgen::transpilation::conditions
#endif

View File

@@ -0,0 +1,60 @@
#ifndef SQLGEN_TRANSPILATION_TO_CONDITION_HPP_
#define SQLGEN_TRANSPILATION_TO_CONDITION_HPP_
#include <type_traits>
#include <vector>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../dynamic/Condition.hpp"
#include "Condition.hpp"
#include "conditions.hpp"
namespace sqlgen::transpilation {
template <class T, class ConditionType>
struct ToCondition;
template <class T, class CondType>
struct ToCondition<T, Condition<CondType>> {
dynamic::Condition operator()(const Condition<CondType>& _cond) const {
return ToCondition<T, std::remove_cvref_t<CondType>>{}(_cond.condition);
}
};
template <class T, class CondType1, class CondType2>
struct ToCondition<T, conditions::And<CondType1, CondType2>> {
dynamic::Condition operator()(
const conditions::And<CondType1, CondType2>& _cond) const {
return dynamic::Condition{
.val = dynamic::Condition::And{
.cond1 = Ref<dynamic::Condition>::make(
ToCondition<T, std::remove_cvref_t<CondType1>>{}(_cond.lhs)),
.cond2 = Ref<dynamic::Condition>::make(
ToCondition<T, std::remove_cvref_t<CondType2>>{}(_cond.rhs)),
}};
}
};
template <class T, class CondType1, class CondType2>
struct ToCondition<T, conditions::Or<CondType1, CondType2>> {
dynamic::Condition operator()(
const conditions::Or<CondType1, CondType2>& _cond) const {
return dynamic::Condition{
.val = dynamic::Condition::Or{
.cond1 = Ref<dynamic::Condition>::make(
ToCondition<T, std::remove_cvref_t<CondType1>>{}(_cond.lhs)),
.cond2 = Ref<dynamic::Condition>::make(
ToCondition<T, std::remove_cvref_t<CondType2>>{}(_cond.rhs)),
}};
}
};
template <class T, class ConditionType>
dynamic::Condition to_condition(const ConditionType& _cond) {
return ToCondition<T, std::remove_cvref_t<ConditionType>>{}(_cond);
}
} // namespace sqlgen::transpilation
#endif