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.

This commit is contained in:
Kovid Goyal 2010-09-15 11:27:39 -06:00
parent 57ca76e68e
commit c006e2e14b

View File

@ -598,7 +598,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
def has_cover(self, index, index_is_id=False): def has_cover(self, index, index_is_id=False):
id = index if index_is_id else self.id(index) 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) return os.access(path, os.R_OK)
def remove_cover(self, id, notify=True): def remove_cover(self, id, notify=True):
@ -609,6 +613,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
except (IOError, OSError): except (IOError, OSError):
time.sleep(0.2) time.sleep(0.2)
os.remove(path) os.remove(path)
self.data.set(id, self.FIELD_MAP['cover'], False, row_is_id=True)
if notify: if notify:
self.notify('cover', [id]) self.notify('cover', [id])
@ -629,6 +634,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
except (IOError, OSError): except (IOError, OSError):
time.sleep(0.2) time.sleep(0.2)
save_cover_data_to(data, path) save_cover_data_to(data, path)
self.data.set(id, self.FIELD_MAP['cover'], True, row_is_id=True)
if notify: if notify:
self.notify('cover', [id]) self.notify('cover', [id])
@ -1087,8 +1093,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
self.set_path(id, True) self.set_path(id, True)
self.notify('metadata', [id]) 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): 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) id = id if index_is_id else self.id(id)
aut_strings = self.conn.get(''' aut_strings = self.conn.get('''
SELECT sort 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 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) aus = mi.author_sort if mi.author_sort else self.author_sort_from_authors(mi.authors)
title = mi.title title = mi.title
if isinstance(aus, str): if isbytestring(aus):
aus = aus.decode(preferred_encoding, 'replace') aus = aus.decode(preferred_encoding, 'replace')
if isinstance(title, str): if isbytestring(title):
title = title.decode(preferred_encoding) title = title.decode(preferred_encoding, 'replace')
obj = self.conn.execute('INSERT INTO books(title, series_index, author_sort) VALUES (?, ?, ?)', obj = self.conn.execute('INSERT INTO books(title, series_index, author_sort) VALUES (?, ?, ?)',
(title, series_index, aus)) (title, series_index, aus))
id = obj.lastrowid id = obj.lastrowid