diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 365b7c4222..bb15f97407 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -972,6 +972,9 @@ class DB: def notes_for(self, field_name, item_id): return self.notes.get_note(self.conn, field_name, item_id) or '' + def notes_data_for(self, field_name, item_id): + return self.notes.get_note_data(self.conn, field_name, item_id) + def set_notes_for(self, field, item_id, doc: str, searchable_text: str, resource_hashes, remove_unused_resources) -> int: id_val = self.tables[field].id_map[item_id] note_id = self.notes.set_note(self.conn, field, item_id, id_val, doc, resource_hashes, searchable_text) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 7263924f63..d9733c75ff 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -680,6 +680,11 @@ class Cache: def notes_for(self, field, item_id) -> str: return self.backend.notes_for(field, item_id) + @read_api + def notes_data_for(self, field, item_id) -> str: + ' Return all notes data as a dict or None if note does not exist ' + return self.backend.notes_data_for(field, item_id) + @write_api def set_notes_for(self, field, item_id, doc: str, searchable_text: str = copy_marked_up_text, resource_hashes=(), remove_unused_resources=False) -> int: return self.backend.set_notes_for(field, item_id, doc, searchable_text, resource_hashes, remove_unused_resources) diff --git a/src/calibre/db/tests/notes.py b/src/calibre/db/tests/notes.py index 132d992ade..80038e3518 100644 --- a/src/calibre/db/tests/notes.py +++ b/src/calibre/db/tests/notes.py @@ -64,6 +64,8 @@ def test_cache_api(self: 'NotesTest'): h1 = cache.add_notes_resource(b'resource1', 'r1.jpg') h2 = cache.add_notes_resource(b'resource2', 'r1.jpg') cache.set_notes_for('authors', author_id, doc, resource_hashes=(h1, h2)) + nd = cache.notes_data_for('authors', author_id) + self.ae(nd, {'id': 1, 'searchable_text': authors[0] + '\n' + doc, 'doc': doc, 'resource_hashes': frozenset({h1, h2})}) # test renaming to a new author preserves notes cache.rename_items('authors', {author_id: 'renamed author'}) raid = cache.get_item_id('authors', 'renamed author')