From c756509cb32afd173580204f37c182ee5257012c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 2 Sep 2011 20:29:35 -0600 Subject: [PATCH] ... --- src/calibre/db/backend.py | 6 ++++-- src/calibre/db/cache.py | 6 ++++-- src/calibre/db/tables.py | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index afafa3a18a..c29e0d5c12 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -25,7 +25,8 @@ from calibre.utils.config import to_json, from_json, prefs, tweaks from calibre.utils.date import utcfromtimestamp, parse_date from calibre.utils.filenames import is_case_sensitive from calibre.db.tables import (OneToOneTable, ManyToOneTable, ManyToManyTable, - SizeTable, FormatsTable, AuthorsTable, IdentifiersTable, CompositeTable) + SizeTable, FormatsTable, AuthorsTable, IdentifiersTable, + CompositeTable, LanguagesTable) # }}} ''' @@ -604,11 +605,12 @@ class DB(object): for col in ('series', 'publisher', 'rating'): tables[col] = ManyToOneTable(col, self.field_metadata[col].copy()) - for col in ('authors', 'tags', 'formats', 'identifiers'): + for col in ('authors', 'tags', 'formats', 'identifiers', 'languages'): cls = { 'authors':AuthorsTable, 'formats':FormatsTable, 'identifiers':IdentifiersTable, + 'languages':LanguagesTable, }.get(col, ManyToManyTable) tables[col] = cls(col, self.field_metadata[col].copy()) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index ea3766ad87..7158fc0267 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -13,7 +13,8 @@ from functools import wraps, partial from calibre.db.locking import create_locks, RecordLock from calibre.db.fields import create_field -from calibre.ebooks.book.base import Metadata +from calibre.db.tables import VirtualTable +from calibre.ebooks.metadata.book.base import Metadata from calibre.utils.date import now def api(f): @@ -189,7 +190,8 @@ class Cache(object): if table.metadata['datatype'] == 'composite': self.composites.add(field) - self.fields['ondevice'] = create_field('ondevice', None) + self.fields['ondevice'] = create_field('ondevice', + VirtualTable('ondevice')) @read_api def field_for(self, name, book_id, default_value=None): diff --git a/src/calibre/db/tables.py b/src/calibre/db/tables.py index fa7b001851..185d15d86b 100644 --- a/src/calibre/db/tables.py +++ b/src/calibre/db/tables.py @@ -13,6 +13,7 @@ from dateutil.tz import tzoffset from calibre.constants import plugins from calibre.utils.date import parse_date, local_tz, UNDEFINED_DATE +from calibre.utils.localization import lang_map from calibre.ebooks.metadata import author_to_author_sort _c_speedup = plugins['speedup'][0] @@ -54,6 +55,19 @@ class Table(object): self.link_table = (link_table if link_table else 'books_%s_link'%self.metadata['table']) +class VirtualTable(Table): + + ''' + A dummy table used for fields that only exist in memory like ondevice + ''' + + def __init__(self, name, table_type=ONE_ONE, datatype='text'): + metadata = {'datatype':datatype, 'table':name} + self.table_type = table_type + Table.__init__(self, name, metadata) + + + class OneToOneTable(Table): ''' @@ -210,3 +224,9 @@ class IdentifiersTable(ManyToManyTable): for key in tuple(self.col_book_map.iterkeys()): self.col_book_map[key] = tuple(self.col_book_map[key]) +class LanguagesTable(ManyToManyTable): + + def read_id_maps(self, db): + ManyToManyTable.read_id_maps(self, db) + lm = lang_map() + self.lang_name_map = {x:lm.get(x, x) for x in self.id_map.itervalues()}