7#include <userver/storages/postgres/io/buffer_io.hpp>
8#include <userver/storages/postgres/io/buffer_io_base.hpp>
9#include <userver/storages/postgres/io/numeric_data.hpp>
10#include <userver/storages/postgres/io/type_mapping.hpp>
12#include <boost/multiprecision/cpp_dec_float.hpp>
14USERVER_NAMESPACE_BEGIN
16namespace storages::postgres {
18template <
unsigned Precision = 50>
19using MultiPrecision = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Precision>>;
23template <
unsigned Precision>
24struct BufferFormatter<MultiPrecision<Precision>> {
25 using Value = MultiPrecision<Precision>;
28 BufferFormatter(
const Value& val)
32 template <
typename Buffer>
33 void operator()(
const UserTypes&, Buffer& buf)
const {
34 auto str_rep = value.str(std::numeric_limits<Value>::max_digits10, std::ios_base::fixed);
35 auto bin_str = detail::StringToNumericBuffer(str_rep);
36 buf.reserve(buf.size() + bin_str.size());
37 std::copy(bin_str.begin(), bin_str.end(), std::back_inserter(buf));
41template <
unsigned Precision>
42struct BufferParser<MultiPrecision<Precision>> : detail::BufferParserBase<MultiPrecision<Precision>> {
43 using BaseType = detail::BufferParserBase<MultiPrecision<Precision>>;
44 using BaseType::BaseType;
45 using NumberType = boost::multiprecision::cpp_dec_float<Precision>;
47 void operator()(
const FieldBuffer& buffer) {
48 auto str = detail::NumericBufferToString(buffer);
51 this->value = NumberType{str.c_str()};
55template <
unsigned Precision>
56struct CppToSystemPg<MultiPrecision<Precision>> : PredefinedOid<PredefinedOids::kNumeric> {};