[odb-users] "Dynamic" joins in views

Boris Kolpackov boris at codesynthesis.com
Thu Mar 1 04:05:02 EST 2012


Hi Nicolas,

Nicolas ALBEZA <n.albeza at gmail.com> writes:

> I was wondering if it was possible to do "dynamic" joins in views, for
> example, to be able to specify a join condition at runtime, without using
> native queries.

Hm, you could try providing a native predicate that includes a reference 
to a global (or class-static, or even a TLS, if your compiler supports
this mechanism) variable:

extern int param;

#pragma db view \
  object(t1)    \
  object(t2: "t1.id = t2.t1_id AND t2.t3_id =" + odb::query<view>::_ref (param))
struct view
{
 ... 
};

> Result SQL query example:
> 
> select field from t1 left join t2 on (t1.id = t2.t1_id && t2.t3_id =
> <dynamic value, eg: 42>);

Isn't this equivalent to:

select field from t1 left join t2 on t1.id = t2.t1_id where t2.t3_id = ?

If you can rewrite the query like this, then you don't need any of the
above and can use C++-integrated queries.

Boris



More information about the odb-users mailing list