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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/** Implementation of the pqxx::tablereader class.
*
* pqxx::tablereader enables optimized batch reads from 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/transaction"
#include "pqxx/internal/gates/transaction-tablereader.hxx"
using namespace pqxx::internal;
pqxx::tablereader::tablereader(
transaction_base &T,
const std::string &Name,
const std::string &Null) :
namedclass{"tablereader", Name},
tablestream(T, Null),
m_done{true}
{
set_up(T, Name);
}
void pqxx::tablereader::set_up(
transaction_base &T,
const std::string &Name,
const std::string &Columns)
{
gate::transaction_tablereader{T}.BeginCopyRead(Name, Columns);
register_me();
m_done = false;
}
pqxx::tablereader::~tablereader() noexcept
{
try
{
reader_close();
}
catch (const std::exception &e)
{
reg_pending_error(e.what());
}
}
bool pqxx::tablereader::get_raw_line(std::string &Line)
{
if (not m_done) try
{
m_done = not gate::transaction_tablereader{m_trans}.read_copy_line(Line);
}
catch (const std::exception &)
{
m_done = true;
throw;
}
return not m_done;
}
void pqxx::tablereader::complete()
{
reader_close();
}
void pqxx::tablereader::reader_close()
{
if (not is_finished())
{
base_close();
// If any lines remain to be read, consume them to not confuse PQendcopy()
if (not m_done)
{
try
{
std::string Dummy;
while (get_raw_line(Dummy)) ;
}
catch (const broken_connection &)
{
try { base_close(); } catch (const std::exception &) {}
throw;
}
catch (const std::exception &e)
{
reg_pending_error(e.what());
}
}
}
}
namespace
{
inline bool is_octalchar(char o) noexcept
{
return (o>='0') and (o<='7');
}
/// Find first tab character at or after start position in string
/** If not found, returns Line.size() rather than string::npos.
*/
std::string::size_type findtab(
const std::string &Line,
std::string::size_type start)
{
// TODO: Fix for multibyte encodings?
const auto here = Line.find('\t', start);
return (here == std::string::npos) ? Line.size() : here;
}
} // namespace
std::string pqxx::tablereader::extract_field(
const std::string &Line,
std::string::size_type &i) const
{
// TODO: Pick better exception types
std::string R;
bool isnull=false;
auto stop = findtab(Line, i);
for (; i < stop; ++i)
{
const char c = Line[i];
switch (c)
{
case '\n': // End of row
// Shouldn't happen, but we may get old-style, newline-terminated lines
i = stop;
break;
case '\\': // Escape sequence
{
const char n = Line[++i];
if (i >= Line.size())
throw failure{"Row ends in backslash."};
switch (n)
{
case 'N': // Null value
if (not R.empty())
throw failure{"Null sequence found in nonempty field."};
R = NullStr();
isnull = true;
break;
case '0': // Octal sequence (3 digits)
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
if ((i+2) >= Line.size())
throw failure{"Row ends in middle of octal value."};
const char n1 = Line[++i];
const char n2 = Line[++i];
if (not is_octalchar(n1) or not is_octalchar(n2))
throw failure{"Invalid octal in encoded table stream."};
R += char(
(digit_to_number(n)<<6) |
(digit_to_number(n1)<<3) |
digit_to_number(n2));
}
break;
case 'b':
// TODO: Escape code?
R += char(8);
break; // Backspace
case 'v':
// TODO: Escape code?
R += char(11);
break; // Vertical tab
case 'f':
// TODO: Escape code?
R += char(12);
break; // Form feed
case 'n':
R += '\n';
break; // Newline
case 't':
R += '\t';
break; // Tab
case 'r':
R += '\r';
break; // Carriage return;
default: // Self-escaped character
R += n;
// This may be a self-escaped tab that we thought was a terminator...
if (i == stop)
{
if ((i+1) >= Line.size())
throw internal_error{"COPY line ends in backslash."};
stop = findtab(Line, i+1);
}
break;
}
}
break;
default:
R += c;
break;
}
}
++i;
if (isnull and (R.size() != NullStr().size()))
throw failure{"Field contains data behind null sequence."};
return R;
}
|