Update 2025-04-24_11:44:19
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,150 @@
|
||||
from typing import (
|
||||
Any,
|
||||
Dict,
|
||||
Mapping,
|
||||
Optional,
|
||||
Sequence,
|
||||
Type,
|
||||
TypeVar,
|
||||
Union,
|
||||
cast,
|
||||
overload,
|
||||
)
|
||||
|
||||
from sqlalchemy import util
|
||||
from sqlalchemy.engine.interfaces import _CoreAnyExecuteParams
|
||||
from sqlalchemy.engine.result import Result, ScalarResult, TupleResult
|
||||
from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession
|
||||
from sqlalchemy.ext.asyncio.result import _ensure_sync_result
|
||||
from sqlalchemy.ext.asyncio.session import _EXECUTE_OPTIONS
|
||||
from sqlalchemy.orm._typing import OrmExecuteOptionsParameter
|
||||
from sqlalchemy.sql.base import Executable as _Executable
|
||||
from sqlalchemy.util.concurrency import greenlet_spawn
|
||||
from typing_extensions import deprecated
|
||||
|
||||
from ...orm.session import Session
|
||||
from ...sql.base import Executable
|
||||
from ...sql.expression import Select, SelectOfScalar
|
||||
|
||||
_TSelectParam = TypeVar("_TSelectParam", bound=Any)
|
||||
|
||||
|
||||
class AsyncSession(_AsyncSession):
|
||||
sync_session_class: Type[Session] = Session
|
||||
sync_session: Session
|
||||
|
||||
@overload
|
||||
async def exec(
|
||||
self,
|
||||
statement: Select[_TSelectParam],
|
||||
*,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> TupleResult[_TSelectParam]: ...
|
||||
|
||||
@overload
|
||||
async def exec(
|
||||
self,
|
||||
statement: SelectOfScalar[_TSelectParam],
|
||||
*,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> ScalarResult[_TSelectParam]: ...
|
||||
|
||||
async def exec(
|
||||
self,
|
||||
statement: Union[
|
||||
Select[_TSelectParam],
|
||||
SelectOfScalar[_TSelectParam],
|
||||
Executable[_TSelectParam],
|
||||
],
|
||||
*,
|
||||
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
|
||||
execution_options: Mapping[str, Any] = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> Union[TupleResult[_TSelectParam], ScalarResult[_TSelectParam]]:
|
||||
if execution_options:
|
||||
execution_options = util.immutabledict(execution_options).union(
|
||||
_EXECUTE_OPTIONS
|
||||
)
|
||||
else:
|
||||
execution_options = _EXECUTE_OPTIONS
|
||||
|
||||
result = await greenlet_spawn(
|
||||
self.sync_session.exec,
|
||||
statement,
|
||||
params=params,
|
||||
execution_options=execution_options,
|
||||
bind_arguments=bind_arguments,
|
||||
_parent_execute_state=_parent_execute_state,
|
||||
_add_event=_add_event,
|
||||
)
|
||||
result_value = await _ensure_sync_result(
|
||||
cast(Result[_TSelectParam], result), self.exec
|
||||
)
|
||||
return result_value # type: ignore
|
||||
|
||||
@deprecated(
|
||||
"""
|
||||
🚨 You probably want to use `session.exec()` instead of `session.execute()`.
|
||||
|
||||
This is the original SQLAlchemy `session.execute()` method that returns objects
|
||||
of type `Row`, and that you have to call `scalars()` to get the model objects.
|
||||
|
||||
For example:
|
||||
|
||||
```Python
|
||||
heroes = await session.execute(select(Hero)).scalars().all()
|
||||
```
|
||||
|
||||
instead you could use `exec()`:
|
||||
|
||||
```Python
|
||||
heroes = await session.exec(select(Hero)).all()
|
||||
```
|
||||
"""
|
||||
)
|
||||
async def execute( # type: ignore
|
||||
self,
|
||||
statement: _Executable,
|
||||
params: Optional[_CoreAnyExecuteParams] = None,
|
||||
*,
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[Dict[str, Any]] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> Result[Any]:
|
||||
"""
|
||||
🚨 You probably want to use `session.exec()` instead of `session.execute()`.
|
||||
|
||||
This is the original SQLAlchemy `session.execute()` method that returns objects
|
||||
of type `Row`, and that you have to call `scalars()` to get the model objects.
|
||||
|
||||
For example:
|
||||
|
||||
```Python
|
||||
heroes = await session.execute(select(Hero)).scalars().all()
|
||||
```
|
||||
|
||||
instead you could use `exec()`:
|
||||
|
||||
```Python
|
||||
heroes = await session.exec(select(Hero)).all()
|
||||
```
|
||||
"""
|
||||
return await super().execute(
|
||||
statement,
|
||||
params=params,
|
||||
execution_options=execution_options,
|
||||
bind_arguments=bind_arguments,
|
||||
_parent_execute_state=_parent_execute_state,
|
||||
_add_event=_add_event,
|
||||
)
|
Reference in New Issue
Block a user