"""
+ dialect: Dialect
dispatch: dispatcher[ConnectionEventsTarget]
_sqla_logger_namespace = "sqlalchemy.engine.Connection"
return self._proxied.invalidated
@property
- def dialect(self) -> Any:
+ def dialect(self) -> Dialect:
r"""Proxy for the :attr:`_engine.Connection.dialect` attribute
on behalf of the :class:`_asyncio.AsyncConnection` class.
return self._proxied.dialect
@dialect.setter
- def dialect(self, attr: Any) -> None:
+ def dialect(self, attr: Dialect) -> None:
self._proxied.dialect = attr
@property
from .sqltypes import TableValueType
from .sqltypes import TupleType
from .type_api import TypeEngine
+ from ..engine import Dialect
from ..util.typing import TypeGuard
_T = TypeVar("_T", bound=Any)
def __call__(self, obj: _CE) -> _CE: ...
+class _HasDialect(Protocol):
+ """protocol for Engine/Connection-like objects that have dialect
+ attribute.
+ """
+
+ @property
+ def dialect(self) -> Dialect: ...
+
+
# match column types that are not ORM entities
_NOT_ENTITY = TypeVar(
"_NOT_ENTITY",
from ._typing import _ByArgument
from ._typing import _ColumnExpressionArgument
from ._typing import _ColumnExpressionOrStrLabelArgument
+ from ._typing import _HasDialect
from ._typing import _InfoType
from ._typing import _PropagateAttrsType
from ._typing import _TypeEngineArgument
from .visitors import anon_map
from ..engine import Connection
from ..engine import Dialect
- from ..engine import Engine
from ..engine.interfaces import _CoreMultiExecuteParams
from ..engine.interfaces import CacheStats
from ..engine.interfaces import CompiledCacheType
@util.preload_module("sqlalchemy.engine.url")
def compile(
self,
- bind: Optional[Union[Engine, Connection]] = None,
+ bind: Optional[_HasDialect] = None,
dialect: Optional[Dialect] = None,
**kw: Any,
) -> Compiled:
def compile( # noqa: A001
self,
- bind: Optional[Union[Engine, Connection]] = None,
+ bind: Optional[_HasDialect] = None,
dialect: Optional[Dialect] = None,
**kw: Any,
) -> SQLCompiler: ...
eq_(m.mock_calls, [])
+ @async_test
+ async def test_statement_compile(self, async_engine):
+ stmt = _select1(async_engine)
+ eq_(str(select(1).compile(async_engine)), stmt)
+ async with async_engine.connect() as conn:
+ eq_(str(select(1).compile(conn)), stmt)
+
def test_clear_compiled_cache(self, async_engine):
async_engine.sync_engine._compiled_cache["foo"] = "bar"
eq_(async_engine.sync_engine._compiled_cache["foo"], "bar")
):
event.listen(async_engine, "checkout", mock.Mock())
- def select1(self, engine):
- if engine.dialect.name == "oracle":
- return "select 1 from dual"
- else:
- return "select 1"
-
@async_test
async def test_sync_before_cursor_execute_engine(self, async_engine):
canary = mock.Mock()
event.listen(async_engine.sync_engine, "before_cursor_execute", canary)
- s1 = self.select1(async_engine)
+ s1 = _select1(async_engine)
async with async_engine.connect() as conn:
sync_conn = conn.sync_connection
await conn.execute(text(s1))
async def test_sync_before_cursor_execute_connection(self, async_engine):
canary = mock.Mock()
- s1 = self.select1(async_engine)
+ s1 = _select1(async_engine)
async with async_engine.connect() as conn:
sync_conn = conn.sync_connection
tasks = [thing(engine) for _ in range(10)]
await asyncio.gather(*tasks)
+
+
+def _select1(engine):
+ if engine.dialect.name == "oracle":
+ return "SELECT 1 FROM DUAL"
+ else:
+ return "SELECT 1"
from sqlalchemy import create_engine
from sqlalchemy import Pool
+from sqlalchemy import select
from sqlalchemy import text
engine = create_engine("postgresql://scott:tiger@localhost/test")
status: str = engine.pool.status()
other_pool: Pool = engine.pool.recreate()
+ ce = select(1).compile(e)
+ ce.statement
+ cc = select(1).compile(conn)
+ cc.statement
print(status, other_pool)
from typing import Any
from sqlalchemy import Connection
+from sqlalchemy import select
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
# EXPECTED_MYPY: Missing positional argument "foo" in call to "run_sync" of "AsyncConnection"
await conn.run_sync(work_sync)
+
+ ce = select(1).compile(e)
+ ce.statement
+ cc = select(1).compile(conn)
+ cc.statement