16#if !defined(PQXX_HEADER_PRE)
17# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
22#include "pqxx/array.hxx"
23#include "pqxx/composite.hxx"
24#include "pqxx/result.hxx"
25#include "pqxx/strconv.hxx"
26#include "pqxx/types.hxx"
60 [[nodiscard]] PQXX_PURE
bool operator==(
field const &)
const noexcept;
67 return not operator==(rhs);
76 [[nodiscard]] PQXX_PURE
char const *name() const &;
79 [[nodiscard]]
oid PQXX_PURE type() const;
82 [[nodiscard]] PQXX_PURE
oid table() const;
85 PQXX_PURE constexpr
row_size_type num() const noexcept {
return col(); }
111 [[nodiscard]] PQXX_PURE std::string_view
view() const &
113 return std::string_view(c_str(), size());
126 [[nodiscard]] PQXX_PURE
char const *c_str() const &;
129 [[nodiscard]] PQXX_PURE
bool is_null() const noexcept;
132 [[nodiscard]] PQXX_PURE size_type size() const noexcept;
139 auto to(T &obj) const ->
140 typename std::enable_if_t<
141 (not std::is_pointer<T>::value or std::is_same<T,
char const *>::value),
150 auto const data{c_str()};
176 template<
typename T>
bool operator>>(T &obj)
const {
return to(obj); }
190 auto to(T &obj, T
const &default_value)
const ->
191 typename std::enable_if_t<
192 (not std::is_pointer<T>::value or std::is_same<T, char const *>::value),
199 obj = from_string<T>(this->view());
207 template<
typename T> T
as(T
const &default_value)
const
210 return default_value;
212 return from_string<T>(this->view());
221 template<
typename T> T
as()
const
232 return from_string<T>(this->view());
240 template<
typename T,
template<
typename>
class O = std::optional>
241 constexpr O<T>
get()
const
265 "Do not construct fields yourself. Get them from the row.")]]
field(
row const &r,
row_size_type c)
noexcept;
269 "Do not construct fields yourself. Get them from the "
270 "row.")]]
field() noexcept = default;
274 constexpr
result const &home() const noexcept {
return m_home; }
275 constexpr result::size_type idx() const noexcept {
return m_row; }
276 constexpr row_size_type col() const noexcept {
return m_col; }
284 m_col{col_num}, m_home{r}, m_row{row_num}
295 result::size_type m_row;
299template<>
inline bool field::to<std::string>(std::string &obj)
const
303 obj = std::string{view()};
309inline bool field::to<std::string>(
310 std::string &obj, std::string
const &default_value)
const
316 obj = std::string{view()};
327template<>
inline bool field::to<char const *>(
char const *&obj)
const
336template<>
inline bool field::to<std::string_view>(std::string_view &obj)
const
346inline bool field::to<std::string_view>(
347 std::string_view &obj, std::string_view
const &default_value)
const
358template<>
inline std::string_view field::as<std::string_view>()
const
368inline std::string_view
369field::as<std::string_view>(std::string_view
const &default_value)
const
371 return is_null() ? default_value : view();
375template<>
inline bool field::to<zview>(zview &obj)
const
385inline bool field::to<zview>(zview &obj, zview
const &default_value)
const
396template<>
inline zview field::as<zview>()
const
405template<>
inline zview field::as<zview>(
zview const &default_value)
const
411template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
415 using char_type = CHAR;
416 using traits_type = TRAITS;
417 using int_type =
typename traits_type::int_type;
418 using pos_type =
typename traits_type::pos_type;
419 using off_type =
typename traits_type::off_type;
420 using openmode = std::ios::openmode;
421 using seekdir = std::ios::seekdir;
426 virtual int sync()
override {
return traits_type::eof(); }
428 virtual pos_type seekoff(off_type, seekdir, openmode)
override
430 return traits_type::eof();
432 virtual pos_type seekpos(pos_type, openmode)
override
434 return traits_type::eof();
436 virtual int_type overflow(int_type)
override {
return traits_type::eof(); }
437 virtual int_type underflow()
override {
return traits_type::eof(); }
440 field const &m_field;
442 int_type initialize()
444 auto g{
static_cast<char_type *
>(
const_cast<char *
>(m_field.
c_str()))};
445 this->setg(g, g, g + std::size(m_field));
446 return int_type(std::size(m_field));
465template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
468 using super = std::basic_istream<CHAR, TRAITS>;
471 using char_type = CHAR;
472 using traits_type = TRAITS;
473 using int_type =
typename traits_type::int_type;
474 using pos_type =
typename traits_type::pos_type;
475 using off_type =
typename traits_type::off_type;
479 super{
nullptr}, m_buf{f}
517template<
typename CHAR>
519 "Do this by hand, probably with better error checking.")]]
inline std::
520 basic_ostream<CHAR> &
523 s.write(value.
c_str(), std::streamsize(std::size(value)));
543 return from_string<T>(value.
view());
556inline std::nullptr_t from_string<std::nullptr_t>(
field const &value)
560 "Extracting non-null field into nullptr_t variable."};
566template<> PQXX_LIBEXPORT std::string
to_string(field
const &value);
Low-level array parser.
Definition: array.hxx:529
Input stream that gets its data from a result field.
Definition: field.hxx:467
Definition: field.hxx:413
Reference to a field in a result set.
Definition: field.hxx:35
PQXX_PURE size_type size() const noexcept
Return number of bytes taken up by the field's value.
Definition: field.cxx:77
T as(T const &default_value) const
Return value as object of given type, or default value if null.
Definition: field.hxx:207
row_size_type m_col
Definition: field.hxx:291
array_parser as_array() const &noexcept
Parse the field as an SQL array.
Definition: field.hxx:253
auto to(T &obj, T const &default_value) const -> typename std::enable_if_t<(not std::is_pointer< T >::value or std::is_same< T, char const * >::value), bool >
Read value into obj; or if null, use default value and return false.
Definition: field.hxx:190
bool operator>>(T &obj) const
Read value into obj; or leave obj untouched and return false if null.
Definition: field.hxx:176
PQXX_PURE bool operator!=(field const &rhs) const noexcept
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:65
PQXX_PURE char const * c_str() const &
Read as plain C string.
Definition: field.cxx:65
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:221
PQXX_PURE std::string_view view() const &
Read as string_view, or an empty one if null.
Definition: field.hxx:111
bool composite_to(T &...fields) const
Read field as a composite value, write its components into fields.
Definition: field.hxx:162
field() noexcept=default
Constructor. Do not call this yourself; libpqxx will do it for you.
PQXX_PURE bool is_null() const noexcept
Is this field's value null?
Definition: field.cxx:71
constexpr O< T > get() const
Return value wrapped in some optional type (empty for nulls).
Definition: field.hxx:241
Result set containing data returned by a query or command.
Definition: result.hxx:73
Reference to one row in a result.
Definition: row.hxx:47
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38
Value conversion failed, e.g. when converting "Hello" to int.
Definition: except.hxx:283
void throw_null_conversion(std::string const &type)
Throw exception for attempt to convert SQL NULL to given type.
Definition: strconv.cxx:255
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:27
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &s, field const &value)
Write a result field to any type of stream.
Definition: field.hxx:521
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:34
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:40
int result_size_type
Number of rows in a result set.
Definition: types.hxx:28
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:514
void parse_composite(pqxx::internal::encoding_group enc, std::string_view text, T &...fields)
Parse a string representation of a value of a composite type.
Definition: composite.hxx:35
unsigned int oid
PostgreSQL database row identifier.
Definition: libpq-forward.hxx:33
PQXX_LIBEXPORT std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:566
T from_string(field const &value)
Convert a field's value to type T.
Definition: field.hxx:532
Traits describing a type's "null value," if any.
Definition: strconv.hxx:91
static TYPE null()
Return a null value.