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
|
/** Implementation of the pqxx::tablewriter class.
*
* pqxx::tablewriter enables optimized batch updates to a database table.
*
* 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.
*/
#include "pqxx/compiler-internal.hxx"
#include "pqxx/tablereader"
#include "pqxx/tablewriter"
#include "pqxx/transaction"
#include "pqxx/internal/gates/transaction-tablewriter.hxx"
using namespace pqxx::internal;
pqxx::tablewriter::tablewriter(
transaction_base &T,
const std::string &WName,
const std::string &Null) :
namedclass{"tablewriter", WName},
tablestream(T, Null)
{
set_up(T, WName);
}
pqxx::tablewriter::~tablewriter() noexcept
{
try
{
writer_close();
}
catch (const std::exception &e)
{
reg_pending_error(e.what());
}
}
void pqxx::tablewriter::set_up(
transaction_base &T,
const std::string &WName,
const std::string &Columns)
{
gate::transaction_tablewriter{T}.BeginCopyWrite(WName, Columns);
register_me();
}
pqxx::tablewriter &pqxx::tablewriter::operator<<(pqxx::tablereader &R)
{
std::string Line;
// TODO: Can we do this in binary mode? (Might require protocol version check)
while (R.get_raw_line(Line)) write_raw_line(Line);
return *this;
}
void pqxx::tablewriter::write_raw_line(const std::string &Line)
{
const std::string::size_type len = Line.size();
gate::transaction_tablewriter{m_trans}.write_copy_line(
((len == 0) or (Line[len-1] != '\n')) ?
Line :
std::string{Line, 0, len-1});
}
void pqxx::tablewriter::complete()
{
writer_close();
}
void pqxx::tablewriter::writer_close()
{
if (not is_finished())
{
base_close();
try
{
gate::transaction_tablewriter{m_trans}.end_copy_write();
}
catch (const std::exception &)
{
try { base_close(); } catch (const std::exception &) {}
throw;
}
}
}
namespace
{
inline char escapechar(char i) noexcept
{
char r = '\0';
switch (i)
{
case 8: r='b'; break; // backspace
case 11: r='v'; break; // vertical tab
case 12: r='f'; break; // form feed
case '\n': r='n'; break; // newline
case '\t': r='t'; break; // tab
case '\r': r='r'; break; // carriage return
case '\\': r='\\'; break; // backslash
}
return r;
}
inline bool unprintable(char i) noexcept
{
return i < ' ' or i > '~';
}
inline char tooctdigit(char c, int n)
{
using unsigned_char = unsigned char;
unsigned int i = unsigned_char(c);
return number_to_digit((i>>(3*n)) & 0x07);
}
} // namespace
std::string pqxx::internal::escape(
const std::string &s,
const std::string &null)
{
if (s == null) return "\\N";
if (s.empty()) return s;
std::string R;
R.reserve(s.size()+1);
for (const auto c: s)
{
const char e = escapechar(c);
if (e)
{
R += '\\';
R += e;
}
else if (unprintable(c))
{
R += "\\";
for (int n=2; n>=0; --n) R += tooctdigit(c, n);
}
else
{
R += c;
}
}
return R;
}
|