5#include "../SqlStatement.hpp"
44template <
typename OtherTable,
51 "The selector template arguments of HasOneThrough must be foreign key column names "
52 "(a SqlRealName) or std::nullopt to resolve the relationship automatically.");
54 static_assert(!IsThrough<OtherTable>,
55 "The referenced record of HasOneThrough must not be wrapped in Through<>, "
56 "only the join record is.");
74 LIGHTWEIGHT_FORCE_INLINE
constexpr void EmplaceRecord(std::shared_ptr<ReferencedRecord> record) { _record = std::move(record); }
77 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord&
Record() noexcept { RequireLoaded();
return *_record.get(); }
80 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const&
Record() const noexcept { RequireLoaded();
return *_record.get(); }
83 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsLoaded() const noexcept {
return _record.get() !=
nullptr; }
86 LIGHTWEIGHT_FORCE_INLINE
void Unload() noexcept { _record.reset(); }
94 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const&
operator*() const noexcept { RequireLoaded();
return *_record; }
102 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ReferencedRecord const*
operator->() const noexcept { RequireLoaded();
return _record.get(); }
110 std::function<std::shared_ptr<ReferencedRecord>()> loadReference {};
116 _loader = std::move(loader);
120 void RequireLoaded()
const
125 if (_loader.loadReference)
126 _record = _loader.loadReference();
135 mutable std::shared_ptr<ReferencedRecord> _record {};
140 template <
typename T>
141 struct IsHasOneThrough: std::false_type
145 template <
typename OtherTable,
typename ThroughSpec, auto OwnerSelector, auto ThroughSelector>
146 struct IsHasOneThrough<HasOneThrough<OtherTable, ThroughSpec, OwnerSelector, ThroughSelector>>: std::true_type
152constexpr bool IsHasOneThrough = detail::IsHasOneThrough<std::remove_cvref_t<T>>::value;
Represents a one-to-one relationship through a join table.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord const & Record() const noexcept
Retrieves the record in this relationship.
LIGHTWEIGHT_FORCE_INLINE void Unload() noexcept
Unloads the record from memory.
void SetAutoLoader(Loader loader)
Used internally to configure on-demand loading of the record.
LIGHTWEIGHT_FORCE_INLINE constexpr bool IsLoaded() const noexcept
Checks if the record is loaded.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord const * operator->() const noexcept
Retrieves the record in this relationship.
LIGHTWEIGHT_FORCE_INLINE constexpr void EmplaceRecord(std::shared_ptr< ReferencedRecord > record)
Emplaces the given record into this relationship.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord * operator->() noexcept
Retrieves the record in this relationship.
static constexpr auto OwnerSelector
Singles out the join record's foreign key pointing at the record owning this relationship.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord const & operator*() const noexcept
Retrieves the record in this relationship.
static constexpr auto ThroughSelector
Singles out ReferencedRecord's foreign key pointing at ThroughRecord.
std::weak_ordering operator<=>(HasOneThrough const &other) const noexcept=default
Default three-way comparison operator.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord & Record() noexcept
Retrieves the record in this relationship.
ThroughRecordOf< ThroughSpec > ThroughRecord
The record type of the "through" side of the relationship.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord & operator*() noexcept
Retrieves the record in this relationship.
OtherTable ReferencedRecord
The record type of the "Other" side of the relationship.
Represents an error when a record is required to be loaded but is not.
Constrains what may be used to single out one of several foreign keys into the same table.
typename detail::ThroughRecordOfHelper< ThroughSpec >::type ThroughRecordOf
Resolves the join record of a through-relationship from its template argument.
constexpr std::nullopt_t AutoDetectRelation
Selector value meaning "resolve the relationship automatically; the match must be unique".