[odb-users] Loading persistent object using custom joins

Boris Kolpackov boris at codesynthesis.com
Mon Mar 4 08:26:46 EST 2013


Hi Patrick,

Patrick Rotsaert <Patrick.Rotsaert at intoit.be> writes:

> Any chance there will be a future possibility to load objects directly
> from arbitrary SQL queries (select tablename.* from...), perhaps only
> specifying the part starting from 'from...' ?

Hm, you are the first one asking for something like this. Views are
normally the answer to the custom query needs (and they allows you to
specify a custom SELECT statement or some portion of it). But in your
case you want the actual objects, presumably so that you can update
them.

I think this particular example (i.e., find all the employers which
have employees under 30) will be better handled by supporting 
containers in queries. Something along these lines:

typedef odb::query<employer> employer_query;
typedef odb::query<employee> employee_query;

employer_query q (employer_query::employees.exist (employee_query::age < 30));

Another option that I just thought of would be to extend the view
mechanism to support loading directly into an object. In other words,
we could have a special kind of view which is essentially just a way
to create arbitrary joins for object queries. Something along these
lines:

#pragma db view object(employer) object(employee)
struct employer_loader
{
  // Lack of data members indicates we are loading the first
  // associated object (employer).
};

typedef odb::query<employer_loader> query;
typedef odb::result<employer> result;

result r (db.query<employer_loader> (query::employee::age < 30));

What do you think?

Boris



More information about the odb-users mailing list