5#include "../DataBinder/Core.hpp"
6#include "../DataBinder/SqlNullValue.hpp"
7#include "../SqlStatement.hpp"
8#include "BelongsTo.hpp"
12#include <reflection-cpp/reflection.hpp>
29template <
typename OtherRecord>
43 using iterator =
typename ReferencedRecordList::iterator;
58 template <typename Callable>
59 void Each(Callable const& callable);
65 [[nodiscard]] std::
size_t Count() const noexcept;
68 [[nodiscard]]
bool IsEmpty() const noexcept;
75 [[nodiscard]] OtherRecord const&
At(std::
size_t index) const;
82 [[nodiscard]] OtherRecord&
At(std::
size_t index);
89 [[nodiscard]] OtherRecord const& operator[](std::
size_t index) const;
96 [[nodiscard]] OtherRecord& operator[](std::
size_t index);
98 [[nodiscard]]
iterator begin() noexcept;
99 [[nodiscard]]
iterator end() noexcept;
103 constexpr std::weak_ordering operator<=>(
HasMany const& other) const noexcept = default;
104 constexpr
bool operator==(
HasMany const& other) const noexcept = default;
105 constexpr
bool operator!=(
HasMany const& other) const noexcept = default;
109 std::function<size_t()> count {};
110 std::function<void()> all {};
113 std::weak_ordering operator<=>(Loader
const& )
const noexcept
115 return std::weak_ordering::equivalent;
123 void RequireLoaded();
126 std::optional<ReferencedRecordList> _records;
127 std::optional<size_t> _count;
131constexpr bool IsHasMany = IsSpecializationOf<HasMany, T>;
133template <
typename OtherRecord>
136 _loader = std::move(loader);
139template <
typename OtherRecord>
146template <
typename OtherRecord>
150 _records = { std::move(records) };
154template <
typename OtherRecord>
161template <
typename OtherRecord>
162template <
typename Callable>
165 if (!_records && _loader.each)
167 _loader.each(callable);
171 for (
auto const& record: All())
175template <
typename OtherRecord>
182template <
typename OtherRecord>
186 return _records->size();
191 return _count.value_or(0);
194template <
typename OtherRecord>
200template <
typename OtherRecord>
204 return *_records->at(index);
207template <
typename OtherRecord>
211 return *_records->at(index);
214template <
typename OtherRecord>
218 return *(*_records)[index];
221template <
typename OtherRecord>
225 return *(*_records)[index];
228template <
typename OtherRecord>
232 return _records->begin();
235template <
typename OtherRecord>
239 return _records->end();
242template <
typename OtherRecord>
246 return _records->begin();
249template <
typename OtherRecord>
253 return _records->end();
This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.
typename ReferencedRecordList::iterator iterator
Iterator type for the list of records.
ReferencedRecordList const & All() const noexcept
Retrieves the list of loaded records.
OtherRecord value_type
Record type of the "many" side of the relationship.
bool IsEmpty() const noexcept
Checks if this 1-to-many relationship is empty.
std::size_t Count() const noexcept
Retrieves the number of records in this 1-to-many relationship.
void Each(Callable const &callable)
Iterates over the list of records and calls the given callable for each record.
void SetAutoLoader(Loader loader) noexcept
Used internally to configure on-demand loading of the records.
std::vector< std::shared_ptr< OtherRecord > > ReferencedRecordList
The list of records on the "many" side of the relationship.
ReferencedRecordList & Emplace(ReferencedRecordList &&records) noexcept
Emplaces the given list of records.
typename ReferencedRecordList::const_iterator const_iterator
Const iterator type for the list of records.
OtherRecord ReferencedRecord
The record type of the "many" side of the relationship.
OtherRecord const & At(std::size_t index) const
Retrieves the record at the given index.
OtherRecord const & operator[](std::size_t index) const
Retrieves the record at the given index.