16 #include "pqxx/compiler-public.hxx" 17 #include "pqxx/compiler-internal-pre.hxx" 18 #include "pqxx/internal/type_utils.hxx" 20 #if defined(PQXX_HAVE_OPTIONAL) 31 #elif defined(PQXX_HAVE_EXP_OPTIONAL) && !defined(PQXX_HIDE_EXP_OPTIONAL) 32 #include <experimental/optional> 35 #include "pqxx/array.hxx" 36 #include "pqxx/result.hxx" 37 #include "pqxx/strconv.hxx" 38 #include "pqxx/types.hxx" 82 bool operator==(
const field &)
const;
88 {
return not operator==(rhs);}
95 const char *name()
const;
120 const char *c_str()
const;
123 bool is_null() const noexcept;
129 size_type size() const noexcept;
135 template<typename T> auto to(T &Obj) const
136 -> typename std::enable_if<(
137 not std::is_pointer<T>::value
138 or std::is_same<T, const
char*>::value
141 const char *
const bytes = c_str();
142 if (bytes[0] ==
'\0' and is_null())
return false;
155 template<
typename T>
auto to(T &Obj,
const T &Default)
const 156 ->
typename std::enable_if<(
157 not std::is_pointer<T>::value
158 or std::is_same<T, const char*>::value
161 const bool NotNull = to(Obj);
162 if (not NotNull) Obj = Default;
170 template<
typename T> T
as(
const T &Default)
const 183 template<
typename T> T
as() const
195 template<
typename T,
template<
typename>
class O
196 #if defined(PQXX_HAVE_OPTIONAL) 198 #elif defined(PQXX_HAVE_EXP_OPTIONAL) && !defined(PQXX_HIDE_EXP_OPTIONAL) 199 = std::experimental::optional
201 > constexpr O<T>
get()
const {
return as<O<T>>(); }
217 size_t idx() const noexcept {
return m_row; }
234 inline bool field::to<std::string>(std::string &Obj)
const 236 const char *
const bytes = c_str();
237 if (bytes[0] ==
'\0' and is_null())
return false;
238 Obj = std::string{bytes, size()};
249 inline bool field::to<const char *>(
const char *&Obj)
const 257 template<
typename CHAR=
char,
typename TRAITS=std::
char_traits<CHAR>>
259 public std::basic_streambuf<CHAR, TRAITS>
277 virtual int sync()
override {
return traits_type::eof(); }
281 {
return traits_type::eof(); }
283 {
return traits_type::eof();}
285 {
return traits_type::eof(); }
287 {
return traits_type::eof(); }
290 const field &m_field;
295 reinterpret_cast<char_type *>(const_cast<char *>(m_field.
c_str()));
296 this->setg(G, G, G + m_field.
size());
311 template<
typename CHAR=
char,
typename TRAITS=std::
char_traits<CHAR>>
313 public std::basic_istream<CHAR, TRAITS>
315 using super = std::basic_istream<CHAR, TRAITS>;
325 { super::init(&m_buf); }
328 field_streambuf<CHAR, TRAITS> m_buf;
354 template<
typename CHAR>
356 std::basic_ostream<CHAR> &S,
const field &F)
358 S.write(F.
c_str(), std::streamsize(F.
size()));
369 template<> PQXX_LIBEXPORT std::string
to_string(
const field &Obj);
372 #include "pqxx/compiler-internal-post.hxx" T as(const T &Default) const
Return value as object of given type, or Default if null.
Definition: field.hxx:170
array_parser as_array() const
Parse the field as an SQL array.
Definition: field.hxx:210
auto to(T &Obj, const T &Default) const -> typename std::enable_if<(not std::is_pointer< T >::value or std::is_same< T, const char * >::value), bool >::type
Read value into Obj; or use Default & return false if null.
Definition: field.hxx:155
typename traits_type::int_type int_type
Definition: field.hxx:320
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
void from_string(const field &F, T &Obj)
Convert a field's string contents to another type.
Definition: field.hxx:365
typename traits_type::pos_type pos_type
Definition: field.hxx:321
virtual int_type underflow() override
Definition: field.hxx:286
Result set containing data returned by a query or command.
Definition: result.hxx:69
virtual pos_type seekoff(off_type, seekdir, openmode) override
Definition: field.hxx:280
CHAR char_type
Definition: field.hxx:262
bool is_null() const noexcept
Is this field's value null?
Definition: field.cxx:68
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
bool operator!=(const field &rhs) const
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:87
Reference to one row in a result.
Definition: row.hxx:40
TRAITS traits_type
Definition: field.hxx:263
typename traits_type::pos_type pos_type
Definition: field.hxx:265
Definition: field.hxx:258
long m_col
Definition: field.hxx:224
bool operator>>(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:148
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:52
size_t idx() const noexcept
Definition: field.hxx:217
row_size_type col() const noexcept
Definition: field.hxx:218
Low-level array parser.
Definition: array.hxx:46
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const field &F)
Write a result field to any type of stream.
Definition: field.hxx:355
basic_fieldstream(const field &F)
Definition: field.hxx:324
CHAR char_type
Definition: field.hxx:318
typename traits_type::int_type int_type
Definition: field.hxx:264
typename traits_type::off_type off_type
Definition: field.hxx:322
const result & home() const noexcept
Definition: field.hxx:216
Reference to a field in a result set.
Definition: field.hxx:49
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:183
std::ios::seekdir seekdir
Definition: field.hxx:268
Input stream that gets its data from a result field.
Definition: field.hxx:312
row_size_type num() const
Definition: field.hxx:104
std::ios::openmode openmode
Definition: field.hxx:267
Traits class for use in string conversions.
Definition: strconv.hxx:51
field_streambuf(const field &F)
Definition: field.hxx:270
virtual int sync() override
Definition: field.hxx:277
constexpr O< T > get() const
Return value wrapped in some optional type (empty for nulls)
Definition: field.hxx:201
const char * c_str() const
Read as plain C string.
Definition: field.cxx:62
TRAITS traits_type
Definition: field.hxx:319
virtual pos_type seekpos(pos_type, openmode) override
Definition: field.hxx:282
typename traits_type::off_type off_type
Definition: field.hxx:266
virtual int_type overflow(int_type) override
Definition: field.hxx:284
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24