From c89d2d34e27f674720c3c497bbd18a26a5bf9f38 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 4 Jul 2011 17:53:47 +0200 Subject: Implement support for database operations callbacks New object pragma: callback. New test: common/callback. New manual section: 10.1.4, "callback". --- odb/callback.hxx | 44 ++++++++++++++++++++++++++++++++++++++++++++ odb/database.ixx | 23 +++-------------------- odb/database.txx | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 odb/callback.hxx 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 +// 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 + +#include + +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 + +#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 inline void database:: - erase (T& obj) - { - // T can be const T while object_type will always be T. - // - typedef typename odb::object_traits::object_type object_type; - typedef odb::object_traits object_traits; - - erase (object_traits::id (obj)); - } - - template - inline void database:: erase (T* p) { typedef typename object_traits::pointer_type object_pointer; @@ -163,15 +151,10 @@ namespace odb inline void database:: erase_ (const typename object_traits::pointer_type& pobj) { - // T can be const T while object_type will always be T. - // - typedef typename odb::object_traits::object_type object_type; - typedef odb::object_traits object_traits; - - typedef typename odb::object_traits::pointer_type pointer_type; - typedef odb::pointer_traits pointer_traits; + typedef typename object_traits::pointer_type pointer_type; + typedef pointer_traits pointer_traits; - erase (object_traits::id (pointer_traits::get_ref (pobj))); + erase (pointer_traits::get_ref (pobj)); } template 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 #include #include +#include #include #include @@ -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::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::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 @@ -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 @@ -172,6 +186,28 @@ namespace odb } template + void database:: + erase (T& obj) + { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + + typedef typename odb::object_traits::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::erase (*this, id); + object_traits::callback (*this, obj, callback_event::post_erase); + } + + template result database:: query (const odb::query::object_type>& q, bool cache) -- cgit v1.1