From e1c007287c79b7bba10fbbf46a0dda81a4f73f18 Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 23 Nov 2014 20:06:58 +0100 Subject: [PATCH] Added portable static asserts --- examples/select.cpp | 33 +++++++++++++++++++++++----- include/sqlpp11/insert_value_list.h | 15 ++++++++++++- include/sqlpp11/select_column_list.h | 15 ++++++++++++- include/sqlpp11/statement.h | 4 ++-- include/sqlpp11/where.h | 1 + 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/examples/select.cpp b/examples/select.cpp index 3bcba136..8b4317c2 100644 --- a/examples/select.cpp +++ b/examples/select.cpp @@ -34,6 +34,9 @@ #include "MockDb.h" #include +static constexpr bool some_condition = true; +static constexpr bool some_other_condition = false; + SQLPP_ALIAS_PROVIDER(cheesecake); MockDb db; @@ -91,8 +94,8 @@ int main() -#if !0 - auto s = select(all_of(p)) +#if 0 + auto s = select(p.id, p.name, f.id.as(cheesecake)) .from(p, f) .where(p.name == any(select(f.name) .from(f) @@ -103,12 +106,32 @@ int main() .limit(3).offset(7); auto x = s.as(sqlpp::alias::x); - for (const auto& row : db(select(p.id, x.name) - .from(p.join(x).on(p.feature == x.feature)) + for (const auto& row : db(select(p.id, p.name, all_of(x).as(x)) + .from(p.join(x).on(p.feature == x.cheesecake)) .where(true))) { int id = row.id; std::string name = row.name; + std::string x_name = row.x.name; + int cheesecake = row.x.cheesecake; + } +#endif + + + +#if !0 + auto dysel = dynamic_select(db).dynamic_columns(p.name).from(p).dynamic_where(); + + if (some_condition) + dysel.selected_columns.add(p.feature); + + if (some_other_condition) + dysel.where.add(p.id > 17); + + for (const auto& row : db(dysel)) + { + std::string name = row.name; + std::string feature = row.at("feature"); } #endif @@ -129,7 +152,5 @@ int main() - - } diff --git a/include/sqlpp11/insert_value_list.h b/include/sqlpp11/insert_value_list.h index d7355b0a..b20efded 100644 --- a/include/sqlpp11/insert_value_list.h +++ b/include/sqlpp11/insert_value_list.h @@ -122,6 +122,17 @@ namespace sqlpp interpretable_list_t _dynamic_values; }; + struct assert_no_unknown_tables_in_insert_assignments_t + { + using type = std::false_type; + + template + static void _() + { + static_assert(wrong_t::value, "at least one insert assignment requires a table which is otherwise not known in the statement"); + } + }; + template struct insert_list_t { @@ -200,7 +211,9 @@ namespace sqlpp template struct _methods_t { - using _consistency_check = consistent_t; + using _consistency_check = typename std::conditional::value, + consistent_t, + assert_no_unknown_tables_in_insert_assignments_t>::type; }; }; diff --git a/include/sqlpp11/select_column_list.h b/include/sqlpp11/select_column_list.h index 627c37ff..af247c7f 100644 --- a/include/sqlpp11/select_column_list.h +++ b/include/sqlpp11/select_column_list.h @@ -147,6 +147,17 @@ namespace sqlpp dynamic_select_column_list _dynamic_columns; }; + struct assert_no_unknown_tables_in_selected_columns_t + { + using type = std::false_type; + + template + static void _() + { + static_assert(wrong_t::value, "at least one selected column requires a table which is otherwise not known in the statement"); + } + }; + // SELECTED COLUMNS template @@ -231,7 +242,9 @@ namespace sqlpp template struct _methods_t { - using _consistency_check = consistent_t; + using _consistency_check = typename std::conditional::value, + consistent_t, + assert_no_unknown_tables_in_selected_columns_t>::type; }; // Result methods diff --git a/include/sqlpp11/statement.h b/include/sqlpp11/statement.h index db91baff..31738147 100644 --- a/include/sqlpp11/statement.h +++ b/include/sqlpp11/statement.h @@ -149,9 +149,9 @@ namespace sqlpp consistent_t, assert_no_parameters_t>::type; using _run_check = detail::get_first_if::_consistency_check...>; + _parameter_check, typename Policies::template _methods_t::_consistency_check..., _table_check>; using _prepare_check = detail::get_first_if::_consistency_check...>; + typename Policies::template _methods_t::_consistency_check..., _table_check>; }; } diff --git a/include/sqlpp11/where.h b/include/sqlpp11/where.h index 667fa7f7..a1de9edc 100644 --- a/include/sqlpp11/where.h +++ b/include/sqlpp11/where.h @@ -128,6 +128,7 @@ namespace sqlpp template struct _methods_t { +#warning: here and elsewhere: add check for missing tables (see select columns, for instance) using _consistency_check = consistent_t; }; };