aboutsummaryrefslogtreecommitdiff
path: root/tracer/object/driver.cxx
blob: 085c6a3a845e1e4c44bbd9de12f49f5805d9f61c (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// file      : tracer/object/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 object persistence operations.
//

#include <memory>
#include <cassert>
#include <iostream>

#include <odb/exceptions.hxx>
#include <odb/transaction.hxx>
#include <odb/tracer/database.hxx>

#include "test.hxx"
#include "test-odb.hxx"

using namespace std;
using namespace odb;

int
main ()
{
  tracer::database db;

  // database operation out of transaction
  //
  cout << "\ntest 001" << endl;
  {
    object o1 (1);
    try
    {
      cout << "s 1" << endl;
      db.persist (o1);
      cout << "s 2.a" << endl;
    }
    catch (const not_in_transaction&)
    {
      cout << "s 2.b" << endl;
    }
  }

  // transient -> persistent
  //
  cout << "\ntest 002" << endl;
  {
    object o1 (1);
    transaction t (db.begin ());
    cout << "s 1" << endl;
    db.persist (o1);
    cout << "s 2" << endl;
    t.commit ();
    cout << "s 3" << endl;
  }

  // transient -> persistent (already exist)
  //
  cout << "\ntest 003" << endl;
  {
    object o1 (0);
    transaction t (db.begin ());
    cout << "s 1" << endl;
    try
    {
      db.persist (o1);
    }
    catch (const object_already_persistent&)
    {
      cout << "object already persistent" << endl;
    }
    cout << "s 2" << endl;
  }

  // persistent -> transient
  //
  cout << "\ntest 004" << endl;
  {
    object o1 (1);
    object o2 (2);
    transaction t (db.begin ());
    cout << "s 1" << endl;
    db.persist (o1);
    db.persist (o2);
    cout << "s 2" << endl;
    db.erase (o1);
    db.erase<object> (2);
    cout << "s 3" << endl;
    t.commit ();
    cout << "s 4" << endl;
  }

  // persistent -> transient (not exist)
  //
  cout << "\ntest 005" << endl;
  {
    object o1 (0);
    transaction t (db.begin ());
    cout << "s 1" << endl;
    try
    {
      db.erase (o1);
    }
    catch (const object_not_persistent&)
    {
      cout << "object not persistent" << endl;
    }
    cout << "s 2" << endl;
  }

  // load new object
  //
  cout << "\ntest 006" << endl;
  {
    transaction t (db.begin ());
    cout << "s 1" << endl;
    auto_ptr<object> o1 (db.load<object> (1));
    cout << "s 2" << endl;
    t.commit ();
    cout << "s 3" << endl;
  }

  // load new object (not exist)
  //
  cout << "\ntest 007" << endl;
  {
    transaction t (db.begin ());
    cout << "s 1" << endl;
    try
    {
      auto_ptr<object> o1 (db.load<object> (0));
    }
    catch (const object_not_persistent&)
    {
      cout << "object not persistent" << endl;
    }
    cout << "s 2" << endl;
  }

  // load into existing object
  //
  cout << "\ntest 008" << endl;
  {
    object o1;
    transaction t (db.begin ());
    cout << "s 1" << endl;
    db.load (1, o1);
    cout << "s 2" << endl;
    t.commit ();
    cout << "s 3" << endl;
  }

  // load into existing object (not exist)
  //
  cout << "\ntest 009" << endl;
  {
    object o1;
    transaction t (db.begin ());
    cout << "s 1" << endl;
    try
    {
      db.load (0, o1);
    }
    catch (const object_not_persistent&)
    {
      cout << "object not persistent" << endl;
    }
    cout << "s 2" << endl;
  }

  // update
  //
  cout << "\ntest 010" << endl;
  {
    transaction t (db.begin ());
    cout << "s 1" << endl;
    auto_ptr<object> o1 (db.load<object> (1));
    cout << "s 2" << endl;
    db.update (*o1);
    cout << "s 3" << endl;
    t.commit ();
    cout << "s 4" << endl;
  }

  // update (not exist)
  //
  cout << "\ntest 011" << endl;
  {
    object o1 (0);
    transaction t (db.begin ());
    cout << "s 1" << endl;
    try
    {
      db.update (o1);
    }
    catch (const object_not_persistent&)
    {
      cout << "object not persistent" << endl;
    }
    cout << "s 2" << endl;
  }

  // find new object
  //
  cout << "\ntest 012" << endl;
  {
    transaction t (db.begin ());
    cout << "s 1" << endl;
    auto_ptr<object> o1 (db.find<object> (1));
    assert (o1.get () != 0);
    auto_ptr<object> o2 (db.find<object> (0));
    assert (o2.get () == 0);
    cout << "s 2" << endl;
    t.commit ();
    cout << "s 3" << endl;
  }

  // load into existing object
  //
  cout << "\ntest 013" << endl;
  {
    object o1;
    transaction t (db.begin ());
    cout << "s 1" << endl;
    bool r (db.find (1, o1));
    assert (r);
    r = db.find (0, o1);
    assert (!r);
    cout << "s 2" << endl;
    t.commit ();
    cout << "s 3" << endl;
  }
}