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

Some more portable static_asserts.

This commit is contained in:
rbock 2016-06-04 09:58:12 +02:00
parent 4bffed3f2d
commit efeefa8179
4 changed files with 62 additions and 39 deletions

View File

@ -145,9 +145,9 @@ namespace sqlpp
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_assignments_t, "at least one argument is not an assignment in set()"); SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_assignments_t, "at least one argument is not an assignment in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_no_duplicates_t, "at least one duplicate column detected in set()"); SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_no_duplicates_t, "at least one duplicate column detected in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_prohibited_t, SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_allowed_t,
"at least one assignment is prohibited by its column definition in set()"); "at least one assignment is prohibited by its column definition in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_one_table_t, SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_set_single_table_t,
"set() arguments contain assignments from more than one table"); "set() arguments contain assignments from more than one table");
SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_static_set_count_args_t, SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_static_set_count_args_t,
"at least one assignment expression required in set()"); "at least one assignment expression required in set()");
@ -163,10 +163,10 @@ namespace sqlpp
// static_check_t<logic::all_t<is_assignment_t<Assignments>::value...>::value, assert_insert_set_assignments_t>, // static_check_t<logic::all_t<is_assignment_t<Assignments>::value...>::value, assert_insert_set_assignments_t>,
// static_check_t<not detail::has_duplicates<lhs_t<Assignments>...>::value, assert_insert_set_no_duplicates_t>, // static_check_t<not detail::has_duplicates<lhs_t<Assignments>...>::value, assert_insert_set_no_duplicates_t>,
// static_check_t<logic::none_t<must_not_insert_t<lhs_t<Assignments>>::value...>::value, // static_check_t<logic::none_t<must_not_insert_t<lhs_t<Assignments>>::value...>::value,
// assert_insert_set_prohibited_t>, // assert_insert_set_allowed_t>,
// static_check_t<sizeof...(Assignments) == 0 or // static_check_t<sizeof...(Assignments) == 0 or
// detail::make_joined_set_t<required_tables_of<lhs_t<Assignments>>...>::size::value == 1, // detail::make_joined_set_t<required_tables_of<lhs_t<Assignments>>...>::size::value == 1,
// assert_insert_set_one_table_t>>; // assert_insert_set_single_table_t>>;
template <typename Expr> template <typename Expr>
struct must_not_insert struct must_not_insert
@ -180,11 +180,11 @@ namespace sqlpp
assert_insert_set_assignments_t>, assert_insert_set_assignments_t>,
static_check_t<not detail::has_duplicates<typename lhs<Assignments>::type...>::value, static_check_t<not detail::has_duplicates<typename lhs<Assignments>::type...>::value,
assert_insert_set_no_duplicates_t>, assert_insert_set_no_duplicates_t>,
static_check_t<logic::none_t<must_not_insert<Assignments>::value...>::value, assert_insert_set_prohibited_t>, static_check_t<logic::none_t<must_not_insert<Assignments>::value...>::value, assert_insert_set_allowed_t>,
static_check_t< static_check_t<
sizeof...(Assignments) == 0 or sizeof...(Assignments) == 0 or
detail::make_joined_set_t<required_tables_of<typename lhs<Assignments>::type>...>::size::value == 1, detail::make_joined_set_t<required_tables_of<typename lhs<Assignments>::type>...>::size::value == 1,
assert_insert_set_one_table_t>>; assert_insert_set_single_table_t>>;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269 // workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269
// template <typename... Assignments> // template <typename... Assignments>

View File

@ -92,7 +92,7 @@ namespace sqlpp
assert_no_unknown_tables_in_selected_columns_t, assert_no_unknown_tables_in_selected_columns_t,
"at least one selected column requires a table which is otherwise not known in the statement"); "at least one selected column requires a table which is otherwise not known in the statement");
SQLPP_PORTABLE_STATIC_ASSERT(assert_no_unknown_aggregates_t, SQLPP_PORTABLE_STATIC_ASSERT(assert_no_unknown_aggregates_t,
"not all columns are made of aggregates, despite group_by or similar"); "not all selected columns are made of aggregates, despite group_by or similar");
// SELECTED COLUMNS // SELECTED COLUMNS
template <typename Database, typename... Columns> template <typename Database, typename... Columns>

View File

