From 62d1dfbeef792a805f46e66cbdf7e0448eb5ed7c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 16 Jul 2013 22:19:58 +0530 Subject: [PATCH] Start work on legacy custom column API --- src/calibre/db/backend.py | 7 +++++++ src/calibre/db/legacy.py | 4 +++- src/calibre/db/tests/legacy.py | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 1f561980bd..20ca7eda3f 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -549,6 +549,7 @@ class DB(object): # Load metadata for custom columns self.custom_column_label_map, self.custom_column_num_map = {}, {} + self.custom_column_num_to_label_map = {} triggers = [] remove = [] custom_tables = self.custom_tables @@ -586,6 +587,7 @@ class DB(object): self.custom_column_num_map[data['num']] = \ self.custom_column_label_map[data['label']] = data + self.custom_column_num_to_label_map[data['num']] = data['label'] # Create Foreign Key triggers if data['normalized']: @@ -785,6 +787,11 @@ class DB(object): self._conn = Connection(self.dbpath) return self._conn + def custom_field_name(self, label=None, num=None): + if label is not None: + return self.field_metadata.custom_field_prefix + label + return self.field_metadata.custom_field_prefix + self.custom_column_num_to_label_map[num] + def close(self): if self._conn is not None: self._conn.close() diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index be979ebed3..9073ed05ae 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -70,6 +70,7 @@ class LibraryDatabase(object): setattr(self, x, getattr(self.data, x)) self.is_case_sensitive = getattr(backend, 'is_case_sensitive', False) + self.custom_field_name = backend.custom_field_name self.last_update_check = self.last_modified() @@ -637,7 +638,8 @@ for field in ('authors', 'tags', 'publisher', 'series'): return func name = field[:-1] if field in {'authors', 'tags'} else field setattr(LibraryDatabase, 'all_%s_names' % name, MT(getter(field))) - LibraryDatabase.all_formats = MT(lambda self:self.new_api.all_field_names('formats')) +LibraryDatabase.all_formats = MT(lambda self:self.new_api.all_field_names('formats')) +LibraryDatabase.all_custom = MT(lambda self, label=None, num=None:self.new_api.all_field_names(self.custom_field_name(label, num))) for func, field in {'all_authors':'authors', 'all_titles':'title', 'all_tags2':'tags', 'all_series':'series', 'all_publishers':'publisher'}.iteritems(): def getter(field): diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index 216a0499ec..3e08274561 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -604,7 +604,17 @@ class LegacyTest(BaseTest): )) db.close() - - + # }}} + + def test_legacy_custom(self): # {{{ + 'Test the legacy API for custom columns' + ndb = self.init_legacy(self.cloned_library) + db = self.init_old(self.cloned_library) + run_funcs(self, db, ndb, ( + ('all_custom', 'series'), + ('all_custom', 'tags'), + ('all_custom', 'rating'), + ('all_custom', 'authors'), + )) # }}}