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
|
// file : common/view/olv/test4.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
#ifndef TEST4_HXX
#define TEST4_HXX
#include <string>
#include <memory> // shared_ptr
#include <odb/core.hxx>
// Test by-value load.
//
#pragma db namespace table("t4_") session
namespace test4
{
#pragma db object
struct object1
{
object1 (int id_ = 0, int n_ = 0): id (id_), n (n_) {}
#pragma db id
int id;
int n;
};
#pragma db object
struct object2
{
object2 (int id_ = 0, const char* s_ = "", object1* o1_ = 0)
: id (id_), s (s_), o1 (o1_) {}
#pragma db id
int id;
std::string s;
object1* o1; // Shallow copy.
};
typedef object1* object1_ptr;
typedef object2* object2_ptr;
#pragma db view object(object1)
struct view1
{
#pragma db member(o1_) virtual(object1_ptr) get(&this.o1) set()
#pragma db transient
object1 o1;
};
#pragma db view object(object1) transient
struct view1a
{
view1a (): o1_null (true) {}
#pragma db member(o1_) virtual(object1_ptr) get(&this.o1) \
set(this.o1_null = !(?))
object1 o1;
bool o1_null;
};
#pragma db view object(object1)
struct view1b
{
view1b (): o1_p (0) {}
#pragma db transient
object1 o1;
#pragma db get(&this.o1) set(o1_p = (?))
object1* o1_p;
};
#pragma db view object(object1)
struct view1c
{
view1c (object1& o1): o1_p (&o1) {}
object1* o1_p;
};
#pragma db view object(object1) object(object2) transient
struct view2
{
#pragma db member(o2_) virtual(object2_ptr) get(&this.o2) set()
#pragma db member(o1_) virtual(object1_ptr) get(&this.o1) set()
object1 o1;
object2 o2;
};
#pragma db view object(object1) object(object2) transient
struct view2a
{
#pragma db member(o2_) virtual(object2_ptr) get(&this.o2) \
set(o2_null = !(?))
#pragma db member(o1_) virtual(object1_ptr) get(&this.o1) set()
object1 o1;
object2 o2;
bool o2_null;
};
// Test loading into raw pointer with non-raw object pointer.
//
using std::shared_ptr;
#pragma db object pointer(shared_ptr)
struct object3
{
object3 (int id_ = 0, int n_ = 0): id (id_), n (n_) {}
#pragma db id
int id;
int n;
};
#pragma db view object(object3)
struct view3
{
// This view implements the following slightly twisted logic: if the
// object is already in the cache, then set o3_p to that. Otherwise,
// load it into the by-value instance. We can also check whether o3_p
// points to o3 to distinguish between the two outcomes.
//
// Since we may be getting the pointer as both smart and raw, we
// need to create a bit of support code to use in the modifier
// expression.
//
void set_o3 (object3* p) {o3_p = p;} // &o3 or NULL.
void set_o3 (shared_ptr<object3> p) {o3_p = p.get ();} // From cache.
#pragma db get(&this.o3) set(set_o3(?))
object3* o3_p;
#pragma db transient
object3 o3;
// Return-by-value support (query_value()).
//
view3 (): o3_p (0) {}
view3 (const view3& x): o3_p (x.o3_p == &x.o3 ? &o3 : x.o3_p), o3 (x.o3) {}
};
}
#endif // TEST4_HXX
|