aboutsummaryrefslogtreecommitdiff
path: root/odb/session.txx
blob: 55155f7fa1a139913656b09b6af781ca6b5d7987 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// file      : odb/session.txx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <utility> // std::make_pair

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

#include <iostream> // @@ tmp

namespace odb
{
  //
  // session::object_proxy_impl
  //

  template <typename T>
  void session::object_proxy_impl<T>::
  persist ()
  {
    traits::insert (db_, ptr_ops::get_ref (obj_));
  }

  template <typename T>
  void session::object_proxy_impl<T>::
  update ()
  {
    traits::update (db_, ptr_ops::get_ref (obj_));
  }

  template <typename T>
  void session::object_proxy_impl<T>::
  erase ()
  {
    traits::erase (db_, traits::id (ptr_ops::get_ref (obj_)));
  }

  template <typename T>
  void session::object_proxy_impl<T>::
  register_id (id_map& idm, object_map::iterator i)
  {
    // For polymorphic types we should use the root of the hierarchy
    // (presumably defined in traits) as a type id so that all types
    // in this hierarchy end up in the same map. See also other places.
    //
    shared_ptr<type_map>& m (idm[&typeid (T)]);

    if (!m)
      m.reset (new (shared) type_map_impl<T>);

    type_map_impl<T>& mi (static_cast<type_map_impl<T>&> (*m));

    std::pair<typename type_map_impl<T>::iterator, bool> r (
      mi.insert (std::make_pair (traits::id (ptr_ops::get_ref (obj_)), i)));

    if (!r.second)
      throw object_already_persistent ();
  }

  template <typename T>
  void session::object_proxy_impl<T>::
  unregister_id (id_map& idm)
  {
    shared_ptr<type_map>& m (idm[&typeid (T)]);

    if (m)
    {
      type_map_impl<T>& mi (static_cast<type_map_impl<T>&> (*m));
      mi.erase (traits::id (ptr_ops::get_ref (obj_)));
    }
  }

  //
  // session
  //

  template <typename T, template <typename> class P>
  typename object_traits<T>::id_type session::
  persist (database& db, P<T> p)
  {
    typedef object_traits<T> traits;
    typedef typename traits::shared_ops ops;

    // P<T> should be the same or convertible to
    // object_traits<T>::shared_ptr.
    //
    const typename traits::shared_ptr& obj (p);

    // For polymorphic types we need to cast the pointer to the
    // root of the hierarchy.
    //
    void* ptr (ops::get_ptr (obj));
    T& ref (ops::get_ref (obj));

    // @@ What if the object that's already in the map is in the
    //    erased state?
    //
    if (object_map_.find (ptr) != object_map_.end ())
      throw object_already_persistent ();

    shared_ptr<object_proxy> pxy (new (shared) object_proxy_impl<T> (db, obj));

    // If we are in a transaction, save this object immediately. This
    // helps with two things: (1) assignment of auto-generated ids and
    // (2) detection of duplicate objects.
    //
    if (transaction::has_current ())
    {
      // @@ What if the manually-assigned id is already in use by
      //    an object waiting to be deleted?
      //
      traits::insert (db, ref);
      pxy->state_ = object_proxy::clean;
    }
    else
      pxy->state_ = object_proxy::transient;

    object_map::iterator i (
      object_map_.insert (std::make_pair (ptr, pxy)).first);

    // If this object has auto-assigned id and we haven't hit the db
    // yet, then the id is "NULL" and we cannot insert this object
    // into the id map.
    //
    if (pxy->state_ != object_proxy::transient ||
        traits::id_source == ids_assigned)
    {
      try
      {
        pxy->register_id (id_map_, i);
      }
      catch (...)
      {
        object_map_.erase (i);
        throw;
      }
    }

    return traits::id (ref);
  }

