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(db_api): make rowcount property NotImplemented #603

Merged
merged 3 commits into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 25 additions & 36 deletions google/cloud/spanner_dbapi/cursor.py
Expand Up @@ -44,8 +44,6 @@

from google.rpc.code_pb2 import ABORTED, OK

_UNSET_COUNT = -1

ColumnDetails = namedtuple("column_details", ["null_ok", "spanner_type"])
Statement = namedtuple("Statement", "sql, params, param_types, checksum, is_insert")

Expand All @@ -60,7 +58,6 @@ class Cursor(object):
def __init__(self, connection):
self._itr = None
self._result_set = None
self._row_count = _UNSET_COUNT
self.lastrowid = None
self.connection = connection
self._is_closed = False
Expand Down Expand Up @@ -119,12 +116,15 @@ def description(self):

@property
def rowcount(self):
"""The number of rows produced by the last `.execute()`.
"""The number of rows produced by the last `execute()` call.

:rtype: int
:returns: The number of rows produced by the last .execute*().
:raises: :class:`NotImplemented`.
"""
return self._row_count
raise NotImplementedError(
"The `rowcount` property is non-operational. Request "
"resulting rows are streamed by the `fetch*()` methods "
"and can't be counted before they are all streamed."
)

def _raise_if_closed(self):
"""Raise an exception if this cursor is closed.
Expand Down Expand Up @@ -153,11 +153,7 @@ def _do_execute_update(self, transaction, sql, params):
result = transaction.execute_update(
sql, params=params, param_types=get_param_types(params)
)
self._itr = None
if type(result) == int:
self._row_count = result

return result
self._itr = iter([result])

def _do_batch_update(self, transaction, statements, many_result_set):
status, res = transaction.batch_update(statements)
Expand Down Expand Up @@ -403,30 +399,23 @@ def _handle_DQL(self, sql, params):
res = snapshot.execute_sql(
sql, params=params, param_types=get_param_types(params)
)
if type(res) == int:
self._row_count = res
self._itr = None
else:
# Immediately using:
# iter(response)
# here, because this Spanner API doesn't provide
# easy mechanisms to detect when only a single item
# is returned or many, yet mixing results that
# are for .fetchone() with those that would result in
# many items returns a RuntimeError if .fetchone() is
# invoked and vice versa.
self._result_set = res
# Read the first element so that the StreamedResultSet can
# return the metadata after a DQL statement. See issue #155.
while True:
try:
self._itr = PeekIterator(self._result_set)
break
except Aborted:
self.connection.retry_transaction()
# Unfortunately, Spanner doesn't seem to send back
# information about the number of rows available.
self._row_count = _UNSET_COUNT
# Immediately using:
# iter(response)
# here, because this Spanner API doesn't provide
# easy mechanisms to detect when only a single item
# is returned or many, yet mixing results that
# are for .fetchone() with those that would result in
# many items returns a RuntimeError if .fetchone() is
# invoked and vice versa.
self._result_set = res
# Read the first element so that the StreamedResultSet can
# return the metadata after a DQL statement. See issue #155.
while True:
try:
self._itr = PeekIterator(self._result_set)
break
except Aborted:
self.connection.retry_transaction()

def __enter__(self):
return self
Expand Down
24 changes: 9 additions & 15 deletions tests/unit/spanner_dbapi/test_cursor.py
Expand Up @@ -62,11 +62,10 @@ def test_property_description(self):
self.assertIsInstance(cursor.description[0], ColumnInfo)

def test_property_rowcount(self):
from google.cloud.spanner_dbapi.cursor import _UNSET_COUNT

connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
self.assertEqual(cursor.rowcount, _UNSET_COUNT)
with self.assertRaises(NotImplementedError):
cursor.rowcount

def test_callproc(self):
from google.cloud.spanner_dbapi.exceptions import InterfaceError
Expand Down Expand Up @@ -94,26 +93,25 @@ def test_close(self, mock_client):
cursor.execute("SELECT * FROM database")

def test_do_execute_update(self):
from google.cloud.spanner_dbapi.cursor import _UNSET_COUNT
from google.cloud.spanner_dbapi.checksum import ResultsChecksum

connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
cursor._checksum = ResultsChecksum()
transaction = mock.MagicMock()

def run_helper(ret_value):
transaction.execute_update.return_value = ret_value
res = cursor._do_execute_update(
cursor._do_execute_update(
transaction=transaction, sql="SELECT * WHERE true", params={},
)
return res
return cursor.fetchall()

expected = "good"
self.assertEqual(run_helper(expected), expected)
self.assertEqual(cursor._row_count, _UNSET_COUNT)
self.assertEqual(run_helper(expected), [expected])

expected = 1234
self.assertEqual(run_helper(expected), expected)
self.assertEqual(cursor._row_count, expected)
self.assertEqual(run_helper(expected), [expected])

def test_execute_programming_error(self):
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
Expand Down Expand Up @@ -706,24 +704,20 @@ def test_setoutputsize(self):

def test_handle_dql(self):
from google.cloud.spanner_dbapi import utils
from google.cloud.spanner_dbapi.cursor import _UNSET_COUNT

connection = self._make_connection(self.INSTANCE, mock.MagicMock())
connection.database.snapshot.return_value.__enter__.return_value = (
mock_snapshot
) = mock.MagicMock()
cursor = self._make_one(connection)

mock_snapshot.execute_sql.return_value = int(0)
mock_snapshot.execute_sql.return_value = [0]
cursor._handle_DQL("sql", params=None)
self.assertEqual(cursor._row_count, 0)
self.assertIsNone(cursor._itr)

mock_snapshot.execute_sql.return_value = "0"
cursor._handle_DQL("sql", params=None)
self.assertEqual(cursor._result_set, "0")
self.assertIsInstance(cursor._itr, utils.PeekIterator)
self.assertEqual(cursor._row_count, _UNSET_COUNT)

def test_context(self):
connection = self._make_connection(self.INSTANCE, self.DATABASE)
Expand Down