[Cubicweb] A Query API experiment

Christophe de Vienne christophe at unlish.com
Tue Jun 10 17:34:42 CEST 2014


Le 10/06/2014 16:42, Nicolas Chauvat a écrit :
> Hi Christophe,
>
> On Tue, Jun 10, 2014 at 04:28:31PM +0200, Christophe de Vienne wrote:
>> A few month ago (at the first cw sprint of the year), I presented a
>> slide on what a query API could be like.
>> [...]
> Thank you for sharing this work.
>
> I find RQL more readable and I have never been a fan of SQLAlchemy's
> query/ORM API, but it is very good that people try new ways of doing
> things with CubicWeb... and maybe I will see the light and learn to
> like it ! :D

I understand your point, but I still think it is worth trying to have
such an API. Among the reasons there is of course an easier access for
people with an SQLAlchemy background, and new comers generally speaking.

But I also find such a builder easier to use compared to raw RQL queries
because we don't need to manipulate variables.
And although variables in RQL can be a very powerful weapon to solve
non-trivial situation, most of the time they get in the way.

I, for example, often do RQL in the cw shell. If I want to reuse a
previous query and add an attribute to the "select" part, I have to
modify two different places in the request.

>>> rql("Any X WHERE X is CWUser")

if I want to get the user login, I have to insert LOGIN in the first
part, and add a criterion which is not a filtering one in the WHERE part :

>>> rql("Any X, LOGIN WHERE X is CWUser, X login LOGIN")

The same thing with the query would be :

>>> q = cnx.query("CWUser"); q.all()

Then get more attributes :

>>> q.eagerload("login").all()


Another case where I like this kind of API is when building a query in
my code. I can clearly separate the code that fetch attributes in one
place, the code that filter things in another, and may be some code to
optimise the query (by lazy/eager loading extra values) in a later place.

That said, raw RQL must remain a first-class citizen in CW, this API is
complementary, not a replacement.

> I have no problem with this cube being hosted on cubicweb.org if you
> want to.

I would appreciate yes, as it is a good way to get code reviews by
differently opinionated and nonetheless very talented developpers :-)

Christophe


More information about the Cubicweb mailing list