aboutsummaryrefslogtreecommitdiff
path: root/odb/session.cxx
blob: 9ee0104ca87a6c10d8ee258fd44445ac8cc0bf20 (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
// file      : odb/session.cxx
// 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/session.hxx>
#include <odb/transaction.hxx>

namespace odb
{
  session::type_map::
  ~type_map ()
  {
  }

  session::object_proxy::
  ~object_proxy ()
  {
  }

  //
  // session
  //

  static session* current_session = 0;

  session::
  session ()
  {
    if (current_session != 0)
      throw already_in_session ();

    current_session = this;
  }

  bool session::
  has_current ()
  {
    return current_session != 0;
  }

  session& session::
  current ()
  {
    if (current_session == 0)
      throw not_in_session ();

    return *current_session;
  }

  void session::
  current (session& s)
  {
    current_session = &s;
  }

  void session::
  reset_current ()
  {
    current_session = 0;
  }

  void session::
  flush ()
  {
    if (!transaction::has_current ())
      throw not_in_transaction ();

    // @@ Order of insertion and deletion can be important (triggers,
    // id assignment, constraints etc).
    //

    for (object_map::iterator i (object_map_.begin ()),
           e (object_map_.end ()); i != e;)
    {
      object_proxy& pxy (*i->second);

      switch (pxy.state_)
      {
      case object_proxy::transient:
        {
          pxy.persist ();

          // If the id is auto-assigned, then we only get it now, so
          // register with the id map.
          //
          if (pxy.id_source_ != ids_assigned)
            pxy.register_id (id_map_, i);

          pxy.state_ = object_proxy::clean;
          ++i;
          break;
        }
      case object_proxy::dirty:
        {
          pxy.update ();
          pxy.state_ = object_proxy::clean;
          ++i;
          break;
        }
      case object_proxy::erased:
        {
          pxy.erase ();
          pxy.unregister_id (id_map_);
          object_map_.erase (i++);
          break;
        }
      case object_proxy::clean:
        {
          // Nothing to do for this case.
          //
          ++i;
          break;
        }
      }
    }
  }
}