Skip to content

Commit f98c34a

Browse files
committed
revert(dbapi): avoid inferring transaction state
1 parent 2442c44 commit f98c34a

3 files changed

Lines changed: 5 additions & 56 deletions

File tree

‎src/sqlitecloud/dbapi2.py‎

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

242241
self.row_factory: Optional[Callable[["Cursor", Tuple], object]] = None
@@ -354,9 +353,6 @@ def commit(self):
354353
"""
355354
Commit any pending transactions on database.
356355
"""
357-
if not self._in_transaction:
358-
return
359-
360356
try:
361357
self._driver.execute("COMMIT;", self.sqlitecloud_connection)
362358
except SQLiteCloudOperationalError as e:
@@ -367,10 +363,6 @@ def commit(self):
367363
):
368364
# compliance to sqlite3
369365
logging.debug(e)
370-
else:
371-
raise
372-
373-
self._in_transaction = False
374366

375367
def rollback(self):
376368
"""
@@ -381,9 +373,6 @@ def rollback(self):
381373
See the documentation on the `ON CONFLICT <https://docs.sqlitecloud.io/docs/sqlite/lang_conflict>`
382374
clause for additional information about the ROLLBACK conflict resolution algorithm.
383375
"""
384-
if not self._in_transaction:
385-
return
386-
387376
try:
388377
self._driver.execute("ROLLBACK;", self.sqlitecloud_connection)
389378
except SQLiteCloudOperationalError as e:
@@ -394,23 +383,6 @@ def rollback(self):
394383
):
395384
# compliance to sqlite3
396385
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
414386

415387
def cursor(self):
416388
"""
@@ -612,8 +584,6 @@ def execute(
612584
sql, parameters, self.connection.sqlitecloud_connection
613585
)
614586

615-
self.connection._update_transaction_state(sql)
616-
617587
self._reset()
618588

619589
if isinstance(result, SQLiteCloudResult):

‎src/tests/integration/test_pandas.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ def test_insert_from_dataframe(self, sqlitecloud_dbapi2_connection):
2929
dfprices.to_sql("PRICES", conn, index=False)
3030
dfmapping.to_sql("TICKER_MAPPING", conn, index=False)
3131

32+
df_actual_tables = pd.read_sql(
33+
"SELECT name FROM sqlite_master WHERE type='table'", conn
34+
)
3235
df_actual_prices = pd.read_sql("SELECT * FROM PRICES", conn)
3336
df_actual_mapping = pd.read_sql("SELECT * FROM TICKER_MAPPING", conn)
3437

38+
assert "PRICES" in df_actual_tables["name"].to_list()
39+
assert "TICKER_MAPPING" in df_actual_tables["name"].to_list()
3540
assert_frame_equal(
3641
df_actual_prices,
3742
dfprices,

‎src/tests/unit/test_dbapi2.py‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,6 @@ 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-
7044
def test_description_empty(self, mocker):
7145
cursor = Cursor(mocker.patch("sqlitecloud.Connection"))
7246

0 commit comments

Comments
 (0)