Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Lightweight::HasMany< OtherRecord, TheInverseSelector > Class Template Reference

This HasMany<OtherRecord> represents a simple one-to-many relationship between two records. More...

#include <HasMany.hpp>

Public Types

using ReferencedRecord = OtherRecord
 The record type of the "many" side of the relationship.
 
using ReferencedRecordList = std::vector< std::shared_ptr< OtherRecord > >
 The list of records on the "many" side of the relationship.
 
using value_type = OtherRecord
 Record type of the "many" side of the relationship.
 
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
 Retrieves the list of loaded records.
 
ReferencedRecordList & All ()
 Retrieves the list of records as mutable reference.
 
template<typename Callable >
void Each (Callable const &callable)
 Iterates over the list of records and calls the given callable for each record.
 
ReferencedRecordList & Emplace (ReferencedRecordList &&records) noexcept
 Emplaces the given list of records.
 
ReferencedRecordList * LoadedRecords () noexcept
 Returns the already-loaded records, or nullptr when the relation is not loaded.
 
std::size_t Count () const noexcept
 Retrieves the number of records in this 1-to-many relationship.
 
bool IsEmpty () const noexcept
 Checks if this 1-to-many relationship is empty.
 
OtherRecord const & At (std::size_t index) const
 Retrieves the record at the given index.
 
OtherRecord & At (std::size_t index)
 Retrieves the record at the given index.
 
OtherRecord const & operator[] (std::size_t index) const
 Retrieves the record at the given index.
 
OtherRecord & operator[] (std::size_t index)
 Retrieves the record at the given index.
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
constexpr std::weak_ordering operator<=> (HasMany const &other) const noexcept=default
 Three-way comparison operator.
 
constexpr bool operator== (HasMany const &other) const noexcept=default
 Equality comparison operator.
 
constexpr bool operator!= (HasMany const &other) const noexcept=default
 Inequality comparison operator.
 
void SetAutoLoader (Loader loader) noexcept
 Used internally to configure on-demand loading of the records.
 

Static Public Attributes

static constexpr auto InverseSelector = TheInverseSelector
 Singles out the foreign key of OtherRecord that backs this relationship.
 

Detailed Description

template<typename OtherRecord, auto TheInverseSelector = AutoDetectRelation>
class Lightweight::HasMany< OtherRecord, TheInverseSelector >

This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.

The HasMany<OtherRecord> is a member of the "one" side of the relationship.

OtherRecord must declare a BelongsTo member that points back to this "one" side. That member is located by matching the relationship type, not by its position in either record, so the two relationship members may be declared at any index. Declaring no such BelongsTo is a compile-time error.

When OtherRecord holds more than one foreign key into this record's table - say a meeting that references the same person table both as its organizer and as whoever writes the minutes - the inverse is ambiguous. Name the foreign key column through TheInverseSelector to single one out:

struct Meeting;
struct Human
{
HasMany<Meeting, SqlRealName { "organizer_id" }> organizedMeetings;
HasMany<Meeting, SqlRealName { "minute_taker_id" }> minutedMeetings;
};
struct Meeting
{
Field<int, PrimaryKey::AutoAssign> id;
BelongsTo<&Human::id, SqlRealName { "organizer_id" }> organizer;
BelongsTo<&Human::id, SqlRealName { "minute_taker_id" }, SqlNullable::Null> minuteTaker;
};
This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.
Definition HasMany.hpp:65
Represents a single column in a table.
Definition Field.hpp:84
Helper class, used to represent a real SQL column names as template arguments.

A meeting with many attendees is a many-to-many instead - see HasManyThrough. The worked example in docs/sql-to-lightweight.md combines both shapes.

Template Parameters
OtherRecordThe record type on the "many" side of the relationship.
TheInverseSelectorSingles out one of several foreign keys, see the RelationSelector concept.
See also
InverseBelongsToIndexOf, RelationSelector
DataMapper, Field, HasManyThrough

Definition at line 64 of file HasMany.hpp.

Member Typedef Documentation

◆ ReferencedRecord

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::ReferencedRecord = OtherRecord

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

Definition at line 72 of file HasMany.hpp.

◆ ReferencedRecordList

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::ReferencedRecordList = std::vector<std::shared_ptr<OtherRecord> >

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

Definition at line 78 of file HasMany.hpp.

◆ value_type

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::value_type = OtherRecord

Record type of the "many" side of the relationship.

Definition at line 81 of file HasMany.hpp.

◆ iterator

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::iterator = ReferencedRecordList::iterator

Iterator type for the list of records.

Definition at line 84 of file HasMany.hpp.

◆ const_iterator

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::const_iterator = ReferencedRecordList::const_iterator

Const iterator type for the list of records.

Definition at line 87 of file HasMany.hpp.

Member Function Documentation

◆ All() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::ReferencedRecordList const & Lightweight::HasMany< OtherRecord, InverseSelector >::All ( ) const
inline

Retrieves the list of loaded records.

