[odb-users] BaseDaoEnabled for ODB
علی دانش
adanesh at noornet.net
Fri Sep 18 00:19:57 EDT 2015
Hi Boris,
Is there a feature in ODB similar to 'Class BaseDaoEnabled' in OrmLite?
(http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/misc/BaseDaoEnabled.html)
I need this feature in my project, I changed the odb code to add this feature. It works up to now and I have not any problem yet, but I am not sure is it a good solution or not? Is it possible for you to review it and inform me about possible problems in it?
Steps to add this feature:
1- Add 'BaseDaoEnabled .hxx':
#ifndef ODB_BASEDAOENABLED_HXX
#define ODB_BASEDAOENABLED_HXX
namespace odb
{
// Base of odb objects and views:
class BaseDaoEnabled
{
template <typename T, bool isBaseDaoEnabled> friend struct SetDaoEnabled;
public:
inline database* getDB() const { return db_; };
protected:
inline void setDB(database* db){ db_ = db; };
private:
database* db_ = nullptr;
};
template <typename T, bool = std::is_base_of<BaseDaoEnabled, T>::value> struct SetDaoEnabled
{
static inline void setDB(T*, odb::database*) {}
};
template <typename T> struct SetDaoEnabled < T, true >
{
static inline void setDB(T*, odb::database*);
};
template <typename T> /*static*/ inline void SetDaoEnabled<T, true>::setDB(T* p, odb::database* db)
{
p->BaseDaoEnabled::setDB(db);
}
}
#endif //ODB_BASEDAOENABLED_HXX
2- odb\forward.hxx, Line 174 (before #include <odb/post.hxx>) add:
a. #include "odb/BaseDaoEnabled.hxx"
3- odb\no_id_object_result.hxx, Line 17, change (a to b):
a. pointer_type p (object_traits::create ());
b. pointer_type p (object_traits::create (&db_));
4- odb\simple-object-result.txx, Line 25, change (a to b):
a. p = object_traits::create ();
b. p = object_traits::create (&db_);
5- odb\view_result.txx, Line 18, change (a to b):
a. pointer_type p (view_traits::create ());
b. pointer_type p (view_traits::create (&db_));
6- odb\traits.hxx
a. Line 44 & 60 & 76, change (i to ii):
i. create ()
ii. create (odb::database* db = nullptr)
b. Line 48 & 64, change (i to ii):
i. return pointer_factory<T, P>::create ();
ii. return pointer_factory<T, P>::create (db);
c. Line 81, add:
i. SetDaoEnabled<T>::setDB(p, db);
7- Generated file "%(Filename)-odb.cxx"; replace (a) with (b) by using a tool after running odb.exe:
a. access::object_factory<object_type, pointer_type>::create ()
b. access::object_factory<object_type, pointer_type>::create (&db)
That’s it. And now I can easily access to db in my models if they are inherited from odb::BaseDaoEnabled. If the model is not inherited from odb::BaseDaoEnabled all things happens like before. In my project each instance of my model may be from different sqlite database and in the model I need to access to db for example to do a query or load another related object and so on.
Best regards,
Ali
More information about the odb-users
mailing list