aboutsummaryrefslogtreecommitdiff
path: root/common/relationship/query/driver.cxx
blob: 97f4e0fd7c8a3404facb6c0146f82564aae9dbd9 (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
164
165
166
167
168
169
170
171
172
173
// file      : common/relationship-query/query/driver.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test relationship queries.
//

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

#include <odb/database.hxx>
#include <odb/session.hxx>
#include <odb/transaction.hxx>

#include <common/config.hxx> // HAVE_CXX11, HAVE_TR1_MEMORY
#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));

#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY)

    //
    //
    {
      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 ("Simple Tech, Inc", ca));
      shared_ptr<employer> ct (new employer ("Complex Tech, Inc", us));

      // person
      //
      shared_ptr<person> p1 (
        new person (1, "John", "Doe", 30, ca, true, za));

      shared_ptr<person> p2 (
        new person (2, "Jane", "Doe", 29, za, false, us));
      p2->husband = p1;

      shared_ptr<person> p3 (
        new person (3, "Joe", "Dirt", 31,  us, true, us));

      shared_ptr<person> p4 (
        new person (4, "Johan", "Johansen", 32, se, false, ca));

      // employee
      //
      shared_ptr<employee> e1 (
        new employee (1, "John", "Doe", 30, ca, true, za, st));

      shared_ptr<employee> e2 (
        new employee (2, "Jane", "Doe", 29, za, false, us, ct));
      e2->husband = p1;

      shared_ptr<employee> e3 (
        new employee (3, "Joe", "Dirt", 31,  us, true, us, st));

      shared_ptr<employee> e4 (
        new employee (4, "Johan", "Johansen", 32, se, false, ca, ct));

      transaction t (db->begin ());
      db->persist (ca);
      db->persist (za);
      db->persist (us);
      db->persist (se);

      db->persist (st);
      db->persist (ct);

      db->persist (p1);
      db->persist (p2);
      db->persist (p3);
      db->persist (p4);

      db->persist (e1);
      db->persist (e2);
      db->persist (e3);
      db->persist (e4);
      t.commit ();
    }

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

    typedef odb::query<employee> e_query;
    typedef odb::result<employee> e_result;

    // Make sure we have an independent JOIN for each relationship.
    //
    {
      session s;
      transaction t (db->begin ());

      p_result pr (db->query<person> (
                   p_query::residence.location->code == "ZA"));
      assert (size (pr) == 1);

      e_result er (db->query<employee> (
                   e_query::residence.location->code == "ZA"));
      assert (size (er) == 1);

      t.commit ();
    }

    // Test Self-JOIN.
    //
    {
      session s;
      transaction t (db->begin ());

      p_result pr (db->query<person> (p_query::husband->last_name == "Doe"));
      assert (size (pr) == 1);

      e_result er (db->query<employee> (e_query::husband->last_name == "Doe"));
      assert (size (er) == 1);

      t.commit ();
    }

    // Test query conditions from both base and derived.
    //
    {
      session s;
      transaction t (db->begin ());

      e_result r (
        db->query<employee> (
          e_query::employed_by->name == "Simple Tech, Inc" &&
          e_query::nationality->code == "US"));

      assert (size (r) == 1);

      t.commit ();
    }

    // Test second-level pointers.
    //
    {
      session s;
      transaction t (db->begin ());

      p_result r (
        db->query<person> (
          p_query::husband->residence.location == "CA"));

      assert (size (r) == 1);

      t.commit ();
    }

#endif // HAVE_CXX11 || HAVE_TR1_MEMORY

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