libpqxx
field.hxx
1 
13 #ifndef PQXX_H_FIELD
14 #define PQXX_H_FIELD
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
18 
19 #if defined(PQXX_HAVE_OPTIONAL)
20 #include <optional>
21 #endif
22 
23 #if defined(PQXX_HAVE_EXP_OPTIONAL)
24 #include <experimental/optional>
25 #endif
26 
27 #include "pqxx/array.hxx"
28 #include "pqxx/result.hxx"
29 #include "pqxx/strconv.hxx"
30 #include "pqxx/types.hxx"
31 
32 
33 // Methods tested in eg. test module test01 are marked with "//[t01]".
34 
35 namespace pqxx
36 {
38 
41 class PQXX_LIBEXPORT field
42 {
43 public:
45 
47 
51  field(const row &R, row_size_type C) noexcept; //[t01]
52 
57 
74  bool operator==(const field &) const; //[t75]
75 
77 
79  bool operator!=(const field &rhs) const //[t82]
80  {return !operator==(rhs);}
82 
87  const char *name() const; //[t11]
89 
91  oid type() const; //[t07]
92 
94  oid table() const; //[t02]
95 
96  row_size_type num() const { return col(); } //[t82]
97 
99  row_size_type table_column() const; //[t93]
101 
106 
112  const char *c_str() const; //[t02]
113 
115  bool is_null() const noexcept; //[t12]
116 
118 
121  size_type size() const noexcept; //[t11]
122 
124  template<typename T> bool to(T &Obj) const //[t03]
125  {
126  const char *const bytes = c_str();
127  if (!bytes[0] && is_null()) return false;
128  from_string(bytes, Obj);
129  return true;
130  }
131 
133  template<typename T> bool operator>>(T &Obj) const //[t07]
134  { return to(Obj); }
135 
137  template<typename T> bool to(T &Obj, const T &Default) const //[t12]
138  {
139  const bool NotNull = to(Obj);
140  if (!NotNull) Obj = Default;
141  return NotNull;
142  }
143 
145 
148  template<typename T> T as(const T &Default) const //[t01]
149  {
150  T Obj;
151  to(Obj, Default);
152  return Obj;
153  }
154 
156  template<typename T> T as() const //[t45]
157  {
158  T Obj;
159  const bool NotNull = to(Obj);
160  if (!NotNull) Obj = string_traits<T>::null();
161  return Obj;
162  }
163 
164 #if defined(PQXX_HAVE_OPTIONAL)
165  template<typename T> std::optional<T> get() const
167  { return get_opt<T, std::optional<T>>(); }
168 #elif defined(PQXX_HAVE_EXP_OPTIONAL)
169  template<typename T> std::experimental::optional<T> get() const
171  { return get_opt<T, std::experimental::optional<T>>(); }
172 #endif
173 
175 
181  array_parser as_array() const { return array_parser(c_str()); }
183 
184 
185 protected:
186  const result &home() const noexcept { return m_home; }
187  size_t idx() const noexcept { return m_row; }
188  row_size_type col() const noexcept { return row_size_type(m_col); }
189 
194  long m_col;
195 
196 private:
198 
203  template<typename T, typename OPTIONAL_T> OPTIONAL_T get_opt() const
204  {
205  if (is_null()) return OPTIONAL_T();
206  else return OPTIONAL_T(as<T>());
207  }
208 
209  result m_home;
210  size_t m_row;
211 };
212 
213 
215 template<>
216 inline bool field::to<std::string>(std::string &Obj) const
217 {
218  const char *const bytes = c_str();
219  if (!bytes[0] && is_null()) return false;
220  Obj = std::string(bytes, size());
221  return true;
222 }
223 
225 
230 template<>
231 inline bool field::to<const char *>(const char *&Obj) const
232 {
233  if (is_null()) return false;
234  Obj = c_str();
235  return true;
236 }
237 
238 
239 template<typename CHAR=char, typename TRAITS=std::char_traits<CHAR>>
241  public std::basic_streambuf<CHAR, TRAITS>
242 {
243 public:
244  using char_type = CHAR;
245  using traits_type = TRAITS;
246  using int_type = typename traits_type::int_type;
247  using pos_type = typename traits_type::pos_type;
248  using off_type = typename traits_type::off_type;
249  using openmode = std::ios::openmode;
250  using seekdir = std::ios::seekdir;
251 
252  explicit field_streambuf(const field &F) : //[t74]
253  m_field(F)
254  {
255  initialize();
256  }
257 
258 protected:
259  virtual int sync() override { return traits_type::eof(); }
260 
261 protected:
263  { return traits_type::eof(); }
264  virtual pos_type seekpos(pos_type, openmode) override
265  {return traits_type::eof();}
266  virtual int_type overflow(int_type) override
267  { return traits_type::eof(); }
268  virtual int_type underflow() override
269  { return traits_type::eof(); }
270 
271 private:
272  const field &m_field;
273 
274  int_type initialize()
275  {
276  char_type *G =
277  reinterpret_cast<char_type *>(const_cast<char *>(m_field.c_str()));
278  this->setg(G, G, G + m_field.size());
279  return int_type(m_field.size());
280  }
281 };
282 
283 
285 
293 template<typename CHAR=char, typename TRAITS=std::char_traits<CHAR>>
295  public std::basic_istream<CHAR, TRAITS>
296 {
297  using super = std::basic_istream<CHAR, TRAITS>;
298 
299 public:
300  using char_type = CHAR;
301  using traits_type = TRAITS;
302  using int_type = typename traits_type::int_type;
303  using pos_type = typename traits_type::pos_type;
304  using off_type = typename traits_type::off_type;
305 
306  basic_fieldstream(const field &F) : super(nullptr), m_buf(F)
307  { super::init(&m_buf); }
308 
309 private:
311 };
312 
314 
316 
336 template<typename CHAR>
337 inline std::basic_ostream<CHAR> &operator<<(
338  std::basic_ostream<CHAR> &S, const field &F) //[t46]
339 {
340  S.write(F.c_str(), std::streamsize(F.size()));
341  return S;
342 }
343 
344 
346 template<typename T>
347 inline void from_string(const field &F, T &Obj) //[t46]
348  { from_string(F.c_str(), Obj, F.size()); }
349 
351 template<> PQXX_LIBEXPORT std::string to_string(const field &Obj); //[t74]
352 
353 } // namespace pqxx
354 #include "pqxx/compiler-internal-post.hxx"
355 #endif
typename traits_type::off_type off_type
Definition: field.hxx:248
virtual int_type overflow(int_type) override
Definition: field.hxx:266
row_size_type num() const
Definition: field.hxx:96
Reference to one row in a result.
Definition: row.hxx:40
bool operator!=(const field &rhs) const
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:79
Input stream that gets its data from a result field.
Definition: field.hxx:294
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
CHAR char_type
Definition: field.hxx:244
bool to(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:124
virtual int sync() override
Definition: field.hxx:259
Definition: field.hxx:240
virtual int_type underflow() override
Definition: field.hxx:268
Low-level array parser.
Definition: array.hxx:38
TRAITS traits_type
Definition: field.hxx:245
array_parser as_array() const
Parse the field as an SQL array.
Definition: field.hxx:181
basic_fieldstream(const field &F)
Definition: field.hxx:306
Result set containing data returned by a query or command.
Definition: result.hxx:67
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:156
size_t idx() const noexcept
Definition: field.hxx:187
const result & home() const noexcept
Definition: field.hxx:186
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: field.hxx:347
bool to(T &Obj, const T &Default) const
Read value into Obj; or use Default & return false if null.
Definition: field.hxx:137
TRAITS traits_type
Definition: field.hxx:301
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:337
virtual pos_type seekoff(off_type, seekdir, openmode) override
Definition: field.hxx:262
field_size_type size_type
Definition: field.hxx:44
unsigned int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
field_streambuf(const field &F)
Definition: field.hxx:252
row_size_type col() const noexcept
Definition: field.hxx:188
typename traits_type::pos_type pos_type
Definition: field.hxx:303
typename traits_type::int_type int_type
Definition: field.hxx:246
std::ios::openmode openmode
Definition: field.hxx:249
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
const char * c_str() const
Read as plain C string.
Definition: field.cxx:62
long m_col
Definition: field.hxx:194
std::ios::seekdir seekdir
Definition: field.hxx:250
bool operator>>(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:133
CHAR char_type
Definition: field.hxx:300
virtual pos_type seekpos(pos_type, openmode) override
Definition: field.hxx:264
typename traits_type::off_type off_type
Definition: field.hxx:304
Reference to a field in a result set.
Definition: field.hxx:41
Traits class for use in string conversions.
Definition: strconv.hxx:39
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
typename traits_type::pos_type pos_type
Definition: field.hxx:247
T as(const T &Default) const
Return value as object of given type, or Default if null.
Definition: field.hxx:148
size_type size() const noexcept
Return number of bytes taken up by the field&#39;s value.
Definition: field.cxx:74