[odb-users] Combining views queries conditions

Boris Kolpackov boris at codesynthesis.com
Thu Nov 12 10:37:43 EST 2015


Hi, 

Даниил Савченко <cfdx at list.ru> writes:

> #pragma db object
> class Object...;
>
> #pragma db view object(Object)
> class View...;
> 
> void f( const query<Object>& q ) {
>  auto res = db->query<View>( q );
> }
> 
> I have tried it and it looks like it works. But is it a hack?

Yes, there are cases where this will/might not work. For example,
a view could join the same object multiple types (and assign
aliases to distinguish between them). If you pass a query condition
that is based on the object, it will be ambiguous.

However, in simple cases, where you don't assign aliases to objects
in views, you can reasonably expect this to work (both queries will
use the same, un-aliased table underneath).


> #pragma db view object(Object1) object(Object2:...)
> class View2...;
> 
> I understand that it is possible to set conditions right in terms of the 
> View fields, but I want to encapsulate creation subconditions in different
> parts of application, and reuse these parts in different places where
> different Views are used. Or maybe there exist recommended way (some best
> practices) to do this.

Hm, I would say, use templates:

template <typename Q1, typename Q2>
auto f (const Q1& q1, const Q2& q2)
{
  return q1 && q2;
}

using Q = query<View2>;

db->query<View2> (f (Q::Object1::name == "Jonn", Q::Object2::age == 18));

Boris



More information about the odb-users mailing list