13 #ifndef PQXX_FIELD_HXX
14 #define PQXX_FIELD_HXX
16 #if !defined(PQXX_HEADER_PRE)
17 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
31 class field_ref_const_row_iterator;
63 m_result(&res), m_row{row_num}, m_column{col_num}
92 return (home() == rhs.home()) and (row_number() == rhs.row_number()) and
93 (column_number() == rhs.column_number());
108 return home().column_name(column_number(), loc);
128 return home().table_column(column_number(), loc);
153 return zview{c_str(), size()};
166 [[nodiscard]]
PQXX_PURE char const *c_str() const & noexcept;
172 [[nodiscard]]
PQXX_PURE size_type size() const noexcept;
179 [[nodiscard]] T as(T const &default_value,
sl loc =
sl::current())
const
182 return default_value;
184 return from_string<T>(this->view(), make_context(loc));
193 template<
typename T> [[nodiscard]] T
as(
sl loc = sl::current())
const
197 if constexpr (not has_null<T>())
200 return make_null<T>();
204 return from_string<T>(this->view(), make_context(loc));
215 template<
typename T>
bool to(T &obj,
sl loc = sl::current())
const
230 bool to(T &obj, T
const &default_value,
sl loc = sl::current())
const
236 obj = from_string<T>(this->view(), make_context(loc));
244 template<
typename T,
template<
typename>
typename O = std::optional>
245 constexpr O<T>
get()
const
251 template<
typename ELEMENT,
auto... ARGS>
252 [[deprecated(
"Use as<pqxx::array<ELEMENT, ...>>().")]] [[nodiscard]]
array<
256 using array_type =
array<ELEMENT, ARGS...>;
281 result
const *m_result =
nullptr;
314 m_home{f.home()}, m_row{f.row_number()}, m_col{f.column_number()}
342 "To compare fields by content, compare their view()s.")]]
PQXX_PURE bool
349 "To compare fields by content, compare their view()s.")]]
PQXX_PURE bool
363 [[nodiscard]]
PQXX_PURE char const *name(
sl = sl::current()) const &;
368 return as_field_ref().type(loc);
374 return as_field_ref().table(loc);
378 [[deprecated(
"Use column_number().")]] [[nodiscard]]
row_size_type
381 return column_number();
412 return {c_str(), size()};
430 return as_field_ref().c_str();
436 return as_field_ref().is_null();
442 return as_field_ref().size();
452 template<
typename T>
bool to(T &obj,
sl loc = sl::current())
const
460 obj = from_string<T>(view(), make_context(loc));
495 return composite_to(sl::current(), fields...);
500 [[deprecated(
"Use to() or as().")]]
bool operator>>(T &obj)
const
510 bool to(T &obj, T
const &default_value,
sl loc = sl::current())
const
512 return as_field_ref().to<T>(obj, default_value, make_context(loc));
520 [[nodiscard]] T
as(T
const &default_value,
sl loc = sl::current())
const
522 return as_field_ref().as<T>(default_value, loc);
531 template<
typename T> [[nodiscard]] T
as(
sl loc = sl::current())
const
533 return as_field_ref().as<T>(loc);
540 template<
typename T,
template<
typename>
typename O = std::optional>
541 constexpr O<T>
get()
const
547 template<
typename ELEMENT,
auto... ARGS>
550 return as_field_ref().as_sql_array<ELEMENT, ARGS...>(loc);
561 "Avoid pqxx::array_parser. "
562 "Instead, use as_sql_array() to convert to "
567 return array_parser{c_str(), m_home.get_encoding_group()};
591 field() noexcept = default;
598 [[nodiscard]]
field_ref as_field_ref() const noexcept
600 return field_ref{home(), row_number(), column_number()};
603 [[nodiscard]] constexpr result
const &home() const noexcept
609 m_home{std::move(r)}, m_row{row_num}, m_col{col_num}
613 [[nodiscard]] conversion_context make_context(
sl loc)
const
615 return conversion_context{home().get_encoding_group(), loc};
666 template<>
inline bool field::to<zview>(
zview &obj,
sl)
const
676 inline bool field::to<zview>(
zview &obj,
zview const &default_value,
sl)
const
694 template<>
inline zview field_ref::as<zview>(
sl loc)
const
702 template<>
inline zview field::as<zview>(
zview const &default_value,
sl)
const
708 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
723 int sync()
override {
return traits_type::eof(); }
727 return traits_type::eof();
735 field const &m_field;
740 auto g{
static_cast<char_type *
>(
const_cast<char *
>(m_field.
c_str()))};
741 this->setg(g, g, g + std::size(m_field));
742 return int_type(std::size(m_field));
763 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
766 using super = std::basic_istream<CHAR, TRAITS>;
775 [[deprecated(
"Use field::as<...>() or field::c_str().")]]
777 super{nullptr}, m_buf{f}
817 template<
typename CHAR>
818 [[deprecated(
"To write a pqxx::field, use its view() method.")]]
inline std::
819 basic_ostream<CHAR> &
822 s.write(value.
c_str(), std::streamsize(std::size(value)));
835 if constexpr (has_null<T>())
842 return from_string<T>(value.
view(), c);
855 if constexpr (has_null<T>())
862 return from_string<T>(value.
view(), c);
877 inline std::nullptr_t from_string<std::nullptr_t>(
field const &value,
ctx c)
881 "Extracting non-null field into nullptr_t variable.", c.loc};
893 return std::string{value.
view()};
901 return std::string{value.
view()};
Low-level parser for C++ arrays.
Definition: array.hxx:738
An SQL array received from the database.
Definition: array.hxx:57
Input stream that gets its data from a result field.
Definition: field.hxx:765
TRAITS traits_type
Definition: field.hxx:770
basic_fieldstream(field const &f)
Definition: field.hxx:776
typename traits_type::pos_type pos_type
Definition: field.hxx:772
typename traits_type::off_type off_type
Definition: field.hxx:773
typename traits_type::int_type int_type
Definition: field.hxx:771
CHAR char_type
Definition: field.hxx:769
Lightweight reference to a field in a result set.
Definition: field.hxx:52
PQXX_PURE result const & home() const noexcept
Definition: field.hxx:70
array< ELEMENT, ARGS... > as_sql_array(sl loc=sl::current()) const
Read SQL array contents as a pqxx::array.
Definition: field.hxx:254
PQXX_PURE bool operator!=(field_ref const &rhs) const noexcept
Definition: field.hxx:95
PQXX_PURE bool operator==(field_ref const &rhs) const noexcept
Definition: field.hxx:90
constexpr PQXX_PURE row_size_type column_number() const noexcept
Return column number. The first column is 0, the second is 1, etc.
Definition: field.hxx:119
field_ref() noexcept=default
field_ref & operator=(field_ref &&) noexcept=default
T as(sl loc=sl::current()) const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:193
PQXX_PURE oid table(sl=sl::current()) const
What table did this column come from?
Definition: field.hxx:652
PQXX_PURE char const * c_str() const &noexcept
Read as plain C string.
Definition: field.hxx:628
constexpr O< T > get() const
Return value wrapped in some optional type (empty for nulls).
Definition: field.hxx:245
PQXX_PURE row_size_type table_column(sl loc=sl::current()) const
What column number in its originating table did this column come from?
Definition: field.hxx:126
PQXX_PURE bool is_null() const noexcept
Is this field's value null?
Definition: field.hxx:634
bool to(T &obj, T const &default_value, sl loc=sl::current()) const
Read value into obj; or if null, set default value and return false.
Definition: field.hxx:230
PQXX_PURE std::string_view view() const &noexcept
Read as zview, or an empty one if null.
Definition: field.hxx:151
PQXX_PURE result_size_type row_number() const noexcept
Definition: field.hxx:74
bool to(T &obj, sl loc=sl::current()) const
Read value into obj; or if null, leave obj untouched.
Definition: field.hxx:215
PQXX_PURE oid type(sl loc=sl::current()) const
Column type.
Definition: field.hxx:646
field_ref & operator=(field_ref const &) noexcept=default
PQXX_PURE char const * name(sl loc=sl::current()) const &
Column name.
Definition: field.hxx:106
field_size_type size_type
A type for holding the number of bytes in a field.
Definition: field.hxx:55
PQXX_PURE size_type size() const noexcept
Return number of bytes taken up by the field's value.
Definition: field.hxx:640
Definition: field.hxx:710
TRAITS traits_type
Definition: field.hxx:713
typename traits_type::off_type off_type
Definition: field.hxx:716
field_streambuf(field const &f)
Definition: field.hxx:720
std::ios::openmode openmode
Definition: field.hxx:717
typename traits_type::pos_type pos_type
Definition: field.hxx:715
int sync() override
Definition: field.hxx:723
typename traits_type::int_type int_type
Definition: field.hxx:714
int_type underflow() override
Definition: field.hxx:731
pos_type seekoff(off_type, seekdir, openmode) override
Definition: field.hxx:725
int_type overflow(int_type) override
Definition: field.hxx:730
CHAR char_type
Definition: field.hxx:712
std::ios::seekdir seekdir
Definition: field.hxx:718
pos_type seekpos(pos_type, openmode) override
Definition: field.hxx:729
Reference to a field in a result set.
Definition: field.hxx:309
PQXX_PURE PQXX_RETURNS_NONNULL char const * c_str() const &noexcept
Read as plain C string.
Definition: field.hxx:428
PQXX_PURE size_type size() const noexcept
Return number of bytes taken up by the field's value.
Definition: field.hxx:440
T as(sl loc=sl::current()) const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:531
row_size_type num() const noexcept
Return column number. The first column is 0, the second is 1, etc.
Definition: field.hxx:379
array_parser as_array() const &noexcept
Parse the field as an SQL array.
Definition: field.hxx:564
T as(T const &default_value, sl loc=sl::current()) const
Return value as object of given type, or default value if null.
Definition: field.hxx:520
constexpr O< T > get() const
Return value wrapped in some optional type (empty for nulls).
Definition: field.hxx:541
field(field_ref const &f)
Definition: field.hxx:313
PQXX_PURE std::string_view view() const &noexcept
Read as zview (which is also a string_view).
Definition: field.hxx:410
field_size_type size_type
Definition: field.hxx:311
array< ELEMENT, ARGS... > as_sql_array(sl loc=sl::current()) const
Read SQL array contents as a pqxx::array.
Definition: field.hxx:548
PQXX_PURE bool is_null() const noexcept
Is this field's value null?
Definition: field.hxx:434
bool operator>>(T &obj) const
Read value into obj; or leave obj untouched and return false if null.
Definition: field.hxx:500
PQXX_PURE result_size_type row_number() const noexcept
This field's row number within the result.
Definition: field.hxx:573
PQXX_PURE row_size_type column_number() const noexcept
This field's column number within the result.
Definition: field.hxx:578
PQXX_PURE bool operator!=(field const &rhs) const noexcept
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:350
bool to(T &obj, T const &default_value, sl loc=sl::current()) const
Read value into obj; or if null, use default value and return false.
Definition: field.hxx:510
bool composite_to(sl loc, T &...fields) const
Read field as a composite value, write its components into fields.
Definition: field.hxx:469
PQXX_PURE oid table(sl loc=sl::current()) const
What table did this column come from?
Definition: field.hxx:372
bool composite_to(T &...fields) const
Read field as a composite value, write its components into fields.
Definition: field.hxx:493
bool to(T &obj, sl loc=sl::current()) const
Read value into obj; or if null, leave obj untouched.
Definition: field.hxx:452
constexpr reference home() const noexcept
The home object. The gate class has full "private" access.
Definition: callgate.hxx:65
Definition: field_ref-const_row_iterator.hxx:15
Definition: result-field_ref.hxx:9
Result set containing data returned by a query or command.
Definition: result.hxx:101
result_size_type size_type
Definition: result.hxx:103
oid column_table(row_size_type col_num, sl=sl::current()) const
What table did this column come from?
Definition: result.cxx:473
oid column_type(row_size_type col_num, sl=sl::current()) const
Return column's type, as an OID from the system catalogue.
Definition: result.cxx:449
Reference to one row in a result.
Definition: row.hxx:415
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
Value conversion failed, e.g. when converting "Hello" to int.
Definition: except.hxx:612
Definition: connection.hxx:94
void throw_null_conversion(std::string const &type, sl loc)
Throw exception for attempt to convert SQL NULL to given type.
Definition: strconv.cxx:142
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
void parse_composite(ctx c, std::string_view text, T &...fields)
Parse a string representation of a value of a composite type.
Definition: composite.hxx:34
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:83
int row_difference_type
Difference between row sizes.
Definition: types.hxx:86
constexpr TYPE make_null() requires(pqxx
Return a null value of TYPE.
Definition: strconv.hxx:781
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:89
constexpr std::string_view name_type() noexcept
Return human-readable name for TYPE.
Definition: types.hxx:277
T from_string(field const &value, ctx c={})
Convert a field's value to type T.
Definition: field.hxx:831
PQXX_LIBEXPORT std::string to_string(field_ref const &value, ctx)
Convert a field_ref to a string.
Definition: field.hxx:891
int result_size_type
Number of rows in a result set.
Definition: types.hxx:77
constexpr bool is_null(TYPE const &value) noexcept
Is value a null?
Definition: strconv.hxx:764
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:820
unsigned int oid
PostgreSQL database row identifier.
Definition: types.hxx:73
conversion_context const & ctx
Convenience alias: const reference to a pqxx::conversion_context.
Definition: strconv.hxx:201
constexpr std::string_view name_type< zview >() noexcept
Definition: zview.hxx:181
constexpr bool operator==(char const lhs[], pqxx::zview rhs) noexcept
Disambiguating comparison operator: leave it to std::string_view.
Definition: zview.hxx:203
Contextual parameters for string conversions implementations.
Definition: strconv.hxx:163