diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-11-22 12:03:11 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-11-22 12:03:11 +0200 |
commit | 531c792dd4eecd246cc1ccebac812d6888464a78 (patch) | |
tree | 9bed050c98a2c407c68e808ae1f1d296a65c5fee /odb/result.txx | |
parent | 1cddfd09a7007f77fc243f178b1ca88ea4d0f4f6 (diff) |
Add session, database operations on pointers and const objects
Currently, session is just an object cache. The persist, update, and
erase database operations are overloaded to also work on object
pointers. All the database operations and the query facility now
support const objects.
New session-related exceptions: not_in_session, already_in_session,
const_object.
Diffstat (limited to 'odb/result.txx')
-rw-r--r-- | odb/result.txx | 72 |
1 files changed, 70 insertions, 2 deletions
diff --git a/odb/result.txx b/odb/result.txx index 3068890..603ee54 100644 --- a/odb/result.txx +++ b/odb/result.txx @@ -3,6 +3,9 @@ // copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#include <odb/session.hxx> +#include <odb/cache-traits.hxx> + namespace odb { template <typename T> @@ -15,12 +18,77 @@ namespace odb typename result_impl<T>::pointer_type& result_impl<T>:: current () { + typedef typename object_traits::pointer_type unrestricted_pointer_type; + typedef typename object_traits::pointer_traits unrestricted_pointer_traits; + if (pointer_traits::null_ptr (current_) && !end_) { - current (traits::create ()); - current (pointer_traits::get_ref (current_)); + if (!session::has_current ()) + { + // Non-const pointer. + // + unrestricted_pointer_type p (object_traits::create ()); + object_type& r (unrestricted_pointer_traits::get_ref (p)); + + current (pointer_type (p)); + current (r); + } + else + { + const id_type& id (current_id ()); + + // First check the session. + // + pointer_type p ( + pointer_cache_traits<pointer_type>::find (database (), id)); + + if (!pointer_traits::null_ptr (p)) + current (p); + else + { + unrestricted_pointer_type up (object_traits::create ()); + + typename + pointer_cache_traits<unrestricted_pointer_type>::insert_guard ig ( + pointer_cache_traits<unrestricted_pointer_type>::insert ( + database (), id, up)); + + object_type& r (unrestricted_pointer_traits::get_ref (up)); + + current (pointer_type (up)); + current (r); + + ig.release (); + } + } } return current_; } + + // + // result_iterator + // + + template <typename T> + void result_iterator<T>:: + load (object_type& x) + { + if (res_->end ()) + return; + + if (!session::has_current ()) + res_->current (x); + else + { + const id_type& id (res_->current_id ()); + + typename reference_cache_traits<object_type>::insert_guard ig ( + reference_cache_traits<object_type>::insert ( + res_->database (), id, x)); + + res_->current (x); + ig.release (); + } + } } |