libpqxx  5.0
field.hxx
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * pqxx/field.hxx
5  *
6  * DESCRIPTION
7  * definitions for the pqxx::field class.
8  * pqxx::field refers to a field in a query result.
9  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/field instead.
10  *
11  * Copyright (c) 2001-2015, Jeroen T. Vermeulen <jtv@xs4all.nl>
12  *
13  * See COPYING for copyright license. If you did not receive a file called
14  * COPYING with this source code, please notify the distributor of this mistake,
15  * or contact the author.
16  *
17  *-------------------------------------------------------------------------
18  */
19 #ifndef PQXX_H_FIELD
20 #define PQXX_H_FIELD
21 
22 #include "pqxx/compiler-public.hxx"
23 #include "pqxx/compiler-internal-pre.hxx"
24 
25 #if defined(PQXX_HAVE_OPTIONAL)
26 #include <optional>
27 #endif
28 
29 #if defined(PQXX_HAVE_EXP_OPTIONAL)
30 #include <experimental/optional>
31 #endif
32 
33 #include "pqxx/strconv"
34 
35 
36 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
37  */
38 
39 namespace pqxx
40 {
41 class result;
42 class row;
43 
44 typedef unsigned int row_size_type;
45 typedef signed int row_difference_type;
46 
48 
51 class PQXX_LIBEXPORT field
52 {
53 public:
54  typedef size_t size_type;
55 
57 
61  field(const row &R, row_size_type C) PQXX_NOEXCEPT; //[t1]
62 
67 
84  bool operator==(const field &) const; //[t75]
85 
87 
89  bool operator!=(const field &rhs) const //[t82]
90  {return !operator==(rhs);}
92 
97  const char *name() const; //[t11]
99 
101  oid type() const; //[t7]
102 
104  oid table() const; //[t2]
105 
106  row_size_type num() const { return col(); } //[t82]
107 
109  row_size_type table_column() const; //[t93]
111 
116 
122  const char *c_str() const; //[t2]
123 
125  template<typename T> bool to(T &Obj) const //[t3]
126  {
127  const char *const bytes = c_str();
128  if (!bytes[0] && is_null()) return false;
129  from_string(bytes, Obj);
130  return true;
131  }
132 
134  template<typename T> bool operator>>(T &Obj) const //[t7]
135  { return to(Obj); }
136 
137 #ifdef PQXX_NO_PARTIAL_CLASS_TEMPLATE_SPECIALISATION
138  template<> bool to<std::string>(std::string &Obj) const;
140 
142 
145  template<> bool to<const char *>(const char *&Obj) const;
146 #endif
147 
149  template<typename T> bool to(T &Obj, const T &Default) const //[t12]
150  {
151  const bool NotNull = to(Obj);
152  if (!NotNull) Obj = Default;
153  return NotNull;
154  }
155 
157 
160  template<typename T> T as(const T &Default) const //[t1]
161  {
162  T Obj;
163  to(Obj, Default);
164  return Obj;
165  }
166 
168  template<typename T> T as() const //[t45]
169  {
170  T Obj;
171  const bool NotNull = to(Obj);
172  if (!NotNull) Obj = string_traits<T>::null();
173  return Obj;
174  }
175 
176 #if defined(PQXX_HAVE_OPTIONAL)
177  template<typename T> std::optional<T> get() const
179  { return get_opt<T, std::optional<T> >(); }
180 #endif
181 
182 #if defined(PQXX_HAVE_EXP_OPTIONAL)
183  template<typename T> std::experimental::optional<T> get() const
185  { return get_opt<T, std::experimental::optional<T> >(); }
186 #endif
187 
189  bool is_null() const PQXX_NOEXCEPT; //[t12]
190 
192 
195  size_type size() const PQXX_NOEXCEPT; //[t11]
197 
198 
199 protected:
200  const result *home() const PQXX_NOEXCEPT { return m_home; }
201  size_t idx() const PQXX_NOEXCEPT { return m_row; }
202  row_size_type col() const PQXX_NOEXCEPT { return m_col; }
203 
205 
206 private:
208 
213  template<typename T, typename OPTIONAL_T> OPTIONAL_T get_opt() const
214  {
215  if (is_null()) return OPTIONAL_T();
216  else return OPTIONAL_T(as<T>());
217  }
218 
219  const result *m_home;
220  size_t m_row;
221 };
222 
223 
225 template<>
226 inline bool field::to<std::string>(std::string &Obj) const
227 {
228  const char *const bytes = c_str();
229  if (!bytes[0] && is_null()) return false;
230  Obj = std::string(bytes, size());
231  return true;
232 }
233 
235 
240 template<>
241 inline bool field::to<const char *>(const char *&Obj) const
242 {
243  if (is_null()) return false;
244  Obj = c_str();
245  return true;
246 }
247 
248 
249 template<typename CHAR=char, typename TRAITS=std::char_traits<CHAR> >
251  public std::basic_streambuf<CHAR, TRAITS>
252 {
253 public:
254  typedef CHAR char_type;
255  typedef TRAITS traits_type;
256  typedef typename traits_type::int_type int_type;
257  typedef typename traits_type::pos_type pos_type;
258  typedef typename traits_type::off_type off_type;
259  typedef std::ios::openmode openmode;
260  typedef std::ios::seekdir seekdir;
261 
262  explicit field_streambuf(const field &F) : //[t74]
263  m_Field(F)
264  {
265  initialize();
266  }
267 
268 protected:
269  virtual int sync() PQXX_OVERRIDE { return traits_type::eof(); }
270 
271 protected:
272  virtual pos_type seekoff(off_type, seekdir, openmode) PQXX_OVERRIDE
273  { return traits_type::eof(); }
274  virtual pos_type seekpos(pos_type, openmode) PQXX_OVERRIDE
275  {return traits_type::eof();}
276  virtual int_type overflow(int_type) PQXX_OVERRIDE
277  { return traits_type::eof(); }
278  virtual int_type underflow() PQXX_OVERRIDE
279  { return traits_type::eof(); }
280 
281 private:
282  const field &m_Field;
283 
284  int_type initialize()
285  {
286  char_type *G =
287  reinterpret_cast<char_type *>(const_cast<char *>(m_Field.c_str()));
288  this->setg(G, G, G + m_Field.size());
289  return int_type(m_Field.size());
290  }
291 };
292 
293 
295 
303 template<typename CHAR=char, typename TRAITS=std::char_traits<CHAR> >
305  public std::basic_istream<CHAR, TRAITS>
306 {
307  typedef std::basic_istream<CHAR, TRAITS> super;
308 
309 public:
310  typedef CHAR char_type;
311  typedef TRAITS traits_type;
312  typedef typename traits_type::int_type int_type;
313  typedef typename traits_type::pos_type pos_type;
314  typedef typename traits_type::off_type off_type;
315 
316  basic_fieldstream(const field &F) : super(0), m_Buf(F)
317  { super::init(&m_Buf); }
318 
319 private:
321 };
322 
324 
325 } // namespace pqxx
326 
327 
328 #include "pqxx/compiler-internal-post.hxx"
329 
330 #endif
unsigned int row_size_type
Definition: field.hxx:42
TRAITS traits_type
Definition: field.hxx:311
std::ios::seekdir seekdir
Definition: field.hxx:260
Definition: field.hxx:250
traits_type::pos_type pos_type
Definition: field.hxx:313
size_t idx() const PQXX_NOEXCEPT
Definition: field.hxx:201
const char * c_str() const
Read as plain C string.
Definition: field.cxx:72
Reference to one row in a result.
Definition: row.hxx:52
bool to(T &Obj, const T &Default) const
Read value into Obj; or use Default &amp; return false if null.
Definition: field.hxx:149
Reference to a field in a result set.
Definition: field.hxx:51
traits_type::int_type int_type
Definition: field.hxx:312
virtual int_type overflow(int_type) PQXX_OVERRIDE
Definition: field.hxx:276
traits_type::pos_type pos_type
Definition: field.hxx:257
basic_fieldstream(const field &F)
Definition: field.hxx:316
virtual pos_type seekoff(off_type, seekdir, openmode) PQXX_OVERRIDE
Definition: field.hxx:272
bool to(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:125
row_size_type m_col
Definition: field.hxx:204
Input stream that gets its data from a result field.
Definition: field.hxx:304
virtual int sync() PQXX_OVERRIDE
Definition: field.hxx:269
signed int row_difference_type
Definition: field.hxx:45
traits_type::int_type int_type
Definition: field.hxx:256
basic_fieldstream< char > fieldstream
Definition: field.hxx:323
Traits class for use in string conversions.
Definition: strconv.hxx:46
virtual int_type underflow() PQXX_OVERRIDE
Definition: field.hxx:278
field_streambuf(const field &F)
Definition: field.hxx:262
T as() const
Return value as object of given type, or throw exception if null.
Definition: field.hxx:168
virtual pos_type seekpos(pos_type, openmode) PQXX_OVERRIDE
Definition: field.hxx:274
size_type size() const PQXX_NOEXCEPT
Return number of bytes taken up by the field&#39;s value.
Definition: field.cxx:84
std::ios::openmode openmode
Definition: field.hxx:259
traits_type::off_type off_type
Definition: field.hxx:258
size_t size_type
Definition: field.hxx:54
CHAR char_type
Definition: field.hxx:254
traits_type::off_type off_type
Definition: field.hxx:314
CHAR char_type
Definition: field.hxx:310
TRAITS traits_type
Definition: field.hxx:255
row_size_type col() const PQXX_NOEXCEPT
Definition: field.hxx:202
Result set containing data returned by a query or command.
Definition: result.hxx:78
row_size_type num() const
Definition: field.hxx:106
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: result.hxx:459
T as(const T &Default) const
Return value as object of given type, or Default if null.
Definition: field.hxx:160
bool operator!=(const field &rhs) const
Byte-by-byte comparison (all nulls are considered equal)
Definition: field.hxx:89
bool operator>>(T &Obj) const
Read value into Obj; or leave Obj untouched and return false if null.
Definition: field.hxx:134