[odb-users] BaseDaoEnabled for ODB

علی دانش adanesh at noornet.net
Sat Sep 19 13:29:07 EDT 2015


Hi Boris,
Your solution is much better than I did. I used and enjoyed. :)
Thanks,
Ali
________________________________________
From: Boris Kolpackov <boris at codesynthesis.com>
Sent: Friday, September 18, 2015 10:44 AM
To: علی دانش
Cc: odb-users at codesynthesis.com
Subject: Re: [odb-users] BaseDaoEnabled for ODB

Hi Ali,

> 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?

Sorry, can't review it, it's just too messy. But I can suggest a
much cleaner solution: The idea is to use ODB database operation
callbacks (Section 14.1.7, "callback"). All that hacking you've
done to make various parts of ODB call you setDB() -- there is
actually support for this in ODB. Sometimes it pays to read the
manual ;-).

In any case, here is the outline (I hate that "DAO" term, so using
a more meaningful names):

#include <odb/core.hxx>
#include <odb/callback.hxx>

struct database_object
{
  mutable odb::database* db_ = nullptr;

  void
  init (odb::callback_event, odb::database& db) const
  {
    db_ = &db;
  }
};

#pragma db object callback(init)
struct object1: database_object
{
  ...
};

#pragma db object callback(init)
struct object2: database_object
{
  ...
};

The really cool thing about this approach is that you can set the
database not only on loads but also on persists and updates. And
you can use the callback_event argument to detect erases and reset
db_ back to nullptr, since the object is no longer persistent.

Now, if you have a persistent common base for all your objects (for
example, that defines the "standard" object id for your model), then
you won't even need to repear that callback() pragma for every
persistent class:

#pragma db object abstract callback(init)
struct database_object
{
  #pragma db id auto
  std::uint64_t id;

  #pragma db transient
  mutable odb::database* db_ = nullptr;

  void
  init (odb::callback_event, odb::database& db) const
  {
    db_ = &db;
  }
};

#pragma db object
struct object1: database_object
{
  ...
};

#pragma db object
struct object2: database_object
{
  ...
};

Boris



More information about the odb-users mailing list