When you execute a compiled statement using Core there is always a dictionary of parameters you pass along. Whatever parameters you put here take precedence. in your example you're using bindparam() directly, so this is the easiest case, just pass "id":
def my_cached_thing():
cache = {}
stmt = sa.select([User.__table__]).where(User.__table__.
c.id == sa.bindparam('id'))
def go(engine, id):
with engine.connect() as conn:
return conn.execution_options(compiled_cache=cache).execute(stmt, id=id)
return go
# in your code
cached_lookup = my_cached_thing()
for id in [1, 10, 15, 27, 19, 38]:
result = cached_lookup(engine, id) # will only compile the statement once
# ... etc
The above thing I did with the nested function etc. is arbitrary, just the main point is, same statement object and same cache dictionary. as stated before, 1.4 will hopefully have an improvement to make all of this transparent.
Also note you can use Core select() constructs etc with the ORM classes directly:
select([User]).where(User.id == bindparam('id'))
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.