aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-08-18 11:38:40 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-08-18 11:38:40 +0200
commit35c3d273bbf1bd8c9c41ac1c3d91f150df0eb280 (patch)
tree7ff18feae934977f4c56a650f29018943686ed63
parent821721c0d4fa9d6246a665b6d585c1574ed4c80f (diff)
Fix custom recursive loading in post_load callback
Before we called the callback while holding the statements locked. As a result, if the callback tried to load another object of this type, it failed. Now we unlock the statements (since we have completely loaded the object from ODB's point of view) and then call the callback. The callback test has been updated to test this situation.
-rw-r--r--odb/sqlite/object-statements.hxx16
-rw-r--r--odb/sqlite/object-statements.ixx16
-rw-r--r--odb/sqlite/object-statements.txx13
-rw-r--r--odb/sqlite/result.txx3
4 files changed, 45 insertions, 3 deletions
diff --git a/odb/sqlite/object-statements.hxx b/odb/sqlite/object-statements.hxx
index c181b17..c9c101d 100644
--- a/odb/sqlite/object-statements.hxx
+++ b/odb/sqlite/object-statements.hxx
@@ -72,6 +72,22 @@ namespace odb
{
}
+ struct auto_unlock
+ {
+ // Unlocks the statement on construction and re-locks it on
+ // destruction.
+ //
+ auto_unlock (object_statements_base&);
+ ~auto_unlock ();
+
+ private:
+ auto_unlock (const auto_unlock&);
+ auto_unlock& operator= (const auto_unlock&);
+
+ private:
+ object_statements_base& s_;
+ };
+
protected:
connection_type& conn_;
bool locked_;
diff --git a/odb/sqlite/object-statements.ixx b/odb/sqlite/object-statements.ixx
index e1ae3ef..f629ea7 100644
--- a/odb/sqlite/object-statements.ixx
+++ b/odb/sqlite/object-statements.ixx
@@ -8,6 +8,22 @@ namespace odb
namespace sqlite
{
//
+ // auto_unlock
+ //
+ inline object_statements_base::auto_unlock::
+ auto_unlock (object_statements_base& s)
+ : s_ (s)
+ {
+ s_.unlock ();
+ }
+
+ inline object_statements_base::auto_unlock::
+ ~auto_unlock ()
+ {
+ s_.lock ();
+ }
+
+ //
// auto_lock
//
template <typename T>
diff --git a/odb/sqlite/object-statements.txx b/odb/sqlite/object-statements.txx
index 0ae9886..9cd8344 100644
--- a/odb/sqlite/object-statements.txx
+++ b/odb/sqlite/object-statements.txx
@@ -70,7 +70,18 @@ namespace odb
if (!delayed_.empty ())
load_delayed_ ();
- object_traits::callback (db, *l.obj, callback_event::post_load);
+ // Temporarily unlock the statement for the post_load call so that
+ // it can load objects of this type recursively. This is safe to do
+ // because we have completely loaded the current object. Also the
+ // delayed_ list is clear before the unlock and should be clear on
+ // re-lock (since a callback can only call public API functions
+ // which will make sure all the delayed loads are processed before
+ // returning).
+ //
+ {
+ auto_unlock u (*this);
+ object_traits::callback (db, *l.obj, callback_event::post_load);
+ }
g.release ();
}
diff --git a/odb/sqlite/result.txx b/odb/sqlite/result.txx
index 728411d..9bcfe5f 100644
--- a/odb/sqlite/result.txx
+++ b/odb/sqlite/result.txx
@@ -60,9 +60,8 @@ namespace odb
object_traits::load_ (statements_, obj);
statements_.load_delayed ();
- object_traits::callback (db, obj, callback_event::post_load);
-
l.unlock ();
+ object_traits::callback (db, obj, callback_event::post_load);
}
template <typename T>