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
|
// file : common/view/driver.cxx
// author : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
// Test views.
//
#include <memory> // std::auto_ptr
#include <cassert>
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <common/common.hxx>
#include "test.hxx"
#include "test-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
try
{
auto_ptr<database> db (create_database (argc, argv));
//
//
{
country* ca (new country ("CA", "Canada"));
country* za (new country ("ZA", "South Africa"));
country* us (new country ("US", "United States"));
country* se (new country ("SE", "Sweden"));
person p1 (1, "John", "Doe", 30, ca);
person p2 (2, "Jane", "Doe", 29, za);
person p3 (3, "Joe", "Dirt", 31, us);
person p4 (4, "Johansen", "Johansen", 32, se);
transaction t (db->begin ());
db->persist (ca);
db->persist (za);
db->persist (us);
db->persist (se);
db->persist (p1);
db->persist (p2);
db->persist (p3);
db->persist (p4);
t.commit ();
}
// view1
//
{
typedef odb::query<view1> query;
typedef odb::result<view1> result;
{
transaction t (db->begin ());
{
result r (db->query<view1> ());
assert (size (r) == 4);
}
{
result r (db->query<view1> ("ORDER BY age"));
assert (size (r) == 4);
}
{
result r (db->query<view1> ("age < 31 ORDER BY age"));
result::iterator i (r.begin ());
assert (i != r.end ());
assert (i->first == "Jane" && i->last == "Doe" && i->age == 29);
assert (++i != r.end ());
view1 v;
i.load (v);
assert (v.first == "John" && v.last == "Doe" && v.age == 30);
}
t.commit ();
}
}
// view2
//
{
typedef odb::query<view2> query;
typedef odb::result<view2> result;
{
transaction t (db->begin ());
{
result r (db->query<view2> ());
result::iterator i (r.begin ());
assert (i != r.end ());
assert (i->count == 4);
}
{
result r (db->query<view2> ("age < 31"));
result::iterator i (r.begin ());
assert (i != r.end ());
assert (i->count == 2);
}
t.commit ();
}
}
// view3
//
{
typedef odb::query<view3> query;
typedef odb::result<view3> result;
{
transaction t (db->begin ());
{
result r (db->query<view3> ());
size_t count (0);
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
if (i->last == "Doe")
assert (i->count == 2);
else if (i->last == "Dirt" ||
i->last == "Johansen")
assert (i->count == 1);
else
assert (false);
count++;
}
assert (count == 3);
}
t.commit ();
}
}
// view4
//
{
typedef odb::query<view4> query;
typedef odb::result<view4> result;
{
transaction t (db->begin ());
{
result r (db->query<view4> ("age > 30 ORDER BY age"));
result::iterator i (r.begin ());
assert (i != r.end ());
assert (i->first == "Joe" && i->last == "Dirt" &&
i->location == "United States");
assert (++i != r.end ());
assert (i->first == "Johansen" && i->last == "Johansen" &&
i->location == "Sweden");
}
t.commit ();
}
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
|