[odb-users] How to make a "lazy" QList?

Boris Kolpackov boris at codesynthesis.com
Fri Nov 2 09:42:47 EDT 2012


Hi Rene,

Rene Jensen <rene at catatonic.dk> writes:

> Have you given any further thoughts to this? Automatic loading and saving
> of lists of objects is increasingly becoming a pain.

Yes, I think it is generally a good idea, perhaps also combined with
change tracking.

Unfortunately (or furtunately; depends on how you look at it I guess)
there is a lot of other "generally good ideas" as well as features
requested by paying customer that are competing with this. For example,
right now, we are working on multi-database support. I will try my best
to squeeze this into the next release but cannot really promise anything.

The only workaround that I could think of is rather dirty. The idea is
to create another persistent class with a subset of data members but
which is mapped to the same table. You can then use this data member
to update only a section of the class.

In fact, with virtual data members you can create a thin wrapper class
for this. Here is an example:

#pragma db object
struct person
{
  #pragma db id auto
  unisgned int id_;

  std::string name_;
  unisgned int age_;
};

#pragma db object table("person")
struct person_name
{
  person_name (person& p): p_ (p) {}

  #pragma db member(id) virtual(unsigned int) id access(p_.id_)
  #pragma db member(name) virtual(std::string) access(p_.name_)

  #pragma db transient
  person& p_;
};

Then, in your code, when you are ready to update person's name,
you do:

person p;

...

p.name_ = "John Doe";

db.update (person_name (p));

In a sesne, this is like a view but it works for updating. Pretty cool,
actually; virtual data members are a very powerful feature.

Boris



More information about the odb-users mailing list