]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
ensure soft_close occurs for fetchmany with server side cursor
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 2 Nov 2021 14:58:01 +0000 (10:58 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 2 Nov 2021 20:31:22 +0000 (16:31 -0400)
commite35b3f499d48ea2963d266bbf689c508cef60d8b
tree5aeb98bc19777e28e94b4fe7a319b9a5f17aa9d6
parent370624ce0eab6439352eaf502a222a7bf415dc14
ensure soft_close occurs for fetchmany with server side cursor

Fixed regression where the :meth:`_engine.CursorResult.fetchmany` method
would fail to autoclose a server-side cursor (i.e. when ``stream_results``
or ``yield_per`` is in use, either Core or ORM oriented results) when the
results were fully exhausted.

All :class:`_result.Result` objects will now consistently raise
:class:`_exc.ResourceClosedError` if they are used after a hard close,
which includes the "hard close" that occurs after calling "single row or
value" methods like :meth:`_result.Result.first` and
:meth:`_result.Result.scalar`. This was already the behavior of the most
common class of result objects returned for Core statement executions, i.e.
those based on :class:`_engine.CursorResult`, so this behavior is not new.
However, the change has been extended to properly accommodate for the ORM
"filtering" result objects returned when using 2.0 style ORM queries,
which would previously behave in "soft closed" style of returning empty
results, or wouldn't actually "soft close" at all and would continue
yielding from the underlying cursor.

As part of this change, also added :meth:`_result.Result.close` to the base
:class:`_result.Result` class and implemented it for the filtered result
implementations that are used by the ORM, so that it is possible to call
the :meth:`_engine.CursorResult.close` method on the underlying
:class:`_engine.CursorResult` when the the ``yield_per`` execution option
is in use to close a server side cursor before remaining ORM results have
been fetched. This was again already available for Core result sets but the
change makes it available for 2.0 style ORM results as well.

Fixes: #7274
Change-Id: Id4acdfedbcab891582a7f8edd2e2e7d20d868e53
(cherry picked from commit 33824a9c06ca555ad208a9925bc7b40fe489fc72)
doc/build/changelog/unreleased_14/7274.rst [new file with mode: 0644]
lib/sqlalchemy/engine/cursor.py
lib/sqlalchemy/engine/result.py
test/base/test_result.py
test/orm/test_query.py
test/sql/test_resultset.py