// file : odb/database.txx // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #include #include #include #include namespace odb { template typename object_traits::id_type database:: persist (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; object_traits::persist (*this, obj); object_traits::reference_cache_traits::insert ( *this, reference_cache_type::convert (obj)); return object_traits::id (obj); } template typename object_traits::id_type database:: persist_ (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; T& obj (pointer_traits::get_ref (pobj)); object_traits::persist (*this, obj); // Get the canonical object pointer and insert it into object cache. // object_traits::pointer_cache_traits::insert ( *this, pointer_cache_type::convert (pobj)); return object_traits::id (obj); } template typename object_traits::pointer_type database:: load (const typename object_traits::id_type& id) { // T is always object_type. // typedef typename object_traits::pointer_type pointer_type; typedef odb::pointer_traits pointer_traits; pointer_type r (find (id)); if (pointer_traits::null_ptr (r)) throw object_not_persistent (); return r; } template void database:: load (const typename object_traits::id_type& id, T& obj) { if (!find (id, obj)) throw object_not_persistent (); } template void database:: reload (T& obj) { // T should be object_type (cannot be const). // typedef odb::object_traits 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 struct database::query_ { static result call (database& db, const odb::query& q) { return object_traits::query (db, q); } }; template struct database::query_ { static result call (database& db, const odb::query& q) { return view_traits::query (db, q); } }; template result database:: query (const odb::query& q, bool cache) { // T is always object_type. // // We don't need to check for transaction here; // object_traits::query () does this. result r (query_::kind>::call (*this, q)); if (cache) r.cache (); return r; } }