new DAL('gae')

3 views
Skip to first unread message

mdipierro

unread,
Dec 12, 2009, 12:42:34 AM12/12/09
to web2py-users
I have re-factored a lot of code in gluon/contrib/gql.py in order to
make it leaner, more readable and add new syntax. Now you can do mix
and match queries like:

q1=(db.table.id==1)
q2=(db.table.id>0)
q3=(db.table.field.belongs(('value1','value2','value3')))

in expressions like

db(q1)(q2)q3).select()
db(q1&q&q3).select()
db(...).update(...)
db(...).delete()

Before this patch, belongs was not supported by web2py on GAE, q1 and
q2 could not be mixed with other conditions.

Now you can do for example:

d=datetime.date.today()
db.define_table('person',Field('name'),Field('birthday','date'))
db.person.insert(name='John',birthday=d)
rows=db(db.person.id>0)(db.person.name.belongs(('John',))).count()
rows=db(db.person.id==1)(db.person.name.belongs(('John',)))\
(birthday==d).update(name='Jim')

Moreover the db['_lastsql'] now contains a complete representation of
the last query on GAE and it can be used for debugging, and both db, db
(...) and can serialized as a string.

Please check it and report any problem.

Also please let me know if you find places in the online documentation
that have just become obsolete.

Massimo

Richard

unread,
Dec 12, 2009, 7:19:43 PM12/12/09
to web2py-users
how did you add belongs support for GAE? I remember in a previous
discussion this was considered not possible/practical:
http://code.google.com/p/web2py/issues/detail?id=56&can=1

mdipierro

unread,
Dec 12, 2009, 10:17:04 PM12/12/09
to web2py-users
At some point GAE added support for the IN operator. I do not know
when but when I found out I added it (belongs maps to IN). Notice that
this is not the same as LIKE.

Massimo

Richard

unread,
Dec 13, 2009, 6:33:18 AM12/13/09
to web2py-users
neat! I need to keep closer track of GAE developments ...

Robin B

unread,
Dec 14, 2009, 12:17:32 AM12/14/09
to web2py-users
IN is not magic, and evaluates1 db query for each member, and should
be used carefully.

q3=(db.table.field.belongs(('value1','value2','value3')))

evaluates 3 db queries to get the result,

Robin

mdipierro

unread,
Dec 14, 2009, 1:04:43 AM12/14/09
to web2py-users
Unless I misunderstand. web2py is not breaking this in 3 GAE queries.
It maps the belongs in the GAE IN operator. Since the IN is executed
at the entity level, before data is aggregated, ordered and filtered,
I do not think there is any significant overhead. Or at least there
should not be. Yet I do not know how GAE is implemented and I have not
benchmarked it.

Massimo

Robin B

unread,
Dec 16, 2009, 12:43:01 PM12/16/09
to web2py-users
Google api resolves the IN operator by using multiple queries:

http://code.google.com/appengine/docs/python/datastore/gqlreference.html

"The IN operator compares value of a property to each item in a list.
The IN operator is equivalent to many = queries, one for each value,
that are ORed together. An entity whose value for the given property
equals any of the values in the list can be returned for the query.

Note: The IN and != operators use multiple queries behind the scenes.
For example, the IN operator executes a separate underlying datastore
query for every item in the list. The entities returned are a result
of the cross-product of all the underlying datastore queries and are
de-duplicated. A maximum of 30 datastore queries are allowed for any
single GQL query."

In SQL IN would be O(1) in GQL it is O(N) so it should be used
carefully.

Robin
Reply all
Reply to author
Forward
0 new messages