aboutsummaryrefslogtreecommitdiff
path: root/odb/result.txx
blob: 603ee5436fb94feff03f57d527c345ae88fd5202 (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
// file      : odb/result.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 <odb/session.hxx>
#include <odb/cache-traits.hxx>

namespace odb
{
  template <typename T>
  result_impl<T>::
  ~result_impl ()
  {
  }

  template <typename T>
  typename result_impl<T>::pointer_type& result_impl<T>::
  current ()
  {
    typedef typename object_traits::pointer_type unrestricted_pointer_type;
    typedef typename object_traits::pointer_traits unrestricted_pointer_traits;

    if (pointer_traits::null_ptr (current_) && !end_)
    {
      if (!session::has_current ())
      {
        // Non-const pointer.
        //
        unrestricted_pointer_type p (object_traits::create ());
        object_type& r (unrestricted_pointer_traits::get_ref (p));

        current (pointer_type (p));
        current (r);
      }
      else
      {
        const id_type& id (current_id ());

        // First check the session.
        //
        pointer_type p (
          pointer_cache_traits<pointer_type>::find (database (), id));

        if (!pointer_traits::null_ptr (p))
          current (p);
        else
        {
          unrestricted_pointer_type up (object_traits::create ());

          typename
            pointer_cache_traits<unrestricted_pointer_type>::insert_guard ig (
            pointer_cache_traits<unrestricted_pointer_type>::insert (
              database (), id, up));

          object_type& r (unrestricted_pointer_traits::get_ref (up));

          current (pointer_type (up));
          current (r);

          ig.release ();
        }
      }
    }

    return current_;
  }

  //
  // result_iterator
  //

  template <typename T>
  void result_iterator<T>::
  load (object_type& x)
  {
    if (res_->end ())
      return;

    if (!session::has_current ())
      res_->current (x);
    else
    {
      const id_type& id (res_->current_id ());

      typename reference_cache_traits<object_type>::insert_guard ig (
        reference_cache_traits<object_type>::insert (
          res_->database (), id, x));

      res_->current (x);
      ig.release ();
    }
  }
}