Skip to content

Commit ac8c4b2

Browse files
author
IlyaFaer
committed
refactor
1 parent 9bb0ebc commit ac8c4b2

File tree

2 files changed

+26
-68
lines changed

2 files changed

+26
-68
lines changed

google/cloud/spanner_dbapi/cursor.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,9 @@ def fetchone(self):
334334
except StopIteration:
335335
return
336336
except Aborted:
337-
self.connection.retry_transaction()
338-
return self.fetchone()
337+
if not self.connection.read_only:
338+
self.connection.retry_transaction()
339+
return self.fetchone()
339340

340341
def fetchall(self):
341342
"""Fetch all (remaining) rows of a query result, returning them as
@@ -350,8 +351,9 @@ def fetchall(self):
350351
self._checksum.consume_result(row)
351352
res.append(row)
352353
except Aborted:
353-
self.connection.retry_transaction()
354-
return self.fetchall()
354+
if not self.connection.read_only:
355+
self.connection.retry_transaction()
356+
return self.fetchall()
355357

356358
return res
357359

@@ -381,8 +383,9 @@ def fetchmany(self, size=None):
381383
except StopIteration:
382384
break
383385
except Aborted:
384-
self.connection.retry_transaction()
385-
return self.fetchmany(size)
386+
if not self.connection.read_only:
387+
self.connection.retry_transaction()
388+
return self.fetchmany(size)
386389

387390
return items
388391

@@ -405,30 +408,21 @@ def _handle_DQL_with_snapshot(self, snapshot, sql, params):
405408
res = snapshot.execute_sql(
406409
sql, params=params, param_types=get_param_types(params)
407410
)
408-
if type(res) == int:
409-
self._row_count = res
410-
self._itr = None
411-
else:
412-
# Immediately using:
413-
# iter(response)
414-
# here, because this Spanner API doesn't provide
415-
# easy mechanisms to detect when only a single item
416-
# is returned or many, yet mixing results that
417-
# are for .fetchone() with those that would result in
418-
# many items returns a RuntimeError if .fetchone() is
419-
# invoked and vice versa.
420-
self._result_set = res
421-
# Read the first element so that the StreamedResultSet can
422-
# return the metadata after a DQL statement. See issue #155.
423-
while True:
424-
try:
425-
self._itr = PeekIterator(self._result_set)
426-
break
427-
except Aborted:
428-
self.connection.retry_transaction()
429-
# Unfortunately, Spanner doesn't seem to send back
430-
# information about the number of rows available.
431-
self._row_count = _UNSET_COUNT
411+
# Immediately using:
412+
# iter(response)
413+
# here, because this Spanner API doesn't provide
414+
# easy mechanisms to detect when only a single item
415+
# is returned or many, yet mixing results that
416+
# are for .fetchone() with those that would result in
417+
# many items returns a RuntimeError if .fetchone() is
418+
# invoked and vice versa.
419+
self._result_set = res
420+
# Read the first element so that the StreamedResultSet can
421+
# return the metadata after a DQL statement. See issue #155.
422+
self._itr = PeekIterator(self._result_set)
423+
# Unfortunately, Spanner doesn't seem to send back
424+
# information about the number of rows available.
425+
self._row_count = _UNSET_COUNT
432426

433427
def _handle_DQL(self, sql, params):
434428
if self.connection.read_only and not self.connection.autocommit:

tests/unit/spanner_dbapi/test_cursor.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,9 @@ def test_handle_dql(self):
707707
) = mock.MagicMock()
708708
cursor = self._make_one(connection)
709709

710-
mock_snapshot.execute_sql.return_value = int(0)
710+
mock_snapshot.execute_sql.return_value = ["0"]
711711
cursor._handle_DQL("sql", params=None)
712-
self.assertEqual(cursor._row_count, 0)
713-
self.assertIsNone(cursor._itr)
714-
715-
mock_snapshot.execute_sql.return_value = "0"
716-
cursor._handle_DQL("sql", params=None)
717-
self.assertEqual(cursor._result_set, "0")
712+
self.assertEqual(cursor._result_set, ["0"])
718713
self.assertIsInstance(cursor._itr, utils.PeekIterator)
719714
self.assertEqual(cursor._row_count, _UNSET_COUNT)
720715

@@ -831,37 +826,6 @@ def test_peek_iterator_aborted(self, mock_client):
831826

832827
retry_mock.assert_called_with()
833828

834-
@mock.patch("google.cloud.spanner_v1.Client")
835-
def test_peek_iterator_aborted_autocommit(self, mock_client):
836-
"""
837-
Checking that an Aborted exception is retried in case it happened while
838-
streaming the first element with a PeekIterator in autocommit mode.
839-
"""
840-
from google.api_core.exceptions import Aborted
841-
from google.cloud.spanner_dbapi.connection import connect
842-
843-
connection = connect("test-instance", "test-database")
844-
845-
connection.autocommit = True
846-
cursor = connection.cursor()
847-
with mock.patch(
848-
"google.cloud.spanner_dbapi.utils.PeekIterator.__init__",
849-
side_effect=(Aborted("Aborted"), None),
850-
):
851-
with mock.patch(
852-
"google.cloud.spanner_dbapi.connection.Connection.retry_transaction"
853-
) as retry_mock:
854-
with mock.patch(
855-
"google.cloud.spanner_dbapi.connection.Connection.run_statement",
856-
return_value=((1, 2, 3), None),
857-
):
858-
with mock.patch(
859-
"google.cloud.spanner_v1.database.Database.snapshot"
860-
):
861-
cursor.execute("SELECT * FROM table_name")
862-
863-
retry_mock.assert_called_with()
864-
865829
@mock.patch("google.cloud.spanner_v1.Client")
866830
def test_fetchone_retry_aborted(self, mock_client):
867831
"""Check that aborted fetch re-executing transaction."""

0 commit comments

Comments
 (0)