diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 0e49af8031..40cbb5529e 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -219,6 +219,8 @@ class Cache(object): field.series_field = self.fields['series'] elif name == 'authors': field.author_sort_field = self.fields['author_sort'] + elif name == 'title': + field.title_sort_field = self.fields['sort'] @read_api def field_for(self, name, book_id, default_value=None): @@ -620,7 +622,6 @@ class Cache(object): @write_api def set_field(self, name, book_id_to_val_map, allow_case_change=True): # TODO: Specialize title/authors to also update path - # TODO: Ensure the sort fields are updated for title/author/series? f = self.fields[name] is_series = f.metadata['datatype'] == 'series' diff --git a/src/calibre/db/tests/writing.py b/src/calibre/db/tests/writing.py index ca8129f64c..0818b580fa 100644 --- a/src/calibre/db/tests/writing.py +++ b/src/calibre/db/tests/writing.py @@ -247,6 +247,10 @@ class WritingTest(BaseTest): ae(c.field_for('author_sort', 1), 'Unknown') ae(c.field_for('author_sort', 2), 'An, Author') ae(c.field_for('author_sort', 3), 'Goyal, Kovid & Layog, Divok') + if name == 'authors': + ae(c.field_for('author_sort', 3), 'Goyal, Kovid & Layog, Divok') + ae(c.field_for('author_sort', 2), 'An, Author') + ae(c.field_for('author_sort', 1), 'Unknown') del cache2 ae(cache.set_field('authors', {1:'KoviD GoyaL'}), {1, 3}) ae(cache.field_for('author_sort', 1), 'GoyaL, KoviD') @@ -276,6 +280,15 @@ class WritingTest(BaseTest): ae(c.field_for('identifiers', 3), {'one':'1', 'two':'2'}) ae(c.field_for('identifiers', 2), {}) ae(c.field_for('identifiers', 1), {'test':'1', 'two':'2'}) + del cache2 + + # Test setting of title sort + ae(sf('title', {1:'The Moose', 2:'Cat'}), {1, 2}) + cache2 = self.init_cache(cl) + for c in (cache, cache2): + ae(c.field_for('sort', 1), 'Moose, The') + ae(c.field_for('sort', 2), 'Cat') + # }}} diff --git a/src/calibre/db/write.py b/src/calibre/db/write.py index 8742c3e609..29a27e16bf 100644 --- a/src/calibre/db/write.py +++ b/src/calibre/db/write.py @@ -12,7 +12,7 @@ from functools import partial from datetime import datetime from calibre.constants import preferred_encoding, ispy3 -from calibre.ebooks.metadata import author_to_author_sort +from calibre.ebooks.metadata import author_to_author_sort, title_sort from calibre.utils.date import (parse_only_date, parse_date, UNDEFINED_DATE, isoformat) from calibre.utils.localization import canonicalize_lang @@ -174,6 +174,10 @@ def one_one_in_books(book_id_val_map, db, field, *args): db.conn.executemany( 'UPDATE books SET %s=? WHERE id=?'%field.metadata['column'], sequence) field.table.book_col_map.update(book_id_val_map) + if field.name == 'title': + # Set the title sort field + field.title_sort_field.writer.set_books( + {k:title_sort(v) for k, v in book_id_val_map.iteritems()}, db) return set(book_id_val_map) def one_one_in_other(book_id_val_map, db, field, *args):