1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/** Definition of the pqxx::tablewriter class.
*
* pqxx::tablewriter enables optimized batch updates to a database table.
*
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablewriter.hxx instead.
*
* Copyright (c) 2000-2019, Jeroen T. Vermeulen.
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this mistake,
* or contact the author.
*/
#ifndef PQXX_H_TABLEWRITER
#define PQXX_H_TABLEWRITER
#include <iterator>
#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"
#include "pqxx/tablestream.hxx"
namespace pqxx
{
/// @deprecated Use stream_to instead.
/** Efficiently write data directly to a database table.
* @warning This class does not work reliably with multibyte encodings. Using
* it with some multi-byte encodings may pose a security risk.
*/
class PQXX_LIBEXPORT tablewriter : public tablestream
{
public:
PQXX_DEPRECATED tablewriter(
transaction_base &,
const std::string &WName,
const std::string &Null=std::string{});
template<typename ITER>
PQXX_DEPRECATED tablewriter(
transaction_base &,
const std::string &WName,
ITER begincolumns,
ITER endcolumns);
template<typename ITER>
PQXX_DEPRECATED tablewriter(
transaction_base &T,
const std::string &WName,
ITER begincolumns,
ITER endcolumns,
const std::string &Null);
~tablewriter() noexcept;
template<typename IT> void insert(IT Begin, IT End);
template<typename TUPLE> void insert(const TUPLE &);
template<typename IT> void push_back(IT Begin, IT End);
template<typename TUPLE> void push_back(const TUPLE &);
template<typename SIZE> void reserve(SIZE) {}
template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
tablewriter &operator<<(tablereader &);
template<typename IT> std::string generate(IT Begin, IT End) const;
template<typename TUPLE> std::string generate(const TUPLE &) const;
virtual void complete() override;
void write_raw_line(const std::string &);
private:
void set_up(
transaction_base &,
const std::string &WName,
const std::string &Columns = std::string{});
PQXX_PRIVATE void writer_close();
};
} // namespace pqxx
namespace std
{
template<>
class back_insert_iterator<pqxx::tablewriter>
{
public:
using iterator_category = output_iterator_tag;
explicit back_insert_iterator(pqxx::tablewriter &W) noexcept :
m_writer{&W} {}
back_insert_iterator &
operator=(const back_insert_iterator &rhs) noexcept
{
m_writer = rhs.m_writer;
return *this;
}
template<typename TUPLE>
back_insert_iterator &operator=(const TUPLE &T)
{
m_writer->insert(T);
return *this;
}
back_insert_iterator &operator++() { return *this; }
back_insert_iterator &operator++(int) { return *this; }
back_insert_iterator &operator*() { return *this; }
private:
pqxx::tablewriter *m_writer;
};
} // namespace std
namespace pqxx
{
template<typename ITER> inline tablewriter::tablewriter(
transaction_base &T,
const std::string &WName,
ITER begincolumns,
ITER endcolumns) :
namedclass{"tablewriter", WName},
tablestream{T, std::string{}}
{
set_up(T, WName, columnlist(begincolumns, endcolumns));
}
template<typename ITER> inline tablewriter::tablewriter(
transaction_base &T,
const std::string &WName,
ITER begincolumns,
ITER endcolumns,
const std::string &Null) :
namedclass{"tablewriter", WName},
tablestream{T, Null}
{
set_up(T, WName, columnlist(begincolumns, endcolumns));
}
namespace internal
{
PQXX_LIBEXPORT std::string escape(
const std::string &s,
const std::string &null);
inline std::string escape_any(
const std::string &s,
const std::string &null)
{ return escape(s, null); }
inline std::string escape_any(
const char s[],
const std::string &null)
{ return s ? escape(std::string{s}, null) : "\\N"; }
template<typename T> inline std::string escape_any(
const T &t,
const std::string &null)
{ return escape(to_string(t), null); }
template<typename IT> class Escaper
{
const std::string &m_null;
public:
explicit Escaper(const std::string &null) : m_null{null} {}
std::string operator()(IT i) const { return escape_any(*i, m_null); }
};
}
template<typename IT>
inline std::string tablewriter::generate(IT Begin, IT End) const
{
return separated_list("\t", Begin, End, internal::Escaper<IT>{NullStr()});
}
template<typename TUPLE>
inline std::string tablewriter::generate(const TUPLE &T) const
{
return generate(std::begin(T), std::end(T));
}
template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
{
write_raw_line(generate(Begin, End));
}
template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
{
insert(std::begin(T), std::end(T));
}
template<typename IT>
inline void tablewriter::push_back(IT Begin, IT End)
{
insert(Begin, End);
}
template<typename TUPLE>
inline void tablewriter::push_back(const TUPLE &T)
{
insert(std::begin(T), std::end(T));
}
template<typename TUPLE>
inline tablewriter &tablewriter::operator<<(const TUPLE &T)
{
insert(T);
return *this;
}
} // namespace pqxx
#include "pqxx/compiler-internal-post.hxx"
#endif
|