Remove leading The,A,An from titles for sorting

This commit is contained in:
Kovid Goyal 2007-05-27 20:11:43 +00:00
parent d561643b79
commit b82044dfa4

View File

@ -40,6 +40,14 @@ def _connect(path):
conn = sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES) conn = sqlite.connect(path, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
conn.row_factory = sqlite.Row conn.row_factory = sqlite.Row
conn.create_aggregate('concat', 1, Concatenate) conn.create_aggregate('concat', 1, Concatenate)
title_pat = re.compile('^(A|The|An\s+)', re.IGNORECASE)
def title_sort(title):
match = title_pat.search(title)
if match:
prep = match.group(1)
title = title.replace(prep, '') + ', ' + prep
return title.strip()
conn.create_function('title_sort', 1, title_sort)
return conn return conn
class LibraryDatabase(object): class LibraryDatabase(object):
@ -148,12 +156,12 @@ class LibraryDatabase(object):
CREATE TRIGGER books_insert_trg CREATE TRIGGER books_insert_trg
AFTER INSERT ON books AFTER INSERT ON books
BEGIN BEGIN
UPDATE books SET sort=NEW.title WHERE id=NEW.id; UPDATE books SET sort=title_sort(NEW.title) WHERE id=NEW.id;
END; END;
CREATE TRIGGER books_update_trg CREATE TRIGGER books_update_trg
AFTER UPDATE ON books AFTER UPDATE ON books
BEGIN BEGIN
UPDATE books SET sort=NEW.title WHERE id=NEW.id; UPDATE books SET sort=title_sort(NEW.title) WHERE id=NEW.id;
END; END;