libpqxx  4.0.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-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_TABLEWRITER
20 #define PQXX_H_TABLEWRITER
21 #include "pqxx/compiler-public.hxx"
22 #include "pqxx/compiler-internal-pre.hxx"
23 #include "pqxx/tablestream"
24 namespace pqxx
25 {
26 class tablereader;
28 
31 class PQXX_LIBEXPORT tablewriter : public tablestream
32 {
33 public:
34  typedef unsigned size_type;
36  const PGSTD::string &WName,
37  const PGSTD::string &Null=PGSTD::string());
38  template<typename ITER> tablewriter(transaction_base &,
39  const PGSTD::string &WName,
40  ITER begincolumns,
41  ITER endcolumns);
42  template<typename ITER> tablewriter(transaction_base &T,
43  const PGSTD::string &WName,
44  ITER begincolumns,
45  ITER endcolumns,
46  const PGSTD::string &Null);
47  ~tablewriter() throw ();
48  template<typename IT> void insert(IT Begin, IT End);
49  template<typename TUPLE> void insert(const TUPLE &);
50  template<typename IT> void push_back(IT Begin, IT End);
51  template<typename TUPLE> void push_back(const TUPLE &);
52  void reserve(size_type) {}
53  template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
55  template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
56  template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
57  virtual void complete();
58  void write_raw_line(const PGSTD::string &);
59 private:
60  void setup(transaction_base &,
61  const PGSTD::string &WName,
62  const PGSTD::string &Columns = PGSTD::string());
63  void PQXX_PRIVATE writer_close();
64 };
65 } // namespace pqxx
66 namespace PGSTD
67 {
68 template<>
69  class back_insert_iterator<pqxx::tablewriter> :
70  public iterator<output_iterator_tag, void,void,void,void>
71 {
72 public:
73  explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
74  m_Writer(&W) {}
75  back_insert_iterator &
76  operator=(const back_insert_iterator &rhs) throw ()
77  {
78  m_Writer = rhs.m_Writer;
79  return *this;
80  }
81  template<typename TUPLE>
82  back_insert_iterator &operator=(const TUPLE &T)
83  {
84  m_Writer->insert(T);
85  return *this;
86  }
87  back_insert_iterator &operator++() { return *this; }
88  back_insert_iterator &operator++(int) { return *this; }
89  back_insert_iterator &operator*() { return *this; }
90 private:
91  pqxx::tablewriter *m_Writer;
92 };
93 } // namespace PGSTD
94 namespace pqxx
95 {
96 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
97  const PGSTD::string &WName,
98  ITER begincolumns,
99  ITER endcolumns) :
100  namedclass("tablewriter", WName),
101  tablestream(T, PGSTD::string())
102 {
103  setup(T, WName, columnlist(begincolumns, endcolumns));
104 }
105 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
106  const PGSTD::string &WName,
107  ITER begincolumns,
108  ITER endcolumns,
109  const PGSTD::string &Null) :
110  namedclass("tablewriter", WName),
111  tablestream(T, Null)
112 {
113  setup(T, WName, columnlist(begincolumns, endcolumns));
114 }
115 namespace internal
116 {
117 PGSTD::string PQXX_LIBEXPORT Escape(
118  const PGSTD::string &s,
119  const PGSTD::string &null);
120 inline PGSTD::string EscapeAny(
121  const PGSTD::string &s,
122  const PGSTD::string &null)
123 { return Escape(s, null); }
124 inline PGSTD::string EscapeAny(
125  const char s[],
126  const PGSTD::string &null)
127 { return s ? Escape(PGSTD::string(s), null) : "\\N"; }
128 template<typename T> inline PGSTD::string EscapeAny(
129  const T &t,
130  const PGSTD::string &null)
131 { return Escape(to_string(t), null); }
132 template<typename IT> class Escaper
133 {
134  const PGSTD::string &m_null;
135 public:
136  explicit Escaper(const PGSTD::string &null) : m_null(null) {}
137  PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
138 };
139 }
140 template<typename IT>
141 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
142 {
143  return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
144 }
145 template<typename TUPLE>
146 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
147 {
148  return generate(T.begin(), T.end());
149 }
150 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
151 {
152  write_raw_line(generate(Begin, End));
153 }
154 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
155 {
156  insert(T.begin(), T.end());
157 }
158 template<typename IT>
159 inline void tablewriter::push_back(IT Begin, IT End)
160 {
161  insert(Begin, End);
162 }
163 template<typename TUPLE>
164 inline void tablewriter::push_back(const TUPLE &T)
165 {
166  insert(T.begin(), T.end());
167 }
168 template<typename TUPLE>
169 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
170 {
171  insert(T);
172  return *this;
173 }
174 } // namespace pqxx
175 #include "pqxx/compiler-internal-post.hxx"
176 #endif