Michael Bayer wrote:
Now, is there any way I can "pre-cook" this (eg: at module-level) such
that I can later just plug in self.id and on_date, bind to a session and
call .all() on it?

It seems a bit wasteful to do all the SQL generation on every query when
it's almost all identical all the time...

easiest way, call your Query from a def.

I'm not 100% what you mean here, but it looks like just put the above lump in a function definition and call it, but that sounds like all the work will still be done when the function is called?

most of the work in SQL
generation isn't internally "cached" anyway so you aren't saving much by
having the same Query lying around.

This is a bit puzzling. Surely all the taking of python Query, select, and_, join, etc objects, running them through dialects, etc and ending up with a string of sql only needs to be done once; then it should just be a case of formatting the values and plugging them into the text string at the bind points, which obviously needs to be done for each call...

what am I missing?

second way, you can use bindparam() for the binds as others have
mentioned, and then params() to set the values as needed, but the missing
link is that you want the Query against your own particular session at the
moment.   I haven't yet gotten the chance to add a "with_session()" method
to Query but you can do this easily enough yourself:

from sqlalchemy.orm.query import Query, _generative

class MyQuery(Query):
   @_generative

what does @_generative do?

   def with_session(self, session):
        self.session = session

Session = sessionmaker(query_cls=MyQuery)

so have your query lying around:

q = Session().query(...).filter(...)

Could I instead just do:

q = MyQuery(...).filter(...)

and then use it with:

q.with_session(my_session).params(foo='bar').all()

?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to