aboutsummaryrefslogtreecommitdiff
path: root/common/relationship/basics/driver.cxx
blob: 14e21e5344e8d2b495ecb559c2c587ed6c18c5d1 (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
// file      : common/relationship/basics/driver.cxx
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test object relationships.
//

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

    aggr a ("aggr");
    a.o1 = new obj1 ("o1", "obj1");
    a.o2.reset (new obj2 ("obj2"));

#ifdef HAVE_CXX11
    a.v2.push_back (obj2_ptr (new obj2 ("v1 obj2 1")));
    a.v2.push_back (0);
    a.v2.push_back (obj2_ptr (new obj2 ("v1 obj2 2")));
#endif

#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY)
    a.o3.reset (new obj3 ("obj3"));

    a.c.num = 123;
    a.c.o3.reset (new obj3 ("c"));

    a.cv.push_back (comp (234, obj3_ptr (new obj3 ("cv 0"))));
    a.cv.push_back (comp (235, obj3_ptr ()));
    a.cv.push_back (comp (236, obj3_ptr (new obj3 ("cv 2"))));
#endif

    a.v1.push_back (new obj1 ("v1 0", "v1 0"));
    a.v1.push_back (0);
    a.v1.push_back (new obj1 ("v1 2", "v1 2"));

    // Set cannot contain NULL pointers.
    //
    a.s1.insert (new obj1 ("s1 0", "s1 0"));
    a.s1.insert (new obj1 ("s1 2", "s1 2"));

    a.m1[0] = new obj1 ("m1 0", "m1 0");
    a.m1[1] = 0;
    a.m1[2] = new obj1 ("m1 2", "m1 2");

    // persist
    //
    {
      transaction t (db->begin ());
      db->persist (a.o1);
      db->persist (a.o2);

#ifdef HAVE_CXX11
      for (obj2_vec::iterator i (a.v2.begin ()); i != a.v2.end (); ++i)
        if (*i)
          db->persist (*i);
#endif

#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY)
      db->persist (a.o3);

      db->persist (a.c.o3);

      for (comp_vec::iterator i (a.cv.begin ()); i != a.cv.end (); ++i)
        if (i->o3)
          db->persist (i->o3);
#endif

      for (obj1_vec::iterator i (a.v1.begin ()); i != a.v1.end (); ++i)
        if (*i)
          db->persist (*i);

      for (obj1_set::iterator i (a.s1.begin ()); i != a.s1.end (); ++i)
        if (*i)
          db->persist (*i);

      for (obj1_map::iterator i (a.m1.begin ()); i != a.m1.end (); ++i)
        if (i->second)
          db->persist (i->second);

      db->persist (a);
      t.commit ();
    }

    // load & compare
    //
    {
      transaction t (db->begin ());
      auto_ptr<aggr> a1 (db->load<aggr> (a.id));
      t.commit ();

      assert (*a1 == a);
    }

    // query
    //
    typedef odb::query<aggr> query;
    typedef odb::result<aggr> result;

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

      result r (db->query<aggr> (query::o1->str == "obj1"));
      assert (!r.empty ());
      assert (r.begin ()->o1->id == a.o1->id);
      assert (size (r) == 1);

      t.commit ();
    }

    // Test NULL pointer.
    //
    delete a.o1;
    a.o1 = 0;
    a.o2.reset ();
#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY)
    a.o3.reset ();
#endif

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

    // load & compare
    //
    {
      transaction t (db->begin ());
      auto_ptr<aggr> a1 (db->load<aggr> (a.id));
      t.commit ();

      assert (*a1 == a);
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}