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)
77 static_assert(std::remove_cvref_t<decltype(std::declval<ReferencedRecord>().[:
ReferencedField:])>::IsPrimaryKey,
78 "The referenced field must be a primary key.");
83 static_assert(std::remove_cvref_t<decltype(std::declval<ReferencedRecord>().*
ReferencedField)>::IsPrimaryKey,
84 "The referenced field must be a primary key.");
92 using ValueType = std::conditional_t<Nullable == SqlNullable::Null, std::optional<BaseType>,
BaseType>;
94 static constexpr auto IsOptional = Nullable == SqlNullable::Null;
95 static constexpr auto IsMandatory = !IsOptional;
96 static constexpr auto IsPrimaryKey =
false;
97 static constexpr auto IsAutoIncrementPrimaryKey =
false;
99 template <
typename... S>
100 requires std::constructible_from<
ValueType, S...>
101 constexpr BelongsTo(S&&... value)
noexcept:
102 _referencedFieldValue(std::forward<S>(value)...)
107#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
113 _record { std::make_unique<ReferencedRecord>(other) }
117 constexpr BelongsTo(BelongsTo
const& other)
noexcept:
118 _referencedFieldValue(other._referencedFieldValue),
119 _loader(std::move(other._loader)),
120 _loaded(other._loaded),
121 _modified(other._modified),
122 _record(other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr)
126 constexpr BelongsTo(BelongsTo&& other)
noexcept:
127 _referencedFieldValue(std::move(other._referencedFieldValue)),
128 _loader(std::move(other._loader)),
129 _loaded(other._loaded),
130 _modified(other._modified),
131 _record(std::move(other._record))
135 BelongsTo& operator=(SqlNullType )
noexcept
137 if (!_referencedFieldValue)
140 _record = std::nullopt;
141 _referencedFieldValue = {};
148#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
155 _record = std::make_unique<ReferencedRecord>(other);
156#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
165 BelongsTo& operator=(BelongsTo
const& other)
170 _referencedFieldValue = other._referencedFieldValue;
171 _loader = std::move(other._loader);
172 _loaded = other._loaded;
173 _modified = other._modified;
174 _record = other._record ? std::make_unique<ReferencedRecord>(*other._record) : nullptr;
179 BelongsTo& operator=(BelongsTo&& other)
noexcept
183 _referencedFieldValue = std::move(other._referencedFieldValue);
184 _loader = std::move(other._loader);
185 _loaded = other._loaded;
186 _modified = other._modified;
187 _record = std::move(other._record);
188 other._loaded =
false;
192 ~BelongsTo() noexcept = default;
195 LIGHTWEIGHT_FORCE_INLINE constexpr
void SetModified(
bool value) noexcept
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool IsModified() const noexcept
207 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ValueType const&
Value() const noexcept
209 return _referencedFieldValue;
215 return _referencedFieldValue;
221 template <
typename Self>
223 requires(IsMandatory)
225 self.RequireLoaded();
226 return *self._record;
231 template <
typename Self>
232 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
Record(
this Self&& self)
235 self.RequireLoaded();
236 return [&]() -> std::optional<std::reference_wrapper<ReferencedRecord>> {
238 return *self._record;
247 template <
typename Self>
249 requires(IsMandatory)
251 self.RequireLoaded();
252 return *self._record;
257 template <
typename Self>
259 requires(IsMandatory)
261 self.RequireLoaded();
262 return self._record.get();
268 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!() const noexcept
270 return !_referencedFieldValue;
274 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator bool() const noexcept
276 return static_cast<bool>(_referencedFieldValue);
283 _record = std::make_unique<ReferencedRecord>();
287 template <
typename Stmt>
288 LIGHTWEIGHT_FORCE_INLINE
void BindOutputColumn(SQLSMALLINT outputIndex, Stmt& stmt)
290 stmt.BindOutputColumn(outputIndex, &_referencedFieldValue);
293 std::weak_ordering operator<=>(BelongsTo
const& other)
const noexcept
295 return _referencedFieldValue <=> other.Value();
298 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
299 std::weak_ordering operator<=>(Field<T, IsPrimaryKeyValue>
const& other)
const noexcept
301 return _referencedFieldValue <=> other.Value();
304 bool operator==(BelongsTo
const& other)
const noexcept
306 return (_referencedFieldValue <=> other.Value()) == std::weak_ordering::equivalent;
309 bool operator!=(BelongsTo
const& other)
const noexcept
311 return (_referencedFieldValue <=> other.Value()) != std::weak_ordering::equivalent;
314 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
315 bool operator==(Field<T, IsPrimaryKeyValue>
const& other)
const noexcept
317 return (_referencedFieldValue <=> other.Value()) == std::weak_ordering::equivalent;
320 template <detail::FieldElementType T, PrimaryKey IsPrimaryKeyValue = PrimaryKey::No>
321 bool operator!=(Field<T, IsPrimaryKeyValue>
const& other)
const noexcept
323 return (_referencedFieldValue <=> other.Value()) != std::weak_ordering::equivalent;
328 std::function<std::optional<ReferencedRecord>()> loadReference {};
334 _loader = std::move(loader);
338 void RequireLoaded()
const
343 if (_loader.loadReference)
345 auto value = _loader.loadReference();
348 _record = std::make_unique<ReferencedRecord>(std::move(value.value()));
353 if constexpr (IsMandatory)
355 throw SqlRequireLoadedError(Reflection::TypeNameOf<std::remove_cvref_t<
decltype(*
this)>>);
360 mutable bool _loaded =
false;
361 bool _modified =
false;
362 mutable std::unique_ptr<ReferencedRecord> _record {};
365template <auto ReferencedField, auto ColumnNameOverr
ideString, SqlNullable Nullable>
366std::ostream& operator<<(std::ostream& os, BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>
const& belongsTo)
368 return os << belongsTo.Value();
373 template <
typename T>
374 struct IsBelongsToType: std::false_type
378 template <auto ReferencedField, auto ColumnNameOverr
ideString, SqlNullable Nullable>
379 struct IsBelongsToType<BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>>: std::true_type
386constexpr bool IsBelongsTo = detail::IsBelongsToType<std::remove_cvref_t<T>>::value;
389concept is_belongs_to = IsBelongsTo<T>;
392constexpr bool IsOptionalBelongsTo =
false;
394template <is_be
longs_to T>
395constexpr bool IsOptionalBelongsTo<T> = T::IsOptional;
397template <auto ReferencedField, auto ColumnNameOverr
ideString, SqlNullable Nullable>
398struct SqlDataBinder<BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>>
400 using SelfType = BelongsTo<ReferencedField, ColumnNameOverrideString, Nullable>;
401 using InnerType = SelfType::ValueType;
403 static constexpr auto ColumnType = SqlDataBinder<InnerType>::ColumnType;
405 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
407 SelfType
const& value,
408 SqlDataBinderCallback& cb)
410 return SqlDataBinder<InnerType>::InputParameter(stmt, column, value.Value(), cb);
413 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN
414 OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, SelfType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
416 auto const sqlReturn = SqlDataBinder<InnerType>::OutputColumn(stmt, column, &result->MutableValue(), indicator, cb);
417 cb.PlanPostProcessOutputColumn([result]() { result->SetModified(
true); });
421 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(
422 SQLHSTMT stmt, SQLUSMALLINT column, SelfType* result, SQLLEN* indicator, SqlDataBinderCallback
const& cb)
noexcept
424 auto const sqlReturn = SqlDataBinder<InnerType>::GetColumn(stmt, column, &result->MutableValue(), indicator, cb);
425 if (SQL_SUCCEEDED(sqlReturn))
426 result->SetModified(
true);
Represents a one-to-one relationship.
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.
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.
LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!() const noexcept
Checks if the field value is NULL.
LIGHTWEIGHT_FORCE_INLINE constexpr bool IsModified() const noexcept
Checks if the field is modified.
LIGHTWEIGHT_FORCE_INLINE constexpr ReferencedRecord * operator->(this Self &&self)
LIGHTWEIGHT_FORCE_INLINE constexpr ValueType & MutableValue() noexcept
Retrieves the mutable reference to the value of the field.
LIGHTWEIGHT_FORCE_INLINE constexpr ValueType const & Value() const noexcept
Retrieves the reference to the value of the field.
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.
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 std::string_view ColumnNameOverride
If not an empty string, this value will be used as the column name in the database.