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/database.txx | 115 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 18 deletions(-) (limited to 'odb/database.txx') diff --git a/odb/database.txx b/odb/database.txx index 31e9153..d98028d 100644 --- a/odb/database.txx +++ b/odb/database.txx @@ -5,43 +5,62 @@ #include #include +#include +#include +#include namespace odb { template typename object_traits::id_type database:: - persist (const T& obj) + persist (T& obj) { - typedef object_traits traits; + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; if (!transaction::has_current ()) throw not_in_transaction (); - traits::persist (*this, obj); - return traits::id (obj); + object_traits::persist (*this, obj); + const typename object_traits::id_type& id (object_traits::id (obj)); + reference_cache_traits::insert (*this, id, obj); + return id; } template typename object_traits::id_type database:: - persist (T& obj) + persist_ (const typename object_traits::pointer_type& pobj) { - typedef object_traits traits; + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + + typedef typename odb::object_traits::pointer_type pointer_type; + typedef odb::pointer_traits pointer_traits; if (!transaction::has_current ()) throw not_in_transaction (); - traits::persist (*this, obj); - return traits::id (obj); + T& obj (pointer_traits::get_ref (pobj)); + object_traits::persist (*this, obj); + const typename object_traits::id_type& id (object_traits::id (obj)); + pointer_cache_traits::insert (*this, id, pobj); + return id; } template typename object_traits::pointer_type database:: load (const typename object_traits::id_type& id) { - typedef object_traits traits; - typename traits::pointer_type r (find (id)); + typedef typename object_traits::pointer_type pointer_type; + typedef odb::pointer_traits pointer_traits; + + pointer_type r (find (id)); - if (traits::pointer_traits::null_ptr (r)) + if (pointer_traits::null_ptr (r)) throw object_not_persistent (); return r; @@ -59,53 +78,113 @@ namespace odb typename object_traits::pointer_type database:: find (const typename object_traits::id_type& id) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + + typedef typename odb::object_traits::pointer_type pointer_type; + typedef odb::pointer_traits pointer_traits; + + // First check the session. + // + { + pointer_type p ( + pointer_cache_traits::find (*this, id)); + + if (!pointer_traits::null_ptr (p)) + return p; + } + if (!transaction::has_current ()) throw not_in_transaction (); // Compiler error pointing here? Perhaps the object doesn't have the // default constructor? // - return object_traits::find (*this, id); + return pointer_type (object_traits::find (*this, id)); } template bool database:: find (const typename object_traits::id_type& id, T& obj) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + + if (!transaction::has_current ()) + throw not_in_transaction (); + + return object_traits::find (*this, id, obj); + } + + template + void database:: + update (T& obj) + { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + if (!transaction::has_current ()) throw not_in_transaction (); - return object_traits::find (*this, id, obj); + object_traits::update (*this, obj); } template void database:: - update (const T& obj) + update_ (const typename object_traits::pointer_type& pobj) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + + typedef typename odb::object_traits::pointer_type pointer_type; + typedef odb::pointer_traits pointer_traits; + if (!transaction::has_current ()) throw not_in_transaction (); - object_traits::update (*this, obj); + object_traits::update (*this, pointer_traits::get_ref (pobj)); } template void database:: erase (const typename object_traits::id_type& id) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + + typedef typename odb::object_traits::pointer_type pointer_type; + if (!transaction::has_current ()) throw not_in_transaction (); - object_traits::erase (*this, id); + object_traits::erase (*this, id); + pointer_cache_traits::erase (*this, id); } template result database:: - query (const odb::query& q, bool cache) + query (const odb::query::object_type>& q, + bool cache) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + typedef odb::object_traits object_traits; + if (!transaction::has_current ()) throw not_in_transaction (); - result r (object_traits::query (*this, q)); + result r (object_traits::template query (*this, q)); if (cache) r.cache (); -- cgit v1.1