22#include "data/cassandra/impl/ManagedObject.hpp"
23#include "data/cassandra/impl/Tuple.hpp"
24#include "util/UnsupportedType.hpp"
27#include <xrpl/basics/base_uint.h>
28#include <xrpl/protocol/AccountID.h>
43namespace data::cassandra::impl {
45template <
typename Type>
47extractColumn(CassRow
const* row, std::size_t idx)
52 auto throwErrorIfNeeded = [](CassError rc, std::string_view label) {
54 auto const tag =
'[' + std::string{label} +
']';
55 throw std::logic_error(tag +
": " + cass_error_desc(rc));
59 using DecayedType = std::decay_t<Type>;
60 using UintTupleType = std::tuple<uint32_t, uint32_t>;
61 using UCharVectorType = std::vector<unsigned char>;
63 if constexpr (std::is_same_v<DecayedType, ripple::uint256>) {
64 cass_byte_t
const* buf =
nullptr;
65 std::size_t bufSize = 0;
66 auto const rc = cass_value_get_bytes(cass_row_get_column(row, idx), &buf, &bufSize);
67 throwErrorIfNeeded(rc,
"Extract ripple::uint256");
68 output = ripple::uint256::fromVoid(buf);
69 }
else if constexpr (std::is_same_v<DecayedType, ripple::AccountID>) {
70 cass_byte_t
const* buf =
nullptr;
71 std::size_t bufSize = 0;
72 auto const rc = cass_value_get_bytes(cass_row_get_column(row, idx), &buf, &bufSize);
73 throwErrorIfNeeded(rc,
"Extract ripple::AccountID");
74 output = ripple::AccountID::fromVoid(buf);
75 }
else if constexpr (std::is_same_v<DecayedType, UCharVectorType>) {
76 cass_byte_t
const* buf =
nullptr;
77 std::size_t bufSize = 0;
78 auto const rc = cass_value_get_bytes(cass_row_get_column(row, idx), &buf, &bufSize);
79 throwErrorIfNeeded(rc,
"Extract vector<unsigned char>");
80 output = UCharVectorType{buf, buf + bufSize};
81 }
else if constexpr (std::is_same_v<DecayedType, UintTupleType>) {
82 auto const* tuple = cass_row_get_column(row, idx);
83 output = TupleIterator::fromTuple(tuple).extract<uint32_t, uint32_t>();
84 }
else if constexpr (std::is_convertible_v<DecayedType, std::string>) {
85 char const* value =
nullptr;
87 auto const rc = cass_value_get_string(cass_row_get_column(row, idx), &value, &len);
88 throwErrorIfNeeded(rc,
"Extract string");
89 output = std::string{value, len};
90 }
else if constexpr (std::is_same_v<DecayedType, bool>) {
91 cass_bool_t flag = cass_bool_t::cass_false;
92 auto const rc = cass_value_get_bool(cass_row_get_column(row, idx), &flag);
93 throwErrorIfNeeded(rc,
"Extract bool");
94 output = flag != cass_bool_t::cass_false;
97 else if constexpr (std::is_convertible_v<DecayedType, int64_t>) {
99 auto const rc = cass_value_get_int64(cass_row_get_column(row, idx), &out);
100 throwErrorIfNeeded(rc,
"Extract int64");
101 output =
static_cast<DecayedType
>(out);
111 Result(CassResult
const* ptr);
113 [[nodiscard]] std::size_t
119 template <
typename... RowTypes>
120 std::optional<std::tuple<RowTypes...>>
122 requires(std::tuple_size<std::tuple<RowTypes...>>{} > 1)
125 auto const* row = cass_result_first_row(*
this);
130 auto advanceId = [&idx]() {
return idx++; };
132 return std::make_optional<std::tuple<RowTypes...>>({extractColumn<RowTypes>(row, advanceId())...});
135 template <
typename RowType>
136 std::optional<RowType>
140 auto const* row = cass_result_first_row(*
this);
143 return std::make_optional<RowType>(extractColumn<RowType>(row, 0));
148 bool hasMore_ =
false;
154 fromResult(
Result const& result);
156 [[maybe_unused]]
bool
162 template <
typename... RowTypes>
163 std::tuple<RowTypes...>
164 extractCurrentRow()
const
168 auto const* row = cass_iterator_get_row(*
this);
171 auto advanceId = [&idx]() {
return idx++; };
173 return {extractColumn<RowTypes>(row, advanceId())...};
177template <
typename... Types>
179 std::reference_wrapper<Result const> ref_;
185 using iterator_category = std::input_iterator_tag;
186 using difference_type = std::size_t;
187 using value_type = std::tuple<Types...>;
195 operator=(
Iterator const&) =
delete;
200 return iterator_.extractCurrentRow<Types...>();
206 return iterator_.extractCurrentRow<Types...>();
212 iterator_.moveForward();
219 return not iterator_.hasMore();
233 return ResultIterator::fromResult(ref_);
Definition ManagedObject.hpp:28
Definition Result.hpp:178
Definition Result.hpp:147
static constexpr bool Unsupported
used for compile time checking of unsupported types
Definition UnsupportedType.hpp:26
Definition Result.hpp:184
Definition Result.hpp:182
Definition Result.hpp:110