Make author sort changes backward compatible

This commit is contained in:
Kovid Goyal 2010-06-17 10:01:48 -06:00
parent f712c3cc3b
commit 54af6259bc
4 changed files with 26 additions and 25 deletions

View File

@ -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:

View File

@ -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,

View File

@ -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;
''')

View File

@ -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)