How to log a Gql query?

192 views
Skip to first unread message

Thomas Kuczek

unread,
May 13, 2008, 10:31:05 AM5/13/08
to Google App Engine
I have a query object representing a query. How can I print the
resulting Gql to log it with the logger framework?

E.g:
class Story(db.Model):
title = db.StringProperty()
date = db.DateTimeProperty()

query = db.Query(Story)

query.filter('title =', 'Foo')
query.order('-date')
query.ancestor(key)

Now I would like to do something like:
logging.debug('Gql is: %s', query)

But this is only giving me an object reference :-)
<google.appengine.ext.db.Query object at 0x227c750>
I am looking for the final Gql that will be submitted as a query if I
call query.get() or query.fetch(10)

Thomas Kuczek

majek04

unread,
May 13, 2008, 10:45:35 AM5/13/08
to google-a...@googlegroups.com
On Tue, May 13, 2008 at 4:31 PM, Thomas Kuczek <thomas...@gmail.com> wrote:
>
> I have a query object representing a query. How can I print the
> resulting Gql to log it with the logger framework?
>
> E.g:
> class Story(db.Model):
> title = db.StringProperty()
> date = db.DateTimeProperty()
>
> query = db.Query(Story)
>
> query.filter('title =', 'Foo')
> query.order('-date')
> query.ancestor(key)
>
> Now I would like to do something like:
> logging.debug('Gql is: %s', query)
>
> But this is only giving me an object reference :-)
> <google.appengine.ext.db.Query object at 0x227c750>

On Tue, May 13, 2008 at 4:31 PM, Thomas Kuczek <thomas...@gmail.com> wrote:
>
> I have a query object representing a query. How can I print the
> resulting Gql to log it with the logger framework?
>
> E.g:
> class Story(db.Model):
> title = db.StringProperty()
> date = db.DateTimeProperty()
>
> query = db.Query(Story)
>
> query.filter('title =', 'Foo')
> query.order('-date')
> query.ancestor(key)
>
> Now I would like to do something like:
> logging.debug('Gql is: %s', query)

I use monkey patching:

def wr(orig_func, self, *args, **kwargs):
t0 = time.time()
r = orig_func(self, *args, **kwargs)
t1 = time.time()
gqls_debugging.append( "%5.0fms GQL %s\n args: %r %r "
% ((t1-t0)*1000, ("%s" % self._ToPb()).replace("\n", "\n
").strip(), args, kwargs) )
return r
monkey_patch(datastore.Query, '_Run', wr)

Full code of my simplistic debugger:
http://ai.pjwstk.edu.pl/~majek/dump/debug.py

To use it you need to inherit from debug.DebugMiddleware
instead of webapp.RequestHandler. (I'm talking about webapp
framework, not django).

Some more information (look for 'debugging datastore access'):
http://popcnt.org/2008/05/google-app-engine-tips.html

Cheers!
Marek Majkowski

--
blog: http://popcnt.org/

Barry Hunter

unread,
May 13, 2008, 10:46:05 AM5/13/08
to google-a...@googlegroups.com
From what I understand the GQL is not actully sent to the server. The
datastore has its own API that is actully pretty close to using the
Query class directly, so parsing of GQL is done in the Python just to
provide users of SQL with a mostly familar interface.

Anyway what I am trying to say, is the code doesnt actully build a GQL
query, so if want one will have to build yourself.

See
http://googleappengine.googlecode.com/svn/trunk/google/appengine/ext/gql/__init__.py

--
Barry

- www.nearby.org.uk - www.geograph.org.uk -

Thomas Kuczek

unread,
May 13, 2008, 3:31:38 PM5/13/08
to Google App Engine
Thank you for all the answers.
Is there an easy way to show something more meaningfull than the
object reference? It doesn't have to show the Gql. It could show the
attributes and some other nice things.

Thomas


On May 13, 4:46 pm, "Barry Hunter" <barrybhun...@googlemail.com>
wrote:
> From what I understand the GQL is not actully sent to the server. The
> datastore has its own API that is actully pretty close to using the
> Query class directly, so parsing of GQL is done in the Python just to
> provide users of SQL with a mostly familar interface.
>
> Anyway what I am trying to say, is the code doesnt actully build a GQL
> query, so if want one will have to build yourself.
>
> Seehttp://googleappengine.googlecode.com/svn/trunk/google/appengine/ext/...

Duncan

unread,
May 14, 2008, 3:44:28 PM5/14/08
to Google App Engine
On May 13, 8:31 pm, Thomas Kuczek <thomas.kuc...@gmail.com> wrote:
> Thank you for all the answers.
> Is there an easy way to show something more meaningfull than the
> object reference? It doesn't have to show the Gql. It could show the
> attributes and some other nice things.
>
It's a bit messy and depends heavily on the internals of a query, but
I knocked up some code to pretty-print queries. See
http://kupuguy.blogspot.com/2008/05/decoding-query-back-to-string.html

Thomas Kuczek

unread,
May 16, 2008, 1:50:39 AM5/16/08
to Google App Engine
Oh, wow.
This is exactly what I was looking for!
Thank you Ducan for your help. I am already using your code in my
nonprofit appengine project.

Greetings
Thomas Kuczek

Duncan

unread,
May 16, 2008, 3:49:25 AM5/16/08
to Google App Engine
On May 16, 6:50 am, Thomas Kuczek <thomas.kuc...@gmail.com> wrote:
> Oh, wow.
> This is exactly what I was looking for!
> Thank you Ducan for your help. I am already using your code in my
> nonprofit appengine project.
>
> Greetings
> Thomas Kuczek
>
I guess you didn't have any problem copying the code (I was quite
pleased that the coloured highlighting doesn't appear to mess up the
ability to copy-paste), but if anyone else wants it, I've now checked
the whole lot in to google code (that includes the blog post itself
which is a runnable doctest):

svn checkout http://kupuguy.googlecode.com/svn/trunk/appengine-doctests
Reply all
Reply to author
Forward
0 new messages