16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
21 #include "pqxx/array.hxx"
22 #include "pqxx/result.hxx"
23 #include "pqxx/strconv.hxx"
24 #include "pqxx/types.hxx"
67 [[nodiscard]]
bool operator==(
field const &)
const;
74 return not operator==(rhs);
82 [[nodiscard]]
char const *name()
const;
86 [[nodiscard]] oid type()
const;
89 [[nodiscard]] oid table()
const;
101 [[nodiscard]] std::string_view view()
const
104 return std::string_view(c_str(), size());
113 [[nodiscard]]
char const *c_str()
const;
116 [[nodiscard]]
bool is_null() const noexcept;
122 [[nodiscard]] size_type size() const noexcept;
129 auto to(T &obj) const -> typename std::enable_if<
130 (not std::is_pointer<T>::value or std::is_same<T,
char const *>::value),
133 auto const bytes{c_str()};
134 if (bytes[0] ==
'\0' and
is_null())
141 template<
typename T>
bool operator>>(T &obj)
const {
return to(obj); }
148 auto to(T &obj, T
const &default_value)
const ->
typename std::enable_if<
149 (not std::is_pointer<T>::value or std::is_same<T, char const *>::value),
152 bool const has_value{to(obj)};
162 template<
typename T> T
as(T
const &default_value)
const
165 to(obj, default_value);
175 template<
typename T> T
as()
const
192 template<
typename T,
template<
typename>
class O = std::optional>
193 constexpr O<T>
get()
const
230 template<>
inline bool field::to<std::string>(std::string &obj)
const
232 char const *
const bytes = c_str();
233 if (bytes[0] ==
'\0' and
is_null())
235 obj = std::string{bytes, size()};
245 template<>
inline bool field::to<char const *>(
char const *&obj)
const
254 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
269 virtual int sync()
override {
return traits_type::eof(); }
273 return traits_type::eof();
277 return traits_type::eof();
283 field const &m_field;
287 auto g{
static_cast<char_type *
>(
const_cast<char *
>(m_field.
c_str()))};
288 this->setg(g, g, g + m_field.
size());
303 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
306 using super = std::basic_istream<CHAR, TRAITS>;
321 field_streambuf<CHAR, TRAITS> m_buf;
347 template<
typename CHAR>
348 inline std::basic_ostream<CHAR> &
351 s.write(value.
c_str(), std::streamsize(value.
size()));
359 return from_string<T>(value.
view());
363 template<> PQXX_LIBEXPORT std::string
to_string(field
const &value);
366 #include "pqxx/internal/compiler-internal-post.hxx"
Reference to one row in a result.
Definition: row.hxx:38
bool is_null(TYPE const &value)
Is value null?
Definition: strconv.hxx:286
result::size_type idx() const noexcept
Definition: field.hxx:214
virtual pos_type seekoff(off_type, seekdir, openmode) override
Definition: field.hxx:271
typename traits_type::off_type off_type
Definition: field.hxx:313
typename traits_type::off_type off_type
Definition: field.hxx:262
typename traits_type::int_type int_type
Definition: field.hxx:260
row_size_type m_col
Definition: field.hxx:221
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
array_parser as_array() const
Parse the field as an SQL array.
Definition: field.hxx:205
std::string_view view() const
Read as string_view.
Definition: field.hxx:102
static TYPE null()
Return a null value.
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
bool operator>>(T &obj) const
Read value into obj; or leave obj untouched and return false if null.
Definition: field.hxx:141
std::ios::seekdir seekdir
Definition: field.hxx:264
void throw_null_conversion(std::string const &type)
Definition: strconv.cxx:241
field_streambuf(field const &f)
Definition: field.hxx:266
row_size_type col() const noexcept
Definition: field.hxx:215
oid table() const
What table did this column come from?
Definition: field.cxx:50
bool is_null() const noexcept
Is this field's value null?
Definition: field.cxx:68
Result set containing data returned by a query or command.
Definition: result.hxx:70
size_type size() const noexcept
Return number of bytes taken up by the field's value.
Definition: field.cxx:74
field_size_type size_type
Definition: field.hxx:35
virtual int_type overflow(int_type) override
Definition: field.hxx:279
row_size_type size_type
Definition: row.hxx:41
auto to(T &obj, T const &default_value) const -> typename std::enable_if<(not std::is_pointer< T >::value or std::is_same< T, char const * >::value), bool >::type
Read value into obj; or if null, use default value and return false.
Definition: field.hxx:148
result_size_type size_type
Definition: result.hxx:73
TRAITS traits_type
Definition: field.hxx:310
CHAR char_type
Definition: field.hxx:309
std::ios::openmode openmode
Definition: field.hxx:263
typename traits_type::pos_type pos_type
Definition: field.hxx:312
row_size_type num() const
Definition: field.hxx:91
T from_string(field const &value)
Convert a field's string contents to another type.
Definition: field.hxx:357
basic_fieldstream(field const &f)
Definition: field.hxx:315
oid type() const
Column type.
Definition: field.cxx:44
virtual int_type underflow() override
Definition: field.hxx:280
Low-level array parser.
Definition: array.hxx:46
bool operator!=(field const &rhs) const
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:72
Definition: field.hxx:255
const result & home() const noexcept
Definition: field.hxx:213
std::char_traits< char > traits_type
Definition: field.hxx:259
const char * name() const
Column name.
Definition: field.cxx:38
constexpr O< T > get() const
Return value wrapped in some optional type (empty for nulls).
Definition: field.hxx:193
Traits describing a type's "null value," if any.
Definition: strconv.hxx:86
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:175
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:349
const char * c_str() const
Read as plain C string.
Definition: field.cxx:62
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
Reference to a field in a result set.
Definition: field.hxx:32
row_size_type table_column() const
What column number in its originating table did this column come from?
Definition: field.cxx:56
bool operator==(field const &) const
Byte-by-byte comparison of two fields (all nulls are considered equal)
Definition: field.cxx:27
virtual int sync() override
Definition: field.hxx:269
typename traits_type::int_type int_type
Definition: field.hxx:311
typename traits_type::pos_type pos_type
Definition: field.hxx:261
Input stream that gets its data from a result field.
Definition: field.hxx:304
T as(T const &default_value) const
Return value as object of given type, or default value if null.
Definition: field.hxx:162
virtual pos_type seekpos(pos_type, openmode) override
Definition: field.hxx:275
char char_type
Definition: field.hxx:258