diff --git a/include/ircd/db/where.h b/include/ircd/db/where.h index c872e0b68..7e1afd9a2 100644 --- a/include/ircd/db/where.h +++ b/include/ircd/db/where.h @@ -31,23 +31,24 @@ namespace ircd::db template struct ircd::db::where { - struct noop; struct equal; struct not_equal; + struct logical_not; + struct logical_and; + struct logical_or; + struct test; + struct noop; virtual bool operator()(const tuple &) const = 0; virtual ~where() noexcept = default; }; -template -struct ircd::db::where::noop -:ircd::db::where +namespace ircd::db { - bool operator()(const tuple &) const override - { - return true; - } -}; + template typename where::logical_or operator||(const where &, const where &); + template typename where::logical_and operator&&(const where &, const where &); + template typename where::logical_not operator!(const where &); +} template struct ircd::db::where::equal @@ -128,3 +129,102 @@ const return not_equal; }); } + +template +struct ircd::db::where::logical_and +:ircd::db::where +{ + const where *a, *b; + + bool operator()(const tuple &t) const override + { + return (*a)(t) && (*b)(t); + } + + logical_and(const where &a, const where &b) + :a{&a} + ,b{&b} + {} +}; + +template +typename ircd::db::where::logical_and +ircd::db::operator&&(const where &a, const where &b) +{ + return { a, b }; +} + +template +struct ircd::db::where::logical_or +:ircd::db::where +{ + const where *a, *b; + + bool operator()(const tuple &t) const override + { + return (*a)(t) || (*b)(t); + } + + logical_or(const where &a, const where &b) + :a{&a} + ,b{&b} + {} +}; + +template +typename ircd::db::where::logical_or +ircd::db::operator||(const where &a, const where &b) +{ + return { a, b }; +} + +template +struct ircd::db::where::logical_not +:ircd::db::where +{ + const where *a; + + bool operator()(const tuple &t) const override + { + return !(*a)(t); + } + + logical_not(const where &a) + :a{&a} + {} +}; + +template +typename ircd::db::where::logical_not +ircd::db::operator!(const where &a) +{ + return { a }; +} + +template +struct ircd::db::where::test +:ircd::db::where +{ + using function = std::function; + + function closure; + + bool operator()(const tuple &t) const override + { + return closure(t); + } + + test(function closure) + :closure{std::move(closure)} + {} +}; + +template +struct ircd::db::where::noop +:ircd::db::where +{ + bool operator()(const tuple &) const override + { + return true; + } +};