19export import :database_payload;
23template <
typename ModelField>
28 if (values.get_indicator(fieldName) == soci::i_null)
39 canonical_type canonical;
42 else if constexpr (std::is_arithmetic_v<canonical_type>)
45 canonical = values.template get<canonical_type>(fieldName);
47 throw ConversionError{
"Unsupported value converter canonical type: " + fieldName};
53 catch (
const std::invalid_argument&)
55 throw ConversionError{
"A database value was rejected by its converter"};
62 else if constexpr (std::is_arithmetic_v<ModelField>)
68 return values.template get<ModelField>(fieldName);
72 throw ConversionError{
"Unsupported related model field type: " + fieldName};
78template <
typename Owner>
81 return std::format(
"{}_{}", owner->tableName, column.name);
84template <
bool JoinedValues>
88 if constexpr (JoinedValues)
90 return std::format(
"{}_{}", relationColumn.name, targetColumn.name);
94 return std::format(
"{}_{}_{}", owner->tableName, relationColumn.name, targetColumn.name);
98template <
typename Related,
typename SchemaType,
bool JoinedValues>
99auto hydrateRelated(Related& related,
const model::ModelView& owner,
const model::ColumnView& relationColumn,
100 const auto& values) ->
void
102 using related_t = std::remove_cv_t<Related>;
105 auto hydrate = [&]<
typename Index>(Index,
auto* field)
107 using field_t = std::decay_t<
decltype(*field)>;
114 constexpr auto* targetColumn = mappedColumn<related_t, SchemaType, Index::value>;
115 if constexpr (targetColumn !=
nullptr)
117 if constexpr (JoinedValues)
119 const auto alias = selectedRelatedAlias<true>(owner, relationColumn, *targetColumn);
122 else if (targetColumn->isPrimaryKey)
124 const auto alias = selectedRelatedAlias<false>(owner, relationColumn, *targetColumn);
134template <
typename Related,
typename SchemaType,
bool JoinedValues>
135[[nodiscard]]
auto relatedPrimaryKeyIsPresent(
const model::ModelView& owner,
const model::ColumnView& relationColumn,
136 const auto& values) ->
bool
139 bool hasPresentPrimaryKey{};
140 bool hasNullPrimaryKey{};
142 for (
const auto primaryKeyIndex : related->primaryKeyIndices)
144 const auto& targetColumn = related->columns[primaryKeyIndex];
145 const auto alias = selectedRelatedAlias<JoinedValues>(owner, relationColumn, targetColumn);
146 if (values.get_indicator(alias) == soci::i_null)
148 hasNullPrimaryKey =
true;
152 hasPresentPrimaryKey =
true;
156 if (not hasPresentPrimaryKey)
160 if (hasNullPrimaryKey)
162 throw ConversionError{
"Cannot hydrate optional relation with a partially null primary key"};
168template <
typename ModelField>
171 template <
typename T,
typename SchemaType,
bool JoinedValues>
179 using related_t = std::remove_cv_t<optional_value_t<ModelField>>;
183 if (not detail::relatedPrimaryKeyIsPresent<related_t, SchemaType, JoinedValues>(owner, column, values))
189 detail::hydrateRelated<related_t, SchemaType, JoinedValues>(related, owner, column, values);
194 detail::hydrateRelated<ModelField, SchemaType, JoinedValues>(*field, owner, column, values);
199 const auto alias = detail::selectedScalarAlias<T>(owner, column);
204 template <
typename T,
typename SchemaType,
bool JoinedValues, std::
size_t FieldIndex>
206 std::integral_constant<std::size_t, FieldIndex>,
const auto& values) ->
void
208 get(field, payload, payload.template columnDescriptor<FieldIndex>(), values);
211 template <
typename T,
typename SchemaType,
bool JoinedValues>
213 std::size_t fieldIndex,
const auto& values) ->
void
215 get(field, payload, payload.columnDescriptor(fieldIndex), values);
222inline auto bindTypedNull(soci::values& values, std::string_view name,
const T& value) ->
void
224 const auto parameterName = std::string{name};
228 values.set(parameterName, value);
229 values.set(parameterName, value, soci::i_null);
248 bindTypedNull(values, name,
static_cast<unsigned long long>(0));
269 throw std::invalid_argument{
"Cannot bind NULL value with unsupported column type"};
276 std::size_t characters{};
277 for (std::size_t offset = 0; offset < text.size();)
279 const auto first =
static_cast<unsigned char>(text[offset++]);
280 std::size_t remaining{};
281 unsigned long codePoint{};
282 unsigned long minimum{};
285 else if (first >= 0xc2 && first <= 0xdf)
288 codePoint = first & 0x1f;
291 else if (first >= 0xe0 && first <= 0xef)
294 codePoint = first & 0x0f;
297 else if (first >= 0xf0 && first <= 0xf4)
300 codePoint = first & 0x07;
305 if (remaining > text.size() - offset)
307 while (remaining > 0)
310 const auto continuation =
static_cast<unsigned char>(text[offset++]);
311 if ((continuation & 0xc0) != 0x80)
313 codePoint = (codePoint << 6) | (continuation & 0x3f);
315 if (codePoint < minimum || codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff))
317 if (++characters > *limit)
318 throw ConversionError{
"A string exceeds its declared character limit"};
322template <
typename ModelField>
325 std::optional<std::size_t> maxStringLength = std::nullopt) ->
void
344 catch (
const std::invalid_argument&)
371 if constexpr (std::is_arithmetic_v<ModelField>)
377 if constexpr (std::same_as<ModelField, std::string>)
379 values.set(fieldName, field);
384 throw std::invalid_argument{
"Unsupported related model field type: " + fieldName};
390template <
typename Related,
typename SchemaType>
391auto serializeRelated(
const Related& related,
const model::ColumnView& relationColumn, soci::values& values) ->
void
393 using related_t = std::remove_cv_t<Related>;
396 auto serialize = [&]<
typename Index>(Index,
const auto* field)
398 using field_t = std::decay_t<
decltype(*field)>;
405 constexpr auto* targetColumn = mappedColumn<related_t, SchemaType, Index::value>;
406 if constexpr (targetColumn !=
nullptr)
408 if (targetColumn->isPrimaryKey)
410 const auto parameterName = std::format(
"{}_{}", relationColumn.name, targetColumn->name);
412 targetColumn->maxStringLength);
421template <
typename Related,
typename SchemaType>
422auto setRelatedNull(soci::values& values,
const model::ColumnView& relationColumn) ->
void
425 for (
const auto primaryKeyIndex : related->primaryKeyIndices)
427 const auto& targetColumn = related->columns[primaryKeyIndex];
428 setNullValue(values, std::format(
"{}_{}", relationColumn.name, targetColumn.name), targetColumn.type.value());
433template <
typename ModelField>
436 template <
typename T,
typename SchemaType,
bool JoinedValues>
440 if (column.isAutoIncrement)
447 using related_t = std::remove_cv_t<optional_value_t<ModelField>>;
457 detail::setRelatedNull<related_t, SchemaType>(values, column);
462 detail::serializeRelated<ModelField, SchemaType>(*field, column, values);
467 setScalarFieldValue(values, std::string{column.name}, *field, column.type.value(), column.maxStringLength);
471 template <
typename T,
typename SchemaType,
bool JoinedValues, std::
size_t FieldIndex>
473 std::integral_constant<std::size_t, FieldIndex>, soci::values& values) ->
void
475 set(field, payload, payload.template columnDescriptor<FieldIndex>(), values);
478 template <
typename T,
typename SchemaType,
bool JoinedValues>
480 std::size_t fieldIndex, soci::values& values) ->
void
482 set(field, payload, payload.columnDescriptor(fieldIndex), values);
Internal signal for a result value that cannot be represented by the requested model or projection fi...
auto setNullValue(soci::values &values, const std::string &name, model::ColumnType type) -> void
auto getScalarFieldValue(const auto &values, const std::string &fieldName) -> ModelField
auto validateStringLength(std::string_view text, std::optional< std::size_t > limit) -> void
auto setScalarFieldValue(soci::values &values, const std::string &fieldName, const ModelField &field, model::ColumnType columnType, std::optional< std::size_t > maxStringLength=std::nullopt) -> void
auto getEncodedScalarValue(const auto &values, const std::string &name) -> T
auto encodeScalarValue(const T &value) -> std::string
auto checkedNumericCast(Source value, std::string_view fieldName) -> Result
auto getNumericValue(const auto &values, const std::string &fieldName) -> Result
auto makeNullableValue(Value value) -> Wrapper
auto bindTypedNull(soci::values &values, std::string_view name, const T &value) -> void
constexpr bool isNullable
constexpr auto modelView() noexcept -> ModelView
constexpr auto fieldPointers(T &value) noexcept
constexpr auto constexpr_for_tuple(Tuple &&t, F &&f, Args... args) -> void
constexpr bool is_relation_collection_v
static auto get(ModelField *field, const BindingPayload< T, SchemaType, JoinedValues > &payload, std::size_t fieldIndex, const auto &values) -> void
static auto get(ModelField *field, const BindingPayload< T, SchemaType, JoinedValues > &, const model::ColumnView &column, const auto &values) -> void
static auto get(ModelField *field, const BindingPayload< T, SchemaType, JoinedValues > &payload, std::integral_constant< std::size_t, FieldIndex >, const auto &values) -> void
static auto set(const ModelField *field, const BindingPayload< T, SchemaType, JoinedValues > &payload, std::integral_constant< std::size_t, FieldIndex >, soci::values &values) -> void
static auto set(const ModelField *field, const BindingPayload< T, SchemaType, JoinedValues > &, const model::ColumnView &column, soci::values &values) -> void
static auto set(const ModelField *field, const BindingPayload< T, SchemaType, JoinedValues > &payload, std::size_t fieldIndex, soci::values &values) -> void
Trivially-copyable handle to one model in immutable Schema storage.