aboutsummaryrefslogtreecommitdiff
path: root/odb/database.txx
diff options
context:
space:
mode:
Diffstat (limited to 'odb/database.txx')
-rw-r--r--odb/database.txx162
1 files changed, 9 insertions, 153 deletions
diff --git a/odb/database.txx b/odb/database.txx
index dd0d995..abc6ba3 100644
--- a/odb/database.txx
+++ b/odb/database.txx
@@ -5,7 +5,6 @@
#include <odb/exceptions.hxx>
#include <odb/transaction.hxx>
#include <odb/session.hxx>
-#include <odb/callback.hxx>
#include <odb/cache-traits.hxx>
#include <odb/pointer-traits.hxx>
@@ -20,14 +19,10 @@ namespace odb
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 ();
-
- object_traits::callback (*this, obj, callback_event::pre_persist);
object_traits::persist (*this, obj);
- object_traits::callback (*this, obj, callback_event::post_persist);
- reference_cache_traits<T>::insert (*this, obj);
+ object_traits::reference_cache_traits::insert (
+ *this, reference_cache_type<T>::convert (obj));
return object_traits::id (obj);
}
@@ -44,16 +39,13 @@ namespace odb
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 ();
-
T& obj (pointer_traits::get_ref (pobj));
-
- object_traits::callback (*this, obj, callback_event::pre_persist);
object_traits::persist (*this, obj);
- object_traits::callback (*this, obj, callback_event::post_persist);
- pointer_cache_traits<pointer_type>::insert (*this, pobj);
+ // Get the canonical object pointer and insert it into object cache.
+ //
+ object_traits::pointer_cache_traits::insert (
+ *this, pointer_cache_type<pointer_type>::convert (pobj));
return object_traits::id (obj);
}
@@ -91,150 +83,14 @@ namespace odb
//
typedef odb::object_traits<T> object_traits;
+ // We don't need to check for transaction here;
+ // object_traits::reload () does this.
+
if (!object_traits::reload (*this, obj))
throw object_not_persistent ();
}
template <typename T>
- typename object_traits<T>::pointer_type database::
- find (const typename object_traits<T>::id_type& id)
- {
- // T is always object_type.
- //
- typedef odb::object_traits<T> object_traits;
- typedef typename object_traits::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 pointer_type (object_traits::find (*this, id));
- }
-
- template <typename T>
- bool database::
- find (const typename object_traits<T>::id_type& id, T& obj)
- {
- // T is always object_type.
- //
- if (!transaction::has_current ())
- throw not_in_transaction ();
-
- return object_traits<T>::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 ();
-
- object_traits::callback (*this, obj,callback_event::pre_update);
-
- // Compiler error pointing here? Perhaps the object is readonly or
- // doesn't have an object id? Such objects cannot be updated.
- //
- object_traits::update (*this, obj);
-
- object_traits::callback (*this, obj, callback_event::post_update);
- }
-
- template <typename T>
- void database::
- 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 ();
-
- T& obj (pointer_traits::get_ref (pobj));
-
- object_traits::callback (*this, obj, callback_event::pre_update);
-
- // Compiler error pointing here? Perhaps the object is readonly or
- // doesn't have an object id? Such objects cannot be updated.
- //
- object_traits::update (*this, obj);
-
- object_traits::callback (*this, obj, callback_event::post_update);
- }
-
- template <typename T>
- void database::
- erase (const typename object_traits<T>::id_type& id)
- {
- // T is always object_type.
- //
- typedef odb::object_traits<T> object_traits;
- typedef typename object_traits::pointer_type pointer_type;
-
- if (!transaction::has_current ())
- throw not_in_transaction ();
-
- object_traits::erase (*this, id);
- pointer_cache_traits<pointer_type>::erase (*this, id);
- }
-
- template <typename T>
- void database::
- erase (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;
- typedef typename object_traits::pointer_type pointer_type;
-
- if (!transaction::has_current ())
- throw not_in_transaction ();
-
- typename object_traits::id_type id (object_traits::id (obj));
-
- object_traits::callback (*this, obj, callback_event::pre_erase);
- object_traits::erase (*this, obj);
- pointer_cache_traits<pointer_type>::erase (*this, id);
- object_traits::callback (*this, obj, callback_event::post_erase);
- }
-
- template <typename T>
- unsigned long long database::
- erase_query (const odb::query<T>& q)
- {
- // T is always object_type.
- //
- if (!transaction::has_current ())
- throw not_in_transaction ();
-
- return object_traits<T>::erase_query (*this, q);
- }
-
- template <typename T>
struct database::query_<T, class_object>
{
static result<T>