libpqxx
tablewriter.hxx
1 
13 #ifndef PQXX_H_TABLEWRITER
14 #define PQXX_H_TABLEWRITER
15 
16 #include <iterator>
17 
18 #include "pqxx/compiler-public.hxx"
19 #include "pqxx/compiler-internal-pre.hxx"
20 
21 #include "pqxx/tablestream.hxx"
22 
23 
24 namespace pqxx
25 {
27 
30 class PQXX_LIBEXPORT tablewriter : public tablestream
31 {
32 public:
35  const std::string &WName,
36  const std::string &Null=std::string());
37  template<typename ITER> tablewriter(
39  const std::string &WName,
40  ITER begincolumns,
41  ITER endcolumns);
42  template<typename ITER> tablewriter(
44  const std::string &WName,
45  ITER begincolumns,
46  ITER endcolumns,
47  const std::string &Null);
48  ~tablewriter() noexcept;
49  template<typename IT> void insert(IT Begin, IT End);
50  template<typename TUPLE> void insert(const TUPLE &);
51  template<typename IT> void push_back(IT Begin, IT End);
52  template<typename TUPLE> void push_back(const TUPLE &);
53  template<typename SIZE> void reserve(SIZE) {}
54  template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
56  template<typename IT> std::string generate(IT Begin, IT End) const;
57  template<typename TUPLE> std::string generate(const TUPLE &) const;
58  virtual void complete() override;
59  void write_raw_line(const std::string &);
60 private:
61  void setup(
63  const std::string &WName,
64  const std::string &Columns = std::string());
65  PQXX_PRIVATE void writer_close();
66 };
67 } // namespace pqxx
68 
69 
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) noexcept :
78  m_writer(&W) {}
79 
80  back_insert_iterator &
81  operator=(const back_insert_iterator &rhs) noexcept
82  {
83  m_writer = rhs.m_writer;
84  return *this;
85  }
86 
87  template<typename TUPLE>
88  back_insert_iterator &operator=(const TUPLE &T)
89  {
90  m_writer->insert(T);
91  return *this;
92  }
93 
94  back_insert_iterator &operator++() { return *this; }
95  back_insert_iterator &operator++(int) { return *this; }
96  back_insert_iterator &operator*() { return *this; }
97 
98 private:
99  pqxx::tablewriter *m_writer;
100 };
101 } // namespace std
102 
103 
104 namespace pqxx
105 {
106 template<typename ITER> inline tablewriter::tablewriter(
107  transaction_base &T,
108  const std::string &WName,
109  ITER begincolumns,
110  ITER endcolumns) :
111  namedclass("tablewriter", WName),
112  tablestream(T, std::string())
113 {
114  setup(T, WName, columnlist(begincolumns, endcolumns));
115 }
116 
117 
118 template<typename ITER> inline tablewriter::tablewriter(
119  transaction_base &T,
120  const std::string &WName,
121  ITER begincolumns,
122  ITER endcolumns,
123  const std::string &Null) :
124  namedclass("tablewriter", WName),
125  tablestream(T, Null)
126 {
127  setup(T, WName, columnlist(begincolumns, endcolumns));
128 }
129 
130 
131 namespace internal
132 {
133 PQXX_LIBEXPORT std::string escape(
134  const std::string &s,
135  const std::string &null);
136 
137 inline std::string escape_any(
138  const std::string &s,
139  const std::string &null)
140 { return escape(s, null); }
141 
142 inline std::string escape_any(
143  const char s[],
144  const std::string &null)
145 { return s ? escape(std::string(s), null) : "\\N"; }
146 
147 template<typename T> inline std::string escape_any(
148  const T &t,
149  const std::string &null)
150 { return escape(to_string(t), null); }
151 
152 
153 template<typename IT> class Escaper
154 {
155  const std::string &m_null;
156 public:
157  explicit Escaper(const std::string &null) : m_null(null) {}
158  std::string operator()(IT i) const { return escape_any(*i, m_null); }
159 };
160 }
161 
162 
163 template<typename IT>
164 inline std::string tablewriter::generate(IT Begin, IT End) const
165 {
166  return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
167 }
168 template<typename TUPLE>
169 inline std::string tablewriter::generate(const TUPLE &T) const
170 {
171  return generate(T.begin(), T.end());
172 }
173 
174 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
175 {
176  write_raw_line(generate(Begin, End));
177 }
178 
179 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
180 {
181  insert(T.begin(), T.end());
182 }
183 
184 template<typename IT>
185 inline void tablewriter::push_back(IT Begin, IT End)
186 {
187  insert(Begin, End);
188 }
189 
190 template<typename TUPLE>
191 inline void tablewriter::push_back(const TUPLE &T)
192 {
193  insert(T.begin(), T.end());
194 }
195 
196 template<typename TUPLE>
197 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
198 {
199  insert(T);
200  return *this;
201 }
202 
203 } // namespace pqxx
204 #include "pqxx/compiler-internal-post.hxx"
205 #endif
tablewriter & operator<<(const TUPLE &)
Definition: tablewriter.hxx:197
void push_back(IT Begin, IT End)
Definition: tablewriter.hxx:185
Definition: tablewriter.hxx:30
void write_raw_line(const std::string &)
Definition: tablewriter.cxx:65
const std::string & NullStr() const
Definition: tablestream.hxx:34
Escaper(const std::string &null)
Definition: tablewriter.hxx:157
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a separated string.
Definition: util.hxx:96
tablewriter(transaction_base &, const std::string &WName, const std::string &Null=std::string())
Definition: tablewriter.cxx:22
namedclass(const std::string &Classname)
Definition: util.hxx:170
std::string operator()(IT i) const
Definition: tablewriter.hxx:158
std::string generate(IT Begin, IT End) const
Definition: tablewriter.hxx:164
Definition: tablewriter.hxx:153
Definition: tablestream.hxx:24
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:130
void insert(IT Begin, IT End)
Definition: tablewriter.hxx:174
Definition: tablereader.hxx:28
std::string escape_any(const std::string &s, const std::string &null)
Definition: tablewriter.hxx:137
STL namespace.
back_insert_iterator & operator*()
Definition: tablewriter.hxx:96
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const field &F)
Write a result field to any type of stream.
Definition: field.hxx:337
static std::string columnlist(ITER colbegin, ITER colend)
Definition: tablestream.hxx:49
void reserve(SIZE)
Definition: tablewriter.hxx:53
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
back_insert_iterator & operator++(int)
Definition: tablewriter.hxx:95
back_insert_iterator(pqxx::tablewriter &W) noexcept
Definition: tablewriter.hxx:77
back_insert_iterator & operator=(const TUPLE &T)
Definition: tablewriter.hxx:88
back_insert_iterator & operator++()
Definition: tablewriter.hxx:94
back_insert_iterator & operator=(const back_insert_iterator &rhs) noexcept
Definition: tablewriter.hxx:81