0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Allow result_t to have a size() function if DbResult has a size() function.

size() must be const, but can be arbitrary type.
size() may be wrong after first next has been called
This commit is contained in:
Aaron Bishop 2017-10-21 16:09:30 -04:00
parent f6cb213e55
commit fb84f9b4a9
3 changed files with 38 additions and 2 deletions

View File

@ -38,6 +38,31 @@ namespace sqlpp
using type = std::input_iterator_tag; using type = std::input_iterator_tag;
}; };
namespace detail
{
template<class DbResult, class = void>
struct result_has_size : std::false_type {};
template<class DbResult>
struct result_has_size<DbResult, void_t<decltype(std::declval<DbResult>().size())>>
: std::true_type {};
template<class DbResult>
constexpr bool result_has_size_v = result_has_size<DbResult>::value;
template<class DbResult, class = void>
struct result_size_type { using type = void; };
template<class DbResult>
struct result_size_type<DbResult, void_t<decltype(std::declval<DbResult>().size())>>
{
using type = decltype(std::declval<DbResult>().size());
};
template<class DbResult>
using result_size_type_t = typename result_size_type<DbResult>::type;
}
template <typename DbResult, typename ResultRow> template <typename DbResult, typename ResultRow>
class result_t class result_t
{ {
@ -139,6 +164,13 @@ namespace sqlpp
{ {
_result.next(_result_row); _result.next(_result_row);
} }
template<class Size = detail::result_size_type_t<DbResult>>
Size size() const
{
static_assert(detail::result_has_size_v<DbResult>, "Underlying connector does not support size()");
return _result.size();
}
}; };
} // namespace sqlpp } // namespace sqlpp

View File

@ -65,9 +65,11 @@ namespace
static_assert(not sqlpp::can_be_null_t<decltype(x.s)>::value, "constant non-null value can not be null"); static_assert(not sqlpp::can_be_null_t<decltype(x.s)>::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)) .from(bar.join(foo).on(foo.omega > bar.alpha))
.unconditionally()).front(); .unconditionally());
const auto& x = result.front();
static_assert(std::is_same<decltype(result.size()), size_t>::value, "db doesn't have size_t result_t.size() ");
static_assert(sqlpp::can_be_null_t<decltype(x.alpha)>::value, "nullable value can always be null"); static_assert(sqlpp::can_be_null_t<decltype(x.alpha)>::value, "nullable value can always be null");
static_assert(not sqlpp::can_be_null_t<decltype(x.gamma)>::value, "left side of (inner) join cannot be null"); static_assert(not sqlpp::can_be_null_t<decltype(x.gamma)>::value, "left side of (inner) join cannot be null");
static_assert(not sqlpp::can_be_null_t<decltype(x.delta)>::value, "right side of (inner) join cannot be null"); static_assert(not sqlpp::can_be_null_t<decltype(x.delta)>::value, "right side of (inner) join cannot be null");

View File

@ -114,6 +114,8 @@ struct MockDbT : public sqlpp::connection
{ {
result_row._invalidate(); result_row._invalidate();
} }
size_t size() const { return 0; }
}; };
// Directly executed statements start here // Directly executed statements start here