-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
71 lines (55 loc) · 1.72 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "memorel/has_many.h"
#include "memorel/belongs_to.h"
#include "memorel/relation.h"
#include "memorel/postgresql.h"
#include "hugopeixoto/enumerable_ostream.h"
#include <iostream>
struct User {
uint64_t id;
std::string name;
Optional<uint64_t> company_id;
};
struct Company {
uint64_t id;
std::string name;
};
struct Users : public memorel::Relation<User> {
OptionalBelongsTo<Company, &User::company_id> company;
};
struct Companies : public memorel::Relation<Company> {
OptionalHasMany<User, &User::company_id> users;
};
template <> memorel::LoadInformation<User> memorel::Info() {
return {"select id, name, company_id from users order by id;",
[](auto result, auto &resource) {
return Fetch(result, resource.id, resource.name, resource.company_id);
}};
}
template <> memorel::LoadInformation<Company> memorel::Info() {
return {"select id, name from companies order by id;",
[](auto result, auto &resource) {
return Fetch(result, resource.id, resource.name);
}};
}
int main() {
Users users;
Companies companies;
PGconn *connection = PQconnectdb("dbname=memorel host=localhost");
Load(connection, users);
Load(connection, companies);
users.company.Load(companies);
companies.users.Load(users);
std::cout << "users: " << users.map(&User::name) << std::endl;
std::cout << "companies: " << companies.map(&Company::name) << std::endl;
for (auto u : users) {
std::cout << u.name << " works for "
<< users.company(u).map(&Company::name).orDefault("no one")
<< std::endl;
}
for (auto c : companies) {
std::cout << c.name << " employs "
<< companies.users.get(c).map(&User::name)
<< std::endl;
}
return 0;
}