Skip to content

Commit 339eb0e

Browse files
r1btswast
andauthored
feat: expose query job on dbapi cursor (#1520)
Co-authored-by: Tim Swast <[email protected]>
1 parent a69348a commit 339eb0e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

google/cloud/bigquery/dbapi/cursor.py

+10
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ def __init__(self, connection):
7979
self._query_job = None
8080
self._closed = False
8181

82+
@property
83+
def query_job(self):
84+
"""google.cloud.bigquery.job.query.QueryJob: The query job created by
85+
the last ``execute*()`` call.
86+
87+
.. note::
88+
If the last ``execute*()`` call was ``executemany()``, this is the
89+
last job created by ``executemany()``."""
90+
return self._query_job
91+
8292
def close(self):
8393
"""Mark the cursor as closed, preventing its further use."""
8494
self._closed = True

tests/unit/test_dbapi_cursor.py

+23
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,29 @@ def test_is_iterable(self):
662662
"Iterating again over the same results should produce no rows.",
663663
)
664664

665+
def test_query_job_wo_execute(self):
666+
from google.cloud.bigquery import dbapi
667+
668+
connection = dbapi.connect(self._mock_client())
669+
cursor = connection.cursor()
670+
self.assertIsNone(cursor.query_job)
671+
672+
def test_query_job_w_execute(self):
673+
from google.cloud.bigquery import dbapi, QueryJob
674+
675+
connection = dbapi.connect(self._mock_client())
676+
cursor = connection.cursor()
677+
cursor.execute("SELECT 1;")
678+
self.assertIsInstance(cursor.query_job, QueryJob)
679+
680+
def test_query_job_w_executemany(self):
681+
from google.cloud.bigquery import dbapi, QueryJob
682+
683+
connection = dbapi.connect(self._mock_client())
684+
cursor = connection.cursor()
685+
cursor.executemany("SELECT %s;", (("1",), ("2",)))
686+
self.assertIsInstance(cursor.query_job, QueryJob)
687+
665688
def test__format_operation_w_dict(self):
666689
from google.cloud.bigquery.dbapi import cursor
667690

0 commit comments

Comments
 (0)