Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0b373d

Browse files
authored
feat: DB API cursors are now iterable (#618)
* feat: make DB API Cursors iterable* Raise error if obtaining iterator of closed Cursor
1 parentf75dcdf commite0b373d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

‎google/cloud/bigquery/dbapi/_helpers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def decorate_public_methods(klass):
276276
"""Apply ``_raise_on_closed()`` decorator to public instance methods.
277277
"""
278278
fornameindir(klass):
279-
ifname.startswith("_"):
279+
ifname.startswith("_")andname!="__iter__":
280280
continue
281281

282282
member=getattr(klass,name)

‎google/cloud/bigquery/dbapi/cursor.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ def setinputsizes(self, sizes):
365365
defsetoutputsize(self,size,column=None):
366366
"""No-op, but for consistency raise an error if cursor is closed."""
367367

368+
def__iter__(self):
369+
self._try_fetch()
370+
returniter(self._query_data)
371+
368372

369373
def_format_operation_list(operation,parameters):
370374
"""Formats parameters in operation in the way BigQuery expects.

‎tests/unit/test_dbapi_cursor.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def test_raises_error_if_closed(self):
178178
"fetchone",
179179
"setinputsizes",
180180
"setoutputsize",
181+
"__iter__",
181182
)
182183

183184
formethodinmethod_names:
@@ -611,6 +612,29 @@ def test_executemany_w_dml(self):
611612
self.assertIsNone(cursor.description)
612613
self.assertEqual(cursor.rowcount,12)
613614

615+
deftest_is_iterable(self):
616+
fromgoogle.cloud.bigqueryimportdbapi
617+
618+
connection=dbapi.connect(
619+
self._mock_client(rows=[("hello","there",7), ("good","bye",-3)])
620+
)
621+
cursor=connection.cursor()
622+
cursor.execute("SELECT foo, bar, baz FROM hello_world WHERE baz < 42;")
623+
624+
rows_iter=iter(cursor)
625+
626+
row=next(rows_iter)
627+
self.assertEqual(row, ("hello","there",7))
628+
row=next(rows_iter)
629+
self.assertEqual(row, ("good","bye",-3))
630+
self.assertRaises(StopIteration,next,rows_iter)
631+
632+
self.assertEqual(
633+
list(cursor),
634+
[],
635+
"Iterating again over the same results should produce no rows.",
636+
)
637+
614638
deftest__format_operation_w_dict(self):
615639
fromgoogle.cloud.bigquery.dbapiimportcursor
616640

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp