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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/** Implementation of libpqxx STL-style cursor classes.
*
* These classes wrap SQL cursors in STL-like interfaces.
*
* 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 <iterator>
#include "pqxx/cursor"
#include "pqxx/result"
#include "pqxx/strconv"
#include "pqxx/transaction"
#include "pqxx/internal/gates/icursor_iterator-icursorstream.hxx"
#include "pqxx/internal/gates/icursorstream-icursor_iterator.hxx"
using namespace pqxx;
using namespace pqxx::internal;
pqxx::cursor_base::difference_type pqxx::cursor_base::all() noexcept
{
// Implemented out-of-line so we don't fall afoul of Visual Studio defining
// min() and max() macros, which turn this expression into malformed code:
return std::numeric_limits<int>::max() - 1;
}
pqxx::cursor_base::difference_type cursor_base::backward_all() noexcept
{
// Implemented out-of-line so we don't fall afoul of Visual Studio defining
// min() and max() macros, which turn this expression into malformed code:
return std::numeric_limits<int>::min() + 1;
}
pqxx::cursor_base::cursor_base(
connection_base &context,
const std::string &Name,
bool embellish_name) :
m_name{embellish_name ? context.adorn_name(Name) : Name}
{
}
result::size_type pqxx::internal::obtain_stateless_cursor_size(sql_cursor &cur)
{
if (cur.endpos() == -1) cur.move(cursor_base::all());
return result::size_type(cur.endpos() - 1);
}
result pqxx::internal::stateless_cursor_retrieve(
sql_cursor &cur,
result::difference_type size,
result::difference_type begin_pos,
result::difference_type end_pos)
{
if (begin_pos < 0 or begin_pos > size)
throw range_error{"Starting position out of range"};
if (end_pos < -1) end_pos = -1;
else if (end_pos > size) end_pos = size;
if (begin_pos == end_pos) return cur.empty_result();
const int direction = ((begin_pos < end_pos) ? 1 : -1);
cur.move((begin_pos-direction) - (cur.pos()-1));
return cur.fetch(end_pos - begin_pos);
}
pqxx::icursorstream::icursorstream(
transaction_base &context,
const std::string &query,
const std::string &basename,
difference_type sstride) :
m_cur{context,
query,
basename,
cursor_base::forward_only,
cursor_base::read_only,
cursor_base::owned,
false},
m_stride{sstride},
m_realpos{0},
m_reqpos{0},
m_iterators{nullptr},
m_done{false}
{
set_stride(sstride);
}
pqxx::icursorstream::icursorstream(
transaction_base &context,
const field &cname,
difference_type sstride,
cursor_base::ownershippolicy op) :
m_cur{context, cname.c_str(), op},
m_stride{sstride},
m_realpos{0},
m_reqpos{0},
m_iterators{nullptr},
m_done{false}
{
set_stride(sstride);
}
void pqxx::icursorstream::set_stride(difference_type n)
{
if (n < 1)
throw argument_error{"Attempt to set cursor stride to " + to_string(n)};
m_stride = n;
}
result pqxx::icursorstream::fetchblock()
{
const result r{m_cur.fetch(m_stride)};
m_realpos += r.size();
if (r.empty()) m_done = true;
return r;
}
icursorstream &pqxx::icursorstream::ignore(std::streamsize n)
{
auto offset = m_cur.move(difference_type(n));
m_realpos += offset;
if (offset < n) m_done = true;
return *this;
}
icursorstream::size_type pqxx::icursorstream::forward(size_type n)
{
m_reqpos += difference_type(n) * m_stride;
return icursorstream::size_type(m_reqpos);
}
void pqxx::icursorstream::insert_iterator(icursor_iterator *i) noexcept
{
gate::icursor_iterator_icursorstream{*i}.set_next(m_iterators);
if (m_iterators)
gate::icursor_iterator_icursorstream{*m_iterators}.set_prev(i);
m_iterators = i;
}
void pqxx::icursorstream::remove_iterator(icursor_iterator *i) const noexcept
{
gate::icursor_iterator_icursorstream igate{*i};
if (i == m_iterators)
{
m_iterators = igate.get_next();
if (m_iterators)
gate::icursor_iterator_icursorstream{*m_iterators}.set_prev(nullptr);
}
else
{
auto prev = igate.get_prev(), next = igate.get_next();
gate::icursor_iterator_icursorstream{*prev}.set_next(next);
if (next) gate::icursor_iterator_icursorstream{*next}.set_prev(prev);
}
igate.set_prev(nullptr);
igate.set_next(nullptr);
}
void pqxx::icursorstream::service_iterators(difference_type topos)
{
if (topos < m_realpos) return;
using todolist = std::multimap<difference_type,icursor_iterator*>;
todolist todo;
for (icursor_iterator *i = m_iterators, *next; i; i = next)
{
gate::icursor_iterator_icursorstream gate{*i};
const auto ipos = gate.pos();
if (ipos >= m_realpos and ipos <= topos)
todo.insert(todolist::value_type(ipos, i));
next = gate.get_next();
}
const auto todo_end = std::end(todo);
for (auto i = std::begin(todo); i != todo_end; )
{
const auto readpos = i->first;
if (readpos > m_realpos) ignore(readpos - m_realpos);
const result r = fetchblock();
for ( ; i != todo_end and i->first == readpos; ++i)
gate::icursor_iterator_icursorstream{*i->second}.fill(r);
}
}
pqxx::icursor_iterator::icursor_iterator() noexcept :
m_pos{0}
{
}
pqxx::icursor_iterator::icursor_iterator(istream_type &s) noexcept :
m_stream{&s},
m_pos{difference_type(gate::icursorstream_icursor_iterator(s).forward(0))}
{
gate::icursorstream_icursor_iterator{*m_stream}.insert_iterator(this);
}
pqxx::icursor_iterator::icursor_iterator(const icursor_iterator &rhs)
noexcept :
m_stream{rhs.m_stream},
m_here{rhs.m_here},
m_pos{rhs.m_pos}
{
if (m_stream)
gate::icursorstream_icursor_iterator{*m_stream}.insert_iterator(this);
}
pqxx::icursor_iterator::~icursor_iterator() noexcept
{
if (m_stream)
gate::icursorstream_icursor_iterator{*m_stream}.remove_iterator(this);
}
icursor_iterator pqxx::icursor_iterator::operator++(int)
{
icursor_iterator old{*this};
m_pos = difference_type(
gate::icursorstream_icursor_iterator{*m_stream}.forward());
m_here.clear();
return old;
}
icursor_iterator &pqxx::icursor_iterator::operator++()
{
m_pos = difference_type(
gate::icursorstream_icursor_iterator{*m_stream}.forward());
m_here.clear();
return *this;
}
icursor_iterator &pqxx::icursor_iterator::operator+=(difference_type n)
{
if (n <= 0)
{
if (n == 0) return *this;
throw argument_error{"Advancing icursor_iterator by negative offset."};
}
m_pos = difference_type(
gate::icursorstream_icursor_iterator{*m_stream}.forward(
icursorstream::size_type(n)));
m_here.clear();
return *this;
}
icursor_iterator &
pqxx::icursor_iterator::operator=(const icursor_iterator &rhs) noexcept
{
if (rhs.m_stream == m_stream)
{
m_here = rhs.m_here;
m_pos = rhs.m_pos;
}
else
{
if (m_stream)
gate::icursorstream_icursor_iterator{*m_stream}.remove_iterator(this);
m_here = rhs.m_here;
m_pos = rhs.m_pos;
m_stream = rhs.m_stream;
if (m_stream)
gate::icursorstream_icursor_iterator{*m_stream}.insert_iterator(this);
}
return *this;
}
bool pqxx::icursor_iterator::operator==(const icursor_iterator &rhs) const
{
if (m_stream == rhs.m_stream) return pos() == rhs.pos();
if (m_stream and rhs.m_stream) return false;
refresh();
rhs.refresh();
return m_here.empty() and rhs.m_here.empty();
}
bool pqxx::icursor_iterator::operator<(const icursor_iterator &rhs) const
{
if (m_stream == rhs.m_stream) return pos() < rhs.pos();
refresh();
rhs.refresh();
return not m_here.empty();
}
void pqxx::icursor_iterator::refresh() const
{
if (m_stream)
gate::icursorstream_icursor_iterator{*m_stream}.service_iterators(pos());
}
void pqxx::icursor_iterator::fill(const result &r)
{
m_here = r;
}
|