libpqxx  3.1.1
tablewriter.hxx
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * pqxx/tablewriter.hxx
5  *
6  * DESCRIPTION
7  * definition of the pqxx::tablewriter class.
8  * pqxx::tablewriter enables optimized batch updates to a database table
9  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablewriter.hxx 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_TABLEWRITER
20 #define PQXX_H_TABLEWRITER
21 
22 #include "pqxx/compiler-public.hxx"
23 #include "pqxx/compiler-internal-pre.hxx"
24 
25 #include "pqxx/tablestream"
26 
27 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
28  */
29 
30 namespace pqxx
31 {
32 class tablereader; // See pqxx/tablereader.h
33 
35 
45 class PQXX_LIBEXPORT tablewriter : public tablestream
46 {
47 public:
48  typedef unsigned size_type;
49 
51  const PGSTD::string &WName,
52  const PGSTD::string &Null=PGSTD::string()); //[t5]
53 
55 
61  template<typename ITER> tablewriter(transaction_base &,
62  const PGSTD::string &WName,
63  ITER begincolumns,
64  ITER endcolumns); //[t9]
65 
67 
79  template<typename ITER> tablewriter(transaction_base &T,
80  const PGSTD::string &WName,
81  ITER begincolumns,
82  ITER endcolumns,
83  const PGSTD::string &Null); //[t9]
84 
85  ~tablewriter() throw (); //[t5]
86 
87  template<typename IT> void insert(IT Begin, IT End); //[t5]
88  template<typename TUPLE> void insert(const TUPLE &); //[t5]
89  template<typename IT> void push_back(IT Begin, IT End); //[t10]
90  template<typename TUPLE> void push_back(const TUPLE &); //[t10]
91 
92  void reserve(size_type) {} //[t9]
93 
94  template<typename TUPLE> tablewriter &operator<<(const TUPLE &); //[t5]
95 
98 
100 
102  template<typename IT> PGSTD::string generate(IT Begin, IT End) const; //[t10]
103  template<typename TUPLE> PGSTD::string generate(const TUPLE &) const; //[t10]
104 
106 
113  virtual void complete(); //[t5]
114 
116  void write_raw_line(const PGSTD::string &);
117 
118 private:
119  void setup(transaction_base &,
120  const PGSTD::string &WName,
121  const PGSTD::string &Columns = PGSTD::string());
122 
123  void PQXX_PRIVATE writer_close();
124 };
125 
126 } // namespace pqxx
127 
128 
129 
130 namespace PGSTD
131 {
133 
136 template<>
137  class back_insert_iterator<pqxx::tablewriter> : //[t9]
138  public iterator<output_iterator_tag, void,void,void,void>
139 {
140 public:
141  explicit back_insert_iterator(pqxx::tablewriter &W) throw () : //[t83]
142  m_Writer(&W) {}
143 
144  back_insert_iterator &
145  operator=(const back_insert_iterator &rhs) throw () //[t83]
146  {
147  m_Writer = rhs.m_Writer;
148  return *this;
149  }
150 
151  template<typename TUPLE>
152  back_insert_iterator &operator=(const TUPLE &T) //[t83]
153  {
154  m_Writer->insert(T);
155  return *this;
156  }
157 
158  back_insert_iterator &operator++() { return *this; } //[t83]
159  back_insert_iterator &operator++(int) { return *this; } //[t83]
160  back_insert_iterator &operator*() { return *this; } //[t83]
161 
162 private:
163  pqxx::tablewriter *m_Writer;
164 };
165 
166 } // namespace PGSTD
167 
168 
169 namespace pqxx
170 {
171 
172 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
173  const PGSTD::string &WName,
174  ITER begincolumns,
175  ITER endcolumns) :
176  namedclass("tablewriter", WName),
177  tablestream(T, PGSTD::string())
178 {
179  setup(T, WName, columnlist(begincolumns, endcolumns));
180 }
181 
182 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
183  const PGSTD::string &WName,
184  ITER begincolumns,
185  ITER endcolumns,
186  const PGSTD::string &Null) :
187  namedclass("tablewriter", WName),
188  tablestream(T, Null)
189 {
190  setup(T, WName, columnlist(begincolumns, endcolumns));
191 }
192 
193 
194 namespace internal
195 {
196 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
197  const PGSTD::string &null);
198 
199 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
200  const PGSTD::string &null) { return Escape(s,null); }
201 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
202  const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
203 template<typename T> inline PGSTD::string EscapeAny(const T &t,
204  const PGSTD::string &null) { return Escape(to_string(t), null); }
205 
206 template<typename IT> class Escaper
207 {
208  const PGSTD::string &m_null;
209 public:
210  explicit Escaper(const PGSTD::string &null) : m_null(null) {}
211  PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
212 };
213 
214 }
215 
216 template<typename IT>
217 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
218 {
219  return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
220 }
221 
222 
223 template<typename TUPLE>
224 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
225 {
226  return generate(T.begin(), T.end());
227 }
228 
229 
230 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
231 {
232  write_raw_line(generate(Begin, End));
233 }
234 
235 
236 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
237 {
238  insert(T.begin(), T.end());
239 }
240 
241 template<typename IT>
242 inline void tablewriter::push_back(IT Begin, IT End)
243 {
244  insert(Begin, End);
245 }
246 
247 template<typename TUPLE>
248 inline void tablewriter::push_back(const TUPLE &T)
249 {
250  insert(T.begin(), T.end());
251 }
252 
253 template<typename TUPLE>
254 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
255 {
256  insert(T);
257  return *this;
258 }
259 
260 } // namespace pqxx
261 
262 
263 #include "pqxx/compiler-internal-post.hxx"
264 
265 #endif
266