@ -147,6 +147,47 @@ namespace sqlpp
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_assignments_t, "update assignments required, i.e. set(...)"); SQLPP_PORTABLE_STATIC_ASSERT(assert_update_assignments_t, "update assignments required, i.e. set(...)");
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_set_assignments_t, "at least one argument is not an assignment in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_set_no_duplicates_t, "at least one duplicate column detected in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_set_allowed_t,
"at least one assignment is prohibited by its column definition in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_set_single_table_t,
"set() contains assignments for columns from more than one table");
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_set_count_args_t, "at least one assignment expression required in set()");
SQLPP_PORTABLE_STATIC_ASSERT(assert_update_dynamic_set_statement_dynamic_t,
"dynamic_set() must not be called in a static statement");
template <typename... Assignments>
using check_update_set_t = static_combined_check_t<
static_check_t<logic::all_t<is_assignment_t<Assignments>::value...>::value, assert_update_set_assignments_t>,
static_check_t<not detail::has_duplicates<typename lhs<Assignments>::type...>::value,
assert_update_set_no_duplicates_t>,
static_check_t<logic::none_t<must_not_update_t<Assignments>::value...>::value, assert_update_set_allowed_t>,
static_check_t<
sizeof...(Assignments) == 0 or
detail::make_joined_set_t<required_tables_of<typename lhs<Assignments>::type>...>::size::value == 1,
assert_update_set_single_table_t>>;
template <typename... Assignments>
struct check_update_static_set
{
using type = static_combined_check_t<check_update_set_t<Assignments...>,
static_check_t<sizeof...(Assignments) != 0, assert_update_set_count_args_t>>;
};
template <typename... Assignments>
using check_update_static_set_t = typename check_update_static_set<Assignments...>::type;
template <typename Database, typename... Assignments>
struct check_update_dynamic_set
{
using type = static_combined_check_t<
static_check_t<not std::is_same<Database, void>::value, assert_update_dynamic_set_statement_dynamic_t>,
check_update_set_t<Assignments...>>;
};
template <typename... Assignments>
using check_update_dynamic_set_t = typename check_update_dynamic_set<Assignments...>::type;
struct no_update_list_t struct no_update_list_t
{ {
using _traits = make_traits<no_value_t, tag::is_where>; using _traits = make_traits<no_value_t, tag::is_where>;
@ -199,14 +240,6 @@ namespace sqlpp
using _database_t = typename Policies::_database_t; using _database_t = typename Policies::_database_t;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269
// template <typename... T>
// using _check = logic::all_t<is_assignment_t<T>::value...>;
template <typename... T>
struct _check : public logic::all_t<is_assignment_t<T>::value...>
{
};
template <typename Check, typename T> template <typename Check, typename T>
using _new_statement_t = new_statement_t<Check::value, Policies, no_update_list_t, T>; using _new_statement_t = new_statement_t<Check::value, Policies, no_update_list_t, T>;
@ -214,23 +247,22 @@ namespace sqlpp
template <typename... Assignments> template <typename... Assignments>
auto set(Assignments... assignments) const auto set(Assignments... assignments) const
-> _new_statement_t<_check<Assignments...>, update_list_t<void, Assignments...>> -> _new_statement_t<check_update_static_set_t<Assignments...>, update_list_t<void, Assignments...>>
{ {
static_assert(sizeof...(Assignments), "at least one assignment expression required in set()"); using Check = check_update_static_set_t<Assignments...>;
static_assert(_check<Assignments...>::value, "at least one argument is not an assignment in set()"); Check{}._();
return _set_impl<void>(_check<Assignments...>{}, assignments...); return _set_impl<void>(Check{}, assignments...);
} }
template <typename... Assignments> template <typename... Assignments>
auto dynamic_set(Assignments... assignments) const auto dynamic_set(Assignments... assignments) const
-> _new_statement_t<_check<Assignments...>, update_list_t<_database_t, Assignments...>> -> _new_statement_t<check_update_dynamic_set_t<Assignments...>, update_list_t<_database_t, Assignments...>>
{ {
static_assert(not std::is_same<_database_t, void>::value, using Check = check_update_dynamic_set_t<Assignments...>;
"dynamic_set() must not be called in a static statement"); Check{}._();
static_assert(_check<Assignments...>::value, "at least one argument is not an assignment in set()");
return _set_impl<_database_t>(_check<Assignments...>{}, assignments...); return _set_impl<_database_t>(Check{}, assignments...);
} }
private: private:
@ -241,15 +273,6 @@ namespace sqlpp
auto _set_impl(const std::true_type&, Assignments... assignments) const auto _set_impl(const std::true_type&, Assignments... assignments) const
-> _new_statement_t<std::true_type, update_list_t<Database, Assignments...>> -> _new_statement_t<std::true_type, update_list_t<Database, Assignments...>>
{ {
static_assert(not detail::has_duplicates<lhs_t<Assignments>...>::value,
"at least one duplicate column detected in set()");
static_assert(logic::none_t<must_not_update_t<lhs_t<Assignments>>::value...>::value,
"at least one assignment is prohibited by its column definition in set()");
using _column_required_tables = detail::make_joined_set_t<required_tables_of<lhs_t<Assignments>>...>;
static_assert(sizeof...(Assignments) ? (_column_required_tables::size::value == 1) : true,
"set() contains assignments for columns from more than one table");
return {static_cast<const derived_statement_t<Policies>&>(*this), 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

@ -87,8 +87,8 @@ namespace
set_static_check<sqlpp::consistent_t>(t.gamma = true, t.delta = 42, t.beta = "fortytwo"); set_static_check<sqlpp::consistent_t>(t.gamma = true, t.delta = 42, t.beta = "fortytwo");
// Try setting alpha // Try setting alpha
set_static_check<sqlpp::assert_insert_set_prohibited_t>(t.alpha = 17, t.beta = "whatever"); set_static_check<sqlpp::assert_insert_set_allowed_t>(t.alpha = 17, t.beta = "whatever");
set_static_check<sqlpp::assert_insert_set_prohibited_t>(t.beta = "whatever", t.alpha = 17); set_static_check<sqlpp::assert_insert_set_allowed_t>(t.beta = "whatever", t.alpha = 17);
// Try omitting gamma // Try omitting gamma
set_static_check<sqlpp::assert_insert_static_set_all_required_t>(t.delta = 42); set_static_check<sqlpp::assert_insert_static_set_all_required_t>(t.delta = 42);
@ -112,7 +112,7 @@ namespace
t.gamma = true); t.gamma = true);
// Try multiple tables // Try multiple tables
set_static_check<sqlpp::assert_insert_set_one_table_t>(f.omega = 41, t.gamma = true); set_static_check<sqlpp::assert_insert_set_single_table_t>(f.omega = 41, t.gamma = true);
} }
// column alpha is not allowed, column gamma is required // column alpha is not allowed, column gamma is required
@ -127,8 +127,8 @@ namespace
set_dynamic_check<sqlpp::consistent_t>(t.gamma = true, t.delta = 42, t.beta = "fortytwo"); set_dynamic_check<sqlpp::consistent_t>(t.gamma = true, t.delta = 42, t.beta = "fortytwo");
// Try setting alpha // Try setting alpha
set_dynamic_check<sqlpp::assert_insert_set_prohibited_t>(t.alpha = 17, t.beta = "whatever"); set_dynamic_check<sqlpp::assert_insert_set_allowed_t>(t.alpha = 17, t.beta = "whatever");
set_dynamic_check<sqlpp::assert_insert_set_prohibited_t>(t.beta = "whatever", t.alpha = 17); set_dynamic_check<sqlpp::assert_insert_set_allowed_t>(t.beta = "whatever", t.alpha = 17);
// Omitting gamma is OK in the dynamic case, since we have to assume that it gets added later // Omitting gamma is OK in the dynamic case, since we have to assume that it gets added later
set_dynamic_check<sqlpp::consistent_t>(t.delta = 42); set_dynamic_check<sqlpp::consistent_t>(t.delta = 42);
@ -153,7 +153,7 @@ namespace
t.gamma = true); t.gamma = true);
// Try multiple tables // Try multiple tables
set_dynamic_check<sqlpp::assert_insert_set_one_table_t>(f.omega = 41, t.gamma = true); set_dynamic_check<sqlpp::assert_insert_set_single_table_t>(f.omega = 41, t.gamma = true);
// Try dynamic_set on a non-dynamic insert // Try dynamic_set on a non-dynamic insert
using CheckResult = sqlpp::check_insert_dynamic_set_t<void>; using CheckResult = sqlpp::check_insert_dynamic_set_t<void>;