#pragma once #include "data/cassandra/impl/ManagedObject.hpp" #include #include #include #include #include #include #include namespace data::cassandra::impl { class Collection : public ManagedObject { static constexpr auto kDeleter = [](CassCollection* ptr) { cass_collection_free(ptr); }; static void throwErrorIfNeeded(CassError const rc, std::string_view const label) { if (rc == CASS_OK) return; auto const tag = '[' + std::string{label} + ']'; throw std::logic_error(tag + ": " + cass_error_desc(rc)); } public: /* implicit */ Collection(CassCollection* ptr); template explicit Collection(std::vector const& value) : ManagedObject{cass_collection_new(CASS_COLLECTION_TYPE_LIST, value.size()), kDeleter} { bind(value); } template void bind(std::vector const& values) const { for (auto const& value : values) append(value); } void append(bool const value) const { auto const rc = cass_collection_append_bool(*this, value ? cass_true : cass_false); throwErrorIfNeeded(rc, "Bind bool"); } void append(int64_t const value) const { auto const rc = cass_collection_append_int64(*this, value); throwErrorIfNeeded(rc, "Bind int64"); } void append(xrpl::uint256 const& value) const { auto const rc = cass_collection_append_bytes( *this, static_cast(static_cast(value.data())), xrpl::uint256::size() ); throwErrorIfNeeded(rc, "Bind xrpl::uint256"); } }; } // namespace data::cassandra::impl