  template <typename T>
  typename object_traits<T>::shared_ptr session::
  find (database& db, typename object_traits<T>::id_type const& id)
  {
    typedef object_traits<T> traits;
    typedef typename traits::shared_ops ops;
    typedef typename traits::shared_ptr obj_ptr;

    // First see if we have this object in the maps.
    //
    shared_ptr<type_map>& m (id_map_[&typeid (T)]);

    if (m)
    {
      typedef type_map_impl<T> map_impl;
      map_impl& mi (static_cast<map_impl&> (*m));
      typename map_impl::iterator i (mi.find (id));

      if (i != mi.end ())
      {
        const object_map::iterator& j (i->second);

        if (j->second->state_ != object_proxy::erased)
        {
          object_proxy_impl<T>& pxy (
            static_cast<object_proxy_impl<T>&> (*j->second));
          return pxy.obj_;
        }
        else
          return obj_ptr ();
      }
    }

    // If we are in transaction then hit the database. Otherwise, the
    // object is not found.
    //
    if (!transaction::has_current ())
      return obj_ptr ();

    obj_ptr obj (traits::find (db, id));

    if (ops::null_ptr (obj))
      return obj;

    // Add this object to our maps.
    //
    void* ptr (ops::get_ptr (obj));
    shared_ptr<object_proxy> pxy (new (shared) object_proxy_impl<T> (db, obj));
    pxy->state_ = object_proxy::clean;

    object_map::iterator i (
      object_map_.insert (
        std::make_pair (ptr, pxy)).first);

    try
    {
      if (!m)
        m.reset (new (shared) type_map_impl<T>);

      type_map_impl<T>& mi (static_cast<type_map_impl<T>&> (*m));
      mi.insert (std::make_pair (id, i));
    }
    catch (...)
    {
      object_map_.erase (i);
      throw;
    }

    return obj;
  }

  template <typename T, template <typename> class P>
  void session::
  erase (database& db, P<T> p)
  {
    typedef object_traits<T> traits;
    typedef typename traits::shared_ops ops;

    // P<T> should be the same or convertible to
    // object_traits<T>::shared_ptr.
    //
    const typename traits::shared_ptr& obj (p);

    // For polymorphic types we need to cast the pointer to the
    // root of the hierarchy.
    //
    void* ptr (ops::get_ptr (obj));
    T& ref (ops::get_ref (obj));

    object_map::iterator i (object_map_.find (ptr));

    if (object_map_.find (ptr) == object_map_.end ())
      throw object_not_persistent ();

    object_proxy& pxy (*i->second);

    switch (pxy.state_)
    {
    case object_proxy::transient:
      {
        // If this object is still transient then all we need to do is
        // purge it from the maps.
        //

        // See if we are registered in the id map.
        //
        if (traits::id_source == ids_assigned)
          pxy.unregister_id (id_map_);

        object_map_.erase (i);
        break;
      }
    case object_proxy::clean:
    case object_proxy::dirty:
      {
        // Ideally we would need to store the object id as well as the
        // version (optimistic concurrency) since the user is free to
        // modify the object state.
        //
        pxy.state_ = object_proxy::erased;
        break;
      }
    case object_proxy::erased:
      {
        // Already erased. Throw to be consistent with the transient
        // case.
        //
        throw object_not_persistent ();
      }
    }
  }

  template <typename T, template <typename> class P>
  void session::
  modified (P<T> p)
  {
    typedef object_traits<T> traits;
    typedef typename traits::shared_ops ops;

    // P<T> should be the same or convertible to
    // object_traits<T>::shared_ptr.
    //
    const typename traits::shared_ptr& obj (p);

    // For polymorphic types we need to cast the pointer to the
    // root of the hierarchy.
    //
    void* ptr (ops::get_ptr (obj));

    object_map::iterator i (object_map_.find (ptr));

    if (object_map_.find (ptr) == object_map_.end ())
      throw object_not_persistent ();

    object_proxy& pxy (*i->second);

    switch (pxy.state_)
    {
    case object_proxy::transient:
    case object_proxy::dirty:
      {
        // Nothing to do here.
        //
        break;
      }
    case object_proxy::clean:
      {
        pxy.state_ = object_proxy::dirty;
        break;
      }
    case object_proxy::erased:
      {
        throw object_not_persistent ();
      }
    }
  }
}