diff --git a/include/sqlpp11/alias.h b/include/sqlpp11/alias.h index e0df5ba8..59b19e44 100644 --- a/include/sqlpp11/alias.h +++ b/include/sqlpp11/alias.h @@ -41,6 +41,11 @@ namespace sqlpp using _alias_t = typename AliasProvider::_alias_t; + sqlpp::compat::optional if_(bool condition) const + { + return condition ? sqlpp::compat::make_optional(*this) : sqlpp::compat::nullopt; + } + expression_alias_t(Expression expression) : _expression(expression) { } diff --git a/tests/core/types/result_row.cpp b/tests/core/types/result_row.cpp index 48e7ad90..0b4d8e61 100644 --- a/tests/core/types/result_row.cpp +++ b/tests/core/types/result_row.cpp @@ -31,9 +31,19 @@ namespace { + template + constexpr bool is_same_type() + { + return std::is_same::value; + } + constexpr auto bar = test::TabBar{}; constexpr auto foo = test::TabFoo{}; + auto db = MockDb{}; +} +int main() +{ static_assert(not sqlpp::can_be_null_t::value, ""); static_assert(sqlpp::can_be_null_t::value, ""); static_assert(not sqlpp::can_be_null_t::value, ""); @@ -41,19 +51,84 @@ namespace const auto seven = sqlpp::value(7).as(sqlpp::alias::s); static_assert(not sqlpp::can_be_null_t::value, ""); - auto db = MockDb{}; - - void single_table() + // Select non-optional column or alias of it. + for (const auto& row : db(select(foo.id, foo.id.as(sqlpp::alias::a), foo.textNnD, foo.textNnD.as(sqlpp::alias::b)) + .from(foo) + .unconditionally())) { + static_assert(is_same_type(), ""); + static_assert(is_same_type(), ""); + static_assert(is_same_type(), ""); + static_assert(is_same_type(), ""); + } + + // Optionally select non-optional column or alias of it. + for (const auto& row : db(select(foo.id.if_(true), foo.id.as(sqlpp::alias::a).if_(true), foo.textNnD.if_(true), + foo.textNnD.as(sqlpp::alias::b).if_(true)) + .from(foo) + .unconditionally())) + { + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + } + + // Select optional column or alias of it. + for (const auto& row : db(select(bar.intN, bar.intN.as(sqlpp::alias::a), bar.textN, bar.textN.as(sqlpp::alias::b)) + .from(bar) + .unconditionally())) + { + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + } + + // Optionally select optional column or alias of it. + for (const auto& row : db(select(bar.intN.if_(true), bar.intN.as(sqlpp::alias::a).if_(true), bar.textN.if_(true), + bar.textN.as(sqlpp::alias::b).if_(true)) + .from(bar) + .unconditionally())) + { + // optional optional are still represented as one level of optional + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + } + + // Select value and optional value. + sqlpp::value(7); + /* + for (const auto& row : db(select(sqlpp::value(7).as(sqlpp::alias::a), + sqlpp::value(sqlpp::compat::optional(7)).as(sqlpp::alias::b)))) + */ + { + /* + static_assert(is_same_type(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + static_assert(is_same_type>(), ""); + */ + } + +#if 0 + + { // result fields are as nullable as the expressions they represent - const auto rows = db(select(bar.id, bar.boolNn, seven).from(bar).unconditionally()); + const auto rows = db(select(bar.id, bar.boolNn, bar.intN, seven).from(bar).unconditionally()); auto& x = rows.front(); +#warning: test with nullable columns, too. +#warning: test with all kinds of functions as well. +#warning: We should actually test for the exact type! static_assert(not is_optional::value, ""); static_assert(not is_optional::value, ""); + static_assert(is_optional::value, ""); static_assert(not is_optional::value, ""); } - } +} void optional_columns() { @@ -63,10 +138,11 @@ namespace #warning: test with nullable columns, too. #warning: test with all kinds of functions as well. #warning: We should actually test for the exact type! - const auto rows = db(select(bar.id.if_(true), bar.boolNn.if_(true)/*, seven*/).from(bar).unconditionally()); + const auto rows = db(select(bar.id.if_(true), bar.boolNn.if_(true), bar.intN.if_(true) /*, seven*/).from(bar).unconditionally()); auto& x = rows.front(); static_assert(is_optional::value, ""); static_assert(is_optional::value, ""); + static_assert(is_optional::value, ""); } } @@ -251,4 +327,5 @@ int main(int, char* []) optional_columns(); join(); aggregates(); +#endif }