Skip to content

Commit

Permalink
support render_postcompile (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee authored Sep 2, 2021
1 parent b4cd10c commit 1f7ca7b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion databases/backends/aiopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ def transaction(self) -> TransactionBackend:
def _compile(
self, query: ClauseElement
) -> typing.Tuple[str, dict, CompilationContext]:
compiled = query.compile(dialect=self._dialect)
compiled = query.compile(
dialect=self._dialect, compile_kwargs={"render_postcompile": True}
)

execution_context = self._dialect.execution_ctx_cls()
execution_context.dialect = self._dialect
Expand Down
4 changes: 3 additions & 1 deletion databases/backends/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def transaction(self) -> TransactionBackend:
def _compile(
self, query: ClauseElement
) -> typing.Tuple[str, dict, CompilationContext]:
compiled = query.compile(dialect=self._dialect)
compiled = query.compile(
dialect=self._dialect, compile_kwargs={"render_postcompile": True}
)

execution_context = self._dialect.execution_ctx_cls()
execution_context.dialect = self._dialect
Expand Down
4 changes: 3 additions & 1 deletion databases/backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def transaction(self) -> TransactionBackend:
return PostgresTransaction(connection=self)

def _compile(self, query: ClauseElement) -> typing.Tuple[str, list, tuple]:
compiled = query.compile(dialect=self._dialect)
compiled = query.compile(
dialect=self._dialect, compile_kwargs={"render_postcompile": True}
)

if not isinstance(query, DDLElement):
compiled_params = sorted(compiled.params.items())
Expand Down
4 changes: 3 additions & 1 deletion databases/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def transaction(self) -> TransactionBackend:
def _compile(
self, query: ClauseElement
) -> typing.Tuple[str, list, CompilationContext]:
compiled = query.compile(dialect=self._dialect)
compiled = query.compile(
dialect=self._dialect, compile_kwargs={"render_postcompile": True}
)

execution_context = self._dialect.execution_ctx_cls()
execution_context.dialect = self._dialect
Expand Down
17 changes: 17 additions & 0 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,20 @@ async def test_posgres_interface(database_url):
):
# avoid checking `id` at index 0 since it may change depending on the launched tests
assert list(result.values())[1:] == ["example1", True]


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_postcompile_queries(database_url):
"""
Since SQLAlchemy 1.4, IN operators needs to do render_postcompile
"""
async with Database(database_url) as database:
query = notes.insert()
values = {"text": "example1", "completed": True}
await database.execute(query, values)

query = notes.select().where(notes.c.id.in_([2, 3]))
results = await database.fetch_all(query=query)

assert len(results) == 0

0 comments on commit 1f7ca7b

Please sign in to comment.