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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// file : common/id/nested/test.hxx
// copyright : Copyright (c) 2009-2018 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
#ifndef TEST_HXX
#define TEST_HXX
#include <string>
#include <vector>
#include <odb/core.hxx>
#include <odb/vector.hxx>
// Simple nested id.
//
#pragma db namespace table("t1_")
namespace test1
{
#pragma db value
struct comp
{
int x;
std::string y;
};
#pragma db object
struct object
{
#pragma db id(y)
comp id;
int z;
std::vector<int> v;
object () {}
object (int x, std::string y, int z_): z (z_) {id.x = x; id.y = y;}
};
inline bool
operator== (object a, object b)
{
return a.id.x == b.id.x && a.id.y == b.id.y && a.z == b.z && a.v == b.v;
}
#pragma db object
struct object1
{
#pragma db id auto
int id;
object* p;
object1 (object* p_ = 0): p (p_) {}
~object1 () {delete p;}
};
inline bool
operator== (const object1& a, const object1& b)
{
return a.id == b.id && *a.p == *b.p;
}
}
// Composite nested id.
//
#pragma db namespace table("t2_")
namespace test2
{
#pragma db value
struct comp1
{
int x;
int y;
};
#pragma db value
struct comp2
{
comp1 c;
std::string s;
};
#pragma db object
struct object
{
#pragma db id(c)
comp2 id;
int z;
odb::vector<int> v;
object () {}
object (int x, int y, std::string s, int z_)
: z (z_) {id.c.x = x; id.c.y = y; id.s = s;}
};
inline bool
operator== (object a, object b)
{
return a.id.c.x == b.id.c.x && a.id.c.y == b.id.c.y &&
a.id.s == b.id.s && a.z == b.z && a.v == b.v;
}
#pragma db object
struct object1
{
#pragma db id auto
int id;
object* p;
object1 (object* p_ = 0): p (p_) {}
~object1 () {delete p;}
};
inline bool
operator== (const object1& a, const object1& b)
{
return a.id == b.id && *a.p == *b.p;
}
// Multiple levels of nesting, just a compile test.
//
#pragma db object
struct object2
{
#pragma db id(c.x)
comp2 id;
int z;
};
}
// Custom/by-value access.
//
#pragma db namespace table("t3_")
namespace test3
{
#pragma db value
struct comp
{
int x;
std::string get_y () const {return y;}
void set_y (std::string v) {y = v;}
#pragma db get(get_y) set(set_y)
std::string y;
};
#pragma db object
struct object
{
comp get_id () const {return id;}
void set_id (comp v) {id = v;}
#pragma db id(y) get(get_id) set(set_id)
comp id;
int z;
object () {}
object (int x, std::string y, int z_): z (z_) {id.x = x; id.y = y;}
};
inline bool
operator== (object a, object b)
{
return a.id.x == b.id.x && a.id.y == b.id.y && a.z == b.z;
}
}
// Polymorphic.
//
#pragma db namespace table("t4_")
namespace test4
{
#pragma db value
struct comp
{
int x;
std::string y;
};
#pragma db object polymorphic
struct base
{
#pragma db id(y)
comp id;
virtual ~base () {}
base () {}
base (int x, std::string y) {id.x = x; id.y = y;}
};
inline bool
operator== (const base& a, const base& b)
{
return a.id.x == b.id.x && a.id.y == b.id.y;
}
#pragma db object
struct object: base
{
int z;
object () {}
object (int x, std::string y, int z_): base (x, y), z (z_) {}
};
inline bool
operator== (const object& a, const object& b)
{
return a.id.x == b.id.x && a.id.y == b.id.y && a.z == b.z;
}
}
#endif // TEST_HXX
|