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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
//
// RecordSet.cpp
//
// Library: Data
// Package: DataCore
// Module: RecordSet
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/RecordSet.h"
#include "Poco/Data/RowFilter.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/Time.h"
#include "Poco/Data/DataException.h"
#include "Poco/DateTime.h"
#include "Poco/UTFString.h"
using namespace Poco::Data::Keywords;
using Poco::DateTime;
using Poco::UTF16String;
namespace Poco {
namespace Data {
const std::size_t RecordSet::UNKNOWN_TOTAL_ROW_COUNT = std::numeric_limits<std::size_t>::max();
RecordSet::RecordSet(const Statement& rStatement,
RowFormatter::Ptr pRowFormatter):
Statement(rStatement),
_currentRow(0),
_pBegin(new RowIterator(this, 0 == rowsExtracted())),
_pEnd(new RowIterator(this, true)),
_totalRowCount(UNKNOWN_TOTAL_ROW_COUNT)
{
if (pRowFormatter) setRowFormatter(pRowFormatter);
}
RecordSet::RecordSet(Session& rSession,
const std::string& query,
RowFormatter::Ptr pRowFormatter):
Statement((rSession << query, now)),
_currentRow(0),
_pBegin(new RowIterator(this, 0 == rowsExtracted())),
_pEnd(new RowIterator(this, true)),
_totalRowCount(UNKNOWN_TOTAL_ROW_COUNT)
{
if (pRowFormatter) setRowFormatter(pRowFormatter);
}
RecordSet::RecordSet(const RecordSet& other):
Statement(other.impl()),
_currentRow(other._currentRow),
_pBegin(new RowIterator(this, 0 == rowsExtracted())),
_pEnd(new RowIterator(this, true)),
_pFilter(other._pFilter),
_totalRowCount(other._totalRowCount)
{
}
RecordSet::~RecordSet()
{
try
{
delete _pBegin;
delete _pEnd;
RowMap::iterator it = _rowMap.begin();
RowMap::iterator end = _rowMap.end();
for (; it != end; ++it) delete it->second;
}
catch (...)
{
poco_unexpected();
}
}
void RecordSet::reset(const Statement& stmt)
{
delete _pBegin;
_pBegin = 0;
delete _pEnd;
_pEnd = 0;
_currentRow = 0;
_totalRowCount = UNKNOWN_TOTAL_ROW_COUNT;
RowMap::iterator it = _rowMap.begin();
RowMap::iterator end = _rowMap.end();
for (; it != end; ++it) delete it->second;
_rowMap.clear();
Statement::operator = (stmt);
_pBegin = new RowIterator(this, 0 == rowsExtracted());
_pEnd = new RowIterator(this, true);
}
Poco::Dynamic::Var RecordSet::value(std::size_t col, std::size_t row, bool useFilter) const
{
if (useFilter && isFiltered() && !isAllowed(row))
throw InvalidAccessException("Row not allowed");
if (isNull(col, row)) return Poco::Dynamic::Var();
switch (columnType(col))
{
case MetaColumn::FDT_BOOL: return value<bool>(col, row, useFilter);
case MetaColumn::FDT_INT8: return value<Int8>(col, row, useFilter);
case MetaColumn::FDT_UINT8: return value<UInt8>(col, row, useFilter);
case MetaColumn::FDT_INT16: return value<Int16>(col, row, useFilter);
case MetaColumn::FDT_UINT16: return value<UInt16>(col, row, useFilter);
case MetaColumn::FDT_INT32: return value<Int32>(col, row, useFilter);
case MetaColumn::FDT_UINT32: return value<UInt32>(col, row, useFilter);
case MetaColumn::FDT_INT64: return value<Int64>(col, row, useFilter);
case MetaColumn::FDT_UINT64: return value<UInt64>(col, row, useFilter);
case MetaColumn::FDT_FLOAT: return value<float>(col, row, useFilter);
case MetaColumn::FDT_DOUBLE: return value<double>(col, row, useFilter);
case MetaColumn::FDT_STRING: return value<std::string>(col, row, useFilter);
case MetaColumn::FDT_WSTRING: return value<UTF16String>(col, row, useFilter);
case MetaColumn::FDT_BLOB: return value<BLOB>(col, row, useFilter);
case MetaColumn::FDT_CLOB: return value<CLOB>(col, row, useFilter);
case MetaColumn::FDT_DATE: return value<Date>(col, row, useFilter);
case MetaColumn::FDT_TIME: return value<Time>(col, row, useFilter);
case MetaColumn::FDT_TIMESTAMP: return value<DateTime>(col, row);
default:
throw UnknownTypeException("Data type not supported.");
}
}
Poco::Dynamic::Var RecordSet::value(const std::string& name, std::size_t row, bool useFilter) const
{
if (useFilter && isFiltered() && !isAllowed(row))
throw InvalidAccessException("Row not allowed");
if (isNull(metaColumn(name).position(), row)) return Poco::Dynamic::Var();
switch (columnType(name))
{
case MetaColumn::FDT_BOOL: return value<bool>(name, row, useFilter);
case MetaColumn::FDT_INT8: return value<Int8>(name, row, useFilter);
case MetaColumn::FDT_UINT8: return value<UInt8>(name, row, useFilter);
case MetaColumn::FDT_INT16: return value<Int16>(name, row, useFilter);
case MetaColumn::FDT_UINT16: return value<UInt16>(name, row, useFilter);
case MetaColumn::FDT_INT32: return value<Int32>(name, row, useFilter);
case MetaColumn::FDT_UINT32: return value<UInt32>(name, row, useFilter);
case MetaColumn::FDT_INT64: return value<Int64>(name, row, useFilter);
case MetaColumn::FDT_UINT64: return value<UInt64>(name, row, useFilter);
case MetaColumn::FDT_FLOAT: return value<float>(name, row, useFilter);
case MetaColumn::FDT_DOUBLE: return value<double>(name, row, useFilter);
case MetaColumn::FDT_STRING: return value<std::string>(name, row, useFilter);
case MetaColumn::FDT_WSTRING: return value<UTF16String>(name, row, useFilter);
case MetaColumn::FDT_BLOB: return value<BLOB>(name, row, useFilter);
case MetaColumn::FDT_DATE: return value<Date>(name, row, useFilter);
case MetaColumn::FDT_TIME: return value<Time>(name, row, useFilter);
case MetaColumn::FDT_TIMESTAMP: return value<DateTime>(name, row, useFilter);
default:
throw UnknownTypeException("Data type not supported.");
}
}
Row& RecordSet::row(std::size_t pos)
{
std::size_t rowCnt = rowCount();
if (0 == rowCnt || pos > rowCnt - 1)
throw RangeException("Invalid recordset row requested.");
RowMap::const_iterator it = _rowMap.find(pos);
Row* pRow = 0;
std::size_t columns = columnCount();
if (it == _rowMap.end())
{
if (_rowMap.size())
{
//reuse first row column names and sorting fields to save some memory
pRow = new Row(_rowMap.begin()->second->names(),
_rowMap.begin()->second->getSortMap(),
getRowFormatter());
for (std::size_t col = 0; col < columns; ++col)
pRow->set(col, value(col, pos));
}
else
{
pRow = new Row;
pRow->setFormatter(getRowFormatter());
for (std::size_t col = 0; col < columns; ++col)
pRow->append(metaColumn(static_cast<UInt32>(col)).name(), value(col, pos));
}
_rowMap.insert(RowMap::value_type(pos, pRow));
}
else
{
pRow = it->second;
poco_check_ptr (pRow);
}
return *pRow;
}
std::size_t RecordSet::rowCount() const
{
poco_assert (extractions().size());
std::size_t rc = subTotalRowCount();
if (!isFiltered()) return rc;
std::size_t counter = 0;
for (int row = 0; row < rc; ++row)
{
if (isAllowed(row)) ++counter;
}
return counter;
}
bool RecordSet::isAllowed(std::size_t row) const
{
if (!isFiltered()) return true;
return _pFilter->isAllowed(row);
}
bool RecordSet::moveFirst()
{
if (subTotalRowCount() > 0)
{
if (!isFiltered())
{
_currentRow = 0;
return true;
}
std::size_t currentRow = 0;
while (!isAllowed(currentRow))
{
if (currentRow >= subTotalRowCount() - 1) return false;
++currentRow;
}
_currentRow = currentRow;
return true;
}
else return false;
}
bool RecordSet::moveNext()
{
std::size_t currentRow = _currentRow;
do
{
if (currentRow >= subTotalRowCount() - 1) return false;
++currentRow;
} while (isFiltered() && !isAllowed(currentRow));
_currentRow = currentRow;
return true;
}
bool RecordSet::movePrevious()
{
std::size_t currentRow = _currentRow;
do
{
if (currentRow <= 0) return false;
--currentRow;
} while (isFiltered() && !isAllowed(currentRow));
_currentRow = currentRow;
return true;
}
bool RecordSet::moveLast()
{
if (subTotalRowCount() > 0)
{
std::size_t currentRow = subTotalRowCount() - 1;
if (!isFiltered())
{
_currentRow = currentRow;
return true;
}
while (!isAllowed(currentRow))
{
if (currentRow <= 0) return false;
--currentRow;
}
_currentRow = currentRow;
return true;
}
else return false;
}
void RecordSet::setRowFormatter(RowFormatter::Ptr pRowFormatter)
{
pRowFormatter->setTotalRowCount(static_cast<int>(getTotalRowCount()));
Statement::setRowFormatter(pRowFormatter);
RowMap::iterator it = _rowMap.begin();
RowMap::iterator end = _rowMap.end();
for (; it != end; ++it) it->second->setFormatter(getRowFormatter());
}
std::ostream& RecordSet::copyNames(std::ostream& os) const
{
std::string names = (*_pBegin)->namesToString();
if (!names.empty()) os << names;
return os;
}
std::ostream& RecordSet::copyValues(std::ostream& os, std::size_t offset, std::size_t length) const
{
RowIterator it = *_pBegin + offset;
RowIterator end = (RowIterator::POSITION_END != length) ? it + length : *_pEnd;
std::copy(it, end, std::ostream_iterator<Row>(os));
return os;
}
void RecordSet::formatValues(std::size_t offset, std::size_t length) const
{
RowIterator it = *_pBegin + offset;
RowIterator end = (RowIterator::POSITION_END != length) ? it + length : *_pEnd;
std::string val;
for (; it != end; ++it) it->formatValues();
}
std::ostream& RecordSet::copy(std::ostream& os, std::size_t offset, std::size_t length) const
{
RowFormatter& rf = const_cast<RowFormatter&>((*_pBegin)->getFormatter());
rf.setTotalRowCount(static_cast<int>(getTotalRowCount()));
if (RowFormatter::FORMAT_PROGRESSIVE == rf.getMode())
{
os << rf.prefix();
copyNames(os);
copyValues(os, offset, length);
os << rf.postfix();
}
else
{
formatNames();
formatValues(offset, length);
os << rf.toString();
}
return os;
}
void RecordSet::filter(const Poco::AutoPtr<RowFilter>& pFilter)
{
_pFilter = pFilter;
}
bool RecordSet::isFiltered() const
{
return _pFilter && !_pFilter->isEmpty();
}
void RecordSet::setTotalRowCount(const std::string& sql)
{
session() << sql, into(_totalRowCount), now;
}
} } // namespace Poco::Data
|