Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/fb-cpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,49 @@ namespace fbcpp
setStruct(value, std::make_index_sequence<N>{});
}

///
/// @brief Retrieves all output columns into a tuple-like type.
/// @tparam T A tuple-like type (std::tuple, std::pair) whose elements match the output column count and types.
/// @return The populated tuple with values from the current row.
/// @throws FbCppException if element count mismatches output column count.
/// @throws FbCppException if a NULL value is encountered for a non-optional element.
///
template <TupleLike T>
T get()
{
using namespace impl::reflection;

constexpr std::size_t N = std::tuple_size_v<T>;

if (N != outDescriptors.size())
{
throw FbCppException("Tuple element count (" + std::to_string(N) +
") does not match output column count (" + std::to_string(outDescriptors.size()) + ")");
}

return getTuple<T>(std::make_index_sequence<N>{});
}

///
/// @brief Sets all input parameters from elements of a tuple-like type.
/// @tparam T A tuple-like type (std::tuple, std::pair) whose elements match the input parameter count.
/// @param value The tuple containing parameter values.
/// @throws FbCppException if element count mismatches input parameter count.
///
template <TupleLike T>
void set(const T& value)
{
constexpr std::size_t N = std::tuple_size_v<T>;

if (N != inDescriptors.size())
{
throw FbCppException("Tuple element count (" + std::to_string(N) +
") does not match input parameter count (" + std::to_string(inDescriptors.size()) + ")");
}

setTuple(value, std::make_index_sequence<N>{});
}

private:
///
/// @brief Validates and returns the descriptor for the given input parameter index.
Expand Down Expand Up @@ -2158,6 +2201,26 @@ namespace fbcpp
(set(static_cast<unsigned>(Is), std::get<Is>(tuple)), ...);
}

///
/// @brief Helper to retrieve all output columns into a tuple.
///
template <typename T, std::size_t... Is>
T getTuple(std::index_sequence<Is...>)
{
using namespace impl::reflection;

return T{getStructField<std::tuple_element_t<Is, T>>(static_cast<unsigned>(Is))...};
}

///
/// @brief Helper to set all input parameters from a tuple.
///
template <typename T, std::size_t... Is>
void setTuple(const T& value, std::index_sequence<Is...>)
{
(set(static_cast<unsigned>(Is), std::get<Is>(value)), ...);
}

///
/// @brief Converts and writes numeric parameter values following descriptor rules.
///
Expand Down
6 changes: 6 additions & 0 deletions src/fb-cpp/StructBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ namespace fbcpp
///
template <typename T>
concept Aggregate = std::is_aggregate_v<T> && !std::is_array_v<T> && !std::is_union_v<T>;

///
/// Concept constraining types to tuple-like types (std::tuple, std::pair, std::array).
///
template <typename T>
concept TupleLike = !Aggregate<T> && requires { typename std::tuple_size<T>::type; };
} // namespace fbcpp

namespace fbcpp::impl::reflection
Expand Down
125 changes: 125 additions & 0 deletions src/test/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2703,3 +2703,128 @@ BOOST_AUTO_TEST_CASE(setStructWithOptionalNull)
}

BOOST_AUTO_TEST_SUITE_END()


BOOST_AUTO_TEST_SUITE(TupleBindingSuite)

BOOST_AUTO_TEST_CASE(getTupleRetrievesAllColumns)
{
using ResultTuple = std::tuple<std::optional<std::int32_t>, std::optional<std::string>, std::optional<double>>;

const auto database = getTempFile("Statement-getTupleRetrievesAllColumns.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select 42, 'hello', 3.14e0 from rdb$database"};
BOOST_REQUIRE(stmt.execute(transaction));

const auto result = stmt.get<ResultTuple>();
BOOST_CHECK(std::get<0>(result).has_value());
BOOST_CHECK_EQUAL(std::get<0>(result).value(), 42);
BOOST_CHECK(std::get<1>(result).has_value());
BOOST_CHECK_EQUAL(std::get<1>(result).value(), "hello");
BOOST_CHECK(std::get<2>(result).has_value());
BOOST_CHECK_CLOSE(std::get<2>(result).value(), 3.14, 0.001);
}

BOOST_AUTO_TEST_CASE(setTupleSetsAllParameters)
{
using ParamTuple = std::tuple<std::int32_t, std::string_view>;

const auto database = getTempFile("Statement-setTupleSetsAllParameters.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select cast(? as integer), cast(? as varchar(50)) from rdb$database"};

stmt.set(ParamTuple{123, "test"});
BOOST_REQUIRE(stmt.execute(transaction));

BOOST_CHECK_EQUAL(stmt.getInt32(0).value(), 123);
BOOST_CHECK_EQUAL(stmt.getString(1).value(), "test");
}

BOOST_AUTO_TEST_CASE(getTupleElementCountMismatchThrows)
{
using WrongTuple = std::tuple<std::optional<std::int32_t>, std::optional<std::int32_t>>;

const auto database = getTempFile("Statement-getTupleElementCountMismatchThrows.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select 1, 2, 3 from rdb$database"};
BOOST_REQUIRE(stmt.execute(transaction));

BOOST_CHECK_THROW(stmt.get<WrongTuple>(), FbCppException);
}

BOOST_AUTO_TEST_CASE(setTupleElementCountMismatchThrows)
{
using WrongTuple = std::tuple<std::int32_t, std::int32_t, std::int32_t>;

const auto database = getTempFile("Statement-setTupleElementCountMismatchThrows.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select cast(? as integer) from rdb$database"};

BOOST_CHECK_THROW(stmt.set(WrongTuple{1, 2, 3}), FbCppException);
}

BOOST_AUTO_TEST_CASE(nullForNonOptionalTupleElementThrows)
{
using NonOptionalTuple = std::tuple<std::int32_t>;

const auto database = getTempFile("Statement-nullForNonOptionalTupleElementThrows.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select cast(null as integer) from rdb$database"};
BOOST_REQUIRE(stmt.execute(transaction));

BOOST_CHECK_THROW(stmt.get<NonOptionalTuple>(), FbCppException);
}

BOOST_AUTO_TEST_CASE(pairAsResultType)
{
using ResultPair = std::pair<std::optional<std::int32_t>, std::optional<std::string>>;

const auto database = getTempFile("Statement-pairAsResultType.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select 100, 'pair test' from rdb$database"};
BOOST_REQUIRE(stmt.execute(transaction));

const auto result = stmt.get<ResultPair>();
BOOST_CHECK(result.first.has_value());
BOOST_CHECK_EQUAL(result.first.value(), 100);
BOOST_CHECK(result.second.has_value());
BOOST_CHECK_EQUAL(result.second.value(), "pair test");
}

BOOST_AUTO_TEST_CASE(setTupleWithOptionalNull)
{
using ParamTuple = std::tuple<std::int32_t, std::optional<std::string_view>>;

const auto database = getTempFile("Statement-setTupleWithOptionalNull.fdb");
Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
Statement stmt{attachment, transaction, "select cast(? as integer), cast(? as varchar(50)) from rdb$database"};

stmt.set(ParamTuple{999, std::nullopt});
BOOST_REQUIRE(stmt.execute(transaction));

BOOST_CHECK_EQUAL(stmt.getInt32(0).value(), 999);
BOOST_CHECK(!stmt.getString(1).has_value());
}

BOOST_AUTO_TEST_SUITE_END()