From 35a17efc0a44d3cf046429a43e425eedb0bedf5c Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Mon, 22 Aug 2022 19:58:57 +0200 Subject: [PATCH] Very rough draft for supporting views (#453) --- include/sqlpp11/into.h | 6 +++++- include/sqlpp11/single_table.h | 9 +++++++-- include/sqlpp11/table.h | 2 +- include/sqlpp11/type_traits.h | 1 + tests/core/usage/Sample.h | 26 ++++++++++++++++++++++++++ tests/core/usage/Select.cpp | 7 +++++++ 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/include/sqlpp11/into.h b/include/sqlpp11/into.h index 9f5a4d90..caf8ae4a 100644 --- a/include/sqlpp11/into.h +++ b/include/sqlpp11/into.h @@ -114,10 +114,14 @@ namespace sqlpp SQLPP_PORTABLE_STATIC_ASSERT(assert_into_t, "into() required"); SQLPP_PORTABLE_STATIC_ASSERT(assert_into_arg_is_table, "argument for into() must be a table"); + SQLPP_PORTABLE_STATIC_ASSERT(assert_into_arg_is_not_a_view, "argument for into() must not be a view"); template struct check_into { - using type = static_combined_check_t::value, assert_into_arg_is_table>>; + using type = static_combined_check_t< + static_check_t::value, assert_into_arg_is_table>, + static_check_t::value, assert_into_arg_is_not_a_view> + >; }; template using check_into_t = typename check_into>::type; diff --git a/include/sqlpp11/single_table.h b/include/sqlpp11/single_table.h index 46ad77c3..33528881 100644 --- a/include/sqlpp11/single_table.h +++ b/include/sqlpp11/single_table.h @@ -60,6 +60,7 @@ namespace sqlpp using _nodes = detail::type_vector; static_assert(is_table_t
::value, "argument has to be a table"); + static_assert(not is_view_t
::value, "argument must not be a view"); static_assert(required_tables_of
::size::value == 0, "table depends on another table"); using _data_t = single_table_data_t; @@ -113,11 +114,15 @@ namespace sqlpp }; }; - SQLPP_PORTABLE_STATIC_ASSERT(assert_update_table_arg_is_table_t, "argument for update() must be a table"); + SQLPP_PORTABLE_STATIC_ASSERT(assert_update_table_arg_is_table, "argument for update() must be a table"); + SQLPP_PORTABLE_STATIC_ASSERT(assert_update_table_arg_is_no_a_view, "argument for update() must not be a view"); template struct check_update_table { - using type = static_combined_check_t::value, assert_update_table_arg_is_table_t>>; + using type = static_combined_check_t< + static_check_t::value, assert_update_table_arg_is_table>, + static_check_t::value, assert_update_table_arg_is_no_a_view> + >; }; template using check_update_table_t = typename check_update_table
::type; diff --git a/include/sqlpp11/table.h b/include/sqlpp11/table.h index 169ced39..8c2fef74 100644 --- a/include/sqlpp11/table.h +++ b/include/sqlpp11/table.h @@ -43,7 +43,7 @@ namespace sqlpp template struct table_t : public ColumnSpec::_alias_t::template _member_t>... { - using _traits = make_traits; + using _traits = make_traits::value>>; using _nodes = detail::type_vector<>; using _provided_tables = detail::type_set
; diff --git a/include/sqlpp11/type_traits.h b/include/sqlpp11/type_traits.h index b0ca4cd5..32b77e80 100644 --- a/include/sqlpp11/type_traits.h +++ b/include/sqlpp11/type_traits.h @@ -182,6 +182,7 @@ namespace sqlpp SQLPP_VALUE_TRAIT_GENERATOR(is_noop) SQLPP_VALUE_TRAIT_GENERATOR(is_missing) SQLPP_VALUE_TRAIT_GENERATOR(is_return_value) + SQLPP_VALUE_TRAIT_GENERATOR(is_view) SQLPP_VALUE_TRAIT_GENERATOR(is_table) SQLPP_VALUE_TRAIT_GENERATOR(is_raw_table) SQLPP_VALUE_TRAIT_GENERATOR(is_pre_join) diff --git a/tests/core/usage/Sample.h b/tests/core/usage/Sample.h index d0119149..50b23dd0 100644 --- a/tests/core/usage/Sample.h +++ b/tests/core/usage/Sample.h @@ -142,6 +142,32 @@ namespace test }; }; }; + struct ViewFoo_ + { + struct _alias_t + { + static constexpr const char _literal[] = "view_foo"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T tabFoo; + T& operator()() + { + return tabFoo; + } + const T& operator()() const + { + return tabFoo; + } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct ViewFoo : sqlpp::table_t + { + }; + namespace TabBar_ { struct Alpha diff --git a/tests/core/usage/Select.cpp b/tests/core/usage/Select.cpp index c18cb109..fa0c8c46 100644 --- a/tests/core/usage/Select.cpp +++ b/tests/core/usage/Select.cpp @@ -68,6 +68,7 @@ int Select(int, char*[]) const auto f = test::TabFoo{}; const auto t = test::TabBar{}; + const auto vf = test::ViewFoo{}; const auto tab_a = f.as(sqlpp::alias::a); getColumn(db, t.alpha); @@ -108,6 +109,12 @@ int Select(int, char*[]) std::cout << row.alpha << std::endl; } + for (const auto& row : + db(select(all_of(vf)).from(vf).where(vf.book == "sql"))) + { + std::cout << row.book << std::endl; + } + for (const auto& row : db(select(all_of(t), all_of(f)) .from(t.join(f).on(t.alpha > f.omega).join(tab_a).on(t.alpha == tab_a.omega)) .unconditionally()))