I've refined my convenience method for select() and made quite a bit of use of it at this point. An updated zoo.py example is at http://edsuom.pastebin.com/552888.

The method looks like this:

def select(self, *args, **kw):
    """
    When called with a single argument, I{name}, this method indicates if
    the named select object already exists and sets its context to I{name}.

    With multiple arguments, the method acts like a call to
    C{sqlalchemy.select(...).compile()}, except that nothing is
    returned. Instead, the resulting select object is stored in the
    current context.

    With no arguments, the method returns the select object for the
    current context.
"""
    if len(args) == 1:
        context = args[0]
        self._context = context
        return self._selects.has_key(context)
    else:
        context = getattr(self, '_context', None)
        if len(args) == 0:
            return self._selects.get(context)
        else:
            self._selects[context] = select(*args, **kw).compile()

Just before a block of code that will use a given select() object (let's call it "foo" in the great programming tradition), insert a conditional block:

if not self.select('foo'):
    self.select(
        [fromStuff],
        whereStuff)

The first call to self.select() indicates whether the select object "foo" has already been created. It also sets the context to "foo" for other calls to self.select(). The second line, which runs only if needed, creates the table "foo."

Then you can simply do self.select().execute(...) and your code will use the cached "foo" select object. Bindparams are very useful in the select constructor, allowing you to compile and cache everything about the select() instance that is fixed and use it with only the custom parameters that need to be custom.

Comments and critiques welcome -- I'm a new but very impressed sqlalchemy user.

- Ed Suominen


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to