def query(stmt, args=(), factory=None):
    """
    Execute a query. This returns an iterator of the result set.
    """
    ctx = Context.current()
    factory = ctx.default_factory if factory is None else factory
    with ctx.mdr:
        return factory(ctx.execute(stmt, args), ctx.mdr)