row.hxx
/* Definitions for the pqxx::result class and support classes.
*
* pqxx::result represents the set of result rows from a database query.
*
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result instead.
*
* Copyright (c) 2000-2023, Jeroen T. Vermeulen.
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this
* mistake, or contact the author.
*/
#ifndef PQXX_H_ROW
#define PQXX_H_ROW
#if !defined(PQXX_HEADER_PRE)
# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
#endif
#include "pqxx/except.hxx"
#include "pqxx/field.hxx"
#include "pqxx/result.hxx"
#include "pqxx/internal/concat.hxx"
namespace pqxx::internal
{
template<typename... T> class result_iter;
} // namespace pqxx::internal
namespace pqxx
{
class PQXX_LIBEXPORT row
{
public:
using size_type = row_size_type;
using difference_type = row_difference_type;
using const_iterator = const_row_iterator;
using iterator = const_iterator;
using reference = field;
using pointer = const_row_iterator;
using const_reverse_iterator = const_reverse_row_iterator;
using reverse_iterator = const_reverse_iterator;
row() noexcept = default;
row(row &&) noexcept = default;
row(row const &) noexcept = default;
row &operator=(row const &) noexcept = default;
row &operator=(row &&) noexcept = default;
[[nodiscard]] PQXX_PURE bool operator==(row const &) const noexcept;
[[nodiscard]] bool operator!=(row const &rhs) const noexcept
{
return not operator==(rhs);
}
[[nodiscard]] const_iterator begin() const noexcept;
[[nodiscard]] const_iterator cbegin() const noexcept;
[[nodiscard]] const_iterator end() const noexcept;
[[nodiscard]] const_iterator cend() const noexcept;
[[nodiscard]] reference front() const noexcept;
[[nodiscard]] reference back() const noexcept;
[[nodiscard]] const_reverse_row_iterator rbegin() const noexcept;
[[nodiscard]] const_reverse_row_iterator crbegin() const noexcept;
[[nodiscard]] const_reverse_row_iterator rend() const noexcept;
[[nodiscard]] const_reverse_row_iterator crend() const noexcept;
[[nodiscard]] reference operator[](size_type) const noexcept;
[[nodiscard]] reference operator[](zview col_name) const;
reference at(size_type) const;
reference at(zview col_name) const;
[[nodiscard]] constexpr size_type size() const noexcept
{
return m_end - m_begin;
}
[[deprecated("Swap iterators, not rows.")]] void swap(row &) noexcept;
[[nodiscard]] constexpr result::size_type rownumber() const noexcept
{
return m_index;
}
[[nodiscard]] size_type column_number(zview col_name) const;
[[nodiscard]] oid column_type(size_type) const;
[[nodiscard]] oid column_type(zview col_name) const
{
return column_type(column_number(col_name));
}
[[nodiscard]] oid column_table(size_type col_num) const;
[[nodiscard]] oid column_table(zview col_name) const
{
return column_table(column_number(col_name));
}
[[nodiscard]] size_type table_column(size_type) const;
[[nodiscard]] size_type table_column(zview col_name) const
{
return table_column(column_number(col_name));
}
[[nodiscard]] constexpr result::size_type num() const noexcept
{
return rownumber();
}
[[deprecated("Row slicing is going away. File a bug if you need it.")]] row
slice(size_type sbegin, size_type send) const;
[[nodiscard, deprecated("Row slicing is going away.")]] PQXX_PURE bool
empty() const noexcept;
template<typename Tuple> void to(Tuple &t) const
{
check_size(std::tuple_size_v<Tuple>);
convert(t);
}
template<typename... TYPE> std::tuple<TYPE...> as() const
{
check_size(sizeof...(TYPE));
using seq = std::make_index_sequence<sizeof...(TYPE)>;
return get_tuple<std::tuple<TYPE...>>(seq{});
}
protected:
friend class const_row_iterator;
friend class result;
row(result const &r, result_size_type index, size_type cols) noexcept;
void check_size(size_type expected) const
{
if (size() != expected)
throw usage_error{internal::concat(
"Tried to extract ", expected, " field(s) from a row of ", size(),
".")};
}
template<typename TUPLE> TUPLE as_tuple() const
{
using seq = std::make_index_sequence<std::tuple_size_v<TUPLE>>;
return get_tuple<TUPLE>(seq{});
}
template<typename... T> friend class pqxx::internal::result_iter;
template<typename Tuple> void convert(Tuple &t) const
{
extract_fields(t, std::make_index_sequence<std::tuple_size_v<Tuple>>{});
}
friend class field;
result m_result;
result::size_type m_index = 0;
// TODO: Remove m_begin and (if possible) m_end when we remove slice().
size_type m_begin = 0;
size_type m_end = 0;
private:
template<typename Tuple, std::size_t... indexes>
void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
{
(extract_value<Tuple, indexes>(t), ...);
}
template<typename Tuple, std::size_t index>
void extract_value(Tuple &t) const;
template<typename TUPLE, std::size_t... indexes>
auto get_tuple(std::index_sequence<indexes...>) const
{
return std::make_tuple(get_field<TUPLE, indexes>()...);
}
template<typename TUPLE, std::size_t index> auto get_field() const
{
return (*this)[index].as<std::tuple_element_t<index, TUPLE>>();
}
};
class PQXX_LIBEXPORT const_row_iterator : public field
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = field const;
using pointer = field const *;
using size_type = row_size_type;
using difference_type = row_difference_type;
using reference = field;
#include "pqxx/internal/ignore-deprecated-pre.hxx"
const_row_iterator() noexcept = default;
#include "pqxx/internal/ignore-deprecated-post.hxx"
const_row_iterator(row const &t, row_size_type c) noexcept :
field{t.m_result, t.m_index, c}
{}
const_row_iterator(field const &F) noexcept : field{F} {}
const_row_iterator(const_row_iterator const &) noexcept = default;
const_row_iterator(const_row_iterator &&) noexcept = default;
[[nodiscard]] constexpr pointer operator->() const noexcept { return this; }
[[nodiscard]] reference operator*() const noexcept { return {*this}; }
const_row_iterator &operator=(const_row_iterator const &) noexcept = default;
const_row_iterator &operator=(const_row_iterator &&) noexcept = default;
const_row_iterator operator++(int) noexcept;
const_row_iterator &operator++() noexcept
{
++m_col;
return *this;
}
const_row_iterator operator--(int) noexcept;
const_row_iterator &operator--() noexcept
{
--m_col;
return *this;
}
const_row_iterator &operator+=(difference_type i) noexcept
{
m_col = size_type(difference_type(m_col) + i);
return *this;
}
const_row_iterator &operator-=(difference_type i) noexcept
{
m_col = size_type(difference_type(m_col) - i);
return *this;
}
[[nodiscard]] constexpr bool
operator==(const_row_iterator const &i) const noexcept
{
return col() == i.col();
}
[[nodiscard]] constexpr bool
operator!=(const_row_iterator const &i) const noexcept
{
return col() != i.col();
}
[[nodiscard]] constexpr bool
operator<(const_row_iterator const &i) const noexcept
{
return col() < i.col();
}
[[nodiscard]] constexpr bool
operator<=(const_row_iterator const &i) const noexcept
{
return col() <= i.col();
}
[[nodiscard]] constexpr bool
operator>(const_row_iterator const &i) const noexcept
{
return col() > i.col();
}
[[nodiscard]] constexpr bool
operator>=(const_row_iterator const &i) const noexcept
{
return col() >= i.col();
}
[[nodiscard]] inline const_row_iterator
operator+(difference_type) const noexcept;
friend const_row_iterator
operator+(difference_type, const_row_iterator const &) noexcept;
[[nodiscard]] inline const_row_iterator
operator-(difference_type) const noexcept;
[[nodiscard]] inline difference_type
operator-(const_row_iterator const &) const noexcept;
};
class PQXX_LIBEXPORT const_reverse_row_iterator : private const_row_iterator
{
public:
using super = const_row_iterator;
using iterator_type = const_row_iterator;
using iterator_type::difference_type;
using iterator_type::iterator_category;
using iterator_type::pointer;
using value_type = iterator_type::value_type;
using reference = iterator_type::reference;
const_reverse_row_iterator() noexcept = default;
const_reverse_row_iterator(const_reverse_row_iterator const &) noexcept =
default;
const_reverse_row_iterator(const_reverse_row_iterator &&) noexcept = default;
explicit const_reverse_row_iterator(super const &rhs) noexcept :
const_row_iterator{rhs}
{
super::operator--();
}
[[nodiscard]] PQXX_PURE iterator_type base() const noexcept;
using iterator_type::operator->;
using iterator_type::operator*;
const_reverse_row_iterator &
operator=(const_reverse_row_iterator const &r) noexcept
{
iterator_type::operator=(r);
return *this;
}
const_reverse_row_iterator operator++() noexcept
{
iterator_type::operator--();
return *this;
}
const_reverse_row_iterator operator++(int) noexcept;
const_reverse_row_iterator &operator--() noexcept
{
iterator_type::operator++();
return *this;
}
const_reverse_row_iterator operator--(int);
const_reverse_row_iterator &operator+=(difference_type i) noexcept
{
iterator_type::operator-=(i);
return *this;
}
const_reverse_row_iterator &operator-=(difference_type i) noexcept
{
iterator_type::operator+=(i);
return *this;
}
[[nodiscard]] const_reverse_row_iterator
operator+(difference_type i) const noexcept
{
return const_reverse_row_iterator{base() - i};
}
[[nodiscard]] const_reverse_row_iterator
operator-(difference_type i) noexcept
{
return const_reverse_row_iterator{base() + i};
}
[[nodiscard]] difference_type
operator-(const_reverse_row_iterator const &rhs) const noexcept
{
return rhs.const_row_iterator::operator-(*this);
}
[[nodiscard]] bool
operator==(const_reverse_row_iterator const &rhs) const noexcept
{
return iterator_type::operator==(rhs);
}
[[nodiscard]] bool
operator!=(const_reverse_row_iterator const &rhs) const noexcept
{
return !operator==(rhs);
}
[[nodiscard]] constexpr bool
operator<(const_reverse_row_iterator const &rhs) const noexcept
{
return iterator_type::operator>(rhs);
}
[[nodiscard]] constexpr bool
operator<=(const_reverse_row_iterator const &rhs) const noexcept
{
return iterator_type::operator>=(rhs);
}
[[nodiscard]] constexpr bool
operator>(const_reverse_row_iterator const &rhs) const noexcept
{
return iterator_type::operator<(rhs);
}
[[nodiscard]] constexpr bool
operator>=(const_reverse_row_iterator const &rhs) const noexcept
{
return iterator_type::operator<=(rhs);
}
};
const_row_iterator
const_row_iterator::operator+(difference_type o) const noexcept
{
// TODO:: More direct route to home().columns()?
return {
row{home(), idx(), home().columns()},
size_type(difference_type(col()) + o)};
}
inline const_row_iterator operator+(
const_row_iterator::difference_type o, const_row_iterator const &i) noexcept
{
return i + o;
}
inline const_row_iterator
const_row_iterator::operator-(difference_type o) const noexcept
{
// TODO:: More direct route to home().columns()?
return {
row{home(), idx(), home().columns()},
size_type(difference_type(col()) - o)};
}
inline const_row_iterator::difference_type
const_row_iterator::operator-(const_row_iterator const &i) const noexcept
{
return difference_type(num() - i.num());
}
template<typename Tuple, std::size_t index>
inline void row::extract_value(Tuple &t) const
{
using field_type = strip_t<decltype(std::get<index>(t))>;
field const f{m_result, m_index, index};
std::get<index>(t) = from_string<field_type>(f);
}
} // namespace pqxx
#endif
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:33
pqxx::row_size_type
int row_size_type
Number of fields in a row of database data.
Definition types.hxx:34
pqxx::strip_t
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition types.hxx:91
pqxx::row_difference_type
int row_difference_type
Difference between row sizes.
Definition types.hxx:37
pqxx::operator+
const_row_iterator operator+(const_row_iterator::difference_type o, const_row_iterator const &i) noexcept
Definition row.hxx:532
pqxx::result_size_type
int result_size_type
Number of rows in a result set.
Definition types.hxx:28
pqxx::internal
Internal items for libpqxx' own use. Do not use these yourself.
Definition composite.hxx:84
pqxx::usage_error
Error in usage of libpqxx library, similar to std::logic_error.
Definition except.hxx:249
pqxx::field
Reference to a field in a result set.
Definition field.hxx:35
pqxx::result
Result set containing data returned by a query or command.
Definition result.hxx:73
pqxx::result::size_type
result_size_type size_type
Definition result.hxx:75
pqxx::internal::result_iter
Definition row.hxx:28
pqxx::row
Reference to one row in a result.
Definition row.hxx:47
pqxx::row::as
std::tuple< TYPE... > as() const
Extract entire row's values into a tuple.
Definition row.hxx:200
pqxx::row::size_type
row_size_type size_type
Definition row.hxx:49
pqxx::row::column_type
oid column_type(zview col_name) const
Return a column's type.
Definition row.hxx:128
pqxx::row::difference_type
row_difference_type difference_type
Definition row.hxx:50
pqxx::row::m_result
result m_result
Result set of which this is one row.
Definition row.hxx:241
pqxx::row::m_index
result::size_type m_index
Row number.
Definition row.hxx:248
pqxx::row::num
constexpr result::size_type num() const noexcept
Definition row.hxx:159
pqxx::row::row
row() noexcept=default
pqxx::row::rownumber
constexpr result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition row.hxx:112
pqxx::row::as_tuple
TUPLE as_tuple() const
Convert to a given tuple of values, don't check sizes.
Definition row.hxx:225
pqxx::row::check_size
void check_size(size_type expected) const
Throw usage_error if row size is not expected.
Definition row.hxx:213
pqxx::row::table_column
size_type table_column(zview col_name) const
What column number in its table did this result column come from?
Definition row.hxx:153
pqxx::row::convert
void convert(Tuple &t) const
Convert entire row to tuple fields, without checking row size.
Definition row.hxx:233
pqxx::row::column_table
oid column_table(zview col_name) const
What table did this column come from?
Definition row.hxx:137
pqxx::const_row_iterator
Iterator for fields in a row. Use as row::const_iterator.
Definition row.hxx:283
pqxx::const_row_iterator::operator-
const_row_iterator operator-(difference_type) const noexcept
Definition row.hxx:539
pqxx::const_row_iterator::operator=
const_row_iterator & operator=(const_row_iterator &&) noexcept=default
pqxx::const_row_iterator::operator*
reference operator*() const noexcept
Definition row.hxx:307
pqxx::const_row_iterator::const_row_iterator
const_row_iterator(field const &F) noexcept
Definition row.hxx:298
pqxx::const_row_iterator::operator+
friend const_row_iterator operator+(difference_type, const_row_iterator const &) noexcept
Definition row.hxx:532
pqxx::const_row_iterator::operator>=
constexpr bool operator>=(const_row_iterator const &i) const noexcept
Definition row.hxx:372
pqxx::const_row_iterator::const_row_iterator
const_row_iterator(const_row_iterator const &) noexcept=default
pqxx::const_row_iterator::operator=
const_row_iterator & operator=(const_row_iterator const &) noexcept=default
pqxx::const_row_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition row.hxx:285
pqxx::const_row_iterator::operator==
constexpr bool operator==(const_row_iterator const &i) const noexcept
Definition row.hxx:347
pqxx::const_row_iterator::pointer
field const * pointer
Definition row.hxx:287
pqxx::const_row_iterator::value_type
field const value_type
Definition row.hxx:286
pqxx::const_row_iterator::operator!=
constexpr bool operator!=(const_row_iterator const &i) const noexcept
Definition row.hxx:352
pqxx::const_row_iterator::const_row_iterator
const_row_iterator(const_row_iterator &&) noexcept=default
pqxx::const_row_iterator::size_type
row_size_type size_type
Definition row.hxx:288
pqxx::const_row_iterator::operator--
const_row_iterator & operator--() noexcept
Definition row.hxx:324
pqxx::const_row_iterator::const_row_iterator
const_row_iterator() noexcept=default
pqxx::const_row_iterator::difference_type
row_difference_type difference_type
Definition row.hxx:289
pqxx::const_row_iterator::operator-=
const_row_iterator & operator-=(difference_type i) noexcept
Definition row.hxx:335
pqxx::const_row_iterator::operator+=
const_row_iterator & operator+=(difference_type i) noexcept
Definition row.hxx:330
pqxx::const_row_iterator::const_row_iterator
const_row_iterator(row const &t, row_size_type c) noexcept
Definition row.hxx:295
pqxx::const_row_iterator::operator>
constexpr bool operator>(const_row_iterator const &i) const noexcept
Definition row.hxx:367
pqxx::const_reverse_row_iterator
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition row.hxx:398
pqxx::const_reverse_row_iterator::operator++
const_reverse_row_iterator operator++() noexcept
Definition row.hxx:439
pqxx::const_reverse_row_iterator::operator!=
bool operator!=(const_reverse_row_iterator const &rhs) const noexcept
Definition row.hxx:494
pqxx::const_reverse_row_iterator::operator>=
constexpr bool operator>=(const_reverse_row_iterator const &rhs) const noexcept
Definition row.hxx:515
pqxx::const_reverse_row_iterator::operator>
constexpr bool operator>(const_reverse_row_iterator const &rhs) const noexcept
Definition row.hxx:510
pqxx::const_reverse_row_iterator::operator-
difference_type operator-(const_reverse_row_iterator const &rhs) const noexcept
Definition row.hxx:478
pqxx::const_reverse_row_iterator::operator+=
const_reverse_row_iterator & operator+=(difference_type i) noexcept
Definition row.hxx:451
pqxx::const_reverse_row_iterator::operator+
const_reverse_row_iterator operator+(difference_type i) const noexcept
Definition row.hxx:468
pqxx::const_reverse_row_iterator::operator-=
const_reverse_row_iterator & operator-=(difference_type i) noexcept
Definition row.hxx:456
pqxx::const_reverse_row_iterator::operator--
const_reverse_row_iterator & operator--() noexcept
Definition row.hxx:445
pqxx::const_reverse_row_iterator::difference_type
row_difference_type difference_type
Definition row.hxx:289
pqxx::const_reverse_row_iterator::operator==
bool operator==(const_reverse_row_iterator const &rhs) const noexcept
Definition row.hxx:489
pqxx::const_reverse_row_iterator::const_reverse_row_iterator
const_reverse_row_iterator() noexcept=default
pqxx::const_reverse_row_iterator::operator-
const_reverse_row_iterator operator-(difference_type i) noexcept
Definition row.hxx:473
pqxx::const_reverse_row_iterator::value_type
iterator_type::value_type value_type
Definition row.hxx:405
pqxx::zview
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38