aboutsummaryrefslogtreecommitdiff
path: root/odb/object-result.txx
blob: 7972da1c223d6e17eccc64d01fab2f5ba2c41301 (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
// file      : odb/object-result.txx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <odb/session.hxx>
#include <odb/cache-traits.hxx>

namespace odb
{
  //
  // object_result_impl
  //

  template <typename T>
  object_result_impl<T>::
  ~object_result_impl ()
  {
  }

  template <typename T>
  typename object_result_impl<T>::pointer_type&
  object_result_impl<T>::
  current ()
  {
    if (pointer_traits::null_ptr (current_) && !end_)
    {
      if (!session::has_current ())
      {
        pointer_type p (object_traits::create ());
        object_type& obj (pointer_traits::get_ref (p));
        current (p);
        load (obj);
      }
      else
      {
        // First check the session.
        //
        const id_type& id (load_id ());

        pointer_type p (
          pointer_cache_traits<pointer_type>::find (database (), id));

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

          typename pointer_cache_traits<pointer_type>::insert_guard ig (
            pointer_cache_traits<pointer_type>::insert (database (), id, p));

          object_type& obj (pointer_traits::get_ref (p));
          current (p);
          load (obj, false);
          ig.release ();
        }
      }
    }

    return current_;
  }

  //
  // object_result_impl_no_id
  //
  template <typename T>
  object_result_impl_no_id<T>::
  ~object_result_impl_no_id ()
  {
  }

  template <typename T>
  typename object_result_impl_no_id<T>::pointer_type&
  object_result_impl_no_id<T>::
  current ()
  {
    if (pointer_traits::null_ptr (current_) && !end_)
    {
      // Objects without ids are not stored in session cache.
      //
      pointer_type p (object_traits::create ());
      object_type& obj (pointer_traits::get_ref (p));
      current (p);
      load (obj);
    }

    return current_;
  }

  //
  // object_result_iterator
  //

  template <typename T, typename ID>
  void object_result_iterator<T, ID>::
  load (object_type& obj)
  {
    if (res_->end ())
      return;

    if (!session::has_current ())
      res_->load (obj);
    else
    {
      typename reference_cache_traits<object_type>::insert_guard ig (
        reference_cache_traits<object_type>::insert (
          res_->database (), res_->load_id (), obj));
      res_->load (obj, false);
      ig.release ();
    }
  }
}