libpqxx  7.0.2
field.hxx
1 /* Definitions for the pqxx::field class.
2  *
3  * pqxx::field refers to a field in a query result.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/field instead.
6  *
7  * Copyright (c) 2000-2020, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_FIELD
14 #define PQXX_H_FIELD
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include <optional>
20 
21 #include "pqxx/array.hxx"
22 #include "pqxx/result.hxx"
23 #include "pqxx/strconv.hxx"
24 #include "pqxx/types.hxx"
25 
26 namespace pqxx
27 {
29 
32 class PQXX_LIBEXPORT field
33 {
34 public:
36 
38 
42  field(row const &r, row_size_type c) noexcept;
43 
48 
65  [[nodiscard]] bool operator==(field const &) const;
66 
68 
70  [[nodiscard]] bool operator!=(field const &rhs) const
71  {
72  return not operator==(rhs);
73  }
75 
80  [[nodiscard]] char const *name() const;
82 
84  [[nodiscard]] oid type() const;
85 
87  [[nodiscard]] oid table() const;
88 
89  row_size_type num() const { return col(); }
90 
92  [[nodiscard]] row_size_type table_column() const;
94 
99  [[nodiscard]] std::string_view view() const
101  {
102  return std::string_view(c_str(), size());
103  }
104 
106 
111  [[nodiscard]] char const *c_str() const;
112 
114  [[nodiscard]] bool is_null() const noexcept;
115 
117 
120  [[nodiscard]] size_type size() const noexcept;
121 
123 
126  template<typename T>
127  auto to(T &obj) const -> typename std::enable_if<
128  (not std::is_pointer<T>::value or std::is_same<T, char const *>::value),
129  bool>::type
130  {
131  auto const bytes{c_str()};
132  if (bytes[0] == '\0' and is_null())
133  return false;
134  from_string(bytes, obj);
135  return true;
136  }
137 
139  template<typename T> bool operator>>(T &obj) const { return to(obj); }
140 
142 
145  template<typename T>
146  auto to(T &obj, T const &default_value) const -> typename std::enable_if<
147  (not std::is_pointer<T>::value or std::is_same<T, char const *>::value),
148  bool>::type
149  {
150  bool const has_value{to(obj)};
151  if (not has_value)
152  obj = default_value;
153  return has_value;
154  }
155 
157 
160  template<typename T> T as(T const &default_value) const
161  {
162  T obj;
163  to(obj, default_value);
164  return obj;
165  }
166 
168 
173  template<typename T> T as() const
174  {
175  T obj;
176  if (not to(obj))
177  {
178  if constexpr (nullness<T>::has_null)
179  obj = nullness<T>::null();
180  else
181  internal::throw_null_conversion(type_name<T>);
182  }
183  return obj;
184  }
185 
187 
190  template<typename T, template<typename> class O = std::optional>
191  constexpr O<T> get() const
192  {
193  return as<O<T>>();
194  }
195 
197 
204  {
205  return array_parser{c_str(), m_home.m_encoding};
206  }
208 
209 
210 protected:
211  result const &home() const noexcept { return m_home; }
212  result::size_type idx() const noexcept { return m_row; }
213  row_size_type col() const noexcept { return m_col; }
214 
220 
221 private:
222  result m_home;
223  result::size_type m_row;
224 };
225 
226 
228 template<> inline bool field::to<std::string>(std::string &obj) const
229 {
230  char const *const bytes = c_str();
231  if (bytes[0] == '\0' and is_null())
232  return false;
233  obj = std::string{bytes, size()};
234  return true;
235 }
236 
238 
243 template<> inline bool field::to<char const *>(char const *&obj) const
244 {
245  if (is_null())
246  return false;
247  obj = c_str();
248  return true;
249 }
250 
251 
252 template<typename CHAR = char, typename TRAITS = std::char_traits<CHAR>>
253 class field_streambuf : public std::basic_streambuf<CHAR, TRAITS>
254 {
255 public:
256  using char_type = CHAR;
257  using traits_type = TRAITS;
258  using int_type = typename traits_type::int_type;
259  using pos_type = typename traits_type::pos_type;
260  using off_type = typename traits_type::off_type;
261  using openmode = std::ios::openmode;
262  using seekdir = std::ios::seekdir;
263 
264  explicit field_streambuf(field const &f) : m_field{f} { initialize(); }
265 
266 protected:
267  virtual int sync() override { return traits_type::eof(); }
268 
270  {
271  return traits_type::eof();
272  }
273  virtual pos_type seekpos(pos_type, openmode) override
274  {
275  return traits_type::eof();
276  }
277  virtual int_type overflow(int_type) override { return traits_type::eof(); }
278  virtual int_type underflow() override { return traits_type::eof(); }
279 
280 private:
281  field const &m_field;
282 
283  int_type initialize()
284  {
285  auto g{static_cast<char_type *>(const_cast<char *>(m_field.c_str()))};
286  this->setg(g, g, g + m_field.size());
287  return int_type(m_field.size());
288  }
289 };
290 
291 
293 
301 template<typename CHAR = char, typename TRAITS = std::char_traits<CHAR>>
302 class basic_fieldstream : public std::basic_istream<CHAR, TRAITS>
303 {
304  using super = std::basic_istream<CHAR, TRAITS>;
305 
306 public:
307  using char_type = CHAR;
308  using traits_type = TRAITS;
309  using int_type = typename traits_type::int_type;
310  using pos_type = typename traits_type::pos_type;
311  using off_type = typename traits_type::off_type;
312 
313  basic_fieldstream(field const &f) : super{nullptr}, m_buf{f}
314  {
315  super::init(&m_buf);
316  }
317 
318 private:
320 };
321 
323 
325 
345 template<typename CHAR>
346 inline std::basic_ostream<CHAR> &
347 operator<<(std::basic_ostream<CHAR> &s, field const &value)
348 {
349  s.write(value.c_str(), std::streamsize(value.size()));
350  return s;
351 }
352 
353 
355 template<typename T> inline T from_string(field const &value)
356 {
357  return from_string<T>(value.view());
358 }
359 
361 template<> PQXX_LIBEXPORT std::string to_string(field const &value);
362 } // namespace pqxx
363 
364 #include "pqxx/internal/compiler-internal-post.hxx"
365 #endif
typename traits_type::pos_type pos_type
Definition: field.hxx:310
TRAITS traits_type
Definition: field.hxx:308
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:146
row_size_type col() const noexcept
Definition: field.hxx:213
bool is_null(TYPE const &value)
Is value null?
Definition: strconv.hxx:286
row_size_type m_col
Definition: field.hxx:219
virtual pos_type seekoff(off_type, seekdir, openmode) override
Definition: field.hxx:269
field_streambuf(field const &f)
Definition: field.hxx:264
Low-level array parser.
Definition: array.hxx:46
virtual int_type underflow() override
Definition: field.hxx:278
virtual pos_type seekpos(pos_type, openmode) override
Definition: field.hxx:273
CHAR char_type
Definition: field.hxx:307
std::ios::openmode openmode
Definition: field.hxx:261
typename traits_type::off_type off_type
Definition: field.hxx:260
virtual int_type overflow(int_type) override
Definition: field.hxx:277
T from_string(field const &value)
Convert a field&#39;s string contents to another type.
Definition: field.hxx:355
Definition: field.hxx:253
CHAR char_type
Definition: field.hxx:256
result const & home() const noexcept
Definition: field.hxx:211
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
std::string_view view() const
Read as string_view.
Definition: field.hxx:100
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
TRAITS traits_type
Definition: field.hxx:257
char const * c_str() const
Read as plain C string.
Definition: field.cxx:62
result::size_type idx() const noexcept
Definition: field.hxx:212
T as(T const &default_value) const
Return value as object of given type, or default value if null.
Definition: field.hxx:160
Traits describing a type&#39;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:173
bool operator!=(field const &rhs) const
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:70
Input stream that gets its data from a result field.
Definition: field.hxx:302
typename traits_type::off_type off_type
Definition: field.hxx:311
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&#39;s value.
Definition: field.cxx:74
row_size_type num() const
Definition: field.hxx:89
static TYPE null()
Return a null value.
result_size_type size_type
Definition: result.hxx:73
void throw_null_conversion(std::string const &type)
Definition: strconv.cxx:241
bool operator>>(T &obj) const
Read value into obj; or leave obj untouched and return false if null.
Definition: field.hxx:139
auto to(T &obj) 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, leave obj untouched and return false.
Definition: field.hxx:127
field_size_type size_type
Definition: field.hxx:35
typename traits_type::int_type int_type
Definition: field.hxx:258
Reference to one row in a result.
Definition: row.hxx:38
array_parser as_array() const
Parse the field as an SQL array.
Definition: field.hxx:203
std::ios::seekdir seekdir
Definition: field.hxx:262
typename traits_type::pos_type pos_type
Definition: field.hxx:259
virtual int sync() override
Definition: field.hxx:267
basic_fieldstream(field const &f)
Definition: field.hxx:313
Reference to a field in a result set.
Definition: field.hxx:32