Loading...
Searching...
No Matches
result_iter.hxx
#ifndef PQXX_H_RESULT_ITER
#define PQXX_H_RESULT_ITER
#include <memory>
#include "pqxx/strconv.hxx"
namespace pqxx
{
class result;
} // namespace pqxx
namespace pqxx::internal
{
// C++20: Replace with generator?
template<typename... TYPE> class result_iter
{
public:
using value_type = std::tuple<TYPE...>;
result_iter() = default;
explicit result_iter(result const &home) :
m_home{&home}, m_size{std::size(home)}
{
if (not std::empty(home))
read();
}
result_iter(result_iter const &) = default;
result_iter &operator++()
{
m_index++;
if (m_index >= m_size)
m_home = nullptr;
else
read();
return *this;
}
bool operator==(result_iter const &rhs) const
{
return m_home == rhs.m_home;
}
bool operator!=(result_iter const &rhs) const { return not(*this == rhs); }
value_type const &operator*() const { return m_value; }
private:
void read() { (*m_home)[m_index].convert(m_value); }
result const *m_home{nullptr};
result::size_type m_index{0};
result::size_type m_size;
value_type m_value;
};
template<typename... TYPE> class result_iteration
{
public:
using iterator = result_iter<TYPE...>;
explicit result_iteration(result const &home) : m_home{home}
{
m_home.expect_columns(sizeof...(TYPE));
}
iterator begin() const
{
if (std::size(m_home) == 0)
return end();
else
return iterator{m_home};
}
iterator end() const { return {}; }
private:
pqxx::result const m_home;
};
} // namespace pqxx::internal
template<typename... TYPE> inline auto pqxx::result::iter() const
{
return pqxx::internal::result_iteration<TYPE...>{*this};
}
template<typename CALLABLE>
inline void pqxx::result::for_each(CALLABLE &&func) const
{
using args_tuple = internal::args_t<decltype(func)>;
constexpr auto sz{std::tuple_size_v<args_tuple>};
static_assert(
sz > 0,
"Callback for for_each must take parameters, one for each column in the "
"result.");
auto const cols{this->columns()};
if (sz != cols)
throw usage_error{internal::concat(
"Callback to for_each takes ", sz, "parameter", (sz == 1) ? "" : "s",
", but result set has ", cols, "field", (cols == 1) ? "" : "s", ".")};
using pass_tuple = pqxx::internal::strip_types_t<args_tuple>;
for (auto const r : *this) std::apply(func, r.as_tuple<pass_tuple>());
}
#endif
pqxx::const_reverse_result_iterator
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition result_iterator.hxx:196
pqxx::internal::result_iter
Iterator for looped unpacking of a result.
Definition result_iter.hxx:27
pqxx::internal::result_iter::result_iter
result_iter()=default
Construct an "end" iterator.
pqxx::internal::result_iter::operator==
bool operator==(result_iter const &rhs) const
Comparison only works for comparing to end().
Definition result_iter.hxx:53
pqxx::internal::result_iteration
Definition result_iter.hxx:72
pqxx::result
Result set containing data returned by a query or command.
Definition result.hxx:92
pqxx::result::for_each
void for_each(CALLABLE &&func) const
Run func on each row, passing the row's fields as parameters.
Definition result_iter.hxx:103
pqxx::result::expect_columns
result expect_columns(row_size_type cols) const
Expect that result consists of exactly cols columns.
Definition result.hxx:363
pqxx::result::iter
auto iter() const
Iterate rows, reading them directly into a tuple of "TYPE...".
Definition result_iter.hxx:96
pqxx::usage_error
Error in usage of libpqxx library, similar to std::logic_error.
Definition except.hxx:249
pqxx::internal
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
pqxx::internal::concat
std::string concat(TYPE... item)
Efficiently combine a bunch of items into one big string.
Definition concat.hxx:31
pqxx::internal::strip_types_t
decltype(strip_types(std::declval< TYPES... >())) strip_types_t
Take a tuple type and apply strip_t to its component types.
Definition util.hxx:629
pqxx::internal::args_t
decltype(args_f(std::declval< CALLABLE >())) args_t
A callable's parameter types, as a tuple.
Definition util.hxx:616
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
pqxx::value_type
strip_t< decltype(*std::begin(std::declval< CONTAINER >()))> value_type
The type of a container's elements.
Definition types.hxx:96