diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index 8caca1f261..88d971ce4d 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -28,10 +28,14 @@ def authors_to_string(authors): else: return '' +_bracket_pat = re.compile(r'[\[({].*?[})\]]') def author_to_author_sort(author): + if not author: + return '' method = tweaks['author_sort_copy_method'] if method == 'copy' or (method == 'comma' and ',' in author): return author + author = _bracket_pat.sub('', author).strip() tokens = author.split() tokens = tokens[-1:] + tokens[:-1] if len(tokens) > 1: diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index c7830187df..fe4aac12b5 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -136,6 +136,23 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.initialize_dynamic() def initialize_dynamic(self): + self.conn.executescript(''' + DROP TRIGGER IF EXISTS author_insert_trg; + CREATE TEMP TRIGGER author_insert_trg + AFTER INSERT ON authors + BEGIN + UPDATE authors SET sort=author_to_author_sort(NEW.name) WHERE id=NEW.id; + END; + DROP TRIGGER IF EXISTS author_update_trg; + CREATE TEMP TRIGGER author_update_trg + BEFORE UPDATE ON authors + BEGIN + UPDATE authors SET sort=author_to_author_sort(NEW.name) + WHERE id=NEW.id AND name <> NEW.name; + END; + ''') + self.conn.execute( + 'UPDATE authors SET sort=author_to_author_sort(name) WHERE sort IS NULL') self.conn.executescript(u''' CREATE TEMP VIEW IF NOT EXISTS tag_browser_news AS SELECT DISTINCT id, diff --git a/src/calibre/library/schema_upgrades.py b/src/calibre/library/schema_upgrades.py index a8ffd9cde4..1ba650f6fd 100644 --- a/src/calibre/library/schema_upgrades.py +++ b/src/calibre/library/schema_upgrades.py @@ -385,28 +385,5 @@ class SchemaUpgrade(object): if table.startswith('custom_column_') and link_table in tables: create_cust_tag_browser_view(table, link_table) - from calibre.ebooks.metadata import author_to_author_sort + self.conn.execute('UPDATE authors SET sort=author_to_author_sort(name)') - aut = self.conn.get('SELECT id, name FROM authors'); - records = [] - for (id, author) in aut: - records.append((id, author.replace('|', ','))) - for id,author in records: - self.conn.execute('UPDATE authors SET sort=? WHERE id=?', - (author_to_author_sort(author.replace('|', ',')).strip(), id)) - self.conn.commit() - self.conn.executescript(''' - DROP TRIGGER IF EXISTS author_insert_trg; - CREATE TRIGGER author_insert_trg - AFTER INSERT ON authors - BEGIN - UPDATE authors SET sort=author_to_author_sort(NEW.name) WHERE id=NEW.id; - END; - DROP TRIGGER IF EXISTS author_update_trg; - CREATE TRIGGER author_update_trg - BEFORE UPDATE ON authors - BEGIN - UPDATE authors SET sort=author_to_author_sort(NEW.name) - WHERE id=NEW.id AND name <> NEW.name; - END; - ''') diff --git a/src/calibre/library/sqlite.py b/src/calibre/library/sqlite.py index 7e0458fba4..85954f6e0f 100644 --- a/src/calibre/library/sqlite.py +++ b/src/calibre/library/sqlite.py @@ -94,6 +94,9 @@ class Connection(sqlite.Connection): return ans[0] return ans.fetchall() +def _author_to_author_sort(x): + if not x: return '' + return author_to_author_sort(x.replace('|', ',')) class DBThread(Thread): @@ -121,7 +124,7 @@ class DBThread(Thread): else: self.conn.create_function('title_sort', 1, title_sort) self.conn.create_function('author_to_author_sort', 1, - lambda x: author_to_author_sort(x.replace('|', ','))) + _author_to_author_sort) self.conn.create_function('uuid4', 0, lambda : str(uuid.uuid4())) # Dummy functions for dynamically created filters self.conn.create_function('books_list_filter', 1, lambda x: 1)