From afcc62fc75ef3610e60bb46a9a576ef4ed385e38 Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 15 Jan 2014 19:42:14 +0100 Subject: [PATCH] Added interpreters for a bunch of functions and table aliases --- include/sqlpp11/any.h | 24 +++++---- include/sqlpp11/avg.h | 24 +++++---- include/sqlpp11/count.h | 24 +++++---- include/sqlpp11/exists.h | 26 ++++++---- include/sqlpp11/from.h | 10 ---- include/sqlpp11/functions.h | 57 ++++++++++++--------- include/sqlpp11/max.h | 24 +++++---- include/sqlpp11/min.h | 24 +++++---- include/sqlpp11/some.h | 24 +++++---- include/sqlpp11/sum.h | 24 +++++---- include/sqlpp11/table.h | 38 +------------- include/sqlpp11/table_alias.h | 88 ++++++++++++++++++++++++++++++++ include/sqlpp11/text.h | 12 ----- include/sqlpp11/verbatim_table.h | 19 ++++--- include/sqlpp11/where.h | 5 -- tests/InterpretTest.cpp | 17 ++++++ 16 files changed, 266 insertions(+), 174 deletions(-) create mode 100644 include/sqlpp11/table_alias.h diff --git a/include/sqlpp11/any.h b/include/sqlpp11/any.h index 12393dff..35883da3 100644 --- a/include/sqlpp11/any.h +++ b/include/sqlpp11/any.h @@ -71,20 +71,24 @@ namespace sqlpp any_t& operator=(any_t&&) = default; ~any_t() = default; - template - void serialize(std::ostream& os, Db& db) const - { - static_assert(Db::_supports_any, "any() not supported by current database"); - os << "ANY("; - _select.serialize(os, db); - os << ")"; - } - - private: Select _select; }; } + template + struct interpreter_t> + { + using T = detail::any_t; + + static Context& _(const T& t, Context& context) + { + context << "EXISTS("; + interpret(t._select, context); + context << ")"; + return context; + } + }; + + template auto exists(T&& t) -> typename detail::exists_t::type> { diff --git a/include/sqlpp11/from.h b/include/sqlpp11/from.h index a20f2e60..91d73c04 100644 --- a/include/sqlpp11/from.h +++ b/include/sqlpp11/from.h @@ -61,16 +61,6 @@ namespace sqlpp _dynamic_tables.emplace_back(std::forward(table)); } - template - void serialize(std::ostream& os, Db& db) const - { - if (sizeof...(TableOrJoin) == 0 and _dynamic_tables.empty()) - return; - os << " FROM "; - detail::serialize_tuple(os, db, _tables, ','); - _dynamic_tables.serialize(os, db, sizeof...(TableOrJoin) == 0); - } - std::tuple _tables; detail::serializable_list _dynamic_tables; }; diff --git a/include/sqlpp11/functions.h b/include/sqlpp11/functions.h index 8b00e25b..645b04a8 100644 --- a/include/sqlpp11/functions.h +++ b/include/sqlpp11/functions.h @@ -27,10 +27,8 @@ #ifndef SQLPP_FUNCTIONS_H #define SQLPP_FUNCTIONS_H -#include #include #include -#include #include #include #include @@ -57,12 +55,6 @@ namespace sqlpp { using _value_type = ValueType; - template - void serialize(std::ostream& os, const Db& db) const - { - os << _verbatim; - } - verbatim_t(const std::string& verbatim): _verbatim(verbatim) {} verbatim_t(std::string&& verbatim): _verbatim(std::forward(verbatim)) {} verbatim_t(const verbatim_t&) = default; @@ -74,6 +66,18 @@ namespace sqlpp std::string _verbatim; }; + template + struct interpreter_t> + { + using T = verbatim_t; + + static Context& _(const T& t, Context& context) + { + context << t._verbatim; + return context; + } + }; + template auto verbatim(StringType&& s) -> verbatim_t { @@ -93,26 +97,31 @@ namespace sqlpp { using _container_t = Container; using _value_type = typename operand_t::type::_value_type; - using _iterator = decltype(std::begin(std::declval<_container_t>())); - - template - void serialize(std::ostream& os, const Db& db) const - { - bool first = true; - for (const auto& entry: _container) - { - if (first) - first = false; - else - os << ','; - - value(entry).serialize(os, db); - } - } _container_t _container; }; + template + struct interpreter_t> + { + using T = value_list_t; + + static Context& _(const T& t, Context& context) + { + bool first = true; + for (const auto& entry: t._container) + { + if (first) + first = false; + else + context << ','; + + interpret(value(entry), context); + } + return context; + } + }; + template auto value_list(Container&& c) -> value_list_t::type> { diff --git a/include/sqlpp11/max.h b/include/sqlpp11/max.h index 1310e674..524d84e8 100644 --- a/include/sqlpp11/max.h +++ b/include/sqlpp11/max.h @@ -70,20 +70,24 @@ namespace sqlpp max_t& operator=(max_t&&) = default; ~max_t() = default; - template - void serialize(std::ostream& os, Db& db) const - { - static_assert(Db::_supports_max, "max not supported by current database"); - os << "MAX("; - _expr.serialize(os, db); - os << ")"; - } - - private: Expr _expr; }; } + template + struct interpreter_t> + { + using T = detail::max_t; + + static Context& _(const T& t, Context& context) + { + context << "MAX("; + interpret(t._expr, context); + context << ")"; + return context; + } + }; + template auto max(T&& t) -> typename detail::max_t::type> { diff --git a/include/sqlpp11/min.h b/include/sqlpp11/min.h index 2e0f6205..20ca7f54 100644 --- a/include/sqlpp11/min.h +++ b/include/sqlpp11/min.h @@ -70,20 +70,24 @@ namespace sqlpp min_t& operator=(min_t&&) = default; ~min_t() = default; - template - void serialize(std::ostream& os, Db& db) const - { - static_assert(Db::_supports_min, "min not supported by current database"); - os << "MIN("; - _expr.serialize(os, db); - os << ")"; - } - - private: Expr _expr; }; } + template + struct interpreter_t> + { + using T = detail::min_t; + + static Context& _(const T& t, Context& context) + { + context << "MIN("; + interpret(t._expr, context); + context << ")"; + return context; + } + }; + template auto min(T&& t) -> typename detail::min_t::type> { diff --git a/include/sqlpp11/some.h b/include/sqlpp11/some.h index f4c8bd40..768279c9 100644 --- a/include/sqlpp11/some.h +++ b/include/sqlpp11/some.h @@ -71,20 +71,24 @@ namespace sqlpp some_t& operator=(some_t&&) = default; ~some_t() = default; - template - void serialize(std::ostream& os, Db& db) const - { - static_assert(Db::_supports_some, "some() not supported by current database"); - os << "SOME("; - _select.serialize(os, db); - os << ")"; - } - - private: Select _select; }; } + template + struct interpreter_t> + { + using T = detail::some_t