diff --git a/google/cloud/bigquery/dbapi/cursor.py b/google/cloud/bigquery/dbapi/cursor.py index 03f3b72ca..0dc8f56ab 100644 --- a/google/cloud/bigquery/dbapi/cursor.py +++ b/google/cloud/bigquery/dbapi/cursor.py @@ -79,6 +79,16 @@ def __init__(self, connection): self._query_job = None self._closed = False + @property + def query_job(self): + """google.cloud.bigquery.job.query.QueryJob: The query job created by + the last ``execute*()`` call. + + .. note:: + If the last ``execute*()`` call was ``executemany()``, this is the + last job created by ``executemany()``.""" + return self._query_job + def close(self): """Mark the cursor as closed, preventing its further use.""" self._closed = True diff --git a/tests/unit/test_dbapi_cursor.py b/tests/unit/test_dbapi_cursor.py index b550bbce0..fc6ea3882 100644 --- a/tests/unit/test_dbapi_cursor.py +++ b/tests/unit/test_dbapi_cursor.py @@ -662,6 +662,29 @@ def test_is_iterable(self): "Iterating again over the same results should produce no rows.", ) + def test_query_job_wo_execute(self): + from google.cloud.bigquery import dbapi + + connection = dbapi.connect(self._mock_client()) + cursor = connection.cursor() + self.assertIsNone(cursor.query_job) + + def test_query_job_w_execute(self): + from google.cloud.bigquery import dbapi, QueryJob + + connection = dbapi.connect(self._mock_client()) + cursor = connection.cursor() + cursor.execute("SELECT 1;") + self.assertIsInstance(cursor.query_job, QueryJob) + + def test_query_job_w_executemany(self): + from google.cloud.bigquery import dbapi, QueryJob + + connection = dbapi.connect(self._mock_client()) + cursor = connection.cursor() + cursor.executemany("SELECT %s;", (("1",), ("2",))) + self.assertIsInstance(cursor.query_job, QueryJob) + def test__format_operation_w_dict(self): from google.cloud.bigquery.dbapi import cursor