22#include "data/cassandra/impl/ManagedObject.hpp"
23#include "data/cassandra/impl/Tuple.hpp"
24#include "util/UnsupportedType.hpp"
26#include <boost/uuid/string_generator.hpp>
27#include <boost/uuid/uuid.hpp>
29#include <xrpl/basics/base_uint.h>
30#include <xrpl/protocol/AccountID.h>
45namespace data::cassandra::impl {
47template <
typename Type>
49extractColumn(CassRow
const* row, std::size_t idx)
54 auto throwErrorIfNeeded = [](CassError rc, std::string_view label) {
56 auto const tag =
'[' + std::string{label} +
']';
57 throw std::logic_error(tag +
": " + cass_error_desc(rc));
61 using DecayedType = std::decay_t<Type>;
62 using UintTupleType = std::tuple<uint32_t, uint32_t>;
63 using UCharVectorType = std::vector<unsigned char>;
65 if constexpr (std::is_same_v<DecayedType, ripple::uint256>) {
66 cass_byte_t
const* buf =
nullptr;
67 std::size_t bufSize = 0;
68 auto const rc = cass_value_get_bytes(cass_row_get_column(row, idx), &buf, &bufSize);
69 throwErrorIfNeeded(rc,
"Extract ripple::uint256");
70 output = ripple::uint256::fromVoid(buf);
71 }
else if constexpr (std::is_same_v<DecayedType, ripple::AccountID>) {
72 cass_byte_t
const* buf =
nullptr;
73 std::size_t bufSize = 0;
74 auto const rc = cass_value_get_bytes(cass_row_get_column(row, idx), &buf, &bufSize);
75 throwErrorIfNeeded(rc,
"Extract ripple::AccountID");
76 output = ripple::AccountID::fromVoid(buf);
77 }
else if constexpr (std::is_same_v<DecayedType, UCharVectorType>) {
78 cass_byte_t
const* buf =
nullptr;
79 std::size_t bufSize = 0;
80 auto const rc = cass_value_get_bytes(cass_row_get_column(row, idx), &buf, &bufSize);
81 throwErrorIfNeeded(rc,
"Extract vector<unsigned char>");
82 output = UCharVectorType{buf, buf + bufSize};
83 }
else if constexpr (std::is_same_v<DecayedType, UintTupleType>) {
84 auto const* tuple = cass_row_get_column(row, idx);
85 output = TupleIterator::fromTuple(tuple).extract<uint32_t, uint32_t>();
86 }
else if constexpr (std::is_convertible_v<DecayedType, std::string>) {
87 char const* value =
nullptr;
89 auto const rc = cass_value_get_string(cass_row_get_column(row, idx), &value, &len);
90 throwErrorIfNeeded(rc,
"Extract string");
91 output = std::string{value, len};
92 }
else if constexpr (std::is_same_v<DecayedType, bool>) {
93 cass_bool_t flag = cass_bool_t::cass_false;
94 auto const rc = cass_value_get_bool(cass_row_get_column(row, idx), &flag);
95 throwErrorIfNeeded(rc,
"Extract bool");
96 output = flag != cass_bool_t::cass_false;
99 else if constexpr (std::is_convertible_v<DecayedType, int64_t>) {
101 auto const rc = cass_value_get_int64(cass_row_get_column(row, idx), &out);
102 throwErrorIfNeeded(rc,
"Extract int64");
103 output =
static_cast<DecayedType
>(out);
104 }
else if constexpr (std::is_convertible_v<DecayedType, boost::uuids::uuid>) {
106 auto const rc = cass_value_get_uuid(cass_row_get_column(row, idx), &uuid);
107 throwErrorIfNeeded(rc,
"Extract uuid");
108 std::string uuidStr(CASS_UUID_STRING_LENGTH,
'0');
109 cass_uuid_string(uuid, uuidStr.data());
111 output = boost::uuids::string_generator{}(uuidStr);
121 Result(CassResult
const* ptr);
123 [[nodiscard]] std::size_t
129 template <
typename... RowTypes>
130 std::optional<std::tuple<RowTypes...>>
132 requires(std::tuple_size<std::tuple<RowTypes...>>{} > 1)
135 auto const* row = cass_result_first_row(*
this);
140 auto advanceId = [&idx]() {
return idx++; };
142 return std::make_optional<std::tuple<RowTypes...>>({extractColumn<RowTypes>(row, advanceId())...});
145 template <
typename RowType>
146 std::optional<RowType>
150 auto const* row = cass_result_first_row(*
this);
153 return std::make_optional<RowType>(extractColumn<RowType>(row, 0));
158 bool hasMore_ =
false;
164 fromResult(
Result const& result);
166 [[maybe_unused]]
bool
172 template <
typename... RowTypes>
173 std::tuple<RowTypes...>
174 extractCurrentRow()
const
178 auto const* row = cass_iterator_get_row(*
this);
181 auto advanceId = [&idx]() {
return idx++; };
183 return {extractColumn<RowTypes>(row, advanceId())...};
187template <
typename... Types>
189 std::reference_wrapper<Result const> ref_;
195 using iterator_category = std::input_iterator_tag;
196 using difference_type = std::size_t;
197 using value_type = std::tuple<Types...>;
205 operator=(
Iterator const&) =
delete;
210 return iterator_.extractCurrentRow<Types...>();
216 return iterator_.extractCurrentRow<Types...>();
222 iterator_.moveForward();
229 return not iterator_.hasMore();
243 return ResultIterator::fromResult(ref_);
Definition ManagedObject.hpp:28
Definition Result.hpp:188
Definition Result.hpp:157
static constexpr bool Unsupported
used for compile time checking of unsupported types
Definition UnsupportedType.hpp:26
Definition Result.hpp:194
Definition Result.hpp:192
Definition Result.hpp:120