3#include <userver/decimal64/decimal64.hpp>
5#include <userver/storages/mysql/impl/binder_fwd.hpp>
6#include <userver/storages/mysql/impl/io/common_binders.hpp>
12template <
int Prec,
typename Policy>
15template <
int Prec,
typename Policy>
16using Decimal64Opt = std::optional<Decimal64<Prec, Policy>>;
21 template <
int Prec,
typename Policy>
22 DecimalWrapper(
const Decimal64<Prec, Policy>& decimal);
25 template <
int Prec,
typename Policy>
26 DecimalWrapper(Decimal64<Prec, Policy>& decimal);
29 template <
int Prec,
typename Policy>
30 DecimalWrapper(std::optional<Decimal64<Prec, Policy>>& opt_decimal);
38 std::string GetValue()
const;
40 void Restore(std::string_view db_representation)
const;
43 static void RestoreCb(
void* source, std::string_view db_representation) {
44 auto* decimal =
static_cast<T*>(source);
47 *decimal = T{db_representation};
51 static void RestoreOptionalCb(
void* source,
52 std::string_view db_representation) {
53 auto* optional =
static_cast<std::optional<T>*>(source);
56 optional->emplace(db_representation);
60 static std::string GetValueCb(
void* source) {
61 auto* decimal =
static_cast<T*>(source);
64 return ToString(*decimal);
67 void* source_{
nullptr};
68 std::string (*get_value_cb_)(
void*){
nullptr};
69 void (*restore_cb_)(
void*, std::string_view){
nullptr};
72template <
int Prec,
typename Policy>
73DecimalWrapper::DecimalWrapper(
const Decimal64<Prec, Policy>& decimal)
76 : source_{
const_cast<Decimal64<Prec, Policy>*>(&decimal)},
79 get_value_cb_{GetValueCb<Decimal64<Prec, Policy>>} {}
81template <
int Prec,
typename Policy>
82DecimalWrapper::DecimalWrapper(Decimal64<Prec, Policy>& decimal)
86 restore_cb_{RestoreCb<Decimal64<Prec, Policy>>} {}
88template <
int Prec,
typename Policy>
89DecimalWrapper::DecimalWrapper(
90 std::optional<Decimal64<Prec, Policy>>& opt_decimal)
91 : source_{&opt_decimal},
94 restore_cb_{RestoreOptionalCb<Decimal64<Prec, Policy>>} {}
96void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
97 io::DecimalWrapper& val);
98void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
99 std::optional<io::DecimalWrapper>& val);
101void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
102 const io::DecimalWrapper& val);
103void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
104 const std::optional<io::DecimalWrapper>& val);
106template <
int Prec,
typename Policy>
107void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
108 ExplicitCRef<Decimal64<Prec, Policy>> field) {
109 DecimalWrapper wrapper{field.Get()};
111 storages::
mysql::impl::io::FreestandingBind(binds, pos, wrapper);
114template <
int Prec,
typename Policy>
115void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
116 ExplicitCRef<Decimal64Opt<Prec, Policy>> field) {
117 const auto wrapper = [&field]() -> std::optional<DecimalWrapper> {
118 if (field.Get().has_value()) {
119 return DecimalWrapper{*field.Get()};
125 storages::
mysql::impl::io::FreestandingBind(binds, pos, wrapper);
128template <
int Prec,
typename Policy>
129void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
130 ExplicitRef<Decimal64<Prec, Policy>> field) {
131 DecimalWrapper wrapper{field.Get()};
133 storages::
mysql::impl::io::FreestandingBind(binds, pos, wrapper);
136template <
int Prec,
typename Policy>
137void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
138 ExplicitRef<Decimal64Opt<Prec, Policy>> field) {
140 std::optional<DecimalWrapper> wrapper{field.Get()};
142 storages::mysql::impl::io::FreestandingBind(binds, pos, wrapper);