-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRowSet.h
More file actions
174 lines (156 loc) · 4.94 KB
/
Copy pathRowSet.h
File metadata and controls
174 lines (156 loc) · 4.94 KB
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
/*
* MIT License
*
* Copyright (c) 2026 F.D.Castel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FBCPP_ROWSET_H
#define FBCPP_ROWSET_H
#include "fb-api.h"
#include "Row.h"
#include "SmartPtrs.h"
#include "NumericConverter.h"
#include "CalendarConverter.h"
#include "Descriptor.h"
#include "Exception.h"
#include <cassert>
#include <cstddef>
#include <span>
#include <vector>
///
/// fb-cpp namespace.
///
namespace fbcpp
{
class Statement;
///
/// @brief A disconnected buffer of rows fetched from a Statement's result set.
///
/// Rows are fetched into a contiguous buffer at construction time. After
/// construction the RowSet is independent of its source Statement and can
/// be used, moved, or destroyed freely.
///
class RowSet final
{
public:
///
/// @brief Fetches up to `maxRows` rows from the current result set of
/// `statement`.
///
/// The statement must have an open result set (i.e. `execute()` was
/// called and it is a SELECT-type statement). Rows are fetched via
/// `IResultSet::fetchNext()` directly into the internal buffer.
///
/// @param statement The statement with an open result set.
/// @param maxRows Maximum number of rows to fetch.
///
explicit RowSet(Statement& statement, unsigned maxRows);
///
/// @brief Fetches up to `maxRows` rows from the current result set of `statement`.
///
/// When `includeCurrentRow` is true, the current output message already fetched by `statement.execute()` is
/// copied as the first row before fetching the remaining rows from the result set.
///
explicit RowSet(Statement& statement, unsigned maxRows, bool includeCurrentRow);
RowSet(RowSet&& o) noexcept
: client{o.client},
count{o.count},
messageLength{o.messageLength},
buffer{std::move(o.buffer)},
descriptors{std::move(o.descriptors)},
statusWrapper{std::move(o.statusWrapper)},
numericConverter{std::move(o.numericConverter)},
calendarConverter{std::move(o.calendarConverter)}
{
o.count = 0;
o.messageLength = 0;
}
RowSet& operator=(RowSet&& o) noexcept
{
if (this != &o)
{
client = o.client;
count = o.count;
messageLength = o.messageLength;
buffer = std::move(o.buffer);
descriptors = std::move(o.descriptors);
statusWrapper = std::move(o.statusWrapper);
numericConverter = std::move(o.numericConverter);
calendarConverter = std::move(o.calendarConverter);
o.count = 0;
o.messageLength = 0;
}
return *this;
}
RowSet(const RowSet&) = delete;
RowSet& operator=(const RowSet&) = delete;
public:
///
/// @brief Returns the number of rows actually fetched.
///
unsigned getCount() const noexcept
{
return count;
}
///
/// @brief Returns the message length (in bytes) of each row.
///
unsigned getMessageLength() const noexcept
{
return messageLength;
}
///
/// @brief Returns a Row view for typed access to the row at `index`.
/// @param index Zero-based row index (must be < getCount()).
///
Row getRow(unsigned index)
{
assert(index < count);
return Row{*client, descriptors, getRawRow(index)};
}
///
/// @brief Returns a span over the raw data of the row at `index`.
/// @param index Zero-based row index (must be < getCount()).
///
std::span<const std::byte> getRawRow(unsigned index) const
{
assert(index < count);
const auto* data = buffer.data() + static_cast<std::size_t>(index) * messageLength;
return {data, messageLength};
}
///
/// @brief Returns the entire contiguous buffer containing all fetched rows.
///
const std::vector<std::byte>& getRawBuffer() const noexcept
{
return buffer;
}
private:
Client* client;
unsigned count = 0;
unsigned messageLength = 0;
std::vector<std::byte> buffer;
std::vector<Descriptor> descriptors;
impl::StatusWrapper statusWrapper;
impl::NumericConverter numericConverter;
impl::CalendarConverter calendarConverter;
};
} // namespace fbcpp
#endif // FBCPP_ROWSET_H