Skip to content

Commit

Permalink
Fix #2445 (Fix bug in new get_item_id() db lookup. Names in the autho…
Browse files Browse the repository at this point in the history
…rs table …)
  • Loading branch information
kovidgoyal committed Sep 23, 2024
1 parent 8c433aa commit ad9a66c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/calibre/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ def __init__(self, name, metadata, link_table=None):
'datetime': c_parse,
'bool': bool
}.get(dt)
self.serialize = None
if name == 'authors':
# Legacy
self.unserialize = lambda x: x.replace('|', ',') if x else ''
self.serialize = lambda x: x.replace(',', '|')
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
Expand Down Expand Up @@ -268,12 +270,16 @@ def item_ids_for_names(self, db, item_names: Iterable[str], case_sensitive: bool
item_names = tuple(item_names)
if case_sensitive:
colname = self.metadata['column']
serialized_names = tuple(map(self.serialize, item_names)) if self.serialize else item_names
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} = ?', ((serialized_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))
res = db.get(f'SELECT {colname}, id FROM {self.metadata["table"]} WHERE {colname} IN ({inq})', serialized_names)
if self.unserialize:
res = ((self.unserialize(name), iid) for name, iid in res)
ans.update(res)
return ans
if len(item_names) == 1:
q = icu_lower(item_names[0])
Expand Down

0 comments on commit ad9a66c

Please sign in to comment.