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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
// file : common/access/test.hxx
// license : GNU GPL v2; see accompanying LICENSE file
#ifndef TEST_HXX
#define TEST_HXX
#include <string>
#include <vector>
#include <cstring> // std::memcpy, std::memcmp, std::memset
#include <memory> // std::unique_ptr
#include <utility> // std::move
#include <odb/core.hxx>
#ifdef ODB_COMPILER
# if defined(ODB_DATABASE_MYSQL)
# define BINARY16_TYPE "BINARY(16)"
# elif defined(ODB_DATABASE_SQLITE)
# define BINARY16_TYPE "BLOB"
# elif defined(ODB_DATABASE_PGSQL)
# define BINARY16_TYPE "BYTEA"
# elif defined(ODB_DATABASE_ORACLE)
# define BINARY16_TYPE "RAW(16)"
# elif defined(ODB_DATABASE_MSSQL)
# define BINARY16_TYPE "BINARY(16)"
# elif defined(ODB_DATABASE_COMMON)
# define BINARY16_TYPE ""
# else
# error unknown database
# endif
#endif
// Test basic accessor/modifier functionality.
//
#pragma db namespace table("t1_")
namespace test1
{
#pragma db object
struct object
{
object (): i6_ (0), i7_ (0) {}
object (unsigned long id, int i6, int i7): id_ (id), i6_ (i6), i7_ (i7) {}
public:
unsigned long id () const {return id_;}
void id (unsigned long id) {id_ = id;}
private:
#pragma db id access(id)
unsigned long id_;
public:
int i1 () const {return i1_;}
int& i1 () {return i1_;}
private:
#pragma db access(i1)
int i1_;
public:
int i2 () const {return i2_;}
void i2 (int i2) {i2_ = i2;}
private:
#pragma db access(i2)
int i2_;
// Prefer reference modifier.
//
public:
int i3 () const {return i3_;}
int& i3 () {return i3_;}
void i3 (int i3);
private:
#pragma db access(i3)
int i3_;
// Prefer reference modifier (reverse function order).
//
public:
int i4 () const {return i4_;}
void i4 (int i4);
int& i4 () {return i4_;}
private:
#pragma db access(i4)
int i4_;
public:
int get_i5 () const {return i5_;}
void set_i5 (int i5) {i5_ = i5;}
private:
#pragma db get(get_i5) set(set_i5)
int i5_;
// Const member via reference.
//
public:
const int& i6 () const {return i6_;}
private:
#pragma db get(i6) set(const_cast<int&> (this.i6 ()))
const int i6_;
// Const member via modifier.
//
public:
int i7 () const {return i7_;}
void i7 (int i7) const {const_cast<int&> (i7_) = i7;}
private:
#pragma db access(i7)
const int i7_;
public:
const char* s1 () const {return s1_.c_str ();}
void s1 (const char* s1) {s1_ = s1;}
//std::string s1 () const {return s1_;}
//void s1 (std::string s1) {s1_ = s1;}
private:
#pragma db get(s1) set(s1((?).c_str ()))
//#pragma db access(s1)
std::string s1_;
// Array member via ref.
//
public:
const char* b1 () const {return b1_;}
char* b1 () {return b1_;}
private:
#pragma db type(BINARY16_TYPE) access(b1)
char b1_[16];
// Array member via modifier.
//
public:
const char* b2 () const {return b2_;}
void b2 (const char* b2) {std::memcpy (b2_, b2, sizeof (b2_));}
private:
#pragma db type(BINARY16_TYPE) access(b2)
char b2_[16];
public:
bool operator== (const object& o) const
{
return id_ == o.id_ &&
i1_ == o.i1_ &&
i2_ == o.i2_ &&
i3_ == o.i3_ &&
i4_ == o.i4_ &&
i5_ == o.i5_ &&
i6_ == o.i6_ &&
i7_ == o.i7_ &&
s1_ == o.s1_ &&
std::memcmp (b1_, o.b1_, sizeof (b1_)) == 0 &&
std::memcmp (b2_, o.b2_, sizeof (b2_)) == 0;
}
};
}
// Test composite accessor/modifier functionality.
//
#pragma db namespace table("t2_")
namespace test2
{
#pragma db value
struct value
{
value () {}
value (int i1, int i2): i1_ (i1), i2_ (i2) {}
bool operator== (const value& v) const
{
return i1_ == v.i1_ && i2_ == v.i2_;
}
public:
int i1 () const {return i1_;}
int& i1 () {return i1_;}
private:
#pragma db access(i1)
int i1_;
public:
int i2 () const {return i2_;}
void i2 (int i2) {i2_ = i2;}
private:
#pragma db access(i2)
int i2_;
};
#pragma db object
struct object
{
object () {}
object (unsigned long id): id_ (id) {}
bool operator== (const object& o) const
{
return id_ == o.id_ &&
v1_ == o.v1_ &&
v2_ == o.v2_ &&
v3_ == o.v3_;
}
public:
unsigned long id () const {return id_;}
void id (unsigned long id) {id_ = id;}
private:
#pragma db id access(id)
unsigned long id_;
public:
const value& v1 () const {return v1_;}
value& v1 () {return v1_;}
private:
#pragma db access(v1)
value v1_;
public:
const value& v2 () const {return v2_;}
void v2 (const value& v2) {v2_ = v2;}
private:
#pragma db access(v2)
value v2_;
public:
int v3_i1 () const {return v3_.i1 ();}
int v3_i2 () const {return v3_.i2 ();}
void v3_i1 (int i1) {v3_.i1 () = i1;}
void v3_i2 (int i2) {v3_.i2 (i2);}
private:
#pragma db get(test2::value (this.v3_i1 (), this.v3_i2 ())) \
set(this.v3_i1 ((?).i1 ()); this.v3_i2 ((?).i2 ()))
value v3_;
};
}
// Test object pointer accessor/modifier functionality.
//
#pragma db namespace table("t3_")
namespace test3
{
struct object1;
typedef std::unique_ptr<object1> object1_ptr;
#pragma db object pointer(object1_ptr)
struct object1
{
object1 () {}
object1 (unsigned long id): id_ (id) {}
public:
unsigned long id () const {return id_;}
void id (unsigned long id) {id_ = id;}
private:
#pragma db id access(id)
unsigned long id_;
};
#pragma db object
struct object2
{
object2 () {}
object2 (unsigned long id): id_ (id) {}
public:
unsigned long id () const {return id_;}
void id (unsigned long id) {id_ = id;}
private:
#pragma db id access(id)
unsigned long id_;
public:
const object1_ptr& p1 () const {return p1_;}
object1_ptr& p1 () {return p1_;}
private:
#pragma db access(p1)
object1_ptr p1_;
public:
const object1_ptr& p2 () const {return p2_;}
void p2 (object1_ptr p2) {p2_ = std::move (p2);}
private:
#pragma db get(p2) set(p2 (std::move (?)))
object1_ptr p2_;
};
}
// Test container accessor/modifier functionality.
//
#pragma db namespace table("t4_")
namespace test4
{
#pragma db value
struct value
{
value (): c3_ (3, 999) {}
value (int v): c1_ (3, v), c2_ (3, v + 1), c3_ (3, v + 2) {}
bool operator== (const value& v) const
{
return c1_ == v.c1_ && c2_ == v.c2_ && c3_ == v.c3_;
}
public:
const std::vector<int>& c1 () const {return c1_;}
std::vector<int>& c1 () {return c1_;}
private:
#pragma db access(c1)
std::vector<int> c1_;
public:
const std::vector<int>& c2 () const {return c2_;}
void c2 (const std::vector<int>& c2) {c2_ = c2;}
private:
#pragma db access(c2)
std::vector<int> c2_;
public:
const std::vector<int> c3_;
};
#pragma db object
struct object
{
object () {}
object (unsigned long id): id_ (id), c3_ (3, 3123), v2_ (2123) {}
bool operator== (const object& o) const
{
return id_ == o.id_ &&
c1_ == o.c1_ &&
c2_ == o.c2_ &&
c3_ == o.c3_ &&
v1_ == o.v1_ &&
v2_ == o.v2_;
}
public:
unsigned long id () const {return id_;}
void id (unsigned long id) {id_ = id;}
private:
#pragma db id access(id)
unsigned long id_;
public:
const std::vector<int>& c1 () const {return c1_;}
std::vector<int>& c1 () {return c1_;}
private:
#pragma db access(c1)
std::vector<int> c1_;
public:
const std::vector<int>& c2 () const {return c2_;}
void c2 (const std::vector<int>& c2) {c2_ = c2;}
private:
#pragma db access(c2)
std::vector<int> c2_;
public:
const std::vector<int>& c3 () const {return c3_;}
private:
#pragma db get(c3) set(const_cast<std::vector<int>&> (this.c3 ()))
const std::vector<int> c3_;
public:
const value& v1 () const {return v1_;}
value& v1 () {return v1_;}
private:
#pragma db access(v1)
value v1_;
public:
const value v2_;
};
}
// Test id accessor/modifier functionality.
//
#pragma db namespace table("t5_")
namespace test5
{
#pragma db object
struct object1
{
object1 (): id_ (0) {}
public:
unsigned long id () const {return id_;}
void id (unsigned long id) {id_ = id;}
private:
#pragma db id auto access(id)
unsigned long id_;
};
#pragma db object
struct object2
{
object2 (): id_ (0) {}
public:
unsigned long id () const {return id_;}
unsigned long& id () {return id_;}
private:
#pragma db id auto access(id)
unsigned long id_;
};
#pragma db object
struct object3
{
object3 (): id_ (0) {}
#pragma db id auto
const unsigned long id_;
};
#pragma db value
struct uuid
{
uuid () {std::memset (data_, 0, sizeof (data_));}
explicit uuid (const char* d) {data (d);}
public:
const char* data () const {return data_;}
void data (const char* d) {std::memcpy (data_, d, sizeof (data_));}
private:
#pragma db type(BINARY16_TYPE) column("") access(data)
char data_[16];
};
#pragma db object
struct object4
{
public:
const uuid& id () const {return id_;}
void id (const uuid& id) {id_ = id;}
private:
#pragma db id access(id)
uuid id_;
};
}
// Test version accessor/modifier functionality.
//
#pragma db namespace table("t6_")
namespace test6
{
#pragma db object optimistic
struct object1
{
object1 (unsigned long id = 0): id_ (id), version_ (0) {}
#pragma db id
unsigned long id_;
public:
unsigned long version () const {return version_;}
void version (unsigned long version) {version_ = version;}
private:
#pragma db version access(version)
unsigned long version_;
};
#pragma db object optimistic
struct object2
{
object2 (): version_ (0) {}
#pragma db id auto
unsigned long id_;
public:
unsigned long version () const {return version_;}
unsigned long& version () {return version_;}
private:
#pragma db version access(version)
unsigned long version_;
};
#pragma db object optimistic
struct object3
{
object3 (unsigned long id = 0): id_ (id), version_ (0) {}
#pragma db id
unsigned long id_;
#pragma db version
const unsigned long version_;
};
}
// Test automatic discovery of accessor/modifier functions.
//
#pragma db namespace table("t7_")
namespace test7
{
#pragma db object
struct object
{
object () {}
object (unsigned long id): id_ (id) {}
#pragma db id
unsigned long id_;
public:
int i1 () const {return i1_;}
int& i1 () {return i1_;}
private:
int i1_;
public:
const int& get_i2 () const {return i2_;}
void set_i2 (int i2) {i2_ = i2;}
private:
int i2_;
public:
const int& getI3 () const {return i3_;}
void setI3 (const int& i3) {i3_ = i3;}
private:
int i3_;
public:
int geti4 () const {return i4;}
int seti4 (int v) {int r (i4); i4 = v; return r;}
private:
int i4;
// Prefer reference modifier.
//
public:
int i5 () const {return i5_;}
int& i5 () {return i5_;}
void i5 (int i5);
private:
int i5_;
// Prefer reference modifier (reverse function order).
//
public:
int i6 () const {return i6_;}
void i6 (int i6);
int& i6 () {return i6_;}
private:
int i6_;
// Custom accessor/modifier regex.
//
public:
int GetI7 () const {return i7_;}
void SetI7 (int i7) {i7_ = i7;}
private:
int i7_;
// Array member via ref.
//
public:
const char* b1 () const {return b1_;}
char* b1 () {return b1_;}
private:
#pragma db type(BINARY16_TYPE)
char b1_[16];
// Array member via modifier.
//
public:
const char* b2 () const {return b2_;}
void b2 (const char* b2) {std::memcpy (b2_, b2, sizeof (b2_));}
private:
#pragma db type(BINARY16_TYPE)
char b2_[16];
public:
bool operator== (const object& o) const
{
return id_ == o.id_ &&
i1_ == o.i1_ &&
i2_ == o.i2_ &&
i3_ == o.i3_ &&
i4 == o.i4 &&
i5_ == o.i5_ &&
i6_ == o.i6_ &&
i7_ == o.i7_ &&
std::memcmp (b1_, o.b1_, sizeof (b1_)) == 0 &&
std::memcmp (b2_, o.b2_, sizeof (b2_)) == 0;
}
};
}
#endif // TEST_HXX
|