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/database.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/database.txx')
-rw-r--r-- | odb/database.txx | 115 |
1 files changed, 97 insertions, 18 deletions
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 <odb/exceptions.hxx> #include <odb/transaction.hxx> +#include <odb/session.hxx> +#include <odb/cache-traits.hxx> +#include <odb/pointer-traits.hxx> namespace odb { template <typename T> typename object_traits<T>::id_type database:: - persist (const T& obj) + persist (T& obj) { - typedef object_traits<T> traits; + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> 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<T>::insert (*this, id, obj); + return id; } template <typename T> typename object_traits<T>::id_type database:: - persist (T& obj) + persist_ (const typename object_traits<T>::pointer_type& pobj) { - typedef object_traits<T> traits; + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + + typedef typename odb::object_traits<T>::pointer_type pointer_type; + typedef odb::pointer_traits<pointer_type> 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<pointer_type>::insert (*this, id, pobj); + return id; } template <typename T> typename object_traits<T>::pointer_type database:: load (const typename object_traits<T>::id_type& id) { - typedef object_traits<T> traits; - typename traits::pointer_type r (find<T> (id)); + typedef typename object_traits<T>::pointer_type pointer_type; + typedef odb::pointer_traits<pointer_type> pointer_traits; + + pointer_type r (find<T> (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<T>::pointer_type database:: find (const typename object_traits<T>::id_type& id) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + + typedef typename odb::object_traits<T>::pointer_type pointer_type; + typedef odb::pointer_traits<pointer_type> pointer_traits; + + // First check the session. + // + { + pointer_type p ( + pointer_cache_traits<pointer_type>::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<T>::find (*this, id); + return pointer_type (object_traits::find (*this, id)); } template <typename T> bool database:: find (const typename object_traits<T>::id_type& id, T& obj) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + + if (!transaction::has_current ()) + throw not_in_transaction (); + + return object_traits::find (*this, id, obj); + } + + template <typename T> + void database:: + update (T& obj) + { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + if (!transaction::has_current ()) throw not_in_transaction (); - return object_traits<T>::find (*this, id, obj); + object_traits::update (*this, obj); } template <typename T> void database:: - update (const T& obj) + update_ (const typename object_traits<T>::pointer_type& pobj) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + + typedef typename odb::object_traits<T>::pointer_type pointer_type; + typedef odb::pointer_traits<pointer_type> pointer_traits; + if (!transaction::has_current ()) throw not_in_transaction (); - object_traits<T>::update (*this, obj); + object_traits::update (*this, pointer_traits::get_ref (pobj)); } template <typename T> void database:: erase (const typename object_traits<T>::id_type& id) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + + typedef typename odb::object_traits<T>::pointer_type pointer_type; + if (!transaction::has_current ()) throw not_in_transaction (); - object_traits<T>::erase (*this, id); + object_traits::erase (*this, id); + pointer_cache_traits<pointer_type>::erase (*this, id); } template <typename T> result<T> database:: - query (const odb::query<T>& q, bool cache) + query (const odb::query<typename object_traits<T>::object_type>& q, + bool cache) { + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits<T>::object_type object_type; + typedef odb::object_traits<object_type> object_traits; + if (!transaction::has_current ()) throw not_in_transaction (); - result<T> r (object_traits<T>::query (*this, q)); + result<T> r (object_traits::template query<T> (*this, q)); if (cache) r.cache (); |