Skip to content

Commit

Permalink
feat: expose query job on dbapi cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
r1b committed Mar 18, 2023
1 parent cbbfeb5 commit a2d3979
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions google/cloud/bigquery/dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_dbapi_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a2d3979

Please sign in to comment.