Hey,



2014-03-10 10:25 GMT+01:00 Muhammad Nasser Al-Noimi <[email protected]>:

> Hi,
>
> Suppose I have two tables related in one-to-many relation:
>
> User: (Master table)
> - id
> - name (string)
> - docs (collection)
>
> Doc: (Details)
> - id
> - name (string)
> - status (string)
> - user (pointer to User)
>
> I want to get all docs for users that have name contains "ar" for example.
> SQL must be something like this:
>
> SELECT name, status, User.name FROM Doc, User WHERE User.id = Doc.user_id
> AND User.name LIKE "%ar%"
>

The simplest, more Dbo-esque, way is to do:

typedef boost::tuple<ptr<Doc>, ptr<User> > DocUser;
session.query<DocUser>("select D, U from Doc D, User U").where("U.id =
D.user_id AND U.name LIKE "%ar%");

The benefit of this form is that you get the actual dbo pointers.

If you really want (or need, but this is rarely a bottle neck?) to optimize
the query to return only three fields, then you can do:

typedef boost::tuple<std::string, std::string, std::string> Info;
session.query<Info>("SELECT name, status, User.name FROM Doc, User WHERE
User.id = Doc.user_id AND User.name LIKE "%ar%");

I find in convenient to use boost::tie() in conjunction with
boost::tuple(), for example:

    typedef dbo::ptr_tuple<A, B, C>::type ABC; // short
boost::tuple<ptr<A>, ptr<B>, ptr<C> >
    typedef dbo::collection<ABC> ABCs;
    ABCs abcs = session.query...

    for (ABCs::const_iterator i = abcs.begin(); i != abcs.end(); ++i) {
      dbo::ptr<A> a_result;
      dbo::ptr<B> b_result;
      dbo::ptr<C> c_result;
      boost::tie(a_result, b_result, c_result) = *i;

      // ...
    }

Regards,
koen
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to