diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 23b6029c3..9c53cf6ed 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -638,34 +638,22 @@ class FixedFieldSort(FieldSort): class SmartArtistSort(Sort): - """ Sort Album or Item on artist sort fields, defaulting back on - artist field if the sort specific field is empty. + """Sort by artist (either album artist or track artist), + prioritizing the sort field over the raw field. """ def __init__(self, model_cls, is_ascending=True): self.model_cls = model_cls self.is_ascending = is_ascending - def select_clause(self): - return "" - - def union_clause(self): - return "" - def order_clause(self): order = "ASC" if self.is_ascending else "DESC" - if 'albumartist_sort' in self.model_cls._fields: - exp1 = 'albumartist_sort' - exp2 = 'albumartist' - elif 'artist_sort' in self.model_cls_fields: - exp1 = 'artist_sort' - exp2 = 'artist' + if 'albumartist' in self.model_cls._fields: + field = 'albumartist' else: - return "" - - order_str = ('(CASE {0} WHEN NULL THEN {1} ' - 'WHEN "" THEN {1} ' - 'ELSE {0} END) {2} ').format(exp1, exp2, order) - return order_str + field = 'artist' + return ('(CASE {0}_sort WHEN NULL THEN {0} ' + 'WHEN "" THEN {0} ' + 'ELSE {0}_sort END) {2} ').format(field, order) class SlowFieldSort(FieldSort): @@ -676,9 +664,6 @@ class SlowFieldSort(FieldSort): return True -special_sorts = {'smartartist': SmartArtistSort} - - def build_sql(model_cls, query, sort): """ Generate a sql statement (and the values that must be injected into it) from a query, sort and a model class. Query and sort objects are returned diff --git a/beets/dbcore/queryparse.py b/beets/dbcore/queryparse.py index 0da997bc4..cf8a75ffe 100644 --- a/beets/dbcore/queryparse.py +++ b/beets/dbcore/queryparse.py @@ -131,10 +131,11 @@ def construct_sort_part(model_cls, part): is_ascending = (part[-1] == '+') if field in model_cls._fields: sort = query.FixedFieldSort(field, is_ascending) - elif field in query.special_sorts: - sort = query.special_sorts[field](model_cls, is_ascending) + elif field == 'smartartist': + # Special case for smart artist sort. + sort = query.SmartArtistSort(model_cls, is_ascending) else: - # Flexible or comptued. + # Flexible or computed. sort = query.SlowFieldSort(field, is_ascending) return sort diff --git a/beets/library.py b/beets/library.py index 3119e1bfa..f053728b1 100644 --- a/beets/library.py +++ b/beets/library.py @@ -139,16 +139,6 @@ class MusicalKey(types.String): PF_KEY_DEFAULT = 'default' -# A little SQL utility. -def _orelse(exp1, exp2): - """Generates an SQLite expression that evaluates to exp1 if exp1 is - non-null and non-empty or exp2 otherwise. - """ - return ("""(CASE {0} WHEN NULL THEN {1} - WHEN "" THEN {1} - ELSE {0} END)""").format(exp1, exp2) - - # Exceptions. class FileOperationError(Exception):