diff --git a/src/calibre/db/__init__.py b/src/calibre/db/__init__.py index eded760cde..781e886567 100644 --- a/src/calibre/db/__init__.py +++ b/src/calibre/db/__init__.py @@ -114,4 +114,7 @@ Various things that require other things before they can be migrated: 3. From refresh in the legacy interface: Rember to flush the composite column template cache. 4. Replace the metadatabackup thread with the new implementation when using the new backend. + 5. In the new API refresh() does not re-read from disk. That might break a + few things, for example content server reloading on db change as well as + dump/restore of db? ''' diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index 6128d6a09a..83392d6d16 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -402,6 +402,13 @@ class LibraryDatabase(object): def delete_conversion_options(self, book_id, fmt, commit=True): self.new_api.delete_conversion_options((book_id,), fmt=fmt) + def set(self, index, field, val, allow_case_change=False): + book_id = self.data.index_to_id(index) + try: + return self.new_api.set_field(field, {book_id:val}, allow_case_change=allow_case_change) + finally: + self.notify('metadata', [book_id]) + # Private interface {{{ def __iter__(self): for row in self.data.iterall(): diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index 76deaea792..037f972010 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -40,6 +40,19 @@ def compare_argspecs(old, new, attr): if not ok: raise AssertionError('The argspec for %s does not match. %r != %r' % (attr, old, new)) +def run_funcs(self, db, ndb, funcs): + for func in funcs: + meth, args = func[0], func[1:] + if callable(meth): + meth(*args) + else: + fmt = lambda x:x + if meth[0] in {'!', '@', '#'}: + fmt = {'!':dict, '@':frozenset, '#':lambda x:set((x or '').split(','))}[meth[0]] + meth = meth[1:] + self.assertEqual(fmt(getattr(db, meth)(*args)), fmt(getattr(ndb, meth)(*args)), + 'The method: %s() returned different results for argument %s' % (meth, args)) + class LegacyTest(BaseTest): ''' Test the emulation of the legacy interface. ''' @@ -137,8 +150,9 @@ class LegacyTest(BaseTest): def test_legacy_direct(self): # {{{ 'Test methods that are directly equivalent in the old and new interface' from calibre.ebooks.metadata.book.base import Metadata - ndb = self.init_legacy() + ndb = self.init_legacy(self.cloned_library) db = self.init_old() + for meth, args in { 'get_next_series_num_for': [('A Series One',)], 'author_sort_from_authors': [(['Author One', 'Author Two', 'Unknown'],)], @@ -221,19 +235,13 @@ class LegacyTest(BaseTest): t = next(tmap.iterkeys()) pmap = cache.get_id_map('publisher') p = next(pmap.iterkeys()) - for x in ( + run_funcs(self, db, ndb, ( ('delete_tag_using_id', t), ('delete_publisher_using_id', p), (db.refresh,), ('all_tag_names',), ('tags', 0), ('tags', 1), ('tags', 2), ('all_publisher_names',), ('publisher', 0), ('publisher', 1), ('publisher', 2), - ): - meth, args = x[0], x[1:] - if callable(meth): - meth(*args) - else: - self.assertEqual((getattr(db, meth)(*args)), (getattr(ndb, meth)(*args)), - 'The method: %s() returned different results for argument %s' % (meth, args)) + )) db.close() # }}} @@ -387,3 +395,24 @@ class LegacyTest(BaseTest): old.close() # }}} + def test_legacy_setters(self): # {{{ + 'Test methods that are directly equivalent in the old and new interface' + ndb = self.init_legacy(self.cloned_library) + db = self.init_old(self.cloned_library) + + run_funcs(self, db, ndb, ( + ('set', 0, 'title', 'newtitle'), + ('set', 0, 'tags', 't1,t2,tag one', True), + ('set', 0, 'authors', 'author one & Author Two', True), + ('set', 0, 'rating', 3.2), + ('set', 0, 'publisher', 'publisher one', True), + (db.refresh,), + ('title', 0), + ('rating', 0), + ('#tags', 0), ('#tags', 1), ('#tags', 2), + ('authors', 0), ('authors', 1), ('authors', 2), + ('publisher', 0), ('publisher', 1), ('publisher', 2), + )) + db.close() + + # }}} diff --git a/src/calibre/db/write.py b/src/calibre/db/write.py index 9bae3e6abb..a257788a60 100644 --- a/src/calibre/db/write.py +++ b/src/calibre/db/write.py @@ -142,7 +142,7 @@ def get_adapter(name, metadata): elif dt == 'comments': ans = single_text elif dt == 'rating': - ans = lambda x: None if x in {None, 0} else min(10., max(0., adapt_number(float, x))) + ans = lambda x: None if x in {None, 0} else min(10, max(0, adapt_number(int, x))) elif dt == 'enumeration': ans = single_text elif dt == 'composite': diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index d23633e433..00c6fb057f 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -2212,7 +2212,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): id = self.data[row][0] col = self.FIELD_MAP[column] - books_to_refresh = set() + books_to_refresh = {id} set_args = (row, col, val) if column == 'authors': val = string_to_authors(val)