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
|
// file : view/driver.cxx
// copyright : not copyrighted - public domain
#include <memory> // std::auto_ptr
#include <iostream>
#include <odb/database.hxx>
#include <odb/session.hxx>
#include <odb/transaction.hxx>
#include "database.hxx" // create_database
#include "employee.hxx"
#include "employee-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
using tr1::shared_ptr;
try
{
auto_ptr<database> db (create_database (argc, argv));
// Create the legacy employee_extra table.
//
{
// First try to drop the table if it exists.
//
{
transaction t (db->begin ());
try
{
#ifndef DATABASE_ORACLE
db->execute ("DROP TABLE view_employee_extra");
#else
db->execute ("DROP TABLE \"view_employee_extra\"");
#endif
t.commit ();
}
catch (const odb::exception&)
{
}
}
{
transaction t (db->begin ());
#ifndef DATABASE_ORACLE
db->execute (
"CREATE TABLE view_employee_extra ("
"employee_id INTEGER NOT NULL,"
"vacation_days INTEGER NOT NULL,"
"previous_employer_id INTEGER)");
#else
db->execute (
"CREATE TABLE \"view_employee_extra\" ("
"\"employee_id\" INTEGER NOT NULL,"
"\"vacation_days\" INTEGER NOT NULL,"
"\"previous_employer_id\" INTEGER)");
#endif
t.commit ();
}
}
// Create a few persistent objects.
//
{
shared_ptr<country> ca (new country ("CA", "Canada"));
shared_ptr<country> za (new country ("ZA", "South Africa"));
shared_ptr<country> us (new country ("US", "United States"));
shared_ptr<country> se (new country ("SE", "Sweden"));
shared_ptr<employer> st (new employer (1, "Simple Tech Ltd"));
shared_ptr<employer> cs (new employer (2, "Complex Systems Inc"));
shared_ptr<employee> e1 (
new employee (1, "John", "Doe", 29, ca, ca, st));
shared_ptr<employee> e2 (
new employee (2, "Jane", "Doe", 30, za, us, cs));
shared_ptr<employee> e3 (
new employee (3, "Joe", "Dirt", 31, us, za, st));
shared_ptr<employee> e4 (
new employee (4, "Johan", "Johansen", 32, se, se, cs));
transaction t (db->begin ());
db->persist (ca);
db->persist (za);
db->persist (us);
db->persist (se);
db->persist (st);
db->persist (cs);
db->persist (e1);
db->persist (e2);
db->persist (e3);
db->persist (e4);
// Populate the legacy table.
//
#ifndef DATABASE_ORACLE
db->execute ("INSERT INTO view_employee_extra ("
"employee_id, vacation_days, previous_employer_id) "
"VALUES (1, 5, 2)");
db->execute ("INSERT INTO view_employee_extra ("
"employee_id, vacation_days, previous_employer_id) "
"VALUES (2, 10, NULL)");
db->execute ("INSERT INTO view_employee_extra ("
"employee_id, vacation_days, previous_employer_id) "
"VALUES (3, 0, NULL)");
db->execute ("INSERT INTO view_employee_extra ("
"employee_id, vacation_days, previous_employer_id) "
"VALUES (4, 15, 1)");
#else
db->execute ("INSERT INTO \"view_employee_extra\" ("
"\"employee_id\", "
"\"vacation_days\", "
"\"previous_employer_id\") "
"VALUES (1, 5, 2)");
db->execute ("INSERT INTO \"view_employee_extra\" ("
"\"employee_id\", "
"\"vacation_days\", "
"\"previous_employer_id\") "
"VALUES (2, 10, NULL)");
db->execute ("INSERT INTO \"view_employee_extra\" ("
"\"employee_id\", "
"\"vacation_days\", "
"\"previous_employer_id\") "
"VALUES (3, 0, NULL)");
db->execute ("INSERT INTO \"view_employee_extra\" ("
"\"employee_id\", "
"\"vacation_days\", "
"\"previous_employer_id\") "
"VALUES (4, 15, 1)");
#endif
t.commit ();
}
// Load names of the employees that are under 31 using the employee_name
// view.
//
{
typedef odb::query<employee_name> query;
typedef odb::result<employee_name> result;
transaction t (db->begin ());
result r (db->query<employee_name> (query::age < 31));
cout << "Employees under 31" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << " " << i->first << " " << i->last << endl;
cout << endl;
t.commit ();
}
// Count the number of employees which has the Doe last name using the
// employee_count view.
//
{
transaction t (db->begin ());
result<employee_count> r (
db->query<employee_count> (query<employee_count>::last == "Doe"));
// Result of this aggregate query contains only one element.
//
cout << r.begin ()->count << " employees with the Doe last name" << endl
<< endl;
t.commit ();
}
// Load the employee-employer information for all the employees with the
// Doe last name using the employee_employer view.
//
{
typedef odb::query<employee_employer> query;
typedef odb::result<employee_employer> result;
transaction t (db->begin ());
// Note that we need to add the object name after query::.
//
result r (db->query<employee_employer> (query::employee::last == "Doe"));
cout << "Employees with the Doe last name" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << " " << i->first << " " << i->last << " "
<< i->employer_name << endl;
cout << endl;
t.commit ();
}
// Calculate min/max employee ages for each employer.
//
{
typedef odb::query<employer_age> query;
typedef odb::result<employer_age> result;
transaction t (db->begin ());
result r (db->query<employer_age> ());
// Some other interesting queries to try:
//
// This one restricts the calculation to a specific employer:
//
// result r (db->query<employer_age> (
// query::employer::name == "Simple Tech Ltd"));
//
// And this one filters the employees based on certain criteria.
//
// result r (db->query<employer_age> (
// query::employee::last == "Doe"));
//
cout << "Min/max employee ages" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << " " << i->employer_name << " "
<< i->min_age << '/' << i->max_age << endl;
cout << endl;
t.commit ();
}
// Load the country information for employees with different residence
// and nationality.
//
{
typedef odb::query<employee_country> query;
typedef odb::result<employee_country> result;
transaction t (db->begin ());
// Note that we use the alias given in the db object pragma after
// query::.
//
result r (db->query<employee_country> (
query::res_country::name != query::nat_country::name));
cout << "Employees residing outside of country of nationality" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << " " << i->first << " " << i->last << " "
<< i->res_country_name << " " << i->nat_country_name << endl;
cout << endl;
t.commit ();
}
// Get the list of employees that have accumulated vacation days.
//
{
typedef odb::result<employee_vacation> result;
transaction t (db->begin ());
// With native views we have to use the native SQL query syntax.
//
#ifndef DATABASE_ORACLE
result r (db->query<employee_vacation> ("vacation_days <> 0"));
#else
result r (db->query<employee_vacation> ("\"vacation_days\" <> 0"));
#endif
cout << "Employees with accumulated vacation days" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << " " << i->id << " " << i->days << endl;
cout << endl;
t.commit ();
}
// Get the list of employees that have accumulated vacation days, this
// time using the improved employee_vacation2 view.
//
{
typedef odb::result<employee_vacation2> result;
transaction t (db->begin ());
#ifndef DATABASE_ORACLE
result r (db->query<employee_vacation2> ("vacation_days <> 0"));
#else
result r (db->query<employee_vacation2> ("\"vacation_days\" <> 0"));
#endif
cout << "Employees with accumulated vacation days (take 2)" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << " " << i->first << " " << i->last << " " << i->days << endl;
cout << endl;
t.commit ();
}
// Show the previous employers using the employee_prev_employer view.
//
{
typedef odb::query<employee_prev_employer> query;
typedef odb::result<employee_prev_employer> result;
transaction t (db->begin ());
result r (db->query<employee_prev_employer> ());
cout << "Previous employees" << endl;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
const nullable<string>& pe (i->prev_employer_name);
cout << " " << i->first << " " << i->last << " "
<< (pe.null () ? string ("N/A") : *pe) << endl;
}
cout << endl;
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
|