diff --git a/.gitignore b/.gitignore index dc193ff2c4..50e7cab888 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ htmlcov/ coverage.xml *,cover .hypothesis/ +.reports/ # Flask stuff: instance/ diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index ffa89168ec..d2334c0651 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -1008,8 +1008,8 @@ def order_clause(self) -> str: if self.case_insensitive: field = ( "(CASE " - 'WHEN TYPEOF({0})="text" THEN LOWER({0}) ' - 'WHEN TYPEOF({0})="blob" THEN LOWER({0}) ' + "WHEN TYPEOF({0})='text' THEN LOWER({0}) " + "WHEN TYPEOF({0})='blob' THEN LOWER({0}) " "ELSE {0} END)".format(self.field) ) else: diff --git a/beets/library.py b/beets/library.py index 367b184ef8..20b30bcd61 100644 --- a/beets/library.py +++ b/beets/library.py @@ -308,11 +308,8 @@ def order_clause(self): order = "ASC" if self.ascending else "DESC" field = "albumartist" if self.album else "artist" collate = "COLLATE NOCASE" if self.case_insensitive else "" - return ( - "(CASE {0}_sort WHEN NULL THEN {0} " - 'WHEN "" THEN {0} ' - "ELSE {0}_sort END) {1} {2}" - ).format(field, collate, order) + + return f"COALESCE(NULLIF({field}_sort, ''), {field}) {collate} {order}" def sort(self, objs): if self.album: diff --git a/docs/changelog.rst b/docs/changelog.rst index 6823f5cce3..9f144c66d2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -166,6 +166,7 @@ New features: Bug fixes: +* :bug:`4709` * :doc:`/plugins/lastimport`: Improve error handling in the `process_tracks` function and enable it to be used with other plugins. * :doc:`/plugins/spotify`: Improve handling of ConnectionError. * :doc:`/plugins/deezer`: Improve Deezer plugin error handling and set requests timeout to 10 seconds. diff --git a/test/test_library.py b/test/test_library.py index c9d0440b51..d161874051 100644 --- a/test/test_library.py +++ b/test/test_library.py @@ -59,7 +59,7 @@ def test_store_changes_database_value(self): self.i.store() new_year = ( self.lib._connection() - .execute("select year from items where " 'title="the title"') + .execute("select year from items where title = ?", (self.i.title,)) .fetchone()["year"] ) self.assertEqual(new_year, 1987) @@ -70,7 +70,7 @@ def test_store_only_writes_dirty_fields(self): self.i.store() new_genre = ( self.lib._connection() - .execute("select genre from items where " 'title="the title"') + .execute("select genre from items where title = ?", (self.i.title,)) .fetchone()["genre"] ) self.assertEqual(new_genre, original_genre) @@ -105,7 +105,8 @@ def test_item_add_inserts_row(self): new_grouping = ( self.lib._connection() .execute( - "select grouping from items " 'where composer="the composer"' + "select grouping from items where composer = ?", + (self.i.composer,), ) .fetchone()["grouping"] ) @@ -119,7 +120,8 @@ def test_library_add_path_inserts_row(self): new_grouping = ( self.lib._connection() .execute( - "select grouping from items " 'where composer="the composer"' + "select grouping from items where composer = ?", + (self.i.composer,), ) .fetchone()["grouping"] )