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

feat: DB API cursors are now iterable#618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
plamut merged 2 commits intogoogleapis:masterfromplamut:iss-607
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletiongoogle/cloud/bigquery/dbapi/_helpers.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -276,7 +276,7 @@ def decorate_public_methods(klass):
"""Apply ``_raise_on_closed()`` decorator to public instance methods.
"""
for name in dir(klass):
if name.startswith("_"):
if name.startswith("_") and name != "__iter__":
continue

member = getattr(klass, name)
Expand Down
4 changes: 4 additions & 0 deletionsgoogle/cloud/bigquery/dbapi/cursor.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -365,6 +365,10 @@ def setinputsizes(self, sizes):
defsetoutputsize(self,size,column=None):
"""No-op, but for consistency raise an error if cursor is closed."""

def__iter__(self):
self._try_fetch()
returniter(self._query_data)


def_format_operation_list(operation,parameters):
"""Formats parameters in operation in the way BigQuery expects.
Expand Down
24 changes: 24 additions & 0 deletionstests/unit/test_dbapi_cursor.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -178,6 +178,7 @@ def test_raises_error_if_closed(self):
"fetchone",
"setinputsizes",
"setoutputsize",
"__iter__",
)

for method in method_names:
Expand DownExpand Up@@ -611,6 +612,29 @@ def test_executemany_w_dml(self):
self.assertIsNone(cursor.description)
self.assertEqual(cursor.rowcount, 12)

def test_is_iterable(self):
from google.cloud.bigquery import dbapi

connection = dbapi.connect(
self._mock_client(rows=[("hello", "there", 7), ("good", "bye", -3)])
)
cursor = connection.cursor()
cursor.execute("SELECT foo, bar, baz FROM hello_world WHERE baz < 42;")

rows_iter = iter(cursor)

row = next(rows_iter)
self.assertEqual(row, ("hello", "there", 7))
row = next(rows_iter)
self.assertEqual(row, ("good", "bye", -3))
self.assertRaises(StopIteration, next, rows_iter)

self.assertEqual(
list(cursor),
[],
"Iterating again over the same results should produce no rows.",
)

def test__format_operation_w_dict(self):
from google.cloud.bigquery.dbapi import cursor

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp