Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose query job on dbapi cursor #1520

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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