From db7c5010714a1127170787c9d0154344144ca867 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 22 Sep 2024 12:55:28 +0100 Subject: [PATCH 1/3] Fix bug in new get_item_id() db lookup. Names in the authors table have commas changed to vertical bars. --- src/calibre/db/tables.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/calibre/db/tables.py b/src/calibre/db/tables.py index f162011e6b85..77dd3955130e 100644 --- a/src/calibre/db/tables.py +++ b/src/calibre/db/tables.py @@ -68,6 +68,10 @@ def __init__(self, name, metadata, link_table=None): if name == 'authors': # Legacy self.unserialize = lambda x: x.replace('|', ',') if x else '' + # Need serialize for get_item_id because in authors, commas become bars + self.serialize = lambda x: x.replace(',', '|') if x else '' + else: + self.serialize = lambda x: x self.link_table = (link_table if link_table else 'books_%s_link'%self.metadata['table']) if self.supports_notes and dt == 'rating': # custom ratings table @@ -269,7 +273,8 @@ def item_ids_for_names(self, db, item_names: Iterable[str], case_sensitive: bool if case_sensitive: colname = self.metadata['column'] if len(item_names) == 1: - iid = db.get(f'SELECT id FROM {self.metadata["table"]} WHERE {colname} = ?', ((item_names[0],)), all=False) + iid = db.get(f'SELECT id FROM {self.metadata["table"]} WHERE {colname} = ?', + ((self.serialize(item_names[0]),)), all=False) return {item_names[0]: iid} inq = ('?,' * len(item_names))[:-1] ans = dict.fromkeys(item_names) From 2fd0359252348c16e96b7a168f9e00c55657bd31 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 22 Sep 2024 17:30:49 +0100 Subject: [PATCH 2/3] Apply the serialized() change when more than one item is being looked up. --- src/calibre/db/tables.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/calibre/db/tables.py b/src/calibre/db/tables.py index 77dd3955130e..4b9959418e9d 100644 --- a/src/calibre/db/tables.py +++ b/src/calibre/db/tables.py @@ -277,9 +277,10 @@ def item_ids_for_names(self, db, item_names: Iterable[str], case_sensitive: bool ((self.serialize(item_names[0]),)), all=False) return {item_names[0]: iid} inq = ('?,' * len(item_names))[:-1] - ans = dict.fromkeys(item_names) - ans.update(db.get(f'SELECT {colname}, id FROM {self.metadata["table"]} WHERE {colname} IN ({inq})', item_names)) - return ans + serialized_names = {self.serialize(v) for v in item_names} + ans = dict.fromkeys(serialized_names) + ans.update(db.get(f'SELECT {colname}, id FROM {self.metadata["table"]} WHERE {colname} IN ({inq})', serialized_names)) + return {self.unserialize(k):v for k,v in ans.items()} if len(item_names) == 1: q = icu_lower(item_names[0]) for iid, name in self.id_map.items(): From 4d175e5e78dda257473afd862f1a303837c9447b Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 22 Sep 2024 17:44:11 +0100 Subject: [PATCH 3/3] Ooops ... unserialize isn't defined for some fields --- src/calibre/db/tables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calibre/db/tables.py b/src/calibre/db/tables.py index 4b9959418e9d..307c91a6ec1a 100644 --- a/src/calibre/db/tables.py +++ b/src/calibre/db/tables.py @@ -280,7 +280,7 @@ def item_ids_for_names(self, db, item_names: Iterable[str], case_sensitive: bool serialized_names = {self.serialize(v) for v in item_names} ans = dict.fromkeys(serialized_names) ans.update(db.get(f'SELECT {colname}, id FROM {self.metadata["table"]} WHERE {colname} IN ({inq})', serialized_names)) - return {self.unserialize(k):v for k,v in ans.items()} + return {(self.unserialize(k) if self.unserialize else k):v for k,v in ans.items()} if len(item_names) == 1: q = icu_lower(item_names[0]) for iid, name in self.id_map.items():