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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// file : common/as/test.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
#ifndef TEST_HXX
#define TEST_HXX
#include <map>
#include <vector>
#include <string>
#include <utility> // pair
#include <odb/core.hxx>
#include <odb/vector.hxx>
#include <odb/nullable.hxx>
// Test basic type mapping functionality.
//
#pragma db namespace table("t1_")
namespace test1
{
enum color {red, green, blue};
inline const char*
color_to_string (color c)
{
return c == red ? "RED" : (c == green ? "GREEN" : "BLUE");
}
inline color
string_to_color (const std::string& s)
{
return s == "RED" ? red : (s == "GREEN" ? green : blue);
}
#pragma db map type(color) as(std::string) \
to(test1::color_to_string (?)) \
from(test1::string_to_color (?))
typedef std::pair<int, int> intp;
#pragma db value
struct comp
{
comp () {}
comp (int n1_, int n2_): n1 (n1_), n2 (n2_) {}
int n1;
int n2;
};
#pragma db map type(intp) as(comp) \
to(test1::comp ((?).first, (?).second)) \
from(test1::intp ((?).n1, (?).n2))
#pragma db object
struct object
{
// Class-scope mapping.
//
#pragma db map type(bool) as(std::string) \
to((?) ? "true" : "false") \
from((?) == "true")
#pragma db id auto
unsigned long id;
bool b;
color c;
intp ip;
std::map<bool, int> m;
std::vector<intp> v;
odb::vector<color> cv;
object () {}
object (bool b_, color c_, int n1, int n2): b (b_), c (c_), ip (n1, n2) {}
};
inline bool
operator== (const object& x, const object y)
{
return
x.b == y.b &&
x.c == y.c &&
x.ip == y.ip &&
x.m == y.m &&
x.v == y.v &&
x.cv == y.cv;
}
}
// Test wrapped simple type mapping.
//
#pragma db namespace table("t2_")
namespace test2
{
#pragma db map type(bool) as(std::string) \
to((?) ? "true" : "false") \
from((?) == "true")
typedef odb::nullable<bool> null_bool;
typedef odb::nullable<std::string> null_string;
/*
#pragma db map type(null_bool) as(null_string) \
to((?) \
? test2::null_string (*(?) ? "true" : "false") \
: test2::null_string ()) \
from((?) \
? test2::null_bool (*(?) == "true") \
: test2::null_bool ())
*/
#pragma db map type(null_bool) as(std::string) \
to((?) ? (*(?) ? "true" : "false") : "null") \
from((?) != "null" \
? test2::null_bool ((?) == "true") \
: test2::null_bool ())
#pragma db object
struct object
{
#pragma db id auto
unsigned long id;
odb::nullable<bool> b;
std::vector<odb::nullable<bool> > v;
};
inline bool
operator== (const object& x, const object y)
{
return x.b == y.b && x.v == y.v;
}
}
// Test wrapped simple type mapping.
//
#pragma db namespace table("t3_")
namespace test3
{
typedef std::pair<int, int> intp;
#pragma db value
struct comp
{
comp () {}
comp (int n1_, int n2_): n1 (n1_), n2 (n2_) {}
int n1;
int n2;
};
typedef odb::nullable<intp> null_intp;
typedef odb::nullable<comp> null_comp;
#pragma db map type(null_intp) as(null_comp) \
to((?) \
? test3::null_comp (test3::comp ((?)->first, (?)->second)) \
: test3::null_comp ()) \
from((?) \
? test3::null_intp (test3::intp ((?)->n1, (?)->n2)) \
: test3::null_intp ())
// Map int pair with both members equal 0 to NULL comp.
//
#pragma db map type(intp) as(null_comp) \
to((?).first != 0 || (?).second != 0 \
? test3::null_comp (test3::comp ((?).first, (?).second)) \
: test3::null_comp ()) \
from((?) \
? test3::intp (test3::intp ((?)->n1, (?)->n2)) \
: test3::intp (0, 0))
#pragma db object
struct object
{
#pragma db id auto
unsigned long id;
odb::nullable<intp> np;
intp ip;
std::vector<odb::nullable<intp> > npv;
odb::vector<intp> ipv;
};
inline bool
operator== (const object& x, const object y)
{
return x.np == y.np && x.ip == y.ip && x.npv == y.npv && x.ipv == y.ipv;
}
}
// Test id type mapping.
//
struct id_type
{
typedef unsigned long value_type;
value_type value;
id_type (value_type v = 0): value (v) {}
operator value_type () const {return value;}
};
#pragma db map type(id_type) as(id_type::value_type)
#pragma db namespace table("t4_")
namespace test4
{
#pragma db object
struct object
{
#pragma db id auto
id_type id;
int i;
odb::vector<int> v;
object () {}
object (int i_): i (i_) {}
};
inline bool
operator== (const object& x, const object y)
{
return x.id == y.id && x.i == y.i && x.v == y.v;
}
}
// Test version type mapping.
//
#pragma db namespace table("t5_")
namespace test5
{
struct version_type
{
typedef unsigned short value_type;
value_type value;
version_type (value_type v = 0): value (v) {}
operator value_type () const {return value;}
version_type& operator++ () {value++; return *this;}
};
#pragma db map type(version_type) as(id_type::value_type)
#pragma db object optimistic
struct object
{
#pragma db id
id_type id;
#pragma db version
version_type v;
int i;
object () {}
object (id_type id_, int i_): id (id_), i (i_) {}
};
inline bool
operator== (const object& x, const object y)
{
return x.id == y.id && x.v == y.v && x.i == y.i;
}
}
#endif // TEST_HXX
|