Skip to content

Commit

Permalink
Very rough draft for supporting views (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbock committed Aug 22, 2022
1 parent 159f4be commit 35a17ef
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
6 changes: 5 additions & 1 deletion include/sqlpp11/into.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
struct check_into
{
using type = static_combined_check_t<static_check_t<is_raw_table_t<T>::value, assert_into_arg_is_table>>;
using type = static_combined_check_t<
static_check_t<is_raw_table_t<T>::value, assert_into_arg_is_table>,
static_check_t<not is_view_t<T>::value, assert_into_arg_is_not_a_view>
>;
};
template <typename T>
using check_into_t = typename check_into<wrap_operand_t<T>>::type;
Expand Down
9 changes: 7 additions & 2 deletions include/sqlpp11/single_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace sqlpp
using _nodes = detail::type_vector<Table>;

static_assert(is_table_t<Table>::value, "argument has to be a table");
static_assert(not is_view_t<Table>::value, "argument must not be a view");
static_assert(required_tables_of<Table>::size::value == 0, "table depends on another table");

using _data_t = single_table_data_t<Database, Table>;
Expand Down Expand Up @@ -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 <typename Table>
struct check_update_table
{
using type = static_combined_check_t<static_check_t<is_table_t<Table>::value, assert_update_table_arg_is_table_t>>;
using type = static_combined_check_t<
static_check_t<is_table_t<Table>::value, assert_update_table_arg_is_table>,
static_check_t<not is_view_t<Table>::value, assert_update_table_arg_is_no_a_view>
>;
};
template <typename Table>
using check_update_table_t = typename check_update_table<Table>::type;
Expand Down
2 changes: 1 addition & 1 deletion include/sqlpp11/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace sqlpp
template <typename Table, typename... ColumnSpec>
struct table_t : public ColumnSpec::_alias_t::template _member_t<column_t<Table, ColumnSpec>>...
{
using _traits = make_traits<no_value_t, tag::is_raw_table, tag::is_table>;
using _traits = make_traits<no_value_t, tag::is_raw_table, tag::is_table, tag_if<tag::is_view, is_view_t<Table>::value>>;

using _nodes = detail::type_vector<>;
using _provided_tables = detail::type_set<Table>;
Expand Down
1 change: 1 addition & 0 deletions include/sqlpp11/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions tests/core/usage/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T tabFoo;
T& operator()()
{
return tabFoo;
}
const T& operator()() const
{
return tabFoo;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::no_value_t, sqlpp::tag::is_view>;
};
struct ViewFoo : sqlpp::table_t<ViewFoo_, TabFoo_::Delta, TabFoo_::Epsilon, TabFoo_::Omega, TabFoo_::Psi, TabFoo_::Book>
{
};

namespace TabBar_
{
struct Alpha
Expand Down
7 changes: 7 additions & 0 deletions tests/core/usage/Select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit 35a17ef

Please sign in to comment.