-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
private_class_members.cpp
87 lines (68 loc) · 2.56 KB
/
private_class_members.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Sometimes data model classes may have private/protected fields to
* be mapped to a storage. In this case you have to pass setter and getter
* function instead of member pointer. Also to specify a column in select
* or where condition you have to add getter or setter function pointer. Both of them
* are mapped to the same column.
* getter must have definition like this: `const F& O::*() const`
* setter: `void O::*(F)`
*/
#include <sqlite_orm/sqlite_orm.h>
#include <iostream>
#include <cassert>
using std::cout;
using std::endl;
class Player {
int id = 0;
std::string name;
public:
Player() {}
Player(std::string name_) : name(std::move(name_)) {}
Player(int id_, std::string name_) : id(id_), name(std::move(name_)) {}
std::string getName() const {
return this->name;
}
void setName(std::string name) {
this->name = std::move(name);
}
int getId() const {
return this->id;
}
void setId(int id) {
this->id = id;
}
};
int main(int, char**) {
using namespace sqlite_orm;
auto storage = make_storage(
"private.sqlite",
make_table(
"players",
make_column("id",
&Player::setId, // setter
&Player::getId, // getter
primary_key()),
make_column("name",
&Player::getName, // BTW order doesn't matter: setter can be placed before getter or opposite.
&Player::setName)));
storage.sync_schema();
storage.remove_all<Player>();
auto soloId = storage.insert(Player("Solo"));
auto playersCount = storage.count<Player>();
cout << "players count = " << playersCount << endl;
assert(playersCount == 1);
cout << "solo = " << storage.dump(storage.get<Player>(soloId)) << endl;
auto deadpoolId = storage.insert(Player("Deadpool"));
cout << "deadpool = " << storage.dump(storage.get<Player>(deadpoolId)) << endl;
playersCount = storage.count<Player>();
cout << "players count = " << playersCount << endl;
assert(playersCount == 2);
auto idsOnly = storage.select(&Player::getId); // or storage.select(&Player::setId);
cout << "idsOnly count = " << idsOnly.size() << endl;
auto somePlayers = storage.get_all<Player>(where(less_than(length(&Player::getName), 5)));
cout << "players with length(name) < 5 = " << somePlayers.size() << endl;
assert(somePlayers.size() == 1);
for(auto& player: somePlayers) {
cout << storage.dump(player) << endl;
}
}