From 37e1d992d234363ff9aef45555678b5ee7203a99 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 1 Nov 2011 12:41:01 +0200 Subject: Implement support for optimistic concurrency New pragmas: optimistic, version. New test: optimistic. New database function: reload(). --- odb/database.hxx | 4 ++++ odb/database.txx | 14 +++++++++++++- odb/exceptions.cxx | 14 ++++++++++---- odb/exceptions.hxx | 7 +++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/odb/database.hxx b/odb/database.hxx index 7e29ddf..8ccc642 100644 --- a/odb/database.hxx +++ b/odb/database.hxx @@ -66,6 +66,10 @@ namespace odb void load (const typename object_traits::id_type& id, T& object); + template + void + reload (T& object); + // Return NULL/false if not found. // template diff --git a/odb/database.txx b/odb/database.txx index 42643aa..b019478 100644 --- a/odb/database.txx +++ b/odb/database.txx @@ -85,6 +85,18 @@ namespace odb } template + void database:: + reload (T& obj) + { + // T should be object_type (cannot be const). + // + typedef odb::object_traits object_traits; + + if (!object_traits::reload (*this, obj)) + throw object_not_persistent (); + } + + template typename object_traits::pointer_type database:: find (const typename object_traits::id_type& id) { @@ -206,7 +218,7 @@ namespace odb typename object_traits::id_type id (object_traits::id (obj)); object_traits::callback (*this, obj, callback_event::pre_erase); - object_traits::erase (*this, id); + object_traits::erase (*this, obj); pointer_cache_traits::erase (*this, id); object_traits::callback (*this, obj, callback_event::post_erase); } diff --git a/odb/exceptions.cxx b/odb/exceptions.cxx index 60692cf..970b3b0 100644 --- a/odb/exceptions.cxx +++ b/odb/exceptions.cxx @@ -71,16 +71,22 @@ namespace odb return "object not persistent"; } - const char* result_not_cached:: + const char* object_already_persistent:: what () const throw () { - return "query result is not cached"; + return "object already persistent"; } - const char* object_already_persistent:: + const char* object_changed:: what () const throw () { - return "object already persistent"; + return "object changed concurrently"; + } + + const char* result_not_cached:: + what () const throw () + { + return "query result is not cached"; } unknown_schema:: diff --git a/odb/exceptions.hxx b/odb/exceptions.hxx index 698baee..5f48717 100644 --- a/odb/exceptions.hxx +++ b/odb/exceptions.hxx @@ -92,6 +92,12 @@ namespace odb what () const throw (); }; + struct LIBODB_EXPORT object_changed: exception + { + virtual const char* + what () const throw (); + }; + struct LIBODB_EXPORT result_not_cached: exception { virtual const char* @@ -140,6 +146,7 @@ namespace odb using odb::timeout; using odb::object_not_persistent; using odb::object_already_persistent; + using odb::object_changed; using odb::result_not_cached; using odb::database_exception; -- cgit v1.1