Skip to content

Commit 0235c2c

Browse files
committed
Enable autoscroll for fetchone
1 parent dffb423 commit 0235c2c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

tests/test_cursor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,18 @@ def test_cursor_next_set(self, session_sync: ydb.QuerySession) -> None:
218218

219219
nextset = cursor.nextset()
220220
assert not nextset
221+
222+
def test_cursor_autoscroll(self, session_sync: ydb.QuerySession) -> None:
223+
with ydb_dbapi.Cursor(
224+
session=session_sync, auto_scroll_result_sets=True
225+
) as cursor:
226+
yql_text = "SELECT 1 as val; SELECT 2 as val; SELECT 3 as val;"
227+
cursor.execute(query=yql_text)
228+
229+
for i in range(3):
230+
res = cursor.fetchone()
231+
assert res is not None
232+
assert res[0] == i + 1
233+
234+
assert cursor.fetchone() is None
235+
assert not cursor.nextset()

ydb_dbapi/cursors.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,29 @@ def __init__(
127127
tx_context: ydb.QueryTxContext | None = None,
128128
table_path_prefix: str = "",
129129
autocommit: bool = True,
130+
auto_scroll_result_sets: bool = False,
130131
) -> None:
131132
self._session = session
132133
self._tx_context = tx_context
133134
self._table_path_prefix = table_path_prefix
134135
self._autocommit = autocommit
136+
self._auto_scroll = auto_scroll_result_sets
135137

136138
self._stream: Iterator | None = None
137139

138140
def fetchone(self) -> tuple | None:
139-
return self._fetchone_from_buffer()
141+
row = self._fetchone_from_buffer()
142+
if not self._auto_scroll:
143+
return row
144+
145+
if row is None:
146+
while self.nextset():
147+
# We should skip empty result sets
148+
row = self._fetchone_from_buffer()
149+
if row is not None:
150+
return row
151+
152+
return row
140153

141154
def fetchmany(self, size: int | None = None) -> list | None:
142155
return self._fetchmany_from_buffer(size)

0 commit comments

Comments
 (0)