Note
This method will on-demand load the records if they are not already loaded, and therefore throws whatever the loader throws. It also throws SqlRequireLoadedError if no auto-loader was configured for this relation.

Definition at line 275 of file HasMany.hpp.

◆ All() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::ReferencedRecordList & Lightweight::HasMany< OtherRecord, InverseSelector >::All ( )
inline

Retrieves the list of records as mutable reference.

Note
This method will on-demand load the records if they are not already loaded, and therefore throws whatever the loader throws. It also throws SqlRequireLoadedError if no auto-loader was configured for this relation.

Definition at line 252 of file HasMany.hpp.

◆ Each()

template<typename OtherRecord , auto InverseSelector>
template<typename Callable >
void Lightweight::HasMany< OtherRecord, InverseSelector >::Each ( Callable const &  callable)

Iterates over the list of records and calls the given callable for each record.

Note
Use this method if you want to iterate over all records but do not need to store them all in memory, e.g. because the full data set wuold be too large.

Definition at line 260 of file HasMany.hpp.

◆ Emplace()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::ReferencedRecordList & Lightweight::HasMany< OtherRecord, InverseSelector >::Emplace ( ReferencedRecordList &&  records)
inlinenoexcept

Emplaces the given list of records.

Definition at line 244 of file HasMany.hpp.

◆ LoadedRecords()

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
ReferencedRecordList * Lightweight::HasMany< OtherRecord, TheInverseSelector >::LoadedRecords ( )
inlinenoexcept

Returns the already-loaded records, or nullptr when the relation is not loaded.

Unlike All(), this never runs the on-demand loader: it reports what is present right now, which is what lets the batched relation loading walk one level deeper (With<A, B>()).

Returns
Pointer to the loaded list, or nullptr if the relation was never loaded.

Definition at line 119 of file HasMany.hpp.

◆ Count()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE std::size_t Lightweight::HasMany< OtherRecord, InverseSelector >::Count ( ) const
inlinenoexcept

Retrieves the number of records in this 1-to-many relationship.

Definition at line 282 of file HasMany.hpp.

◆ IsEmpty()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE bool Lightweight::HasMany< OtherRecord, InverseSelector >::IsEmpty ( ) const
inlinenoexcept

Checks if this 1-to-many relationship is empty.

Definition at line 294 of file HasMany.hpp.

References Count.

◆ At() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord const & Lightweight::HasMany< OtherRecord, InverseSelector >::At ( std::size_t  index) const
inline

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 300 of file HasMany.hpp.

◆ At() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord & Lightweight::HasMany< OtherRecord, InverseSelector >::At ( std::size_t  index)
inline

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 307 of file HasMany.hpp.

◆ operator[]() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord const & Lightweight::HasMany< OtherRecord, InverseSelector >::operator[] ( std::size_t  index) const
inline

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 314 of file HasMany.hpp.

◆ operator[]() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord & Lightweight::HasMany< OtherRecord, InverseSelector >::operator[] ( std::size_t  index)
inline

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 321 of file HasMany.hpp.

◆ begin() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::iterator Lightweight::HasMany< OtherRecord, InverseSelector >::begin ( )
inline

Returns an iterator to the beginning of the record list.

Note
On-demand loads the records, and therefore throws what the loader throws.

Definition at line 329 of file HasMany.hpp.

Referenced by Lightweight::HasMany< OtherRecord, TheInverseSelector >::begin().

◆ end() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::iterator Lightweight::HasMany< OtherRecord, InverseSelector >::end ( )
inline

Returns an iterator to the end of the record list.

Note
On-demand loads the records, and therefore throws what the loader throws.

Definition at line 336 of file HasMany.hpp.

Referenced by Lightweight::HasMany< OtherRecord, TheInverseSelector >::end().

◆ begin() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::const_iterator Lightweight::HasMany< OtherRecord, InverseSelector >::begin ( ) const
inline

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

Note
On-demand loads the records, and therefore throws what the loader throws.

Definition at line 344 of file HasMany.hpp.

References Lightweight::HasMany< OtherRecord, TheInverseSelector >::begin().

◆ end() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::const_iterator Lightweight::HasMany< OtherRecord, InverseSelector >::end ( ) const
inline

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

Note
On-demand loads the records, and therefore throws what the loader throws.

Definition at line 352 of file HasMany.hpp.

References Lightweight::HasMany< OtherRecord, TheInverseSelector >::end().

◆ SetAutoLoader()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE void Lightweight::HasMany< OtherRecord, InverseSelector >::SetAutoLoader ( Loader  loader)
inlinenoexcept

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

Definition at line 219 of file HasMany.hpp.

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

Member Data Documentation

◆ InverseSelector

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
constexpr auto Lightweight::HasMany< OtherRecord, TheInverseSelector >::InverseSelector = TheInverseSelector
staticconstexpr

Singles out the foreign key of OtherRecord that backs this relationship.

Definition at line 75 of file HasMany.hpp.


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