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" 45 "Do not construct fields yourself. Get them from the row.")]]
field(
row const &r,
row_size_type c) noexcept;
49 "Do not construct fields yourself. Get them from the " 50 "row.")]]
field() noexcept = default;
74 [[nodiscard]] PQXX_PURE
bool operator==(
field const &) const;
79 [[nodiscard]] PQXX_PURE
bool operator!=(
field const &rhs) const noexcept
81 return not operator==(rhs);
89 [[nodiscard]] PQXX_PURE
char const *name()
const &;
93 [[nodiscard]] oid PQXX_PURE type()
const;
96 [[nodiscard]] PQXX_PURE oid table()
const;
114 [[nodiscard]] PQXX_PURE std::string_view
view() const &
116 return std::string_view(c_str(), size());
129 [[nodiscard]] PQXX_PURE
char const *c_str()
const &;
132 [[nodiscard]] PQXX_PURE
bool is_null()
const noexcept;
135 [[nodiscard]] PQXX_PURE
size_type size()
const noexcept;
142 auto to(T &obj)
const ->
typename std::enable_if_t<
143 (not std::is_pointer<T>::value or std::is_same<T, char const *>::value),
152 auto const bytes{c_str()};
178 template<
typename T>
bool operator>>(T &obj)
const {
return to(obj); }
192 auto to(T &obj, T
const &default_value)
const ->
typename std::enable_if_t<
193 (not std::is_pointer<T>::value or std::is_same<T, char const *>::value),
200 obj = from_string<T>(this->view());
208 template<
typename T> T
as(T
const &default_value)
const 211 return default_value;
213 return from_string<T>(this->view());
222 template<
typename T> T
as()
const 233 return from_string<T>(this->view());
241 template<
typename T,
template<
typename>
class O = std::optional>
242 constexpr O<T>
get()
const 263 constexpr
result const &
home() const noexcept {
return m_home; }
273 m_col{col_num}, m_home{r}, m_row{row_num}
288 template<>
inline bool field::to<std::string>(std::string &obj)
const 292 obj = std::string{view()};
298 inline bool field::to<std::string>(
299 std::string &obj, std::string
const &default_value)
const 305 obj = std::string{view()};
316 template<>
inline bool field::to<char const *>(
char const *&obj)
const 325 template<>
inline bool field::to<std::string_view>(std::string_view &obj)
const 335 inline bool field::to<std::string_view>(
336 std::string_view &obj, std::string_view
const &default_value)
const 347 template<>
inline std::string_view field::as<std::string_view>()
const 357 inline std::string_view
358 field::as<std::string_view>(std::string_view
const &default_value)
const 360 return is_null() ? default_value : view();
364 template<>
inline bool field::to<zview>(
zview &obj)
const 374 inline bool field::to<zview>(
zview &obj,
zview const &default_value)
const 385 template<>
inline zview field::as<zview>()
const 394 template<>
inline zview field::as<zview>(
zview const &default_value)
const 400 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
415 virtual int sync()
override {
return traits_type::eof(); }
419 return traits_type::eof();
423 return traits_type::eof();
429 field const &m_field;
433 auto g{
static_cast<char_type *
>(
const_cast<char *
>(m_field.
c_str()))};
434 this->setg(g, g, g + std::size(m_field));
435 return int_type(std::size(m_field));
449 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
452 using super = std::basic_istream<CHAR, TRAITS>;
493 template<
typename CHAR>
494 inline std::basic_ostream<CHAR> &
495 operator<<(std::basic_ostream<CHAR> &s,
field const &value)
497 s.write(value.c_str(), std::streamsize(std::size(value)));
517 return from_string<T>(value.
view());
530 inline std::nullptr_t from_string<std::nullptr_t>(
field const &value)
532 if (not value.is_null())
534 "Extracting non-null field into nullptr_t variable."};
540 template<> PQXX_LIBEXPORT std::string
to_string(
field const &value);
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:222
Low-level array parser.
Definition: array.hxx:47
TRAITS traits_type
Definition: field.hxx:405
T as(T const &default_value) const
Return value as object of given type, or default value if null.
Definition: field.hxx:208
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
Definition: field.hxx:401
std::ios::openmode openmode
Definition: field.hxx:409
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
Reference to one row in a result.
Definition: row.hxx:46
constexpr size_type size() const noexcept
Definition: row.hxx:108
PQXX_PURE bool is_null() const noexcept
Is this field's value null?
Definition: field.cxx:71
virtual pos_type seekoff(off_type, seekdir, openmode) override
Definition: field.hxx:417
TRAITS traits_type
Definition: field.hxx:456
Reference to a field in a result set.
Definition: field.hxx:34
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:40
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:533
field(result const &r, result_size_type row_num, row_size_type col_num) noexcept
Definition: field.hxx:270
field_size_type size_type
Definition: field.hxx:37
typename traits_type::int_type int_type
Definition: field.hxx:457
bool composite_to(T &...fields) const
Read field as a composite value, write its components into fields.
Definition: field.hxx:164
typename traits_type::off_type off_type
Definition: field.hxx:459
bool operator>>(T &obj) const
Read value into obj; or leave obj untouched and return false if null.
Definition: field.hxx:178
T from_string(field const &value)
Convert a field's value to type T.
Definition: field.hxx:506
PQXX_PURE std::string_view view() const &
Read as string_view, or an empty one if null.
Definition: field.hxx:114
virtual pos_type seekpos(pos_type, openmode) override
Definition: field.hxx:421
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:34
typename traits_type::pos_type pos_type
Definition: field.hxx:458
void PQXX_COLD throw_null_conversion(std::string const &type)
Definition: strconv.cxx:253
auto to(T &obj) 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, leave obj untouched and return false.
Definition: field.hxx:142
std::ios::seekdir seekdir
Definition: field.hxx:410
typename traits_type::off_type off_type
Definition: field.hxx:408
typename traits_type::pos_type pos_type
Definition: field.hxx:407
PQXX_PURE char const * c_str() const &
Read as plain C string.
Definition: field.cxx:65
result_size_type size_type
Definition: result.hxx:75
constexpr result::size_type idx() const noexcept
Definition: field.hxx:264
CHAR char_type
Definition: field.hxx:404
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:192
virtual int sync() override
Definition: field.hxx:415
array_parser as_array() const &
Parse the field as an SQL array.
Definition: field.hxx:255
constexpr row_size_type col() const noexcept
Definition: field.hxx:265
constexpr result const & home() const noexcept
Definition: field.hxx:263
virtual int_type underflow() override
Definition: field.hxx:426
Result set containing data returned by a query or command.
Definition: result.hxx:72
field_streambuf(field const &f)
Definition: field.hxx:412
basic_fieldstream(field const &f)
Definition: field.hxx:461
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
virtual int_type overflow(int_type) override
Definition: field.hxx:425
row_size_type m_col
Definition: field.hxx:280
static TYPE null()
Return a null value.
Input stream that gets its data from a result field.
Definition: field.hxx:450
Value conversion failed, e.g. when converting "Hello" to int.
Definition: except.hxx:187
PQXX_PURE constexpr row_size_type num() const noexcept
Return row number. The first row is row 0, the second is row 1, etc.
Definition: field.hxx:99
Traits describing a type's "null value," if any.
Definition: strconv.hxx:92
int result_size_type
Number of rows in a result set.
Definition: types.hxx:28
typename traits_type::int_type int_type
Definition: field.hxx:406
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:367
CHAR char_type
Definition: field.hxx:455