API for kiwidude

This commit is contained in:
Kovid Goyal 2012-06-30 21:18:31 +05:30
parent 39e8b5305c
commit d70cb9f6e2

View File

@ -1203,7 +1203,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if m: if m:
return m['mtime'] return m['mtime']
def format_metadata(self, id_, fmt, allow_cache=True): def format_metadata(self, id_, fmt, allow_cache=True, update_db=False,
commit=False):
if not fmt: if not fmt:
return {} return {}
fmt = fmt.upper() fmt = fmt.upper()
@ -1218,6 +1219,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
ans['size'] = stat.st_size ans['size'] = stat.st_size
ans['mtime'] = utcfromtimestamp(stat.st_mtime) ans['mtime'] = utcfromtimestamp(stat.st_mtime)
self.format_metadata_cache[id_][fmt] = ans self.format_metadata_cache[id_][fmt] = ans
if update_db:
self.conn.execute(
'UPDATE data SET uncompressed_size=? WHERE format=? AND'
' book=?', (stat.st_size, fmt, id_))
if commit:
self.conn.commit()
return ans return ans
def format_hash(self, id_, fmt): def format_hash(self, id_, fmt):
@ -2564,7 +2571,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return [] return []
return result return result
def rename_series(self, old_id, new_name): def rename_series(self, old_id, new_name, change_index=True):
new_name = new_name.strip() new_name = new_name.strip()
new_id = self.conn.get( new_id = self.conn.get(
'''SELECT id from series '''SELECT id from series
@ -2577,22 +2584,24 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# New series exists. Must update the link, then assign a # New series exists. Must update the link, then assign a
# new series index to each of the books. # new series index to each of the books.
# Get the list of books where we must update the series index if change_index:
books = self.conn.get('''SELECT books.id # Get the list of books where we must update the series index
FROM books, books_series_link as lt books = self.conn.get('''SELECT books.id
WHERE books.id = lt.book AND lt.series=? FROM books, books_series_link as lt
ORDER BY books.series_index''', (old_id,)) WHERE books.id = lt.book AND lt.series=?
ORDER BY books.series_index''', (old_id,))
# Now update the link table # Now update the link table
self.conn.execute('''UPDATE books_series_link self.conn.execute('''UPDATE books_series_link
SET series=? SET series=?
WHERE series=?''',(new_id, old_id,)) WHERE series=?''',(new_id, old_id,))
# Now set the indices if change_index:
for (book_id,) in books: # Now set the indices
# Get the next series index for (book_id,) in books:
index = self.get_next_series_num_for(new_name) # Get the next series index
self.conn.execute('''UPDATE books index = self.get_next_series_num_for(new_name)
SET series_index=? self.conn.execute('''UPDATE books
WHERE id=?''',(index, book_id,)) SET series_index=?
WHERE id=?''',(index, book_id,))
self.dirty_books_referencing('series', new_id, commit=False) self.dirty_books_referencing('series', new_id, commit=False)
self.conn.commit() self.conn.commit()
@ -3684,4 +3693,12 @@ books_series_link feeds
s = self.conn.get('''SELECT book FROM books_plugin_data WHERE name=?''', (name,)) s = self.conn.get('''SELECT book FROM books_plugin_data WHERE name=?''', (name,))
return [x[0] for x in s] return [x[0] for x in s]
def get_usage_count_by_id(self, field):
fm = self.field_metadata[field]
if not fm.get('link_column', None):
raise ValueError('%s is not an is_multiple field')
return self.conn.get(
'SELECT {0}, count(*) FROM books_{1}_link GROUP BY {0}'.format(
fm['link_column'], fm['table']))