Skip to content

Commit 78be470

Browse files
committed
fix(dbapi): skip redundant autocommit commands
1 parent cd2e08a commit 78be470

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎src/sqlitecloud/dbapi2.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def __init__(
236236
) -> None:
237237
self._driver = Driver()
238238
self._autocommit = True
239+
self._in_transaction = False
239240
self.sqlitecloud_connection = sqlitecloud_connection
240241

241242
self.row_factory: Optional[Callable[["Cursor", Tuple], object]] = None
@@ -353,6 +354,9 @@ def commit(self):
353354
"""
354355
Commit any pending transactions on database.
355356
"""
357+
if not self._in_transaction:
358+
return
359+
356360
try:
357361
self._driver.execute("COMMIT;", self.sqlitecloud_connection)
358362
except SQLiteCloudOperationalError as e:
@@ -363,6 +367,10 @@ def commit(self):
363367
):
364368
# compliance to sqlite3
365369
logging.debug(e)
370+
else:
371+
raise
372+
373+
self._in_transaction = False
366374

367375
def rollback(self):
368376
"""
@@ -373,6 +381,9 @@ def rollback(self):
373381
See the documentation on the `ON CONFLICT <https://docs.sqlitecloud.io/docs/sqlite/lang_conflict>`
374382
clause for additional information about the ROLLBACK conflict resolution algorithm.
375383
"""
384+
if not self._in_transaction:
385+
return
386+
376387
try:
377388
self._driver.execute("ROLLBACK;", self.sqlitecloud_connection)
378389
except SQLiteCloudOperationalError as e:
@@ -383,6 +394,23 @@ def rollback(self):
383394
):
384395
# compliance to sqlite3
385396
logging.debug(e)
397+
else:
398+
raise
399+
400+
self._in_transaction = False
401+
402+
def _update_transaction_state(self, sql: str) -> None:
403+
"""Track explicit transaction control statements issued through a cursor."""
404+
parts = sql.lstrip().split(None, 1)
405+
if not parts:
406+
return
407+
408+
command = parts[0].rstrip(";").upper()
409+
410+
if command == "BEGIN":
411+
self._in_transaction = True
412+
elif command in {"COMMIT", "END", "ROLLBACK"}:
413+
self._in_transaction = False
386414

387415
def cursor(self):
388416
"""
@@ -584,6 +612,8 @@ def execute(
584612
sql, parameters, self.connection.sqlitecloud_connection
585613
)
586614

615+
self.connection._update_transaction_state(sql)
616+
587617
self._reset()
588618

589619
if isinstance(result, SQLiteCloudResult):

‎src/tests/unit/test_dbapi2.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ def test_connect_with_connection_string_and_parameters(mocker: MockerFixture):
4141

4242

4343
class TestCursor:
44+
def test_commit_and_rollback_are_noops_in_autocommit_mode(self, mocker):
45+
connection = Connection(mocker.Mock())
46+
execute = mocker.patch.object(connection._driver, "execute")
47+
48+
connection.commit()
49+
connection.rollback()
50+
51+
execute.assert_not_called()
52+
53+
@pytest.mark.parametrize(
54+
"method, command", [("commit", "COMMIT;"), ("rollback", "ROLLBACK;")]
55+
)
56+
def test_explicit_transaction_uses_transaction_control_command(
57+
self, mocker, method, command
58+
):
59+
connection = Connection(mocker.Mock())
60+
cursor = connection.cursor()
61+
mocker.patch.object(cursor._driver, "execute_statement")
62+
execute = mocker.patch.object(connection._driver, "execute")
63+
64+
cursor.execute("BEGIN")
65+
getattr(connection, method)()
66+
67+
execute.assert_called_once_with(command, connection.sqlitecloud_connection)
68+
assert not connection._in_transaction
69+
4470
def test_description_empty(self, mocker):
4571
cursor = Cursor(mocker.patch("sqlitecloud.Connection"))
4672

0 commit comments

Comments
 (0)