aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-11-01 12:41:01 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-11-01 12:41:01 +0200
commit37e1d992d234363ff9aef45555678b5ee7203a99 (patch)
tree0cb5e7066c02619233c1fde66f21b4704087b37b
parent7aee40f95c26ae3c5af2c723af57ba316f99cf0c (diff)
Implement support for optimistic concurrency
New pragmas: optimistic, version. New test: optimistic. New database function: reload().
-rw-r--r--odb/database.hxx4
-rw-r--r--odb/database.txx14
-rw-r--r--odb/exceptions.cxx14
-rw-r--r--odb/exceptions.hxx7
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<T>::id_type& id, T& object);
+ template <typename T>
+ void
+ reload (T& object);
+
// Return NULL/false if not found.
//
template <typename T>
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 <typename T>
+ void database::
+ reload (T& obj)
+ {
+ // T should be object_type (cannot be const).
+ //
+ typedef odb::object_traits<T> object_traits;
+
+ if (!object_traits::reload (*this, obj))
+ throw object_not_persistent ();
+ }
+
+ template <typename T>
typename object_traits<T>::pointer_type database::
find (const typename object_traits<T>::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<pointer_type>::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;