5#include "../DataBinder/Core.hpp"
6#include "../DataBinder/SqlNullValue.hpp"
7#include "../SqlColumnTypeDefinitions.hpp"
27auto inline Unwrap = [](
auto v) {
55template <auto TheReferencedField, auto ColumnNameOverr
ideString = std::nullopt, SqlNullable Nullable = SqlNullable::NotNull>
64 if constexpr (!std::same_as<
decltype(ColumnNameOverrideString), std::nullopt_t>)
65 return std::string_view { ColumnNameOverrideString };
67 return std::string_view {};
70#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
78 "The referenced field must be a primary key.");
84 "The referenced field must be a primary key.");
92 using ValueType = std::conditional_t<Nullable == SqlNullable::Null, std::optional<BaseType>,
BaseType>;
95 static constexpr auto IsOptional = Nullable == SqlNullable::Null;
104 template <
typename... S>
105 requires std::constructible_from<
ValueType, S...>
107 _referencedFieldValue(std::forward<S>(value)...)
113#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
119 _record { std::make_unique<ReferencedRecord>(other) }
125 _referencedFieldValue(other._referencedFieldValue),
126 _loader(std::move(other._loader)),
127 _loaded(other._loaded),
128 _modified(other._modified),
129 _record(other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr)
135 _referencedFieldValue(std::move(other._referencedFieldValue)),
136 _loader(std::move(other._loader)),
137 _loaded(other._loaded),
138 _modified(other._modified),
139 _record(std::move(other._record))
146 if (!_referencedFieldValue)
149 _record = std::nullopt;
150 _referencedFieldValue = {};
158#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
165 _record = std::make_unique<ReferencedRecord>(other);
166#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
181 _referencedFieldValue = other._referencedFieldValue;
182 _loader = std::move(other._loader);
183 _loaded = other._loaded;
184 _modified = other._modified;
185 _record = other._record ? std::make_unique<ReferencedRecord>(*other._record) :
nullptr;
195 _referencedFieldValue = std::move(other._referencedFieldValue);
196 _loader = std::move(other._loader);
197 _loaded = other._loaded;
198 _modified = other._modified;
199 _record = std::move(other._record);
200 other._loaded =
false;
207 LIGHTWEIGHT_FORCE_INLINE constexpr
void SetModified(
bool value) noexcept
213 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsModified() const noexcept
219 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ValueType const&
Value() const noexcept
221 return _referencedFieldValue;
227 return _referencedFieldValue;
233 template <
typename Self>
237 self.RequireLoaded();
238 return *self._record;
243 template <
typename Self>
244 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
Record(
this Self&& self)
247 self.RequireLoaded();
248 return [&]() -> std::optional<std::reference_wrapper<ReferencedRecord>> {
250 return *self._record;
259 template <
typename Self>
263 self.RequireLoaded();
264 return *self._record;
269 template <
typename Self>
273 self.RequireLoaded();
274 return self._record.get();
280 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!() const noexcept
282 return !_referencedFieldValue;
286 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator bool() const noexcept
288 return static_cast<bool>(_referencedFieldValue);
295 _record = std::make_unique<ReferencedRecord>();
300 template <
typename Stmt>
303 stmt.BindOutputColumn(outputIndex, &_referencedFieldValue);
309 return _referencedFieldValue <=> other.Value();
313 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
316 return _referencedFieldValue <=> other.Value();
322 return (_referencedFieldValue <=> other.Value()) == std::weak_ordering::equivalent;
328 return (_referencedFieldValue <=> other.Value()) != std::weak_ordering::equivalent;
332 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
335 return (_referencedFieldValue <=> other.Value()) == std::weak_ordering::equivalent;
339 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
342 return (_referencedFieldValue <=> other.Value()) != std::weak_ordering::equivalent;
347 std::function<std::optional<ReferencedRecord>()> loadReference {};
353 _loader = std::move(loader);
357 void RequireLoaded()
const
362 if (_loader.loadReference)
364 auto value = _loader.loadReference();
367 _record = std::make_unique<ReferencedRecord>(std::move(value.value()));
374 throw SqlRequireLoadedError(Reflection::TypeNameOf<std::remove_cvref_t<
decltype(*
this)>>);
379 mutable bool _loaded =
false;
380 bool _modified =
false;
381 mutable std::unique_ptr<ReferencedRecord> _record {};
384template <auto ReferencedField, auto ColumnNameOverr
ideString, SqlNullable Nullable>
385std::ostream& operator<<(std::ostream& os, BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>
const& belongsTo)
387 return os << belongsTo.Value();
392 template <
typename T>
393 struct IsBelongsToType: std::false_type
397 template <auto ReferencedField, auto ColumnNameOverr
ideString, SqlNullable Nullable>
398 struct IsBelongsToType<BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>>: std::true_type
405constexpr bool IsBelongsTo = detail::IsBelongsToType<std::remove_cvref_t<T>>::value;
408concept is_belongs_to = IsBelongsTo<T>;
411constexpr bool IsOptionalBelongsTo =
false;
413template <is_be
longs_to T>
414constexpr bool IsOptionalBelongsTo<T> = T::IsOptional;
416template <auto ReferencedField, auto ColumnNameOverr
ideString, SqlNullable Nullable>
417struct SqlDataBinder<BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>>
419 using SelfType = BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>;
420 using InnerType = SelfType::ValueType;
422 static constexpr auto ColumnType = SqlDataBinder<InnerType>::ColumnType;
424 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
426 SelfType
const& value,
427 SqlDataBinderCallback& cb)
429 return SqlDataBinder<InnerType>::InputParameter(stmt, column, value.Value(), cb);
432 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN
433 OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, SelfType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
435 auto const sqlReturn = SqlDataBinder<InnerType>::OutputColumn(stmt, column, &result->MutableValue(), indicator, cb);
436 cb.PlanPostProcessOutputColumn([result]() { result->SetModified(
true); });
440 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(
441 SQLHSTMT stmt, SQLUSMALLINT column, SelfType* result, SQLLEN* indicator, SqlDataBinderCallback
const& cb)
noexcept
443 auto const sqlReturn = SqlDataBinder<InnerType>::GetColumn(stmt, column, &result->MutableValue(), indicator, cb);
444 if (SQL_SUCCEEDED(sqlReturn))
445 result->SetModified(
true);
Represents a one-to-one relationship.
BelongsTo & operator=(BelongsTo &&other) noexcept
Move assignment operator.
static constexpr auto IsAutoIncrementPrimaryKey
Indicates that a BelongsTo field is never an auto-increment primary key.
bool operator==(BelongsTo const &other) const noexcept
Equality comparison operator.
std::weak_ordering operator<=>(Field< T, IsPrimaryKeyValue > const &other) const noexcept
Three-way comparison operator with a Field.
bool operator==(Field< T, IsPrimaryKeyValue > const &other) const noexcept
Equality comparison operator with a Field.
std::weak_ordering operator<=>(BelongsTo const &other) const noexcept
Three-way comparison operator.
bool operator!=(BelongsTo const &other) const noexcept
Inequality comparison operator.
BelongsTo & operator=(SqlNullType) noexcept
Assigns NULL to the relationship, clearing the loaded record.
std::remove_cvref_t< decltype(std::declval< ReferencedRecord >().*ReferencedField)>::ValueType BaseType
Represents the base column type of the foreign key, matching the primary key of the other record.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord & EmplaceRecord()
Emplaces a record into the relationship. This will mark the relationship as loaded.
constexpr BelongsTo(S &&... value) noexcept
Constructs a new BelongsTo with the given value(s) forwarded to the underlying value type.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord & operator*(this Self &&self) noexcept
static constexpr auto ReferencedField
The field in the other record that references the current record.
static constexpr auto IsMandatory
Indicates whether this relationship is mandatory (non-nullable).
LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!() const noexcept
Checks if the field value is NULL.
static constexpr auto IsPrimaryKey
Indicates that a BelongsTo field is never a primary key.
BelongsTo & operator=(BelongsTo const &other)
Copy assignment operator.
LIGHTWEIGHT_FORCE_INLINE void BindOutputColumn(SQLSMALLINT outputIndex, Stmt &stmt)
Binds the foreign key value to the given output column index on the statement.
LIGHTWEIGHT_FORCE_INLINE constexpr bool IsModified() const noexcept
Checks if the field is modified.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord * operator->(this Self &&self)
constexpr BelongsTo(BelongsTo &&other) noexcept
Move constructor.
LIGHTWEIGHT_FORCE_INLINE constexpr ValueType & MutableValue() noexcept
Retrieves the mutable reference to the value of the field.
BelongsTo & operator=(ReferencedRecord &other)
Assigns a referenced record, updating the foreign key and loaded state.
LIGHTWEIGHT_FORCE_INLINE constexpr ValueType const & Value() const noexcept
Retrieves the reference to the value of the field.
constexpr BelongsTo(BelongsTo const &other) noexcept
Copy constructor.
LIGHTWEIGHT_FORCE_INLINE constexpr void SetModified(bool value) noexcept
Marks the field as modified or unmodified.
std::conditional_t< Nullable==SqlNullable::Null, std::optional< BaseType >, BaseType > ValueType
LIGHTWEIGHT_FORCE_INLINE constexpr decltype(auto) Record(this Self &&self)
void SetAutoLoader(Loader loader) noexcept
Used internally to configure on-demand loading of the record.
constexpr BelongsTo(ReferencedRecord const &other) noexcept
Constructs a new BelongsTo from the given referenced record, copying its primary key.
MemberClassType< decltype(TheReferencedField)> ReferencedRecord
Represents the record type of the other field.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord const & Record(this Self &&self)
Retrieves a record from the relationship. When the record is not optional.
static constexpr auto IsOptional
Indicates whether this relationship is optional (nullable).
static constexpr std::string_view ColumnNameOverride
If not an empty string, this value will be used as the column name in the database.
bool operator!=(Field< T, IsPrimaryKeyValue > const &other) const noexcept
Inequality comparison operator with a Field.
Represents a single column in a table.