On Apr 25, 2010, at 7:46 PM, Michael Bayer wrote:

> 
> You can acheive mostly the result you want using a custom SQL expression 
> construct, in conjunction with sqlalchemy.ext.compiler.  The construct would 
> peek into a context-specific dictionary for its contents at compile time and 
> forward onto it's corresponding construct.   You'd stick the dictionary on 
> the the outermost containing construct, which you can then grab from the 
> compiler instance.

here's that:

from sqlalchemy import *
from sqlalchemy.sql.expression import ColumnElement
from sqlalchemy.ext.compiler import compiles

def with_sql_tokens(element, **tokens):
    element._sql_tokens = tokens
    return element

class SQLToken(ColumnElement):
    def __init__(self, name):
        self.name = name

@compiles(SQLToken)
def visit_sql_token(element, compiler, **kw):
    value = compiler.statement._sql_tokens[element.name]
    return compiler.process(value)

if __name__ == '__main__':
    from sqlalchemy.sql import table, column
    t1 = table('sometable', column('c1'), column('c2'))
    
    s1 = select([t1]).where(SQLToken('whereclause'))
    
    print with_sql_tokens(s1, whereclause=or_(t1.c.c1=='foo', t1.c.c2=='bar'))


-- 
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