aboutsummaryrefslogtreecommitdiff
path: root/odb/session.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-06-04 16:33:08 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-06-04 16:33:08 +0200
commit6f0d40508286afc8cdd72a0b5f807d5c2a589cfc (patch)
treef22a38560560664cae731c37537c2bb04bffdc2a /odb/session.cxx
parente0f3efd9df3e7eefa06777717f23905022d1949e (diff)
Initial implementation
Diffstat (limited to 'odb/session.cxx')
-rw-r--r--odb/session.cxx120
1 files changed, 120 insertions, 0 deletions
diff --git a/odb/session.cxx b/odb/session.cxx
new file mode 100644
index 0000000..9ee0104
--- /dev/null
+++ b/odb/session.cxx
@@ -0,0 +1,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;
+ }
+ }
+ }
+ }
+}