Lightweight 0.20260625.0
Loading...
Searching...
No Matches
Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector > Class Template Reference

This API represents a many-to-many relationship between two records through a third record. More...

#include <HasManyThrough.hpp>

Public Types

using ThroughRecord = ThroughRecordT
 The record type of the "through" side of the relationship.
 
using ReferencedRecord = ReferencedRecordT
 The record type of the "many" side of the relationship.
 
using ReferencedRecordList = std::vector< std::shared_ptr< ReferencedRecord > >
 The list of records on the "many" side of the relationship.
 
using value_type = ReferencedRecord
 Value type for range-based iteration.
 
using iterator = ReferencedRecordList::iterator
 Iterator type for the list of records.
 
using const_iterator = ReferencedRecordList::const_iterator
 Const iterator type for the list of records.
 

Public Member Functions

ReferencedRecordList const & All () const noexcept
 Retrieves the list of loaded records.
 
ReferencedRecordListAll () noexcept
 Retrieves the list of records as mutable reference.
 
ReferencedRecordListEmplace (ReferencedRecordList &&records) noexcept
 Emplaces the given list of records into this relationship.
 
std::size_t Count () const
 Retrieves the number of records in this relationship.
 
bool IsEmpty () const
 Checks if this relationship is empty.
 
ReferencedRecord const & At (std::size_t index) const
 Retrieves the record at the given index.
 
ReferencedRecordAt (std::size_t index)
 Retrieves the record at the given index.
 
ReferencedRecord const & operator[] (std::size_t index) const
 Retrieves the record at the given index.
 
ReferencedRecordoperator[] (std::size_t index)
 Retrieves the record at the given index.
 
iterator begin () noexcept
 Returns an iterator to the beginning of the record list.
 
iterator end () noexcept
 Returns an iterator to the end of the record list.
 
const_iterator begin () const noexcept
 Returns a const iterator to the beginning of the record list.
 
const_iterator end () const noexcept
 Returns a const iterator to the end of the record list.
 
std::weak_ordering operator<=> (HasManyThrough const &other) const noexcept=default
 Default three-way comparison operator.
 
void SetAutoLoader (Loader loader) noexcept
 Used internally to configure on-demand loading of the records.
 
void Reload ()
 Reloads the records from the database.
 
template<typename Callable >
void Each (Callable const &callable)
 Iterates over all records in this relationship.
 

Static Public Attributes

static constexpr auto OwnerSelector = TheOwnerSelector
 Singles out the join record's foreign key pointing at the record owning this relationship.
 
static constexpr auto ReferencedSelector = TheReferencedSelector
 Singles out the join record's foreign key pointing at ReferencedRecord.
 

Detailed Description

template<typename ReferencedRecordT, typename ThroughRecordT, auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
class Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >

This API represents a many-to-many relationship between two records through a third record.

The join record must declare one BelongsTo pointing back at the record owning this relationship, and one pointing at the referenced record. Both are located by matching the relationship type.

When the join record cannot be resolved that way - most notably a self-referential many-to-many, where both of its foreign keys point at the same table - name the two foreign key columns explicitly:

struct Friendship;
struct Human
{
HasManyThrough<Human, Friendship, SqlRealName { "a_id" }, SqlRealName { "b_id" }> friends;
};
struct Friendship
{
Field<int, PrimaryKey::AutoAssign> id;
BelongsTo<&Human::id, SqlRealName { "a_id" }> a;
BelongsTo<&Human::id, SqlRealName { "b_id" }> b;
};
This API represents a many-to-many relationship between two records through a third record.
Represents a single column in a table.
Definition Field.hpp:84
Helper class, used to represent a real SQL column names as template arguments.
Template Parameters
ReferencedRecordTThe record type on the "many" side of the relationship.
ThroughRecordTThe join record type.
TheOwnerSelectorSingles out the join record's foreign key pointing at the owning record.
TheReferencedSelectorSingles out the join record's foreign key pointing at ReferencedRecordT.
See also
DataMapper, Field, HasMany, RelationSelector

Definition at line 54 of file HasManyThrough.hpp.

Member Typedef Documentation

◆ ThroughRecord

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
using Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::ThroughRecord = ThroughRecordT

The record type of the "through" side of the relationship.

Definition at line 62 of file HasManyThrough.hpp.

◆ ReferencedRecord

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
using Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::ReferencedRecord = ReferencedRecordT

The record type of the "many" side of the relationship.

Definition at line 65 of file HasManyThrough.hpp.

◆ ReferencedRecordList

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
using Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::ReferencedRecordList = std::vector<std::shared_ptr<ReferencedRecord> >

The list of records on the "many" side of the relationship.

