53 if constexpr (!std::same_as<
decltype(ColumnNameOverrideString), std::nullopt_t>)
54 return std::string_view { ColumnNameOverrideString };
56 return std::string_view {};
59#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
66 static_assert(std::remove_cvref_t<decltype(std::declval<ReferencedRecord>().[:
ReferencedField:])>::IsPrimaryKey,
67 "The referenced field must be a primary key.");
72 static_assert(std::remove_cvref_t<decltype(std::declval<ReferencedRecord>().*
ReferencedField)>::IsPrimaryKey,
73 "The referenced field must be a primary key.");
81 using ValueType = std::conditional_t<Nullable == SqlNullable::Null, std::optional<BaseType>,
BaseType>;
83 static constexpr auto IsOptional = Nullable == SqlNullable::Null;
84 static constexpr auto IsMandatory = !IsOptional;
85 static constexpr auto IsPrimaryKey =
false;
86 static constexpr auto IsAutoIncrementPrimaryKey =
false;
88 template <
typename... S>
89 requires std::constructible_from<
ValueType, S...>
90 constexpr BelongsTo(S&&... value)
noexcept:
91 _referencedFieldValue(std::forward<S>(value)...)
96#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
102 _record { std::make_unique<ReferencedRecord>(other) }
106 constexpr BelongsTo(BelongsTo
const& other)
noexcept:
107 _referencedFieldValue(other._referencedFieldValue),
108 _loader(std::move(other._loader)),
109 _loaded(other._loaded),
110 _modified(other._modified),
111 _record(other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr)
115 constexpr BelongsTo(BelongsTo&& other)
noexcept:
116 _referencedFieldValue(std::move(other._referencedFieldValue)),
117 _loader(std::move(other._loader)),
118 _loaded(other._loaded),
119 _modified(other._modified),
120 _record(std::move(other._record))
124 BelongsTo& operator=(SqlNullType )
noexcept
126 if (!_referencedFieldValue)
129 _record = std::nullopt;
130 _referencedFieldValue = {};
137#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
144 _record = std::make_unique<ReferencedRecord>(other);
145#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
154 BelongsTo& operator=(BelongsTo
const& other)
159 _referencedFieldValue = other._referencedFieldValue;
160 _loader = std::move(other._loader);
161 _loaded = other._loaded;
162 _modified = other._modified;
163 _record = other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr;
168 BelongsTo& operator=(BelongsTo&& other)
noexcept
172 _referencedFieldValue = std::move(other._referencedFieldValue);
173 _loader = std::move(other._loader);
174 _loaded = other._loaded;
175 _modified = other._modified;
176 _record = std::move(other._record);
177 other._loaded =
false;
181 ~BelongsTo() noexcept = default;
186 LIGHTWEIGHT_FORCE_INLINE constexpr
void SetModified(
bool value) noexcept { _modified = value; }
189 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsModified() const noexcept {
return _modified; }
192 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ValueType const&
Value() const noexcept {
return _referencedFieldValue; }
195 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ValueType&
MutableValue() noexcept {
return _referencedFieldValue; }
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const&
Record()
const { RequireLoaded();
return *_record; }
207 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const&
operator*() const noexcept { RequireLoaded();
return *_record; }
216 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!() const noexcept {
return !_referencedFieldValue; }
219 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator bool() const noexcept {
return static_cast<bool>(_referencedFieldValue); }
227 _record = std::make_unique<ReferencedRecord>();
231 LIGHTWEIGHT_FORCE_INLINE
void BindOutputColumn(SQLSMALLINT outputIndex,
SqlStatement& stmt)
233 stmt.BindOutputColumn(outputIndex, &_referencedFieldValue);
236 std::weak_ordering operator<=>(BelongsTo
const& other)
const noexcept
238 return _referencedFieldValue <=> other.Value();
241 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
242 std::weak_ordering operator<=>(Field<T, IsPrimaryKeyValue>
const& other)
const noexcept
244 return _referencedFieldValue <=> other.Value();
247 bool operator==(BelongsTo
const& other)
const noexcept
249 return (_referencedFieldValue <=> other.Value()) == std::weak_ordering::equivalent;
252 bool operator!=(BelongsTo
const& other)
const noexcept
254 return (_referencedFieldValue <=> other.Value()) != std::weak_ordering::equivalent;
257 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
258 bool operator==(Field<T, IsPrimaryKeyValue>
const& other)
const noexcept
260 return (_referencedFieldValue <=> other.Value()) == std::weak_ordering::equivalent;
263 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
264 bool operator!=(Field<T, IsPrimaryKeyValue>
const& other)
const noexcept
266 return (_referencedFieldValue <=> other.Value()) != std::weak_ordering::equivalent;
271 std::function<std::optional<ReferencedRecord>()> loadReference {};
277 _loader = std::move(loader);
286 if (_loader.loadReference)
288 auto value = _loader.loadReference();
291 _record = std::make_unique<ReferencedRecord>(std::move(value.value()));
297 throw SqlRequireLoadedError(Reflection::TypeNameOf<std::remove_cvref_t<
decltype(*
this)>>);
302 bool _loaded =
false;
303 bool _modified =
false;
304 std::unique_ptr<ReferencedRecord> _record {};