aboutsummaryrefslogtreecommitdiff
path: root/common/inverse/driver.cxx
blob: e3c8b22261d0d0a5b7dee065f934ce74bad20ba1 (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
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
// file      : common/inverse/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test bidirectional relationships with inverse sides.
//

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

#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/session.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));

    // Naked pointer version.
    //
    {
      obj1_ptr o1 (new obj1);
      obj2_ptr o2 (new obj2);
      obj3_ptr o3_1 (new obj3);
      obj3_ptr o3_2 (new obj3);

      o1->id = "obj1";
      o1->o2 = o2;
      o1->o3.insert (o3_1);
      o1->o3.insert (o3_2);

      o2->str = "obj2";
      o2->o1 = o1;

      o3_1->str = "obj3 1";
      o3_1->o1 = o1;

      o3_2->str = "obj3 3";
      o3_2->o1 = o1;

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

        // obj2 and obj3 come first to get object id assigned.
        //
        db->persist (o3_1);
        db->persist (o3_2);
        db->persist (o2);
        db->persist (o1);

        t.commit ();
      }

      // load
      //
      {
        session s;
        transaction t (db->begin ());
        obj2_ptr x2 (db->load<obj2> (o2->id));
        obj3_ptr x3_1 (db->load<obj3> (o3_1->id));
        obj3_ptr x3_2 (db->load<obj3> (o3_2->id));
        t.commit ();

        assert (x2->str == o2->str);
        assert (x2->o1->id == o1->id);
        assert (x2->o1->o2 == x2);

        assert (x3_1->str == o3_1->str);
        assert (x3_2->str == o3_2->str);
        assert (x3_1->o1 == x3_2->o1);
        assert (x3_1->o1->id == o1->id);
        assert (x3_1->o1->o3.find (x3_1) != x3_1->o1->o3.end ());
        assert (x3_1->o1->o3.find (x3_2) != x3_1->o1->o3.end ());

        delete x2->o1;
      }

      delete o1;
    }

    // TR1 pointer version.
    //
#ifdef HAVE_TR1_MEMORY
    {
      tr1_obj1_ptr o1 (new tr1_obj1);
      tr1_obj2_ptr o2 (new tr1_obj2);
      tr1_obj3_ptr o3_1 (new tr1_obj3);
      tr1_obj3_ptr o3_2 (new tr1_obj3);

      o1->id = "obj1";
      o1->o2 = o2;
      o1->o3.push_back (o3_1);
      o1->o3.push_back (o3_2);

      o2->str = "obj2";
      o2->o1 = o1;

      o3_1->str = "obj3 1";
      o3_1->o1 = o1;

      o3_2->str = "obj3 3";
      o3_2->o1 = o1;

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

        // obj2 and obj3 come first to get object id assigned.
        //
        db->persist (o3_1);
        db->persist (o3_2);
        db->persist (o2);
        db->persist (o1);

        t.commit ();
      }

      // load
      //
      {
        session s;
        transaction t (db->begin ());
        tr1_obj2_ptr x2 (db->load<tr1_obj2> (o2->id));
        tr1_obj3_ptr x3_1 (db->load<tr1_obj3> (o3_1->id));
        tr1_obj3_ptr x3_2 (db->load<tr1_obj3> (o3_2->id));
        t.commit ();

        assert (x2->str == o2->str);
        assert (x2->o1.lock ()->id == o1->id);
        assert (x2->o1.lock ()->o2 == x2);

        assert (x3_1->str == o3_1->str);
        assert (x3_2->str == o3_2->str);
        assert (x3_1->o1.lock () == x3_2->o1.lock ());
        assert (x3_1->o1.lock ()->id == o1->id);
        assert (x3_1->o1.lock ()->o3[0] == x3_1);
        assert (x3_1->o1.lock ()->o3[1] == x3_2);
      }
    }
#endif
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}