aboutsummaryrefslogtreecommitdiff
path: root/odb/database.txx
blob: b79c6f8800412ea365a485b74c370389ba6f2c83 (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
// file      : odb/database.txx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

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

namespace odb
{
  template <typename T>
  typename object_traits<T>::id_type database::
  persist (T& obj)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    object_traits::persist (*this, obj);
    const typename object_traits::id_type& id (object_traits::id (obj));
    reference_cache_traits<T>::insert (*this, id, obj);
    return id;
  }

  template <typename T>
  typename object_traits<T>::id_type database::
  persist_ (const typename object_traits<T>::pointer_type& pobj)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    typedef typename odb::object_traits<T>::pointer_type pointer_type;
    typedef odb::pointer_traits<pointer_type> pointer_traits;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    T& obj (pointer_traits::get_ref (pobj));
    object_traits::persist (*this, obj);
    const typename object_traits::id_type& id (object_traits::id (obj));
    pointer_cache_traits<pointer_type>::insert (*this, id, pobj);
    return id;
  }

  template <typename T>
  typename object_traits<T>::pointer_type database::
  load (const typename object_traits<T>::id_type& id)
  {
    typedef typename object_traits<T>::pointer_type pointer_type;
    typedef odb::pointer_traits<pointer_type> pointer_traits;

    pointer_type r (find<T> (id));

    if (pointer_traits::null_ptr (r))
      throw object_not_persistent ();

    return r;
  }

  template <typename T>
  void database::
  load (const typename object_traits<T>::id_type& id, T& obj)
  {
    if (!find<T> (id, obj))
      throw object_not_persistent ();
  }

  template <typename T>
  typename object_traits<T>::pointer_type database::
  find (const typename object_traits<T>::id_type& id)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    typedef typename odb::object_traits<T>::pointer_type pointer_type;
    typedef odb::pointer_traits<pointer_type> pointer_traits;

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

      if (!pointer_traits::null_ptr (p))
        return p;
    }

    if (!transaction::has_current ())
      throw not_in_transaction ();

    // Compiler error pointing here? Perhaps the object doesn't have the
    // default constructor?
    //
    return pointer_type (object_traits::find (*this, id));
  }

  template <typename T>
  bool database::
  find (const typename object_traits<T>::id_type& id, T& obj)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    return object_traits::find (*this, id, obj);
  }

  template <typename T>
  void database::
  update (T& obj)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    object_traits::update (*this, obj);
  }

  template <typename T>
  void database::
  update_ (const typename object_traits<T>::pointer_type& pobj)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    typedef typename odb::object_traits<T>::pointer_type pointer_type;
    typedef odb::pointer_traits<pointer_type> pointer_traits;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    object_traits::update (*this, pointer_traits::get_ref (pobj));
  }

  template <typename T>
  void database::
  erase (const typename object_traits<T>::id_type& id)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    typedef typename odb::object_traits<T>::pointer_type pointer_type;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    object_traits::erase (*this, id);
    pointer_cache_traits<pointer_type>::erase (*this, id);
  }

  template <typename T>
  result<T> database::
  query (const odb::query<typename object_traits<T>::object_type>& q,
         bool cache)
  {
    // T can be const T while object_type will always be T.
    //
    typedef typename odb::object_traits<T>::object_type object_type;
    typedef odb::object_traits<object_type> object_traits;

    if (!transaction::has_current ())
      throw not_in_transaction ();

    result<T> r (object_traits::template query<T> (*this, q));

    if (cache)
      r.cache ();

    return r;
  }
}