5#include "../SqlStatement.hpp"
46template <
typename OtherTable,
53 "The selector template arguments of HasOneThrough must be foreign key column names "
54 "(a SqlRealName) or std::nullopt to resolve the relationship automatically.");
56 static_assert(!IsThrough<OtherTable>,
57 "The referenced record of HasOneThrough must not be wrapped in Through<>, "
58 "only the join record is.");
74 LIGHTWEIGHT_FORCE_INLINE
constexpr void EmplaceRecord(std::shared_ptr<ReferencedRecord> record)
76 _record = std::move(record);
91 return Load().transform([
this] {
return std::reference_wrapper<ReferencedRecord> { *_record }; });
97 return Load().transform([
this] {
return std::reference_wrapper<ReferencedRecord const> { *_record }; });
101 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsLoaded() const noexcept
103 return _record.get() !=
nullptr;
107 LIGHTWEIGHT_FORCE_INLINE
void Unload() noexcept
133 return _record.get();
140 return _record.get();
150 std::function<RelationResult<std::shared_ptr<ReferencedRecord>>()>
loadReference {};
156 _loader = std::move(loader);
169 return std::unexpected { *_loadError };
177 _loadError = loaded.error();
178 return std::unexpected { loaded.error() };
180 _record = std::move(*loaded);
184 void LoadOrThrow()
const
186 if (
auto const loaded = Load(); !loaded)
187 throw SqlRequireLoadedError(Reflection::TypeNameOf<std::remove_cvref_t<
decltype(*
this)>>, loaded.error());
193 mutable std::shared_ptr<ReferencedRecord> _record {};
194 mutable std::optional<RelationError> _loadError {};
199 template <
typename T>
200 struct IsHasOneThrough: std::false_type
204 template <
typename OtherTable,
typename ThroughSpec, auto OwnerSelector, auto ThroughSelector>
205 struct IsHasOneThrough<HasOneThrough<OtherTable, ThroughSpec, OwnerSelector, ThroughSelector>>: std::true_type
211constexpr bool IsHasOneThrough = detail::IsHasOneThrough<std::remove_cvref_t<T>>::value;
Represents a one-to-one relationship through a join table.
LIGHTWEIGHT_FORCE_INLINE void Unload() noexcept
Unloads the record from memory, so the next access loads it again.
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 ReferencedRecord * operator->()
Retrieves the record in this relationship, loading it on first access.
LIGHTWEIGHT_FORCE_INLINE constexpr void EmplaceRecord(std::shared_ptr< ReferencedRecord > record)
Emplaces the given record into this relationship.
static constexpr auto OwnerSelector
Singles out the join record's foreign key pointing at the record owning this relationship.
LIGHTWEIGHT_FORCE_INLINE ReferencedRecord & operator*()
Retrieves the record in this relationship, loading it on first access.
static constexpr auto ThroughSelector
Singles out ReferencedRecord's foreign key pointing at ThroughRecord.
LIGHTWEIGHT_FORCE_INLINE ReferencedRecord const * operator->() const
Retrieves the record in this relationship, loading it on first access.
std::weak_ordering operator<=>(HasOneThrough const &other) const noexcept=default
Default three-way comparison operator.
LIGHTWEIGHT_FORCE_INLINE RelationResult< std::reference_wrapper< ReferencedRecord > > Record()
Retrieves the record in this relationship, loading it on first access.
LIGHTWEIGHT_FORCE_INLINE RelationResult< std::reference_wrapper< ReferencedRecord const > > Record() const
Retrieves the record in this relationship, loading it on first access.
ThroughRecordOf< ThroughSpec > ThroughRecord
The record type of the "through" side of the relationship.
LIGHTWEIGHT_FORCE_INLINE ReferencedRecord const & operator*() const
Retrieves the record in this relationship, loading it on first access.
OtherTable ReferencedRecord
The record type of the "Other" side of the relationship.
Constrains what may be used to single out one of several foreign keys into the same table.
detail::ThroughRecordOfHelper< ThroughSpec >::type ThroughRecordOf
Resolves the join record of a through-relationship from its template argument.
std::expected< T, RelationError > RelationResult
The outcome of a relation access: the requested value, or why it is unavailable.
constexpr std::nullopt_t AutoDetectRelation
Selector value meaning "resolve the relationship automatically; the match must be unique".
Carries the deferred load, installed by the DataMapper.
std::function< RelationResult< std::shared_ptr< ReferencedRecord > >()> loadReference
Loads the referenced record: RelationError::NotFound when there is none.