diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index 5dff1c0f11..5172ad523c 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -508,7 +508,7 @@ def __iter__(self): # Slow sort. Must build the full list first. objects = [] for row in self.rows: - obj = self._generate_results(row) + obj = self._make_model(row) # check the predicate if any if not self.query or self.query.match(obj): objects.append(obj) @@ -518,12 +518,12 @@ def __iter__(self): yield o else: for row in self.rows: - obj = self._generate_results(row) + obj = self._make_model(row) # check the predicate if any if not self.query or self.query.match(obj): yield obj - def _generate_results(self, row): + def _make_model(self, row): # Get the flexible attributes for the object. with self.db.transaction() as tx: flex_rows = tx.query( @@ -771,15 +771,12 @@ def _fetch(self, model_cls, query, sort_order=None): Sort object. """ - sql, subvals, slow_query, slow_sort = build_sql(model_cls, query, - sort_order) + sql, subvals, query, sort = build_sql(model_cls, query, sort_order) with self.transaction() as tx: rows = tx.query(sql, subvals) - return Results(model_cls, rows, self, - None if not slow_query else query, - None if not slow_sort else sort_order) + return Results(model_cls, rows, self, query, sort) def _get(self, model_cls, id): """Get a Model object by its id or None if the id does not diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 6b1b2541ea..6a10f2533b 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -713,31 +713,34 @@ def sort(self, items): 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. + from a query, sort and a model class. Query and sort objects are returned + only for slow query and slow sort operation. """ where, subvals = query.clause() - slow_query = where is None + if where is not None: + query = None if not sort: sort_select = "" sort_union = "" sort_order = "" - slow_sort = False + sort = None elif isinstance(sort, basestring): sort_select = "" sort_union = "" sort_order = " ORDER BY {0}".format(sort) \ if sort else "" - slow_sort = False + sort = None elif isinstance(sort, Sort): select_clause = sort.select_clause() sort_select = " ,{0} ".format(select_clause) \ if select_clause else "" sort_union = sort.union_clause() - slow_sort = sort.is_slow() order_clause = sort.order_clause() sort_order = " ORDER BY {0}".format(order_clause) \ if order_clause else "" + if sort.is_slow(): + sort = None sql = ("SELECT {table}.* {sort_select} FROM {table} {sort_union} WHERE " "{query_clause} {sort_order}").format( @@ -748,4 +751,4 @@ def build_sql(model_cls, query, sort): sort_order=sort_order ) - return sql, subvals, slow_query, slow_sort + return sql, subvals, query, sort diff --git a/beets/library.py b/beets/library.py index 7270dc8c01..c0c9ebe1a1 100644 --- a/beets/library.py +++ b/beets/library.py @@ -546,7 +546,7 @@ def destination(self, fragment=False, basedir=None, platform=None, for query, path_format in path_formats: if query == PF_KEY_DEFAULT: continue - (query, _) = get_query(query, type(self)) + (query, _) = get_query_sort(query, type(self)) if query.match(self): # The query matches the item! Use the corresponding path # format. @@ -887,7 +887,7 @@ def store(self): # Query construction helper. -def get_query(val, model_cls): +def get_query_sort(val, model_cls): """Take a value which may be None, a query string, a query string list, or a Query object, and return a suitable Query object and Sort object. @@ -1016,8 +1016,8 @@ def _fetch(self, model_cls, query, sort_order=None): """Parse a query and fetch. If a order specification is present in the query string the sort_order argument is ignored. """ - (query, sort) = get_query(query, model_cls) - sort = sort_order if sort is None else sort + query, sort = get_query_sort(query, model_cls) + sort = sort or sort_order return super(Library, self)._fetch( model_cls, query, sort diff --git a/beetsplug/ihate.py b/beetsplug/ihate.py index 85e3382d46..3e86759c60 100644 --- a/beetsplug/ihate.py +++ b/beetsplug/ihate.py @@ -17,7 +17,7 @@ import logging from beets.plugins import BeetsPlugin from beets.importer import action -from beets.library import get_query +from beets.library import get_query_sort from beets.library import Item from beets.library import Album @@ -57,9 +57,9 @@ def do_i_hate_this(cls, task, action_patterns): for query_string in action_patterns: query = None if task.is_album: - (query, _) = get_query(query_string, Album) + (query, _) = get_query_sort(query_string, Album) else: - (query, _) = get_query(query_string, Item) + (query, _) = get_query_sort(query_string, Item) if any(query.match(item) for item in task.imported_items()): return True return False diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index 12672ce42c..6beb0ad598 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -42,7 +42,7 @@ def _items_for_query(lib, playlist, album=False): query_strings = [query_strings] model = library.Album if album else library.Item query = dbcore.OrQuery( - [library.get_query(q, model)[0] for q in query_strings] + [library.get_query_sort(q, model)[0] for q in query_strings] ) # Execute query, depending on type.