aboutsummaryrefslogtreecommitdiff
path: root/common/composite/driver.cxx
blob: bf8e668cadad88a5db6bd67375abfa56cc0a1258 (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
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
// file      : common/composite/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 composite value types.
//

#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;

int
main (int argc, char* argv[])
{
  try
  {
    auto_ptr<database> db (create_database (argc, argv));

    for (unsigned short i (0); i < 2; ++i)
    {
      person p (1);
      p.name_.first = "Joe";
      p.name_.last = "Dirt";
      p.name_.title = "Mr";
      p.name_.alias.first = "Anthony";
      p.name_.alias.last = "Clean";
      p.name_.nick = "Squeaky";
      p.name_.flags.nick = true;
      p.name_.flags.alias = false;
      p.age_ = 32;

      // persist
      //
      {
        transaction t (db->begin ());
        db->persist (p);
        t.commit ();
      }

      // load & check
      //
      {
        transaction t (db->begin ());
        auto_ptr<person> p1 (db->load<person> (1));
        t.commit ();

        assert (p == *p1);
      }

      p.name_.title = "Mrs";
      p.name_.alias.first = "Anthonia";
      p.name_.flags.nick = false;
      p.name_.flags.alias = true;

      // update
      //
      {
        transaction t (db->begin ());
        db->update (p);
        t.commit ();
      }

      // load & check
      //
      {
        transaction t (db->begin ());
        auto_ptr<person> p1 (db->load<person> (1));
        t.commit ();

        assert (p == *p1);
      }

      typedef odb::query<person> query;
      typedef odb::result<person> result;

      // query
      //
      {
        transaction t (db->begin ());

        result r (db->query<person> (query::name::first == "Joe"));

        assert (r.size () == 1);
        assert (*r.begin () == p);

        t.commit ();
      }

      // query
      //
      {
        transaction t (db->begin ());

        result r (db->query<person> (query::name::flags::alias));

        assert (r.size () == 1);
        assert (*r.begin () == p);

        t.commit ();
      }

      // erase
      //
      if (i == 0)
      {
        transaction t (db->begin ());
        db->erase<person> (1);
        t.commit ();
      }
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}