Lightweight 0.20260213.0
Loading...
Searching...
No Matches
Entities.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include <Lightweight/Lightweight.hpp>
5
6#include <ostream>
7
8struct Person
9{
12 Light::Field<bool> is_active { true };
14
15 std::weak_ordering operator<=>(Person const& other) const = default;
16};
17
18// This is a test to only partially query a table row (a few columns)
19struct PersonName
20{
23
24 static constexpr std::string_view TableName = Light::RecordTableName<Person>;
25};
26
27inline std::ostream& operator<<(std::ostream& os, Person const& value)
28{
29 return os << std::format("Person {{ id: {}, name: {}, is_active: {}, age: {} }}",
30 value.id.Value(),
31 value.name.Value(),
32 value.is_active.Value(),
33 value.age.Value().value_or(-1));
34}
35
36struct User;
37struct Email;
38
39struct User
40{
43
44 Light::HasMany<Email> emails {};
45};
46
47struct Email
48{
51 Light::BelongsTo<Member(User::id), Light::SqlRealName { "user_id" }> user {};
52
53 constexpr std::weak_ordering operator<=>(Email const& other) const = default;
54};
55
56struct NullableForeignKeyUser
57{
59 Light::BelongsTo<Member(User::id), Light::SqlRealName { "user_id" }, Light::SqlNullable::Null> user {};
60};
61
62struct Physician;
63struct Appointment;
64struct Patient;
65
66struct Physician
67{
70 Light::HasMany<Appointment> appointments;
72
73 constexpr std::weak_ordering operator<=>(Physician const& other) const
74 {
75 if (auto result = id.Value() <=> other.id.Value(); result != std::weak_ordering::equivalent)
76 return result;
77
78 if (auto result = name.Value() <=> other.name.Value(); result != std::weak_ordering::equivalent)
79 return result;
80
81 return std::weak_ordering::equivalent;
82 }
83};
84
85struct Patient
86{
90 Light::HasMany<Appointment> appointments;
92
93 constexpr std::weak_ordering operator<=>(Patient const& other) const
94 {
95 if (auto result = id.Value() <=> other.id.Value(); result != std::weak_ordering::equivalent)
96 return result;
97
98 if (auto result = name.Value() <=> other.name.Value(); result != std::weak_ordering::equivalent)
99 return result;
100
101 if (auto result = comment.Value() <=> other.comment.Value(); result != std::weak_ordering::equivalent)
102 return result;
103
104 return std::weak_ordering::equivalent;
105 }
106};
107
108struct Appointment
109{
113 Light::BelongsTo<Member(Physician::id), Light::SqlRealName { "physician_id" }> physician;
114 Light::BelongsTo<Member(Patient::id), Light::SqlRealName { "patient_id" }> patient;
115
116 constexpr std::weak_ordering operator<=>(Appointment const& other) const
117 {
118 if (auto const result = id.Value() <=> other.id.Value(); result != std::weak_ordering::equivalent)
119 return result;
120
121 if (auto const result = date.Value() <=> other.date.Value(); result != std::weak_ordering::equivalent)
122 return result;
123
124 if (auto const result = comment.Value() <=> other.comment.Value(); result != std::weak_ordering::equivalent)
125 return result;
126
127 if (auto const result = physician.Value() <=> other.physician.Value(); result != std::weak_ordering::equivalent)
128 return result;
129
130 if (auto const result = patient.Value() <=> other.patient.Value(); result != std::weak_ordering::equivalent)
131 return result;
132
133 return std::weak_ordering::equivalent;
134 }
135};
136
137struct EntryWithIntPrimaryKey
138{
141};
Represents a one-to-one relationship.
Definition BelongsTo.hpp:57
This API represents a many-to-many relationship between two records through a third record.
This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.
Definition HasMany.hpp:34
Represents a single column in a table.
Definition Field.hpp:84
constexpr T const & Value() const noexcept
Returns the value of the field.
Definition Field.hpp:317
Helper class, used to represent a real SQL column names as template arguments.