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.ixx | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 172 insertions(+), 5 deletions(-) (limited to 'odb/database.ixx') diff --git a/odb/database.ixx b/odb/database.ixx index 8085210..86ccd15 100644 --- a/odb/database.ixx +++ b/odb/database.ixx @@ -11,30 +11,197 @@ namespace odb } template + inline typename object_traits::id_type database:: + persist (T* p) + { + typedef typename object_traits::pointer_type object_pointer; + + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const object_pointer& pobj (p); + + return persist_ (pobj); + } + + template class P> + inline typename object_traits::id_type database:: + persist (const P& p) + { + typedef typename object_traits::pointer_type object_pointer; + + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const object_pointer& pobj (p); + + return persist_ (pobj); + } + + template class P> + inline typename object_traits::id_type database:: + persist (P& p) + { + const P& cr (p); + return persist (cr); + } + + template + inline typename object_traits::id_type database:: + persist (const typename object_traits::pointer_type& pobj) + { + return persist_ (pobj); + } + + template inline void database:: - erase (const T& obj) + update (T* p) { - erase (object_traits::id (obj)); + typedef typename object_traits::pointer_type object_pointer; + + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const object_pointer& pobj (p); + + update_ (pobj); + } + + template class P> + inline void database:: + update (const P& p) + { + typedef typename object_traits::pointer_type object_pointer; + + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const object_pointer& pobj (p); + + update_ (pobj); + } + + template class P> + inline void database:: + update (P& p) + { + const P& cr (p); + update (cr); + } + + template + inline void database:: + update (const typename object_traits::pointer_type& pobj) + { + update_ (pobj); + } + + template + inline void database:: + erase (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; + + erase (object_traits::id (obj)); + } + + template + inline void database:: + erase (T* p) + { + typedef typename object_traits::pointer_type object_pointer; + + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const object_pointer& pobj (p); + + erase_ (pobj); + } + + template class P> + inline void database:: + erase (const P& p) + { + typedef typename object_traits::pointer_type object_pointer; + + // The passed pointer should be the same or implicit-convertible + // to the object pointer. This way we make sure the object pointer + // does not assume ownership of the passed object. + // + const object_pointer& pobj (p); + + erase_ (pobj); + } + + template class P> + inline void database:: + erase (P& p) + { + const P& cr (p); + erase (cr); + } + + template + inline void database:: + erase (const typename object_traits::pointer_type& pobj) + { + erase_ (pobj); + } + + template + inline void database:: + erase_ (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; + + erase (object_traits::id (pointer_traits::get_ref (pobj))); } template inline result database:: query (bool cache) { - return query (odb::query (), cache); + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + + return query (odb::query (), cache); } template inline result database:: query (const char* q, bool cache) { - return query (odb::query (q), cache); + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + + return query (odb::query (q), cache); } template inline result database:: query (const std::string& q, bool cache) { - return query (odb::query (q), cache); + // T can be const T while object_type will always be T. + // + typedef typename odb::object_traits::object_type object_type; + + return query (odb::query (q), cache); } } -- cgit v1.1