0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Merge pull request #219 from cszawisza/set_tuple

add set(tuple<...>) implementation for insert and update statements
This commit is contained in:
Roland Bock 2018-02-19 21:47:46 +01:00 committed by GitHub
commit fb68e8a7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 12 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.idea
CMakeLists.txt.user

View File

@ -123,8 +123,8 @@ namespace sqlpp
template <typename Database, typename... Assignments>
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(assignments), _columns( columns_from_tuple(assignments) ), _values( values_from_tuple(assignments) )
{
}
@ -140,6 +140,31 @@ namespace sqlpp
std::tuple<rhs_t<Assignments>...> _values;
interpretable_list_t<Database> _dynamic_columns;
interpretable_list_t<Database> _dynamic_values;
private:
template< size_t... Indexes >
auto columns_from_tuple( detail::index_sequence<Indexes... >, std::tuple<Assignments ...> assignments ) -> decltype (_columns)
{
(void) assignments;
return decltype(_columns)(std::get<Indexes>(assignments)._lhs...);
}
auto columns_from_tuple(std::tuple<Assignments ...> assignments) -> decltype (_columns) {
const auto seq = detail::make_index_sequence<sizeof... (Assignments)>{};
return columns_from_tuple(seq, assignments);
}
template< size_t... Indexes >
auto values_from_tuple( detail::index_sequence<Indexes... >, std::tuple<Assignments ...> assignments ) -> decltype(_values)
{
(void) assignments;
return decltype(_values)(std::get<Indexes>(assignments)._rhs...);
}
auto values_from_tuple( std::tuple<Assignments ...> assignments ) -> decltype(_values)
{
const auto seq = detail::make_index_sequence<sizeof... (Assignments)>{};
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<check_insert_static_set_t<Assignments...>, insert_list_t<void, Assignments...>>
{
using Check = check_insert_static_set_t<Assignments...>;
return _set_impl<void>(Check{}, assignments...);
return _set_impl<void>(Check{}, std::make_tuple(assignments...));
}
template <typename... Assignments>
auto set(std::tuple<Assignments...> assignments) const
-> _new_statement_t<check_insert_static_set_t<Assignments...>, insert_list_t<void, Assignments...>>
{
using Check = check_insert_static_set_t<Assignments...>;
return _set_impl<void>(Check{}, assignments);
}
template <typename... Assignments>
auto dynamic_set(Assignments... assignments) const
-> _new_statement_t<check_insert_dynamic_set_t<_database_t, Assignments...>,
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 <typename... Assignments>
auto dynamic_set(std::tuple<Assignments...> assignments) const
-> _new_statement_t<check_insert_dynamic_set_t<_database_t, Assignments...>,
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<Check>;
template <typename Database, typename... Assignments>
auto _set_impl(consistent_t /*unused*/, Assignments... assignments) const
auto _set_impl(consistent_t /*unused*/,std::tuple<Assignments...> assignments) const
-> _new_statement_t<consistent_t, insert_list_t<Database, Assignments...>>
{
return {static_cast<const derived_statement_t<Policies>&>(*this),
insert_list_data_t<Database, Assignments...>{assignments...}};
insert_list_data_t<Database, Assignments...>{assignments}};
}
};
};

View File

@ -38,7 +38,7 @@ namespace sqlpp
template <typename Database, typename... Assignments>
struct update_list_data_t
{
update_list_data_t(Assignments... assignments) : _assignments(assignments...)
update_list_data_t(std::tuple<Assignments ...> assignments ) : _assignments(assignments)
{
}
@ -258,7 +258,15 @@ namespace sqlpp
-> _new_statement_t<check_update_static_set_t<Assignments...>, update_list_t<void, Assignments...>>
{
using Check = check_update_static_set_t<Assignments...>;
return _set_impl<void>(Check{}, assignments...);
return _set_impl<void>(Check{}, std::make_tuple(assignments...));
}
template <typename... Assignments>
auto set(std::tuple<Assignments...> assignments) const
-> _new_statement_t<check_update_static_set_t<Assignments...>, update_list_t<void, Assignments...>>
{
using Check = check_update_static_set_t<Assignments...>;
return _set_impl<void>(Check{}, assignments);
}
template <typename... Assignments>
@ -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<Check>;
template <typename Database, typename... Assignments>
auto _set_impl(consistent_t /*unused*/, Assignments... assignments) const
auto _set_impl(consistent_t /*unused*/, std::tuple<Assignments...> assignments) const
-> _new_statement_t<consistent_t, update_list_t<Database, Assignments...>>
{
return {static_cast<const derived_statement_t<Policies>&>(*this),
update_list_data_t<Database, Assignments...>{assignments...}};
update_list_data_t<Database, Assignments...>{assignments}};
}
};
};

View File

@ -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<sqlpp::integer>("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;
}

View File

@ -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<sqlpp::integer>("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;
}