diff --git a/.gitignore b/.gitignore index 485dee64..c844e265 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +CMakeLists.txt.user diff --git a/include/sqlpp11/insert_value_list.h b/include/sqlpp11/insert_value_list.h index 2c67b021..feceddc3 100644 --- a/include/sqlpp11/insert_value_list.h +++ b/include/sqlpp11/insert_value_list.h @@ -123,8 +123,8 @@ namespace sqlpp template struct insert_list_data_t { - insert_list_data_t(Assignments... assignments) - : _assignments(assignments...), _columns(assignments._lhs...), _values(assignments._rhs...) + insert_list_data_t(std::tuple assignments) + : _assignments(assignments), _columns( columns_from_tuple(assignments) ), _values( values_from_tuple(assignments) ) { } @@ -140,6 +140,31 @@ namespace sqlpp std::tuple...> _values; interpretable_list_t _dynamic_columns; interpretable_list_t _dynamic_values; + private: + template< size_t... Indexes > + auto columns_from_tuple( detail::index_sequence, std::tuple assignments ) -> decltype (_columns) + { + (void) assignments; + return decltype(_columns)(std::get(assignments)._lhs...); + } + + auto columns_from_tuple(std::tuple assignments) -> decltype (_columns) { + const auto seq = detail::make_index_sequence{}; + return columns_from_tuple(seq, assignments); + } + + template< size_t... Indexes > + auto values_from_tuple( detail::index_sequence, std::tuple assignments ) -> decltype(_values) + { + (void) assignments; + return decltype(_values)(std::get(assignments)._rhs...); + } + + auto values_from_tuple( std::tuple assignments ) -> decltype(_values) + { + const auto seq = detail::make_index_sequence{}; + return values_from_tuple(seq, assignments); + } }; SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_assignments_t, "at least one argument is not an assignment in set()"); @@ -511,16 +536,32 @@ namespace sqlpp -> _new_statement_t, insert_list_t> { using Check = check_insert_static_set_t; - return _set_impl(Check{}, assignments...); + return _set_impl(Check{}, std::make_tuple(assignments...)); } + template + auto set(std::tuple assignments) const + -> _new_statement_t, insert_list_t> + { + using Check = check_insert_static_set_t; + return _set_impl(Check{}, assignments); + } template auto dynamic_set(Assignments... assignments) const -> _new_statement_t, insert_list_t<_database_t, Assignments...>> { using Check = check_insert_dynamic_set_t<_database_t, Assignments...>; - return _set_impl<_database_t>(Check{}, assignments...); + return _set_impl<_database_t>(Check{}, std::make_tuple(assignments...)); + } + + template + auto dynamic_set(std::tuple assignments) const + -> _new_statement_t, + insert_list_t<_database_t, Assignments...>> + { + using Check = check_insert_dynamic_set_t<_database_t, Assignments...>; + return _set_impl<_database_t>(Check{}, assignments); } private: @@ -548,11 +589,11 @@ namespace sqlpp auto _set_impl(Check, Assignments... assignments) const -> inconsistent; template - auto _set_impl(consistent_t /*unused*/, Assignments... assignments) const + auto _set_impl(consistent_t /*unused*/,std::tuple assignments) const -> _new_statement_t> { return {static_cast&>(*this), - insert_list_data_t{assignments...}}; + insert_list_data_t{assignments}}; } }; }; diff --git a/include/sqlpp11/update_list.h b/include/sqlpp11/update_list.h index 768f9d13..b27d008e 100644 --- a/include/sqlpp11/update_list.h +++ b/include/sqlpp11/update_list.h @@ -38,7 +38,7 @@ namespace sqlpp template struct update_list_data_t { - update_list_data_t(Assignments... assignments) : _assignments(assignments...) + update_list_data_t(std::tuple assignments ) : _assignments(assignments) { } @@ -258,7 +258,15 @@ namespace sqlpp -> _new_statement_t, update_list_t> { using Check = check_update_static_set_t; - return _set_impl(Check{}, assignments...); + return _set_impl(Check{}, std::make_tuple(assignments...)); + } + + template + auto set(std::tuple assignments) const + -> _new_statement_t, update_list_t> + { + using Check = check_update_static_set_t; + return _set_impl(Check{}, assignments); } template @@ -267,7 +275,7 @@ namespace sqlpp update_list_t<_database_t, Assignments...>> { using Check = check_update_dynamic_set_t<_database_t, Assignments...>; - return _set_impl<_database_t>(Check{}, assignments...); + return _set_impl<_database_t>(Check{}, std::make_tuple(assignments...)); } private: @@ -275,11 +283,11 @@ namespace sqlpp auto _set_impl(Check, Assignments... assignments) const -> inconsistent; template - auto _set_impl(consistent_t /*unused*/, Assignments... assignments) const + auto _set_impl(consistent_t /*unused*/, std::tuple assignments) const -> _new_statement_t> { return {static_cast&>(*this), - update_list_data_t{assignments...}}; + update_list_data_t{assignments}}; } }; }; diff --git a/tests/Insert.cpp b/tests/Insert.cpp index 0c6e61b7..43b7c1d0 100644 --- a/tests/Insert.cpp +++ b/tests/Insert.cpp @@ -71,10 +71,15 @@ int Insert(int, char* []) db(multi_insert); + auto values=[&t](){ + return std::make_tuple(t.gamma = true, t.delta = sqlpp::tvin(0)); + }; + db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::verbatim("17+4"))); db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::null)); db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::default_value)); db(insert_into(t).set(t.gamma = true, t.delta = sqlpp::tvin(0))); + db(insert_into(t).set(values())); return 0; } diff --git a/tests/Update.cpp b/tests/Update.cpp index 7a4bf569..78d181e9 100644 --- a/tests/Update.cpp +++ b/tests/Update.cpp @@ -63,11 +63,15 @@ int Update(int, char* []) db(u); + auto values=[&t](){ + return std::make_tuple(t.delta += t.alpha, t.beta = "no cake this time"); + }; + db(update(t).set(t.delta = sqlpp::verbatim("17+4")).unconditionally()); db(update(t).set(t.delta = sqlpp::null).unconditionally()); db(update(t).set(t.delta = sqlpp::default_value).unconditionally()); db(update(t).set(t.delta += t.alpha * 2, t.beta += " and cake").unconditionally()); - + db(update(t).set(values()).unconditionally()); return 0; }