diff --git a/include/sqlpp11/detail/serializable_list.h b/include/sqlpp11/detail/serializable_list.h index 67bc0c48..215497ed 100644 --- a/include/sqlpp11/detail/serializable_list.h +++ b/include/sqlpp11/detail/serializable_list.h @@ -30,6 +30,8 @@ #include #include +// FIXME: Needs to leave detail namespace +// FIXME: serializable is not a very good name any more... namespace sqlpp { namespace detail @@ -55,29 +57,6 @@ namespace sqlpp _serializables.emplace_back(std::forward(expr)); } - void serialize(std::ostream& os, Db& db, bool first) const - { - for (const auto entry : _serializables) - { - if (not first) - os << ','; - entry.serialize(os, db); - first = false; - } - } - - template - void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const - { - for (const auto entry : _serializables) - { - if (not first) - os << separator; - entry.serialize(os, db); - first = false; - } - } - }; template<> @@ -86,29 +65,61 @@ namespace sqlpp template void emplace_back(const T&) {} - constexpr std::size_t size() const + static constexpr std::size_t size() { return 0; } - constexpr bool empty() const + static constexpr bool empty() { return true; } - template - void serialize(std::ostream&, Db&, bool) const - { - } - - template - void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const - { - } - }; } + + template + struct serializable_list_interpreter_t + { + using T = List; + + template + static Context& _(const T& t, const Separator& separator, Context& context) + { + bool first = true; + for (const auto entry : t._serializables) + { + if (not first) + { + context << separator; + first = false; + } + interpret(t, context); + } + return context; + } + }; + + template + struct serializable_list_interpreter_t> + { + using T = detail::serializable_list; + + template + static Context& _(const T& t, const Separator& separator, Context& context) + { + return context; + } + }; + + template + auto interpret_serializable_list(const T& t, const Separator& separator, Context& context) + -> decltype(serializable_list_interpreter_t::type, typename std::decay::type>::_(t, separator, context)) + { + return serializable_list_interpreter_t::type, typename std::decay::type>::_(t, separator, context); + } + } #endif diff --git a/include/sqlpp11/from.h b/include/sqlpp11/from.h index 7e82de70..a20f2e60 100644 --- a/include/sqlpp11/from.h +++ b/include/sqlpp11/from.h @@ -74,6 +74,25 @@ namespace sqlpp std::tuple _tables; detail::serializable_list _dynamic_tables; }; + + template + struct interpreter_t> + { + using T = from_t; + + static Context& _(const T& t, Context& context) + { + if (sizeof...(TableOrJoin) == 0 and t._dynamic_tables.empty()) + return context; + context << " FROM "; + interpret_tuple(t._tables, ',', context); + if (sizeof...(TableOrJoin) and not t._dynamic_tables.empty()) + context << ','; + interpret_serializable_list(t._dynamic_tables, ',', context); + return context; + } + }; + } #endif diff --git a/include/sqlpp11/interpreter.h b/include/sqlpp11/interpreter.h index 3daf6da3..7bd1c206 100644 --- a/include/sqlpp11/interpreter.h +++ b/include/sqlpp11/interpreter.h @@ -32,7 +32,7 @@ namespace sqlpp { - template + template struct interpreter_t { static void _(const T& t, Context& context) diff --git a/include/sqlpp11/select_expression_list.h b/include/sqlpp11/select_expression_list.h index 30b0b381..e7191c7f 100644 --- a/include/sqlpp11/select_expression_list.h +++ b/include/sqlpp11/select_expression_list.h @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include #include @@ -185,7 +185,7 @@ namespace sqlpp static Context& _(const T& t, Context& context) { interpret_tuple(t._expressions, ',', context); - if (not t._dynamic_expressions.empty()) + if (sizeof...(NamedExpr) and not t._dynamic_expressions.empty()) context << ','; interpret(t._dynamic_expressions, context); return context; diff --git a/include/sqlpp11/select_pseudo_table.h b/include/sqlpp11/select_pseudo_table.h index 78932454..15049b90 100644 --- a/include/sqlpp11/select_pseudo_table.h +++ b/include/sqlpp11/select_pseudo_table.h @@ -44,7 +44,7 @@ namespace sqlpp typename Select, typename... NamedExpr > - struct select_pseudo_table_t: public sqlpp::table_base_t, select_column_spec_t...> { diff --git a/include/sqlpp11/table_base.h b/include/sqlpp11/table.h similarity index 88% rename from include/sqlpp11/table_base.h rename to include/sqlpp11/table.h index 0f832d25..98d7af70 100644 --- a/include/sqlpp11/table_base.h +++ b/include/sqlpp11/table.h @@ -24,8 +24,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SQLPP_TABLE_BASE_H -#define SQLPP_TABLE_BASE_H +#ifndef SQLPP_TABLE_H +#define SQLPP_TABLE_H #include #include @@ -37,8 +37,10 @@ namespace sqlpp { + struct table_base_t {}; + template - struct table_base_t: public ColumnSpec::_name_t::template _member_t>... + struct table_t: public table_base_t, public ColumnSpec::_name_t::template _member_t>... { using _table_set = detail::set; // Hint need a set here to be similar to a join (which always represents more than one table) using _all_columns = typename detail::make_set...>::type; @@ -117,12 +119,10 @@ namespace sqlpp return {*static_cast(this)}; } - template - void serialize(std::ostream& os, Db& db) const - { - static_cast(this)->serialize_impl(os, db); - } - + const Table& ref() const + { + return *static_cast(this); + } }; template @@ -131,6 +131,19 @@ namespace sqlpp return {}; } + template + struct interpreter_t::value, void>::type> + { + using T = X; + + static Context& _(const T& t, Context& context) + { + context << "table"; + return context; + } + }; + + } #endif diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index 229b4933..7e02922e 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -43,6 +43,7 @@ int main() interpret(t.beta + "kaesekuchen", printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta), printer).flush(); + interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t), printer).flush(); return 0; } diff --git a/tests/TabSample.h b/tests/TabSample.h index 4c1de4d3..c962675d 100644 --- a/tests/TabSample.h +++ b/tests/TabSample.h @@ -27,7 +27,7 @@ #ifndef SQLPP_TAB_SAMPLE_H #define SQLPP_TAB_SAMPLE_H -#include +#include #include @@ -72,7 +72,7 @@ namespace TabFoo_ }; } -struct TabFoo: sqlpp::table_base_t< +struct TabFoo: sqlpp::table_t< TabFoo, TabFoo_::Epsilon, TabFoo_::Omega @@ -166,7 +166,7 @@ namespace TabSample_ }; } -struct TabSample: sqlpp::table_base_t< +struct TabSample: sqlpp::table_t< TabSample, TabSample_::Alpha, TabSample_::Beta,