diff options
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 (); |