From d36037472fb97bfdc5f5783b0eed38550560fee5 Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 4 Jun 2014 09:26:10 +0200 Subject: [PATCH] Migrated update to generic statement --- include/sqlpp11/update.h | 10 +-- include/sqlpp11/vendor/update_list.h | 130 ++++++++++++++++++--------- tests/CMakeLists.txt | 4 +- tests/UpdateTest.cpp | 4 +- 4 files changed, 97 insertions(+), 51 deletions(-) diff --git a/include/sqlpp11/update.h b/include/sqlpp11/update.h index 1368d6a4..7c9ab963 100644 --- a/include/sqlpp11/update.h +++ b/include/sqlpp11/update.h @@ -27,6 +27,8 @@ #ifndef SQLPP_UPDATE_H #define SQLPP_UPDATE_H +#include + #include #include #include @@ -34,13 +36,10 @@ #include #include #include -#include - -#include -#include namespace sqlpp { +#if 0 template struct update_t; @@ -178,9 +177,10 @@ namespace sqlpp } }; } +#endif template - using blank_update_t = update_t; diff --git a/include/sqlpp11/vendor/update_list.h b/include/sqlpp11/vendor/update_list.h index a4755658..8cd99bea 100644 --- a/include/sqlpp11/vendor/update_list.h +++ b/include/sqlpp11/vendor/update_list.h @@ -36,6 +36,24 @@ namespace sqlpp { namespace vendor { + // UPDATE ASSIGNMENTS DATA + template + struct update_list_data_t + { + update_list_data_t(Assignments... assignments): + _assignments(assignments...) + {} + + update_list_data_t(const update_list_data_t&) = default; + update_list_data_t(update_list_data_t&&) = default; + update_list_data_t& operator=(const update_list_data_t&) = default; + update_list_data_t& operator=(update_list_data_t&&) = default; + ~update_list_data_t() = default; + + std::tuple _assignments; + typename vendor::interpretable_list_t _dynamic_assignments; + }; + // UPDATE ASSIGNMENTS template struct update_list_t @@ -61,57 +79,70 @@ namespace sqlpp static_assert(::sqlpp::detail::is_subset_of<_value_table_set, _column_table_set>::value, "set() contains values from foreign tables"); */ - update_list_t& _update_list() { return *this; } + // Data + using _data_t = update_list_data_t; - update_list_t(Assignments... assignments): - _assignments(assignments...) - {} - - update_list_t(const update_list_t&) = default; - update_list_t(update_list_t&&) = default; - update_list_t& operator=(const update_list_t&) = default; - update_list_t& operator=(update_list_t&&) = default; - ~update_list_t() = default; - - template - struct _methods_t + // Member implementation with data and methods + template + struct _impl_t { template - void add_set_ntc(Assignment assignment) + void add_ntc(Assignment assignment) { - add_set(assignment); + add(assignment); } template - void add_set(Assignment assignment) + void add(Assignment assignment) { - static_assert(_is_dynamic::value, "add_set must not be called for static from()"); - static_assert(is_assignment_t::value, "invalid assignment argument in add_set()"); - static_assert(sqlpp::detail::not_t::value, "add_set() argument must not be updated"); - static_assert(TableCheckRequired::value or Policies::template _no_unknown_tables::value, "assignment uses tables unknown to this statement in add_set()"); + static_assert(_is_dynamic::value, "add must not be called for static from()"); + static_assert(is_assignment_t::value, "invalid assignment argument in add()"); + static_assert(sqlpp::detail::not_t::value, "add() argument must not be updated"); + static_assert(TableCheckRequired::value or Policies::template _no_unknown_tables::value, "assignment uses tables unknown to this statement in add()"); using ok = ::sqlpp::detail::all_t< _is_dynamic::value, is_assignment_t::value, not must_not_update_t::value>; - _add_set_impl(assignment, ok()); // dispatch to prevent compile messages after the static_assert + _add_impl(assignment, ok()); // dispatch to prevent compile messages after the static_assert } private: template - void _add_set_impl(Assignment assignment, const std::true_type&) + void _add_impl(Assignment assignment, const std::true_type&) { - return static_cast(this)->_update_list()._dynamic_assignments.emplace_back(assignment); + return _data._dynamic_assignments.emplace_back(assignment); } template - void _add_set_impl(Assignment assignment, const std::false_type&); + void _add_impl(Assignment assignment, const std::false_type&); + public: + _data_t _data; }; + // Member template for adding the named member to a statement + template + struct _member_t + { + using _data_t = update_list_data_t; - std::tuple _assignments; - typename vendor::interpretable_list_t _dynamic_assignments; + _impl_t assignments; + _impl_t& operator()() { return assignments; } + const _impl_t& operator()() const { return assignments; } + + template + static auto _get_member(T t) -> decltype(t.assignments) + { + return t.assignments; + } + }; + + // Additional methods for the statement + template + struct _methods_t + { + }; }; struct no_update_list_t @@ -119,6 +150,33 @@ namespace sqlpp using _traits = make_traits; using _recursive_traits = make_recursive_traits<>; + // Data + using _data_t = no_data_t; + + // Member implementation with data and methods + template + struct _impl_t + { + _data_t _data; + }; + + // Member template for adding the named member to a statement + template + struct _member_t + { + using _data_t = no_data_t; + + _impl_t no_assignments; + _impl_t& operator()() { return no_assignments; } + const _impl_t& operator()() const { return no_assignments; } + + template + static auto _get_member(T t) -> decltype(t.no_assignments) + { + return t.no_assignments; + } + }; + template struct _methods_t { @@ -130,7 +188,7 @@ namespace sqlpp auto set(Args... args) -> _new_statement_t> { - return { *static_cast(this), update_list_t{args...} }; + return { *static_cast(this), update_list_data_t{args...} }; } template @@ -138,16 +196,16 @@ namespace sqlpp -> _new_statement_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_set must not be called in a static statement"); - return { *static_cast(this), vendor::update_list_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::update_list_data_t<_database_t, Args...>{args...} }; } }; }; // Interpreters template - struct serializer_t> + struct serializer_t> { - using T = update_list_t; + using T = update_list_data_t; static Context& _(const T& t, Context& context) { @@ -159,18 +217,6 @@ namespace sqlpp return context; } }; - - template - struct serializer_t - { - using T = no_update_list_t; - - static Context& _(const T& t, Context& context) - { - return context; - } - }; - } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d7fcb902..280fe484 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,8 +8,8 @@ endmacro () #build_and_run(InterpretTest) #build_and_run(InsertTest) -build_and_run(RemoveTest) -#build_and_run(UpdateTest) +#build_and_run(RemoveTest) +build_and_run(UpdateTest) #build_and_run(SelectTest) #build_and_run(SelectTypeTest) #build_and_run(FunctionTest) diff --git a/tests/UpdateTest.cpp b/tests/UpdateTest.cpp index fcf0c6d3..cc75f897 100644 --- a/tests/UpdateTest.cpp +++ b/tests/UpdateTest.cpp @@ -61,8 +61,8 @@ int main() serialize(update(t).set(t.gamma = false).where(t.beta != "transparent"), printer).str(); serialize(update(t).set(t.beta = "opaque").where(t.beta != t.beta), printer).str(); auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where(); - u.add_set(t.gamma = false); - u.add_where(t.gamma != false); + u.assignments.add(t.gamma = false); + u.where.add(t.gamma != false); serialize(u, printer).str(); db(u);