Skip to content

Commit 249e569

Browse files
committed
response iterator as context manager
1 parent c8e74a8 commit 249e569

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

examples/query-service/basic_example.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,40 @@ def callee(session):
3838

3939
print("=" * 50)
4040
print("INSERT WITH COMMIT TX")
41-
tx = session.transaction()
4241

43-
tx.begin()
42+
with session.transaction() as tx:
43+
tx.begin()
4444

45-
tx.execute("""INSERT INTO example (key, value) VALUES (1, "onepieceisreal");""")
45+
with tx.execute("""INSERT INTO example (key, value) VALUES (1, "onepieceisreal");""") as results:
46+
pass
4647

47-
for result_set in tx.execute("""SELECT COUNT(*) as rows_count FROM example;"""):
48-
print(f"rows: {str(result_set.rows)}")
48+
with tx.execute("""SELECT COUNT(*) as rows_count FROM example;""") as results:
49+
for result_set in results:
50+
print(f"rows: {str(result_set.rows)}")
4951

50-
tx.commit()
52+
tx.commit()
5153

52-
print("=" * 50)
53-
print("AFTER COMMIT TX")
54+
print("=" * 50)
55+
print("AFTER COMMIT TX")
5456

55-
for result_set in session.execute("""SELECT COUNT(*) as rows_count FROM example;"""):
56-
print(f"rows: {str(result_set.rows)}")
57+
for result_set in session.execute("""SELECT COUNT(*) as rows_count FROM example;"""):
58+
print(f"rows: {str(result_set.rows)}")
5759

5860
print("=" * 50)
5961
print("INSERT WITH ROLLBACK TX")
6062

61-
tx = session.transaction()
63+
with session.transaction() as tx:
64+
tx.begin()
6265

63-
tx.begin()
66+
tx.execute("""INSERT INTO example (key, value) VALUES (2, "onepieceisreal");""")
6467

65-
tx.execute("""INSERT INTO example (key, value) VALUES (2, "onepieceisreal");""")
68+
for result_set in tx.execute("""SELECT COUNT(*) as rows_count FROM example;"""):
69+
print(f"rows: {str(result_set.rows)}")
6670

67-
for result_set in tx.execute("""SELECT COUNT(*) as rows_count FROM example;"""):
68-
print(f"rows: {str(result_set.rows)}")
69-
70-
tx.rollback()
71+
tx.rollback()
7172

72-
print("=" * 50)
73-
print("AFTER ROLLBACK TX")
73+
print("=" * 50)
74+
print("AFTER ROLLBACK TX")
7475

7576
for result_set in session.execute("""SELECT COUNT(*) as rows_count FROM example;"""):
7677
print(f"rows: {str(result_set.rows)}")

tests/query/test_query_transaction.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,11 @@ def test_context_manager_does_not_hide_exceptions(self, tx: BaseTxContext):
6868
with pytest.raises(Exception):
6969
with tx:
7070
raise Exception()
71+
72+
def test_execute_as_context_manager(self, tx: BaseTxContext):
73+
tx.begin()
74+
75+
with tx.execute("select 1;") as results:
76+
res = [result_set for result_set in results]
77+
78+
assert len(res) == 1

ydb/query/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from .. import convert
1818
from .. import issues
19+
from .. import _utilities
1920

2021

2122
class QueryClientSettings:
@@ -215,3 +216,12 @@ def decorator(rpc_state, response_pb, session_state, *args, **kwargs):
215216
raise
216217

217218
return decorator
219+
220+
221+
class SyncResponseContextIterator(_utilities.SyncResponseIterator):
222+
def __enter__(self):
223+
return self
224+
225+
def __exit__(self, exc_type, exc_val, exc_tb):
226+
for _ in self.it:
227+
pass

ydb/query/transaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from .. import (
77
_apis,
88
issues,
9-
_utilities,
109
)
1110
from .._grpc.grpcwrapper import ydb_query as _ydb_query
1211
from .._grpc.grpcwrapper import ydb_query_public_types as _ydb_query_public
@@ -340,7 +339,7 @@ def execute(
340339
parameters=parameters,
341340
concurrent_result_sets=concurrent_result_sets,
342341
)
343-
self._prev_stream = _utilities.SyncResponseIterator(
342+
self._prev_stream = base.SyncResponseContextIterator(
344343
stream_it,
345344
lambda resp: base.wrap_execute_query_response(
346345
rpc_state=None,

0 commit comments

Comments
 (0)