diff --git a/include/sqlpp11/result.h b/include/sqlpp11/result.h index 109cdfed..b38cf86b 100644 --- a/include/sqlpp11/result.h +++ b/include/sqlpp11/result.h @@ -38,6 +38,31 @@ namespace sqlpp using type = std::input_iterator_tag; }; + namespace detail + { + template + struct result_has_size : std::false_type {}; + + template + struct result_has_size().size())>> + : std::true_type {}; + + template + constexpr bool result_has_size_v = result_has_size::value; + + template + struct result_size_type { using type = void; }; + + template + struct result_size_type().size())>> + { + using type = decltype(std::declval().size()); + }; + + template + using result_size_type_t = typename result_size_type::type; + } + template class result_t { @@ -139,6 +164,13 @@ namespace sqlpp { _result.next(_result_row); } + + template> + Size size() const + { + static_assert(detail::result_has_size_v, "Underlying connector does not support size()"); + return _result.size(); + } }; } // namespace sqlpp diff --git a/test_types/result_row.cpp b/test_types/result_row.cpp index 31858a91..b17140d2 100644 --- a/test_types/result_row.cpp +++ b/test_types/result_row.cpp @@ -65,9 +65,11 @@ namespace static_assert(not sqlpp::can_be_null_t::value, "constant non-null value can not be null"); } { - const auto& x = db(select(bar.alpha, foo.delta, bar.gamma, seven) + auto&& result = db(select(bar.alpha, foo.delta, bar.gamma, seven) .from(bar.join(foo).on(foo.omega > bar.alpha)) - .unconditionally()).front(); + .unconditionally()); + const auto& x = result.front(); + static_assert(std::is_same::value, "db doesn't have size_t result_t.size() "); static_assert(sqlpp::can_be_null_t::value, "nullable value can always be null"); static_assert(not sqlpp::can_be_null_t::value, "left side of (inner) join cannot be null"); static_assert(not sqlpp::can_be_null_t::value, "right side of (inner) join cannot be null"); diff --git a/tests/MockDb.h b/tests/MockDb.h index 8e5d5308..5c9bb432 100644 --- a/tests/MockDb.h +++ b/tests/MockDb.h @@ -114,6 +114,8 @@ struct MockDbT : public sqlpp::connection { result_row._invalidate(); } + + size_t size() const { return 0; } }; // Directly executed statements start here