From 531c792dd4eecd246cc1ccebac812d6888464a78 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 22 Nov 2010 12:03:11 +0200 Subject: 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. --- odb/result.txx | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) (limited to 'odb/result.txx') 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 +#include + namespace odb { template @@ -15,12 +18,77 @@ namespace odb typename result_impl::pointer_type& result_impl:: 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::find (database (), id)); + + if (!pointer_traits::null_ptr (p)) + current (p); + else + { + unrestricted_pointer_type up (object_traits::create ()); + + typename + pointer_cache_traits::insert_guard ig ( + pointer_cache_traits::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 + void result_iterator:: + 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::insert_guard ig ( + reference_cache_traits::insert ( + res_->database (), id, x)); + + res_->current (x); + ig.release (); + } + } } -- cgit v1.1