0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 12:29:41 +08:00

add set(tuple<...>) implementation for insert and update statements

This commit is contained in:
Bartosz Wieczorek 2018-02-15 07:55:43 +01:00
parent f963a91a8e
commit e50ee42364
5 changed files with 81 additions and 3 deletions

1
.gitignore vendored
View File

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

View File

@ -128,6 +128,11 @@ namespace sqlpp
{
}
insert_list_data_t(std::tuple<Assignments...> 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<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 ...> t ) -> decltype (_columns)
{
return decltype(_columns)(std::get<Indexes>(t)._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)
{
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()");
@ -514,6 +542,13 @@ namespace sqlpp
return _set_impl<void>(Check{}, 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...>,
@ -523,6 +558,15 @@ namespace sqlpp
return _set_impl<_database_t>(Check{}, 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:
template <typename Check, typename... Columns>
auto _columns_impl(Check, Columns... cols) const -> inconsistent<Check>;
@ -554,6 +598,14 @@ namespace sqlpp
return {static_cast<const derived_statement_t<Policies>&>(*this),
insert_list_data_t<Database, Assignments...>{assignments...}};
}
template <typename Database, typename... Assignments>
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}};
}
};
};

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)
{
}
@ -261,6 +261,14 @@ namespace sqlpp
return _set_impl<void>(Check{}, 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>
auto dynamic_set(Assignments... assignments) const
-> _new_statement_t<check_update_dynamic_set_t<_database_t, Assignments...>,
@ -279,7 +287,15 @@ namespace sqlpp
-> _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...>{std::forward_as_tuple(assignments...)}};
}
template <typename Database, typename... Assignments>
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}};
}
};
};

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;
}