49 if constexpr (!std::same_as<
decltype(ColumnNameOverrideString), std::nullopt_t>)
50 return std::string_view { ColumnNameOverrideString };
52 return std::string_view {};
58 static_assert(std::remove_cvref_t<decltype(std::declval<ReferencedRecord>().*
ReferencedField)>::IsPrimaryKey,
59 "The referenced field must be a primary key.");
64 static constexpr auto IsOptional =
true;
65 static constexpr auto IsMandatory = !IsOptional;
66 static constexpr auto IsPrimaryKey =
false;
67 static constexpr auto IsAutoIncrementPrimaryKey =
false;
69 template <
typename... S>
70 requires std::constructible_from<
ValueType, S...>
71 constexpr BelongsTo(S&&... value)
noexcept:
72 _referencedFieldValue(std::forward<S>(value)...)
79 _record { std::make_unique<ReferencedRecord>(other) }
84 _referencedFieldValue(other._referencedFieldValue),
85 _loader(std::move(other._loader)),
86 _loaded(other._loaded),
87 _modified(other._modified),
88 _record(other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr)
93 _referencedFieldValue(std::move(other._referencedFieldValue)),
94 _loader(std::move(other._loader)),
95 _loaded(other._loaded),
96 _modified(other._modified),
97 _record(std::move(other._record))
103 if (!_referencedFieldValue)
106 _record = std::nullopt;
107 _referencedFieldValue = {};
117 _record = std::make_unique<ReferencedRecord>(other);
128 _referencedFieldValue = other._referencedFieldValue;
129 _loader = std::move(other._loader);
130 _loaded = other._loaded;
131 _modified = other._modified;
132 _record = other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr;
141 _referencedFieldValue = std::move(other._referencedFieldValue);
142 _loader = std::move(other._loader);
143 _loaded = other._loaded;
144 _modified = other._modified;
145 _record = std::move(other._record);
146 other._loaded =
false;
155 LIGHTWEIGHT_FORCE_INLINE constexpr
void SetModified(
bool value) noexcept { _modified = value; }
158 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsModified() const noexcept {
return _modified; }
161 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ValueType const&
Value() const noexcept {
return _referencedFieldValue; }
164 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ValueType&
MutableValue() noexcept {
return _referencedFieldValue; }
170 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const&
Record()
const { RequireLoaded();
return *_record; }
173 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsLoaded() const noexcept {
return _loaded; }
176 LIGHTWEIGHT_FORCE_INLINE
void Unload() noexcept { _record =
nullptr; _loaded =
false; }
182 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const&
operator*() const noexcept { RequireLoaded();
return *_record; }
191 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!() const noexcept {
return !_referencedFieldValue; }
194 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator bool() const noexcept {
return static_cast<bool>(_referencedFieldValue); }
202 _record = std::make_unique<ReferencedRecord>();
206 LIGHTWEIGHT_FORCE_INLINE
void BindOutputColumn(SQLSMALLINT outputIndex,
SqlStatement& stmt)
208 stmt.BindOutputColumn(outputIndex, &_referencedFieldValue);
211 template <auto OtherReferencedField>
214 return _referencedFieldValue <=> other.
Value();
217 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
220 return _referencedFieldValue <=> other.
Value();
223 template <auto OtherReferencedField>
226 return (_referencedFieldValue <=> other.
Value()) == std::weak_ordering::equivalent;
229 template <auto OtherReferencedField>
232 return (_referencedFieldValue <=> other.
Value()) != std::weak_ordering::equivalent;
235 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
238 return (_referencedFieldValue <=> other.
Value()) == std::weak_ordering::equivalent;
241 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
244 return (_referencedFieldValue <=> other.
Value()) != std::weak_ordering::equivalent;
249 std::function<std::optional<ReferencedRecord>()> loadReference {};
255 _loader = std::move(loader);
264 if (_loader.loadReference)
266 auto value = _loader.loadReference();
269 _record = std::make_unique<ReferencedRecord>(std::move(value.value()));
280 bool _loaded =
false;
281 bool _modified =
false;
282 std::unique_ptr<ReferencedRecord> _record {};