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..be260e78 100644 --- a/include/sqlpp11/insert_value_list.h +++ b/include/sqlpp11/insert_value_list.h @@ -128,6 +128,11 @@ namespace sqlpp { } + insert_list_data_t(std::tuple assignments) + : _assignments(assignments), _columns( columns_from_tuple(assignments) ), _values( values_from_tuple(assignments) ) + { + } + insert_list_data_t(const insert_list_data_t&) = default; insert_list_data_t(insert_list_data_t&&) = default; insert_list_data_t& operator=(const insert_list_data_t&) = default; @@ -140,6 +145,29 @@ 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 t ) -> decltype (_columns) + { + return decltype(_columns)(std::get(t)._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) + { + 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()"); @@ -514,6 +542,13 @@ namespace sqlpp return _set_impl(Check{}, 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, @@ -523,6 +558,15 @@ namespace sqlpp return _set_impl<_database_t>(Check{}, 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: template auto _columns_impl(Check, Columns... cols) const -> inconsistent; @@ -554,6 +598,14 @@ namespace sqlpp return {static_cast&>(*this), insert_list_data_t{assignments...}}; } + + template + auto _set_impl(consistent_t /*unused*/,std::tuple assignments) const + -> _new_statement_t> + { + return {static_cast&>(*this), + insert_list_data_t{assignments}}; + } }; }; diff --git a/include/sqlpp11/update_list.h b/include/sqlpp11/update_list.h index 768f9d13..662525b8 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) { } @@ -261,6 +261,14 @@ namespace sqlpp return _set_impl(Check{}, 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 auto dynamic_set(Assignments... assignments) const -> _new_statement_t, @@ -279,7 +287,15 @@ namespace sqlpp -> _new_statement_t> { return {static_cast&>(*this), - update_list_data_t{assignments...}}; + update_list_data_t{std::forward_as_tuple(assignments...)}}; + } + + template + auto _set_impl(consistent_t /*unused*/, std::tuple assignments) const + -> _new_statement_t> + { + return {static_cast&>(*this), + 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; }