aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-07-04 17:53:47 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-07-04 17:53:47 +0200
commitc89d2d34e27f674720c3c497bbd18a26a5bf9f38 (patch)
tree069235941e24e1ea9ad8331e0404b90d0b283970
parentbf70d160de580c735953cedd023bfea98c7ca920 (diff)
Implement support for database operations callbacks
New object pragma: callback. New test: common/callback. New manual section: 10.1.4, "callback".
-rw-r--r--odb/callback.hxx44
-rw-r--r--odb/database.ixx23
-rw-r--r--odb/database.txx38
3 files changed, 84 insertions, 21 deletions
diff --git a/odb/callback.hxx b/odb/callback.hxx
new file mode 100644
index 0000000..16d89ef
--- /dev/null
+++ b/odb/callback.hxx
@@ -0,0 +1,44 @@
+// file : odb/callback.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef ODB_CALLBACK_HXX
+#define ODB_CALLBACK_HXX
+
+#include <odb/pre.hxx>
+
+#include <odb/details/export.hxx>
+
+namespace odb
+{
+ struct LIBODB_EXPORT callback_event
+ {
+ enum value
+ {
+ pre_persist,
+ post_persist,
+ pre_load,
+ post_load,
+ pre_update,
+ post_update,
+ pre_erase,
+ post_erase
+ };
+
+ callback_event (value v): v_ (v) {}
+ operator value () const {return v_;}
+
+ private:
+ value v_;
+ };
+
+ namespace core
+ {
+ using odb::callback_event;
+ }
+}
+
+#include <odb/post.hxx>
+
+#endif // ODB_CALLBACK_HXX
diff --git a/odb/database.ixx b/odb/database.ixx
index 252f866..bd3fb44 100644
--- a/odb/database.ixx
+++ b/odb/database.ixx
@@ -104,18 +104,6 @@ namespace odb
template <typename T>
inline void database::
- erase (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;
-
- erase<T> (object_traits::id (obj));
- }
-
- template <typename T>
- inline void database::
erase (T* p)
{
typedef typename object_traits<T>::pointer_type object_pointer;
@@ -163,15 +151,10 @@ namespace odb
inline void database::
erase_ (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;
+ typedef typename object_traits<T>::pointer_type pointer_type;
+ typedef pointer_traits<pointer_type> pointer_traits;
- erase<T> (object_traits::id (pointer_traits::get_ref (pobj)));
+ erase<T> (pointer_traits::get_ref (pobj));
}
template <typename T>
diff --git a/odb/database.txx b/odb/database.txx
index b79c6f8..832c7ad 100644
--- a/odb/database.txx
+++ b/odb/database.txx
@@ -6,6 +6,7 @@
#include <odb/exceptions.hxx>
#include <odb/transaction.hxx>
#include <odb/session.hxx>
+#include <odb/callback.hxx>
#include <odb/cache-traits.hxx>
#include <odb/pointer-traits.hxx>
@@ -23,7 +24,10 @@ namespace odb
if (!transaction::has_current ())
throw not_in_transaction ();
+ object_traits::callback (*this, obj, callback_event::pre_persist);
object_traits::persist (*this, obj);
+ object_traits::callback (*this, obj, callback_event::post_persist);
+
const typename object_traits::id_type& id (object_traits::id (obj));
reference_cache_traits<T>::insert (*this, id, obj);
return id;
@@ -45,7 +49,11 @@ namespace odb
throw not_in_transaction ();
T& obj (pointer_traits::get_ref (pobj));
+
+ object_traits::callback (*this, obj, callback_event::pre_persist);
object_traits::persist (*this, obj);
+ object_traits::callback (*this, obj, callback_event::post_persist);
+
const typename object_traits::id_type& id (object_traits::id (obj));
pointer_cache_traits<pointer_type>::insert (*this, id, pobj);
return id;
@@ -132,7 +140,9 @@ namespace odb
if (!transaction::has_current ())
throw not_in_transaction ();
+ object_traits::callback (*this, obj,callback_event::pre_update);
object_traits::update (*this, obj);
+ object_traits::callback (*this, obj, callback_event::post_update);
}
template <typename T>
@@ -150,7 +160,11 @@ namespace odb
if (!transaction::has_current ())
throw not_in_transaction ();
- object_traits::update (*this, pointer_traits::get_ref (pobj));
+ T& obj (pointer_traits::get_ref (pobj));
+
+ object_traits::callback (*this, obj, callback_event::pre_update);
+ object_traits::update (*this, obj);
+ object_traits::callback (*this, obj, callback_event::post_update);
}
template <typename T>
@@ -172,6 +186,28 @@ namespace odb
}
template <typename T>
+ void database::
+ erase (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;
+
+ typedef typename odb::object_traits<T>::pointer_type pointer_type;
+
+ if (!transaction::has_current ())
+ throw not_in_transaction ();
+
+ typename object_traits::id_type id (object_traits::id (obj));
+
+ object_traits::callback (*this, obj, callback_event::pre_erase);
+ object_traits::erase (*this, id);
+ pointer_cache_traits<pointer_type>::erase (*this, id);
+ object_traits::callback (*this, obj, callback_event::post_erase);
+ }
+
+ template <typename T>
result<T> database::
query (const odb::query<typename object_traits<T>::object_type>& q,
bool cache)