Skip to content

Commit

Permalink
chore: avoid explicit commit if we are in autocommit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 10, 2024
1 parent 8f39d78 commit c37d5c3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions ibis/backends/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,19 @@ def drop_database(self, name: str, force: bool = False) -> None:
def begin(self):
con = self.con
cur = con.cursor()

if not con.autocommit_mode:
con.begin()

try:
yield cur
except Exception:
con.rollback()
if not con.autocommit_mode:
con.rollback()
raise
else:
con.commit()
if not con.autocommit_mode:
con.commit()
finally:
cur.close()

Expand All @@ -279,14 +285,19 @@ def raw_sql(self, query: str | sg.Expression, **kwargs: Any) -> Any:
con = self.con
cursor = con.cursor()

if not con.autocommit_mode:
con.begin()

try:
cursor.execute(query, **kwargs)
except Exception:
con.rollback()
if not con.autocommit_mode:
con.rollback()
cursor.close()
raise
else:
con.commit()
if not con.autocommit_mode:
con.commit()
return cursor

# TODO: disable positional arguments
Expand Down

0 comments on commit c37d5c3

Please sign in to comment.