diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 5fea619811..fb20341a97 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -363,6 +363,18 @@ def save_annotations_list_to_cursor(cursor, alist, sync_annots_user, book_id, bo alist = tuple(annotations_as_copied_list(other_amap)) save_annotations_for_book(cursor, book_id, book_fmt, alist, user_type='web', user=sync_annots_user) + +def save_last_read_position_to_cursor(cursor, book_id, fmt, user='_', device='_', cfi=None, epoch=None, pos_frac=0): + if cfi: + cursor.execute( + 'INSERT OR REPLACE INTO last_read_positions' + '(book,format,user,device,cfi,epoch,pos_frac) VALUES (?,?,?,?,?,?,?)', + (book_id, fmt.upper(), user, device, cfi, epoch or time.time(), pos_frac)) + else: + cursor.execute( + 'DELETE FROM last_read_positions WHERE book=? AND format=? AND user=? AND device=?', + (book_id, fmt.upper(), user, device)) + # }}} @@ -2646,6 +2658,9 @@ class DB: INSERT INTO {1}({1}) VALUES('rebuild'); '''.format('annotations_fts', 'annotations_fts_stemmed')) + def set_last_read_position(self, book_id, fmt, user='_', device='_', cfi=None, epoch=None, pos_frac=0): + save_last_read_position_to_cursor(self.conn.cursor(), book_id, fmt, user, device, cfi, epoch, pos_frac) + def conversion_options(self, book_id, fmt): for (data,) in self.conn.get('SELECT data FROM conversion_options WHERE book=? AND format=?', (book_id, fmt.upper())): if data: diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 292c66dc9e..31659a9131 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -3311,17 +3311,7 @@ class Cache: @write_api def set_last_read_position(self, book_id, fmt, user='_', device='_', cfi=None, epoch=None, pos_frac=0): - fmt = fmt.upper() - device = device or '_' - user = user or '_' - if not cfi: - self.backend.execute( - 'DELETE FROM last_read_positions WHERE book=? AND format=? AND user=? AND device=?', - (book_id, fmt, user, device)) - else: - self.backend.execute( - 'INSERT OR REPLACE INTO last_read_positions(book,format,user,device,cfi,epoch,pos_frac) VALUES (?,?,?,?,?,?,?)', - (book_id, fmt, user, device, cfi, epoch or time(), pos_frac)) + self.backend.set_last_read_position(book_id, fmt, user, device, cfi, epoch, pos_frac) @write_api # doesn't need write access but sqlite does require only a single thread to access the db during backup def export_library(self, library_key, exporter, progress=None, abort=None): diff --git a/src/calibre/gui2/viewer/integration.py b/src/calibre/gui2/viewer/integration.py index ccc549299f..cd9c2cb7d5 100644 --- a/src/calibre/gui2/viewer/integration.py +++ b/src/calibre/gui2/viewer/integration.py @@ -139,8 +139,7 @@ def save_last_read_position_in_gui(library_broker, msg) -> bool: data = json.loads(msg) db = library_broker.get(data['library_id']) if db: - db = db.new_api - db.set_last_read_position( + db.new_api.set_last_read_position( int(data['book_id']), data['book_fmt'].upper(), user='local', device='calibre-desktop-viewer', cfi=data['cfi'], pos_frac=data['pos_frac']) @@ -158,7 +157,7 @@ def save_last_read_position_to_library(book_library_details, cfi, pos_frac, cali import apsw - from calibre.db.backend import Connection + from calibre.db.backend import Connection, save_last_read_position_to_cursor dbpath = book_library_details['dbpath'] try: conn = apsw.Connection(dbpath, flags=apsw.SQLITE_OPEN_READWRITE) @@ -168,20 +167,10 @@ def save_last_read_position_to_library(book_library_details, cfi, pos_frac, cali conn.setbusytimeout(Connection.BUSY_TIMEOUT) if not database_has_last_read_positions_support(conn.cursor()): return - from time import time with conn: - if cfi: - conn.cursor().execute( - 'INSERT OR REPLACE INTO last_read_positions' - '(book,format,user,device,cfi,epoch,pos_frac) VALUES (?,?,?,?,?,?,?)', - (book_library_details['book_id'], book_library_details['fmt'].upper(), - 'local', 'calibre-desktop-viewer', cfi, time(), pos_frac)) - else: - conn.cursor().execute( - 'DELETE FROM last_read_positions' - ' WHERE book=? AND format=? AND user=? AND device=?', - (book_library_details['book_id'], book_library_details['fmt'].upper(), - 'local', 'calibre-desktop-viewer')) + save_last_read_position_to_cursor( + conn.cursor(), book_library_details['book_id'], book_library_details['fmt'], + 'local', 'calibre-desktop-viewer', cfi, pos_frac=pos_frac) finally: conn.close() @@ -195,7 +184,7 @@ class LastReadPositionSaver(Thread): DEBOUNCE_SECONDS = 3.0 def __init__(self): - Thread.__init__(self, name='LastReadPositionSaver', daemon=True) + super().__init__(name='LastReadPositionSaver', daemon=True) self._lock = Lock() self._pending = None self._event = Event()