Definition at line 74 of file HasManyThrough.hpp.

◆ value_type

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
using Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::value_type = ReferencedRecord

Value type for range-based iteration.

Definition at line 77 of file HasManyThrough.hpp.

◆ iterator

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
using Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::iterator = ReferencedRecordList::iterator

Iterator type for the list of records.

Definition at line 79 of file HasManyThrough.hpp.

◆ const_iterator

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
using Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::const_iterator = ReferencedRecordList::const_iterator

Const iterator type for the list of records.

Definition at line 81 of file HasManyThrough.hpp.

Member Function Documentation

◆ All() [1/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecordList const & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::All ( ) const
noexcept

◆ All() [2/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecordList & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::All ( )
noexcept

Retrieves the list of records as mutable reference.

Definition at line 225 of file HasManyThrough.hpp.

◆ Emplace()

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecordList & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::Emplace ( ReferencedRecordList &&  records)
noexcept

Emplaces the given list of records into this relationship.

Definition at line 237 of file HasManyThrough.hpp.

◆ Count()

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
std::size_t Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::Count ( ) const

Retrieves the number of records in this relationship.

Definition at line 245 of file HasManyThrough.hpp.

◆ IsEmpty()

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
bool Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::IsEmpty ( ) const

Checks if this relationship is empty.

Definition at line 258 of file HasManyThrough.hpp.

◆ At() [1/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecord const & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::At ( std::size_t  index) const

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will throw if the index is out of bounds.

Definition at line 265 of file HasManyThrough.hpp.

◆ At() [2/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecord & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::At ( std::size_t  index)

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will throw if the index is out of bounds.

Definition at line 272 of file HasManyThrough.hpp.

◆ operator[]() [1/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecord const & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::operator[] ( std::size_t  index) const

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will NOT throw if the index is out of bounds. The behaviour is undefined.

Definition at line 279 of file HasManyThrough.hpp.

◆ operator[]() [2/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::ReferencedRecord & Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::operator[] ( std::size_t  index)

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will NOT throw if the index is out of bounds. The behaviour is undefined.

Definition at line 286 of file HasManyThrough.hpp.

◆ begin() [1/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::iterator Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::begin ( )
noexcept

Returns an iterator to the beginning of the record list.

Definition at line 293 of file HasManyThrough.hpp.

◆ end() [1/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::iterator Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::end ( )
noexcept

Returns an iterator to the end of the record list.

Definition at line 300 of file HasManyThrough.hpp.

◆ begin() [2/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::const_iterator Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::begin ( ) const
noexcept

Returns a const iterator to the beginning of the record list.

Definition at line 307 of file HasManyThrough.hpp.

◆ end() [2/2]

template<typename ReferencedRecordT , typename ThroughRecordT , auto OwnerSelector, auto ReferencedSelector>
HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::const_iterator Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, OwnerSelector, ReferencedSelector >::end ( ) const
noexcept

Returns a const iterator to the end of the record list.

Definition at line 314 of file HasManyThrough.hpp.

◆ SetAutoLoader()

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
void Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::SetAutoLoader ( Loader  loader)
inlinenoexcept

Used internally to configure on-demand loading of the records.

Definition at line 146 of file HasManyThrough.hpp.

Referenced by Lightweight::DataMapper::ConfigureRelationAutoLoading().

◆ Reload()

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
void Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::Reload ( )
inline

Reloads the records from the database.

Definition at line 152 of file HasManyThrough.hpp.

◆ Each()

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
template<typename Callable >
void Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::Each ( Callable const &  callable)
inline

Iterates over all records in this relationship.

Parameters
callableThe callable to invoke for each record.
Note
This method will on-demand load the records if they are not already loaded, but not hold them all in memory.

Definition at line 165 of file HasManyThrough.hpp.

References Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::All().

Member Data Documentation

◆ OwnerSelector

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
constexpr auto Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::OwnerSelector = TheOwnerSelector
staticconstexpr

Singles out the join record's foreign key pointing at the record owning this relationship.

Definition at line 68 of file HasManyThrough.hpp.

◆ ReferencedSelector

template<typename ReferencedRecordT , typename ThroughRecordT , auto TheOwnerSelector = AutoDetectRelation, auto TheReferencedSelector = AutoDetectRelation>
constexpr auto Lightweight::HasManyThrough< ReferencedRecordT, ThroughRecordT, TheOwnerSelector, TheReferencedSelector >::ReferencedSelector = TheReferencedSelector
staticconstexpr

Singles out the join record's foreign key pointing at ReferencedRecord.

Definition at line 71 of file HasManyThrough.hpp.


The documentation for this class was generated from the following file: