ORM C++
Loading...
Searching...
No Matches
result_binding.cppm
Go to the documentation of this file.
1module;
2
3#include <exception>
4#include <optional>
5#include <sstream>
6#include <stdexcept>
7#include <string>
8#include <string_view>
9#include <type_traits>
10#include <utility>
11#include <variant>
12
13export module orm:database_result_binding;
14
15import orm.reflection;
16import :foundation;
17import :model;
18import :expressions;
19import :sql_contracts;
20export import :database_object_binding;
21export import :database_result_storage;
22
23namespace orm::db::binding
24{
25/**
26 * @brief Binding info.
27 *
28 * This struct is used to store information about a binding how to treat data of a specific type.
29 */
31{
32 bool joinedValues = false;
33};
34} // namespace orm::db::binding
35namespace orm::db::binding
36{
37template <typename Owner, typename Target, typename SchemaType, bool JoinedValues>
39{
40 static_assert(SchemaType::template contains<Owner> && SchemaType::template contains<Target>,
41 "Collection binding endpoints must belong to the selected Schema");
42 mutable Target value;
44};
45} // namespace orm::db::binding
46
47namespace orm::db::binding
48{
49template <typename T>
51{
52 mutable T value;
53};
54
55template <typename ResultField>
57{
58 static auto get(ResultField* /*field*/, const std::string& fieldName, const auto& /*values*/) -> void
59 {
60 throw ConversionError{"Unsupported projection result field type: " + fieldName};
61 }
62};
63
64template <typename ResultField>
65auto parseNumericProjectionValue(const std::string& value, const std::string& fieldName) -> ResultField
66{
67 return parseNumericValue<ResultField>(value, fieldName);
68}
69
70template <typename ResultField, typename StoredField>
71auto tryGetNumericProjectionValue(ResultField* field, const std::string& fieldName, const auto& values) -> bool
72{
73 try
74 {
75 *field = checkedNumericCast<ResultField>(values.template get<StoredField>(fieldName), fieldName);
76
77 return true;
78 }
79 catch (const ConversionError&)
80 {
81 throw;
82 }
83 catch (const std::exception&)
84 {
85 return false;
86 }
87}
88
89template <typename ResultField>
90auto getNumericProjectionValue(ResultField* field, const std::string& fieldName, const auto& values) -> void
91{
92 *field = getNumericValue<ResultField>(values, fieldName);
93}
94
95template <typename ResultField>
96auto readProjectedValue(const auto& values, const std::string& fieldName) -> ResultField
97{
98 if constexpr (UuidAdaptable<ResultField>)
99 {
100 return getEncodedScalarValue<ResultField>(values, fieldName);
101 }
102 else if constexpr (ValueConvertible<ResultField>)
103 {
104 using canonical_type = typename ValueConverter<ResultField>::Value;
105 try
106 {
109 else
110 {
111 canonical_type canonical{};
112 ObjectFieldFromProjectionValues<canonical_type>::get(&canonical, fieldName, values);
114 }
115 }
116 catch (const ConversionError&)
117 {
118 throw;
119 }
120 catch (const std::invalid_argument&)
121 {
122 throw ConversionError{"A database value was rejected by its converter"};
123 }
124 }
125 else
126 {
127 ResultField value{};
128 ObjectFieldFromProjectionValues<ResultField>::get(&value, fieldName, values);
129 return value;
130 }
131}
132
133template <ValueConvertible ResultField>
134// requires(!UuidAdaptable<ResultField>)
136{
137 static auto get(ResultField* field, const std::string& fieldName, const auto& values) -> void
138 {
139 *field = readProjectedValue<ResultField>(values, fieldName);
140 }
141};
142
143template <SociEncodedValue ResultField>
144struct ObjectFieldFromProjectionValues<ResultField>
145{
146 static auto get(ResultField* field, const std::string& fieldName, const auto& values) -> void
147 {
148 *field = getEncodedScalarValue<ResultField>(values, fieldName);
149 }
150};
151
152template <SociDefaultSupported ResultField>
153struct ObjectFieldFromProjectionValues<ResultField>
154{
155 static auto get(ResultField* field, const std::string& fieldName, const auto& values) -> void
156 {
157 if constexpr (std::is_arithmetic_v<ResultField>)
158 {
159 getNumericProjectionValue(field, fieldName, values);
160 }
161 else
162 {
163 *field = values.template get<ResultField>(fieldName);
164 }
165 }
166};
167
168template <NullableAdaptable ResultField>
169struct ObjectFieldFromProjectionValues<ResultField>
170{
171 static auto get(ResultField* field, const std::string& fieldName, const auto& values) -> void
172 {
173 if (values.get_indicator(fieldName) == soci::i_null)
174 {
175 if constexpr (isOptional<ResultField>)
176 field->reset();
177 else
179 return;
180 }
181
182 using contained_type = typename NullableAdapter<ResultField>::Value;
183 if constexpr (isOptional<ResultField>)
184 {
185 auto value = readProjectedValue<contained_type>(values, fieldName);
186 if constexpr (std::is_move_constructible_v<contained_type>)
187 field->emplace(std::move(value));
188 else
189 field->emplace(value);
190 }
191 else
193 }
194};
195
196template <typename ResultField, typename SociType>
198{
199 static auto get(ResultField* field, const std::string& fieldName, const auto& values) -> void
200 {
201 SociType value{};
202 getNumericProjectionValue(&value, fieldName, values);
203 *field = checkedNumericCast<ResultField>(value, fieldName);
204 }
205};
206
207template <SociConvertableToDouble ResultField>
208struct ObjectFieldFromProjectionValues<ResultField> : ObjectFieldFromProjectionValuesWithCast<ResultField, double>
209{
210};
211
212template <SociConvertableToInt ResultField>
213struct ObjectFieldFromProjectionValues<ResultField> : ObjectFieldFromProjectionValuesWithCast<ResultField, int>
214{
215};
216
217template <SociConvertableToLongLong ResultField>
218struct ObjectFieldFromProjectionValues<ResultField> : ObjectFieldFromProjectionValuesWithCast<ResultField, long long>
219{
220};
221
222template <SociConvertableToUnsignedLongLong ResultField>
223struct ObjectFieldFromProjectionValues<ResultField>
224 : ObjectFieldFromProjectionValuesWithCast<ResultField, unsigned long long>
225{
226};
227} // namespace orm::db::binding
228
229namespace orm::db::binding
230{
231inline auto bindNull(soci::values& values, std::string_view name, model::ColumnType logicalType) -> void
232{
233 switch (logicalType)
234 {
241 bindTypedNull(values, name, int{});
242 return;
245 bindTypedNull(values, name, static_cast<unsigned long long>(0));
246 return;
248 bindTypedNull(values, name, static_cast<long long>(0));
249 return;
252 bindTypedNull(values, name, 0.0);
253 return;
255 bindTypedNull(values, name, std::string{});
256 return;
262 bindTypedNull(values, name, std::string{});
263 return;
264 }
265
266 throw std::invalid_argument{"Cannot bind NULL parameter with unsupported column type"};
267}
268
269inline auto hasCompatibleStorage(const BoundValue& value) -> bool
270{
271 if (value.isNull())
272 {
273 return true;
274 }
275
276 return query::QueryValue::isCompatibleStorage(value.logicalType, value.value.value());
277}
278
279inline auto bindBoundValue(soci::values& values, std::string_view name, const BoundValue& value) -> void
280{
281 if (value.isNull())
282 {
283 bindNull(values, name, value.logicalType);
284 return;
285 }
286
287 if (not hasCompatibleStorage(value))
288 {
289 throw ConversionError{"Bound value storage does not match its logical column type"};
290 }
291
292 const auto parameterName = std::string{name};
293 std::visit([&values, &parameterName, columnType = value.logicalType](const auto& storedValue)
294 { setScalarFieldValue(values, parameterName, storedValue, columnType); }, value.value.value());
295}
296
297inline auto bindStatementParameter(soci::values& values, const StatementParameter& parameter) -> void
298{
299 bindBoundValue(values, parameter.name, parameter.getBoundValue());
300}
301} // namespace orm::db::binding
Internal signal for a result value that cannot be represented by the requested model or projection fi...
Definition numeric.cppm:83
static auto isCompatibleStorage(model::ColumnType logicalType, const Value &storedValue) noexcept -> bool
Checks both the canonical variant alternative and its logical range.
Definition value.cppm:128
auto bindBoundValue(soci::values &values, std::string_view name, const BoundValue &value) -> void
auto getEncodedScalarValue(const auto &values, const std::string &name) -> T
Definition numeric.cppm:151
auto checkedNumericCast(Source value, std::string_view fieldName) -> Result
Definition numeric.cppm:229
auto getNumericValue(const auto &values, const std::string &fieldName) -> Result
Definition numeric.cppm:421
auto getNumericProjectionValue(ResultField *field, const std::string &fieldName, const auto &values) -> void
std::vector< query::QueryValue > PrimaryKey
auto parseNumericProjectionValue(const std::string &value, const std::string &fieldName) -> ResultField
auto makeNullableValue(Value value) -> Wrapper
Definition numeric.cppm:64
auto tryGetNumericProjectionValue(ResultField *field, const std::string &fieldName, const auto &values) -> bool
auto readProjectedValue(const auto &values, const std::string &fieldName) -> ResultField
auto bindStatementParameter(soci::values &values, const StatementParameter &parameter) -> void
auto hasCompatibleStorage(const BoundValue &value) -> bool
auto parseNumericValue(std::string_view value, std::string_view fieldName) -> Result
Definition numeric.cppm:338
auto bindTypedNull(soci::values &values, std::string_view name, const T &value) -> void
constexpr bool isOptional
Definition payload.cppm:121
auto bindNull(soci::values &values, std::string_view name, model::ColumnType logicalType) -> void
consteval auto logicalType() -> model::ColumnType
Definition mapping.cppm:602
static auto get(ResultField *field, const std::string &fieldName, const auto &values) -> void
static auto get(ResultField *field, const std::string &fieldName, const auto &values) -> void
static auto get(ResultField *, const std::string &fieldName, const auto &) -> void