From 627dd5f3156350c5dc7eb10f28260979e6d709a5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 12 Nov 2007 21:09:34 +0000 Subject: [PATCH] Add database API for fetching all the books in a series. --- src/libprs500/library/database.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/libprs500/library/database.py b/src/libprs500/library/database.py index 131b5fad99..1bb4dc2b5e 100644 --- a/src/libprs500/library/database.py +++ b/src/libprs500/library/database.py @@ -799,9 +799,32 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE; if ans: return ans[0] - def series_index(self, index): - id = self.id(index) - return self.conn.execute('SELECT series_index FROM books WHERE id=?', (id,)).fetchone()[0] + def series_index(self, index, index_is_id=False): + if not index_is_id: + index = self.id(index) + return self.conn.execute('SELECT series_index FROM books WHERE id=?', (index,)).fetchone()[0] + + def books_in_series(self, series_id): + ''' + Return an ordered list of all books in the series. + The list contains book ids. + ''' + ans = self.conn.execute('SELECT book from books_series_link WHERE series=?', + (series_id,)).fetchall() + if not ans: + return [] + ans = [id[0] for id in ans] + ans.sort(cmp = lambda x, y: cmp(self.series_index(x, True), self.series_index(y, True))) + return ans + + def books_in_series_of(self, index): + ''' + Return an ordered list of all books in the series that the book indetified by index belongs to. + If the book does not belong to a series return an empty list. The list contains book ids. + ''' + series_id = self.series_id(index) + return self.books_in_series(series_id) + def comments(self, index): '''Comments as string or None'''