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 : client{&statement.getAttachment().getClient()},
37 statusWrapper{statement.getAttachment().getClient()},
38 numericConverter{statement.getAttachment().getClient()},
39 calendarConverter{statement.getAttachment().getClient()}
40{
41 assert(statement.isValid());
42 assert(statement.hasCurrentRow() || statement.getResultSetHandle());
43
44 descriptors = statement.getOutputDescriptors();
45
46 auto outMetadata = statement.getOutputMetadata();
47 messageLength = outMetadata->getMessageLength(&statusWrapper);
48
49 buffer.resize(static_cast<std::size_t>(maxRows) * messageLength);
50
51 auto resultSet = statement.getResultSetHandle();
52 auto* dest = buffer.data();
53
54 if (statement.hasCurrentRow() && maxRows > 0u)
55 {
56 auto& currentRow = statement.getOutputMessage();
57 assert(currentRow.size() == messageLength);
58 std::copy(currentRow.begin(), currentRow.end(), dest);
59 dest += messageLength;
60 ++count;
61 statement.clearCurrentRow();
62 }
63
64 if (!resultSet)
65 {
66 buffer.resize(static_cast<std::size_t>(dest - buffer.data()));
67 return;
68 }
69
70 for (unsigned i = count; i < maxRows; ++i)
71 {
72 if (resultSet->fetchNext(&statusWrapper, dest) != fb::IStatus::RESULT_OK)
73 break;
74
75 dest += messageLength;
76 ++count;
77 }
78
79 buffer.resize(static_cast<std::size_t>(dest - buffer.data()));
80}
RowSet(Statement &statement, unsigned maxRows)
Fetches up to maxRows rows from statement.
Definition RowSet.cpp:35
Prepares, executes, and fetches SQL statements against a Firebird attachment.
Definition Statement.h:146
bool hasCurrentRow() const noexcept
Returns whether the statement is positioned on a current output row.
Definition Statement.h:224
std::vector< std::byte > & getOutputMessage() noexcept
Provides direct access to the raw output message buffer.
Definition Statement.h:274
const std::vector< Descriptor > & getOutputDescriptors() noexcept
Provides cached descriptors for each output column.
Definition Statement.h:308
bool isValid() noexcept
Returns whether the Statement object is valid.
Definition Statement.h:213
FbRef< fb::IResultSet > getResultSetHandle() noexcept
Provides access to the underlying Firebird currently open result set handle, if any.
Definition Statement.h:242
FbRef< fb::IMessageMetadata > getOutputMetadata() noexcept
Returns the metadata describing columns produced by the statement.
Definition Statement.h:266
fb-cpp namespace.
Definition Array.h:43