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/database.txx | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'odb/database.txx') 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