fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
RowSet.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 F.D.Castel
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "RowSet.h"
26#include "Attachment.h"
27#include "Client.h"
28#include "Statement.h"
29#include <algorithm>
30
31using namespace fbcpp;
32using namespace fbcpp::impl;
33
34
35RowSet::RowSet(Statement& statement, unsigned maxRows)
36 : RowSet{statement, maxRows, false}
37{
38}
39
40RowSet::RowSet(Statement& statement, unsigned maxRows, bool includeCurrentRow)
41 : client{&statement.getAttachment().getClient()},
42 statusWrapper{statement.getAttachment().getClient()},
43 numericConverter{statement.getAttachment().getClient()},
44 calendarConverter{statement.getAttachment().getClient()}
45{
46 assert(statement.isValid());
47 assert(includeCurrentRow || statement.getResultSetHandle());
48
49 descriptors = statement.getOutputDescriptors();
50
51 auto outMetadata = statement.getOutputMetadata();
52 messageLength = outMetadata->getMessageLength(&statusWrapper);
53
54 buffer.resize(static_cast<std::size_t>(maxRows) * messageLength);
55
56 auto resultSet = statement.getResultSetHandle();
57 auto* dest = buffer.data();
58
59 if (includeCurrentRow && maxRows > 0u)
60 {
61 auto& currentRow = statement.getOutputMessage();
62 assert(currentRow.size() == messageLength);
63 std::copy(currentRow.begin(), currentRow.end(), dest);
64 dest += messageLength;
65 ++count;
66 }
67
68 if (!resultSet)
69 {
70 buffer.resize(static_cast<std::size_t>(dest - buffer.data()));
71 return;
72 }
73
74 for (unsigned i = count; i < maxRows; ++i)
75 {
76 if (resultSet->fetchNext(&statusWrapper, dest) != fb::IStatus::RESULT_OK)
77 break;
78
79 dest += messageLength;
80 ++count;
81 }
82
83 buffer.resize(static_cast<std::size_t>(dest - buffer.data()));
84}
A disconnected buffer of rows fetched from a Statement's result set.
Definition RowSet.h:56
RowSet(Statement &statement, unsigned maxRows)
Fetches up to maxRows rows from the current result set of statement.
Definition RowSet.cpp:35
Prepares, executes, and fetches SQL statements against a Firebird attachment.
Definition Statement.h:138
std::vector< std::byte > & getOutputMessage() noexcept
Provides direct access to the raw output message buffer.
Definition Statement.h:252
const std::vector< Descriptor > & getOutputDescriptors() noexcept
Provides cached descriptors for each output column.
Definition Statement.h:286
bool isValid() noexcept
Returns whether the Statement object is valid.
Definition Statement.h:202
FbRef< fb::IResultSet > getResultSetHandle() noexcept
Provides access to the underlying Firebird currently open result set handle, if any.
Definition Statement.h:220
FbRef< fb::IMessageMetadata > getOutputMetadata() noexcept
Returns the metadata describing columns produced by the statement.
Definition Statement.h:244
fb-cpp namespace.
Definition Attachment.h:45