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