libpqxx: field.hxx Source File

libpqxx 4.0.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

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-2011, 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 #include "pqxx/strconv"
26
27
28 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
29 */
30
31 namespace pqxx
32 {
33 class result;
34 class tuple;
35
36 typedef unsigned int tuple_size_type;
37 typedef signed int tuple_difference_type;
38
40
43 class PQXX_LIBEXPORT field
44 {
45 public:
46 typedef size_t size_type;
47
49
53 field(const tuple &T, tuple_size_type C) throw (); //[t1]
54
59
60
76 bool operator==(const field &) const; //[t75]
77
79
81 bool operator!=(const field &rhs) const //[t82]
82 {return !operator==(rhs);}
84
89
90 const char *name() const; //[t11]
91
93 oid type() const; //[t7]
94
96 oid table() const; //[t2]
97
98 tuple_size_type num() const { return col(); } //[t82]
99
101 tuple_size_type table_column() const; //[t93]
103
108
109
114 const char *c_str() const; //[t2]
115
117 template<typename T> bool to(T &Obj) const //[t3]
118 {
119 const char *const bytes = c_str();
120 if (!bytes[0] && is_null()) return false;
121 from_string(bytes, Obj);
122 return true;
123 }
124
126 template<typename T> bool operator>>(T &Obj) const //[t7]
127 { return to(Obj); }
128
129 #ifdef PQXX_NO_PARTIAL_CLASS_TEMPLATE_SPECIALISATION
130
131 template<> bool to<PGSTD::string>(PGSTD::string &Obj) const;
132
134
137 template<> bool to<const char *>(const char *&Obj) const;
138 #endif
139
141 template<typename T> bool to(T &Obj, const T &Default) const //[t12]
142 {
143 const bool NotNull = to(Obj);
144 if (!NotNull) Obj = Default;
145 return NotNull;
146 }
147
149
152 template<typename T> T as(const T &Default) const //[t1]
153 {
154 T Obj;
155 to(Obj, Default);
156 return Obj;
157 }
158
160 template<typename T> T as() const //[t45]
161 {
162 T Obj;
163 const bool NotNull = to(Obj);
164 if (!NotNull) Obj = string_traits<T>::null();
165 return Obj;
166 }
167
168 bool is_null() const throw (); //[t12]
169 size_type size() const throw (); //[t11]
171
172
173 protected:
174 const result *home() const throw () { return m_home; }
175 size_t idx() const throw () { return m_row; }
176 tuple_size_type col() const throw () { return m_col; }
177
178 tuple_size_type m_col;
179
180 private:
181 const result *m_home;
182 size_t m_row;
183 };
184
185
187 template<>
188 inline bool field::to<PGSTD::string>(PGSTD::string &Obj) const
189 {
190 const char *const bytes = c_str();
191 if (!bytes[0] && is_null()) return false;
192 Obj = PGSTD::string(bytes, size());
193 return true;
194 }
195
197
202 template<>
203 inline bool field::to<const char *>(const char *&Obj) const
204 {
205 if (is_null()) return false;
206 Obj = c_str();
207 return true;
208 }
209
210
211 template<typename CHAR=char, typename TRAITS=PGSTD::char_traits<CHAR> >
212 class field_streambuf :
213 #ifdef PQXX_HAVE_STREAMBUF
214 public PGSTD::basic_streambuf<CHAR, TRAITS>
215 #else
216 public PGSTD::streambuf
217 #endif
218 {
219 public:
220 typedef CHAR char_type;
221 typedef TRAITS traits_type;
222 typedef typename traits_type::int_type int_type;
223 #ifdef PQXX_HAVE_STREAMBUF
224 typedef typename traits_type::pos_type pos_type;
225 typedef typename traits_type::off_type off_type;
226 #else
227 typedef streamoff off_type;
228 typedef streampos pos_type;
229 #endif
230 typedef PGSTD::ios::openmode openmode;
231 typedef PGSTD::ios::seekdir seekdir;
232
233 explicit field_streambuf(const field &F) : //[t74]
234 m_Field(F)
235 {
236 initialize();
237 }
238
239 #ifdef PQXX_HAVE_STREAMBUF
240 protected:
241 #endif
242 virtual int sync() { return traits_type::eof(); }
243
244 protected:
245 virtual pos_type seekoff(off_type, seekdir, openmode)
246 { return traits_type::eof(); }
247 virtual pos_type seekpos(pos_type, openmode) {return traits_type::eof();}
248 virtual int_type overflow(int_type) { return traits_type::eof(); }
249 virtual int_type underflow() { return traits_type::eof(); }
250
251 private:
252 const field &m_Field;
253
254 int_type initialize()
255 {
256 char_type *G =
257 reinterpret_cast<char_type *>(const_cast<char *>(m_Field.c_str()));
258 this->setg(G, G, G + m_Field.size());
259 return int_type(m_Field.size());
260 }
261 };
262
263
265
273 template<typename CHAR=char, typename TRAITS=PGSTD::char_traits<CHAR> >
274 class basic_fieldstream :
275 #ifdef PQXX_HAVE_STREAMBUF
276 public PGSTD::basic_istream<CHAR, TRAITS>
277 #else
278 public PGSTD::istream
279 #endif
280 {
281 #ifdef PQXX_HAVE_STREAMBUF
282 typedef PGSTD::basic_istream<CHAR, TRAITS> super;
283 #else
284 typedef PGSTD::istream super;
285 #endif
286
287 public:
288 typedef CHAR char_type;
289 typedef TRAITS traits_type;
290 typedef typename traits_type::int_type int_type;
291 typedef typename traits_type::pos_type pos_type;
292 typedef typename traits_type::off_type off_type;
293
294 basic_fieldstream(const field &F) : super(0), m_Buf(F)
295 { super::init(&m_Buf); }
296
297 private:
298 field_streambuf<CHAR, TRAITS> m_Buf;
299 };
300
301 typedef basic_fieldstream<char> fieldstream;
302
303 } // namespace pqxx
304
305
306 #include "pqxx/compiler-internal-post.hxx"
307
308 #endif