From 54d45e97ae75a27fe1d7b3ade0a98d7cd3f2a1dc Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 15 Jan 2014 08:24:42 +0100 Subject: [PATCH] Added interpret support for remove() --- include/sqlpp11/remove.h | 29 ++++++++++++++++++----------- include/sqlpp11/table.h | 2 +- include/sqlpp11/using.h | 19 +++++++++++++++++++ tests/InterpretTest.cpp | 7 ++++++- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/include/sqlpp11/remove.h b/include/sqlpp11/remove.h index d84b0d31..0a27a726 100644 --- a/include/sqlpp11/remove.h +++ b/include/sqlpp11/remove.h @@ -57,6 +57,9 @@ namespace sqlpp static_assert(is_noop::value or is_using_t::value, "invalid 'Using' argument"); static_assert(is_noop::value or is_where_t::value, "invalid 'Where' argument"); + + // FIXME: We might want to have everywhere() or all() to indicate that everything is to be removed, same with update and select + template using set_using_t = remove_t; template @@ -134,17 +137,6 @@ namespace sqlpp return *this; } - - template - const remove_t& serialize(std::ostream& os, Db& db) const - { - os << "DELETE FROM "; - _table.serialize(os, db); - _using.serialize(os, db); - _where.serialize(os, db); - return *this; - } - static constexpr size_t _get_static_no_of_parameters() { return _parameter_list_t::size::value; @@ -174,6 +166,21 @@ namespace sqlpp Where _where; }; + template + struct interpreter_t> + { + using T = remove_t; + + static Context& _(const T& t, Context& context) + { + context << "DELETE FROM "; + interpret(t._table, context); + interpret(t._using, context); + interpret(t._where, context); + return context; + } + }; + template constexpr remove_t::type> remove_from(Table&& table) { diff --git a/include/sqlpp11/table.h b/include/sqlpp11/table.h index 98d7af70..1287868d 100644 --- a/include/sqlpp11/table.h +++ b/include/sqlpp11/table.h @@ -138,7 +138,7 @@ namespace sqlpp static Context& _(const T& t, Context& context) { - context << "table"; + context << T::_name_t::_get_name(); // FIXME: need a special rule for pseudo tables return context; } }; diff --git a/include/sqlpp11/using.h b/include/sqlpp11/using.h index 8bc7f3d1..10e5b905 100644 --- a/include/sqlpp11/using.h +++ b/include/sqlpp11/using.h @@ -79,6 +79,25 @@ namespace sqlpp detail::serializable_list _dynamic_tables; }; + template + struct interpreter_t> + { + using T = using_t; + + static Context& _(const T& t, Context& context) + { + if (sizeof...(Table) == 0 and t._dynamic_tables.empty()) + return context; + context << " USING "; + interpret_tuple(t._tables, ',', context); + if (sizeof...(Table) and not t._dynamic_tables.empty()) + context << ','; + interpret_serializable_list(t._dynamic_tables, ',', context); + return context; + } + }; + + } #endif diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index 6b57c575..9cefaea6 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -25,9 +25,10 @@ #include "TabSample.h" #include "MockDb.h" -#include #include +#include #include +#include #include #include @@ -70,5 +71,9 @@ int main() interpret(update(t).set(t.gamma = true), printer).flush(); interpret(update(t).set(t.gamma = true).where(t.beta.in("kaesekuchen", "cheesecake")), printer).flush(); + interpret(remove_from(t), printer).flush(); + interpret(remove_from(t).where(t.alpha == sqlpp::tvin(0)), printer).flush(); + interpret(remove_from(t).using_(t).where(t.alpha == sqlpp::tvin(0)), printer).flush(); + return 0; }