libpqxx  3.1.1
tablereader.hxx
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * pqxx/tablereader.hxx
5  *
6  * DESCRIPTION
7  * definition of the pqxx::tablereader class.
8  * pqxx::tablereader enables optimized batch reads from a database table
9  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablereader instead.
10  *
11  * Copyright (c) 2001-2008, 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_TABLEREADER
20 #define PQXX_H_TABLEREADER
21 
22 #include "pqxx/compiler-public.hxx"
23 #include "pqxx/compiler-internal-pre.hxx"
24 
25 #include "pqxx/result"
26 #include "pqxx/tablestream"
27 
28 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
29  */
30 
31 namespace pqxx
32 {
33 
35 
49 class PQXX_LIBEXPORT tablereader : public tablestream
50 {
51 public:
53  const PGSTD::string &Name,
54  const PGSTD::string &Null=PGSTD::string()); //[t6]
55 
57 
59  template<typename ITER>
61  const PGSTD::string &Name,
62  ITER begincolumns,
63  ITER endcolumns); //[t80]
64 
65  template<typename ITER> tablereader(transaction_base &,
66  const PGSTD::string &Name,
67  ITER begincolumns,
68  ITER endcolumns,
69  const PGSTD::string &Null); //[t80]
70 
71  ~tablereader() throw (); //[t6]
72 
73  template<typename TUPLE> tablereader &operator>>(TUPLE &); //[t8]
74 
75  operator bool() const throw () { return !m_Done; } //[t6]
76  bool operator!() const throw () { return m_Done; } //[t6]
77 
79 
83  bool get_raw_line(PGSTD::string &Line); //[t8]
84 
85  template<typename TUPLE>
86  void tokenize(PGSTD::string, TUPLE &) const; //[t8]
87 
89 
96  virtual void complete(); //[t8]
97 
98 private:
99  void setup(transaction_base &T,
100  const PGSTD::string &RName,
101  const PGSTD::string &Columns=PGSTD::string());
102  void PQXX_PRIVATE reader_close();
103  PGSTD::string extract_field(const PGSTD::string &,
104  PGSTD::string::size_type &) const;
105 
106  bool m_Done;
107 };
108 
109 
110 // TODO: Find meaningful definition of input iterator
111 
112 
113 template<typename ITER> inline
115  const PGSTD::string &Name,
116  ITER begincolumns,
117  ITER endcolumns) :
118  namedclass(Name, "tablereader"),
119  tablestream(T, PGSTD::string()),
120  m_Done(true)
121 {
122  setup(T, Name, columnlist(begincolumns, endcolumns));
123 }
124 
125 template<typename ITER> inline
127  const PGSTD::string &Name,
128  ITER begincolumns,
129  ITER endcolumns,
130  const PGSTD::string &Null) :
131  namedclass(Name, "tablereader"),
132  tablestream(T, Null),
133  m_Done(true)
134 {
135  setup(T, Name, columnlist(begincolumns, endcolumns));
136 }
137 
138 
139 template<typename TUPLE>
140 inline void tablereader::tokenize(PGSTD::string Line, TUPLE &T) const
141 {
142  PGSTD::back_insert_iterator<TUPLE> ins = PGSTD::back_inserter(T);
143 
144  // Filter and tokenize line, inserting tokens at end of T
145  PGSTD::string::size_type here=0;
146  while (here < Line.size()) *ins++ = extract_field(Line, here);
147 }
148 
149 
150 template<typename TUPLE>
152 {
153  PGSTD::string Line;
154  if (get_raw_line(Line)) tokenize(Line, T);
155  return *this;
156 }
157 
158 
159 } // namespace pqxx
160 
161 #include "pqxx/compiler-internal-post.hxx"
162 
163 #endif
164