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"
34class PQXX_LIBEXPORT field
60 [[nodiscard]] PQXX_PURE
bool operator==(field
const &)
const noexcept;
65 [[nodiscard]] PQXX_PURE
bool operator!=(field
const &rhs)
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()};
162 template<
typename... T>
bool composite_to(T &...fields)
const
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
225 if constexpr (not nullness<T>::has_null)
226 internal::throw_null_conversion(type_name<T>);
228 return nullness<T>::null();
232 return from_string<T>(this->view());
240 template<
typename T,
template<
typename>
class O = std::optional>
241 constexpr O<T> get()
const
247 template<
typename ELEMENT,
auto... ARGS>
248 array<ELEMENT, ARGS...> as_sql_array()
const
250 using array_type = array<ELEMENT, ARGS...>;
254 internal::throw_null_conversion(type_name<array_type>);
256 return array_type{this->view(), this->m_home.m_encoding};
267 "Avoid pqxx::array_parser. "
268 "Instead, use as_sql_array() to convert to pqxx::array.")]]
269 array_parser as_array() const & noexcept
271 return array_parser{c_str(), m_home.m_encoding};
281 "Do not construct fields yourself. Get them from the row.")]] field(row
const &r, row_size_type c)
noexcept;
285 "Do not construct fields yourself. Get them from the "
286 "row.")]] field() noexcept = default;
290 constexpr result const &home() const noexcept {
return m_home; }
291 constexpr result::size_type idx() const noexcept {
return m_row; }
292 constexpr row_size_type col() const noexcept {
return m_col; }
298 result
const &r, result_size_type row_num, row_size_type col_num) noexcept
300 m_col{col_num}, m_home{r}, m_row{row_num}
311 result::size_type m_row;
315template<>
inline bool field::to<std::string>(std::string &
obj)
const
319 obj = std::string{view()};
325inline bool field::to<std::string>(
332 obj = std::string{view()};
343template<>
inline bool field::to<char const *>(
char const *&
obj)
const
352template<>
inline bool field::to<std::string_view>(std::string_view &
obj)
const
362inline bool field::to<std::string_view>(
374template<>
inline std::string_view field::as<std::string_view>()
const
384inline std::string_view
385field::as<std::string_view>(std::string_view
const &
default_value)
const
391template<>
inline bool field::to<zview>(zview &
obj)
const
395 obj = zview{c_str(), size()};
407 obj = zview{c_str(), size()};
412template<>
inline zview field::as<zview>()
const
417 return zview{c_str(), size()};
427template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
431 using char_type =
CHAR;
432 using traits_type =
TRAITS;
433 using int_type =
typename traits_type::int_type;
434 using pos_type =
typename traits_type::pos_type;
435 using off_type =
typename traits_type::off_type;
436 using openmode = std::ios::openmode;
437 using seekdir = std::ios::seekdir;
442 virtual int sync()
override {
return traits_type::eof(); }
444 virtual pos_type seekoff(off_type, seekdir, openmode)
override
446 return traits_type::eof();
448 virtual pos_type seekpos(pos_type, openmode)
override
450 return traits_type::eof();
452 virtual int_type overflow(int_type)
override {
return traits_type::eof(); }
453 virtual int_type underflow()
override {
return traits_type::eof(); }
456 field
const &m_field;
458 int_type initialize()
460 auto g{
static_cast<char_type *
>(
const_cast<char *
>(m_field.c_str()))};
461 this->
setg(
g,
g,
g + std::size(m_field));
462 return int_type(std::size(m_field));
481template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
484 using super = std::basic_istream<CHAR, TRAITS>;
487 using char_type =
CHAR;
488 using traits_type =
TRAITS;
489 using int_type =
typename traits_type::int_type;
490 using pos_type =
typename traits_type::pos_type;
491 using off_type =
typename traits_type::off_type;
495 super{
nullptr}, m_buf{
f}
533template<
typename CHAR>
535 "Do this by hand, probably with better error checking.")]]
inline std::
536 basic_ostream<CHAR> &
537 operator<<(std::basic_ostream<CHAR> &
s, field
const &value)
539 s.write(value.c_str(), std::streamsize(std::size(value)));
574 if (
not value.is_null())
576 "Extracting non-null field into nullptr_t variable."};
582template<> PQXX_LIBEXPORT std::string to_string(field
const &value);
Input stream that gets its data from a result field.
Definition field.hxx:483
Result set containing data returned by a query or command.
Definition result.hxx:92
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:264
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:537
std::size_t field_size_type
Number of bytes in a field of database data.
Definition types.hxx:40
constexpr char array_separator
Element separator between SQL array elements of this type.
Definition strconv.hxx:559
int row_size_type
Number of fields in a row of database data.
Definition types.hxx:34
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition strconv.hxx:515
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
std::nullptr_t from_string< std::nullptr_t >(field const &value)
Convert a field's value to nullptr_t.
Definition field.hxx:572
T from_string(field const &value)
Convert a field's value to type T.
Definition field.hxx:548
Traits describing a type's "null value," if any.
Definition strconv.hxx:91