From efeefa817950e9555c36546f19f1772316510280 Mon Sep 17 00:00:00 2001 From: rbock Date: Sat, 4 Jun 2016 09:58:12 +0200 Subject: [PATCH] Some more portable static_asserts. --- include/sqlpp11/insert_value_list.h | 12 ++--- include/sqlpp11/select_column_list.h | 2 +- include/sqlpp11/update_list.h | 75 ++++++++++++++++++---------- test_static_asserts/insert.cpp | 12 ++--- 4 files changed, 62 insertions(+), 39 deletions(-) diff --git a/include/sqlpp11/insert_value_list.h b/include/sqlpp11/insert_value_list.h index 391af7a9..2cae2a63 100644 --- a/include/sqlpp11/insert_value_list.h +++ b/include/sqlpp11/insert_value_list.h @@ -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_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()"); - 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"); SQLPP_PORTABLE_STATIC_ASSERT(assert_insert_static_set_count_args_t, "at least one assignment expression required in set()"); @@ -163,10 +163,10 @@ namespace sqlpp // static_check_t::value...>::value, assert_insert_set_assignments_t>, // static_check_t...>::value, assert_insert_set_no_duplicates_t>, // static_check_t>::value...>::value, - // assert_insert_set_prohibited_t>, + // assert_insert_set_allowed_t>, // static_check_t>...>::size::value == 1, - // assert_insert_set_one_table_t>>; + // assert_insert_set_single_table_t>>; template struct must_not_insert @@ -180,11 +180,11 @@ namespace sqlpp assert_insert_set_assignments_t>, static_check_t::type...>::value, assert_insert_set_no_duplicates_t>, - static_check_t::value...>::value, assert_insert_set_prohibited_t>, + static_check_t::value...>::value, assert_insert_set_allowed_t>, static_check_t< sizeof...(Assignments) == 0 or detail::make_joined_set_t::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 // template diff --git a/include/sqlpp11/select_column_list.h b/include/sqlpp11/select_column_list.h index d96cf2e3..19f79eac 100644 --- a/include/sqlpp11/select_column_list.h +++ b/include/sqlpp11/select_column_list.h @@ -92,7 +92,7 @@ namespace sqlpp assert_no_unknown_tables_in_selected_columns_t, "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, - "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 template diff --git a/include/sqlpp11/update_list.h b/include/sqlpp11/update_list.h index ab4af1b0..a3c1b625 100644 --- a/include/sqlpp11/update_list.h +++ b/include/sqlpp11/update_list.h @@ -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_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 + using check_update_set_t = static_combined_check_t< + static_check_t::value...>::value, assert_update_set_assignments_t>, + static_check_t::type...>::value, + assert_update_set_no_duplicates_t>, + static_check_t::value...>::value, assert_update_set_allowed_t>, + static_check_t< + sizeof...(Assignments) == 0 or + detail::make_joined_set_t::type>...>::size::value == 1, + assert_update_set_single_table_t>>; + + template + struct check_update_static_set + { + using type = static_combined_check_t, + static_check_t>; + }; + + template + using check_update_static_set_t = typename check_update_static_set::type; + + template + struct check_update_dynamic_set + { + using type = static_combined_check_t< + static_check_t::value, assert_update_dynamic_set_statement_dynamic_t>, + check_update_set_t>; + }; + + template + using check_update_dynamic_set_t = typename check_update_dynamic_set::type; + struct no_update_list_t { using _traits = make_traits; @@ -199,14 +240,6 @@ namespace sqlpp using _database_t = typename Policies::_database_t; - // workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269 - // template - // using _check = logic::all_t::value...>; - template - struct _check : public logic::all_t::value...> - { - }; - template using _new_statement_t = new_statement_t; @@ -214,23 +247,22 @@ namespace sqlpp template auto set(Assignments... assignments) const - -> _new_statement_t<_check, update_list_t> + -> _new_statement_t, update_list_t> { - static_assert(sizeof...(Assignments), "at least one assignment expression required in set()"); - static_assert(_check::value, "at least one argument is not an assignment in set()"); + using Check = check_update_static_set_t; + Check{}._(); - return _set_impl(_check{}, assignments...); + return _set_impl(Check{}, assignments...); } template auto dynamic_set(Assignments... assignments) const - -> _new_statement_t<_check, update_list_t<_database_t, Assignments...>> + -> _new_statement_t, update_list_t<_database_t, Assignments...>> { - static_assert(not std::is_same<_database_t, void>::value, - "dynamic_set() must not be called in a static statement"); - static_assert(_check::value, "at least one argument is not an assignment in set()"); + using Check = check_update_dynamic_set_t; + Check{}._(); - return _set_impl<_database_t>(_check{}, assignments...); + return _set_impl<_database_t>(Check{}, assignments...); } private: @@ -241,15 +273,6 @@ namespace sqlpp auto _set_impl(const std::true_type&, Assignments... assignments) const -> _new_statement_t> { - static_assert(not detail::has_duplicates...>::value, - "at least one duplicate column detected in set()"); - static_assert(logic::none_t>::value...>::value, - "at least one assignment is prohibited by its column definition in set()"); - - using _column_required_tables = detail::make_joined_set_t>...>; - static_assert(sizeof...(Assignments) ? (_column_required_tables::size::value == 1) : true, - "set() contains assignments for columns from more than one table"); - return {static_cast&>(*this), update_list_data_t{assignments...}}; } diff --git a/test_static_asserts/insert.cpp b/test_static_asserts/insert.cpp index 2236add0..9a37014b 100644 --- a/test_static_asserts/insert.cpp +++ b/test_static_asserts/insert.cpp @@ -87,8 +87,8 @@ namespace set_static_check(t.gamma = true, t.delta = 42, t.beta = "fortytwo"); // Try setting alpha - set_static_check(t.alpha = 17, t.beta = "whatever"); - set_static_check(t.beta = "whatever", t.alpha = 17); + set_static_check(t.alpha = 17, t.beta = "whatever"); + set_static_check(t.beta = "whatever", t.alpha = 17); // Try omitting gamma set_static_check(t.delta = 42); @@ -112,7 +112,7 @@ namespace t.gamma = true); // Try multiple tables - set_static_check(f.omega = 41, t.gamma = true); + set_static_check(f.omega = 41, t.gamma = true); } // column alpha is not allowed, column gamma is required @@ -127,8 +127,8 @@ namespace set_dynamic_check(t.gamma = true, t.delta = 42, t.beta = "fortytwo"); // Try setting alpha - set_dynamic_check(t.alpha = 17, t.beta = "whatever"); - set_dynamic_check(t.beta = "whatever", t.alpha = 17); + set_dynamic_check(t.alpha = 17, t.beta = "whatever"); + set_dynamic_check(t.beta = "whatever", t.alpha = 17); // Omitting gamma is OK in the dynamic case, since we have to assume that it gets added later set_dynamic_check(t.delta = 42); @@ -153,7 +153,7 @@ namespace t.gamma = true); // Try multiple tables - set_dynamic_check(f.omega = 41, t.gamma = true); + set_dynamic_check(f.omega = 41, t.gamma = true); // Try dynamic_set on a non-dynamic insert using CheckResult = sqlpp::check_insert_dynamic_set_t;