Skip to content

Commit

Permalink
Update base.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gitofanindya authored Oct 25, 2024
1 parent 88f8a6f commit 62a40e6
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions nxdrive/dao/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Query formatting in this file is based on http://www.sqlstyle.guide/
"""
import datetime
import sqlite3
import sys
from contextlib import suppress
from logging import getLogger
Expand All @@ -20,29 +22,25 @@


class AutoRetryCursor(Cursor):
def adapt_datetime_iso(self, val):
return datetime.datetime.fromtimestamp(str(val), datetime.UTC)

Check warning on line 26 in nxdrive/dao/base.py

View check run for this annotation

Codecov / codecov/patch

nxdrive/dao/base.py#L26

Added line #L26 was not covered by tests
def execute(self, sql: str, parameters: Iterable[Any] = ()) -> Cursor:
count = 1
while True:
count += 1
try:
if parameters:
import datetime
import sqlite3
new_param = []
for param in parameters:
if type(param) == datetime.datetime:
def adapt_datetime_iso(val):
return datetime.datetime.fromtimestamp(str(val), datetime.UTC)
mtime = sqlite3.register_adapter(param, adapt_datetime_iso)
new_param.append(mtime)
else:
new_param.append(param)

new_param = tuple(new_param)
res = super().execute(sql, new_param)
else:
res = super().execute(sql, parameters)
return res
if not parameters:
return super().execute(sql, parameters)
new_param = []
for param in parameters:
if type(param) == datetime.datetime:
mtime = sqlite3.register_adapter(param, self.adapt_datetime_iso)
new_param.append(mtime)
else:
new_param.append(param)
new_param = tuple(new_param)
return super().execute(sql, new_param)

except OperationalError as exc:
log.info(
f"Retry locked database #{count}, {sql=}, {parameters=}",
Expand Down

0 comments on commit 62a40e6

Please sign in to comment.