|
3 | 3 | import contextlib
|
4 | 4 | import getpass
|
5 | 5 | from operator import methodcaller
|
6 |
| -from typing import TYPE_CHECKING, Any, Literal |
| 6 | +from typing import TYPE_CHECKING, Any, Literal, Mapping |
7 | 7 |
|
8 | 8 | import sqlalchemy as sa
|
9 | 9 |
|
@@ -532,19 +532,25 @@ def _get_temp_view_definition(
|
532 | 532 | def _register_temp_view_cleanup(self, name: str, raw_name: str) -> None:
|
533 | 533 | pass
|
534 | 534 |
|
535 |
| - def _create_temp_view( |
| 535 | + def _get_compiled_statement( |
536 | 536 | self,
|
537 |
| - view: sa.Table, |
538 | 537 | definition: sa.sql.Selectable,
|
539 |
| - ) -> None: |
| 538 | + name: str, |
| 539 | + compile_kwargs: Mapping[str, Any] | None = None, |
| 540 | + ): |
| 541 | + if compile_kwargs is None: |
| 542 | + compile_kwargs = {} |
| 543 | + compiled = definition.compile(compile_kwargs=compile_kwargs) |
| 544 | + defn = self._get_temp_view_definition(name, definition=compiled) |
| 545 | + return defn, compiled.params |
| 546 | + |
| 547 | + def _create_temp_view(self, view: sa.Table, definition: sa.sql.Selectable) -> None: |
540 | 548 | raw_name = view.name
|
541 | 549 | if raw_name not in self._temp_views and raw_name in self.list_tables():
|
542 | 550 | raise ValueError(f"{raw_name} already exists as a table or view")
|
543 |
| - |
544 |
| - name = self.con.dialect.identifier_preparer.quote_identifier(raw_name) |
545 |
| - compiled = definition.compile() |
546 |
| - defn = self._get_temp_view_definition(name, definition=compiled) |
547 |
| - query = sa.text(defn).bindparams(**compiled.params) |
548 |
| - self.con.execute(query) |
| 551 | + name = self.con.dialect.identifier_preparer.quote_identifier(view.name) |
| 552 | + compiled, params = self._get_compiled_statement(definition, name) |
| 553 | + with self.begin() as con: |
| 554 | + con.execute(compiled, **params) |
549 | 555 | self._temp_views.add(raw_name)
|
550 | 556 | self._register_temp_view_cleanup(name, raw_name)
|
0 commit comments