From c006e2e14bebef07898a934bdb6225ea14b6280f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 15 Sep 2010 11:27:39 -0600 Subject: [PATCH] Database: Update has_cover cache when setting/removing covers so that the search returns correct results. Also fix an exception that could occur when adding books with a db that has been upgraded from very old SQL. --- src/calibre/library/database2.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 2df6b3bdc4..f5f0f724ba 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -598,7 +598,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): def has_cover(self, index, index_is_id=False): id = index if index_is_id else self.id(index) - path = os.path.join(self.library_path, self.path(id, index_is_id=True), 'cover.jpg') + try: + path = os.path.join(self.abspath(id, index_is_id=True), 'cover.jpg') + except: + # Can happen if path has not yet been set + return False return os.access(path, os.R_OK) def remove_cover(self, id, notify=True): @@ -609,6 +613,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): except (IOError, OSError): time.sleep(0.2) os.remove(path) + self.data.set(id, self.FIELD_MAP['cover'], False, row_is_id=True) if notify: self.notify('cover', [id]) @@ -629,6 +634,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): except (IOError, OSError): time.sleep(0.2) save_cover_data_to(data, path) + self.data.set(id, self.FIELD_MAP['cover'], True, row_is_id=True) if notify: self.notify('cover', [id]) @@ -1087,8 +1093,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.set_path(id, True) self.notify('metadata', [id]) - # Given a book, return the list of author sort strings for the book's authors def authors_sort_strings(self, id, index_is_id=False): + ''' + Given a book, return the list of author sort strings + for the book's authors + ''' id = id if index_is_id else self.id(id) aut_strings = self.conn.get(''' SELECT sort @@ -1744,10 +1753,10 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): series_index = 1.0 if mi.series_index is None else mi.series_index aus = mi.author_sort if mi.author_sort else self.author_sort_from_authors(mi.authors) title = mi.title - if isinstance(aus, str): + if isbytestring(aus): aus = aus.decode(preferred_encoding, 'replace') - if isinstance(title, str): - title = title.decode(preferred_encoding) + if isbytestring(title): + title = title.decode(preferred_encoding, 'replace') obj = self.conn.execute('INSERT INTO books(title, series_index, author_sort) VALUES (?, ?, ?)', (title, series_index, aus)) id = obj.lastrowid