aboutsummaryrefslogtreecommitdiff
path: root/inheritance/reuse/driver.cxx
blob: b9a22dae2b9766d766002d041b0396106f0513c0 (plain)
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
// file      : inheritance/reuse/driver.cxx
// copyright : not copyrighted - public domain

#include <memory>   // std::auto_ptr
#include <iostream>

#include <odb/database.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[])
{
  try
  {
    auto_ptr<database> db (create_database (argc, argv));

    // Add a few employees and contractors to the database.
    //
    {
      permanent_employee p1 ("John", "Doe");
      permanent_employee p2 ("Jane", "Doe");

      temporary_employee t1 ("John", "Smith", 6);
      temporary_employee t2 ("Jane", "Smith", 12);

      contractor c1 ("Joe", "Doe", "j.doe@example.com");
      contractor c2 ("Joe", "Smith", "j.smith@example.com");

      transaction t (db->begin ());
      db->persist (p1);
      db->persist (p2);
      db->persist (t1);
      db->persist (t2);
      db->persist (c1);
      db->persist (c2);
      t.commit ();
    }

    // Lookup a contractor based on the email address.
    //
    {
      transaction t (db->begin ());
      auto_ptr<contractor> c (db->load<contractor> ("j.smith@example.com"));
      t.commit ();

      cout << c->first () << " " << c->last () << " " << c->email () << endl;
    }

    // Query for temporary employees that have John as the first name.
    //
    {
      typedef odb::query<temporary_employee> query;
      typedef odb::result<temporary_employee> result;

      transaction t (db->begin ());

      result r (db->query<temporary_employee> (query::first == "John"));

      for (result::iterator i (r.begin ()); i != r.end (); ++i)
      {
        cout << i->first () << " " << i->last () << " "
             << i->duration () << " months" << endl;
      }

      t.commit ();
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}