Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked #4709 after latest release #5447

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ There are a few coding conventions we use in beets:
.. code-block:: python

with g.lib.transaction() as tx:
rows = tx.query('SELECT DISTINCT "{0}" FROM "{1}" ORDER BY "{2}"'
rows = tx.query("SELECT DISTINCT '{0}' FROM '{1}' ORDER BY '{2}'"
.format(field, model._table, sort_field))

To fetch Item objects from the database, use lib.items(…) and supply
Expand All @@ -248,7 +248,7 @@ There are a few coding conventions we use in beets:
.. code-block:: python

with lib.transaction() as tx:
rows = tx.query('SELECT …')
rows = tx.query("SELECT …")

Transaction objects help control concurrent access to the database
and assist in debugging conflicting accesses.
Expand Down
4 changes: 2 additions & 2 deletions beets/dbcore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
return unicodedata.normalize("NFC", s)

@classmethod
def string_match(cls, pattern: Pattern, value: str) -> bool:

Check failure on line 306 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "Pattern"
return pattern.search(cls._normalize(value)) is not None


Expand Down Expand Up @@ -465,7 +465,7 @@
"""Return a set with field names that this query operates on."""
return reduce(or_, (sq.field_names for sq in self.subqueries))

def __init__(self, subqueries: Sequence = ()):

Check failure on line 468 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "Sequence"
self.subqueries = subqueries

# Act like a sequence.
Expand All @@ -476,7 +476,7 @@
def __getitem__(self, key):
return self.subqueries[key]

def __iter__(self) -> Iterator:

Check failure on line 479 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "Iterator"
return iter(self.subqueries)

def __contains__(self, subq) -> bool:
Expand Down Expand Up @@ -525,7 +525,7 @@
"""Return a set with field names that this query operates on."""
return set(self.fields)

def __init__(self, pattern, fields, cls: Type[FieldQuery]):

Check failure on line 528 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "FieldQuery"
self.pattern = pattern
self.fields = fields
self.query_class = cls
Expand Down Expand Up @@ -563,7 +563,7 @@
query is initialized.
"""

subqueries: MutableSequence

Check failure on line 566 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "MutableSequence"

def __setitem__(self, key, value):
self.subqueries[key] = value
Expand Down Expand Up @@ -908,7 +908,7 @@
"""
return None

def sort(self, items: List) -> List:

Check failure on line 911 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "List"
"""Sort the list of objects and return a list."""
return sorted(items)

Expand Down Expand Up @@ -1002,7 +1002,7 @@
self.ascending = ascending
self.case_insensitive = case_insensitive

def sort(self, objs: Collection):

Check failure on line 1005 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "Collection"
# TODO: Conversion and null-detection here. In Python 3,
# comparisons with None fail. We should also support flexible
# attributes with different types without falling over.
Expand Down Expand Up @@ -1040,8 +1040,8 @@
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:
Expand All @@ -1061,7 +1061,7 @@
class NullSort(Sort):
"""No sorting. Leave results unsorted."""

def sort(self, items: List) -> List:

Check failure on line 1064 in beets/dbcore/query.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "List"
return items

def __nonzero__(self) -> bool:
Expand Down
7 changes: 2 additions & 5 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,8 @@
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:
Expand Down Expand Up @@ -1609,12 +1606,12 @@
timeout = beets.config["timeout"].as_number()
super().__init__(path, timeout=timeout)

self.directory = normpath(directory or platformdirs.user_music_path())

Check failure on line 1609 in beets/library.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 1 to "normpath" has incompatible type "Union[str, Any]"; expected "bytes"

self.path_formats = path_formats
self.replacements = replacements

self._memotable = {} # Used for template substitution performance.

Check failure on line 1614 in beets/library.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Need type annotation for "_memotable" (hint: "_memotable: Dict[<type>, <type>] = ...")

# Adding objects to the database.

Expand Down
2 changes: 1 addition & 1 deletion beetsplug/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def _get_unique_table_field_values(model, field, sort_field):
raise KeyError
with g.lib.transaction() as tx:
rows = tx.query(
'SELECT DISTINCT "{}" FROM "{}" ORDER BY "{}"'.format(
"SELECT DISTINCT '{}' FROM '{}' ORDER BY '{}'".format(
field, model._table, sort_field
)
)
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Bug fixes:
issues in the future.
:bug:`5289`
* :doc:`plugins/discogs`: Fix the ``TypeError`` when there is no description.
* Remove single quotes from all SQL queries
:bug:`4709`

For packagers:

Expand Down
10 changes: 6 additions & 4 deletions test/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)
assert new_year == 1987
Expand All @@ -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"]
)
assert new_genre == original_genre
Expand Down Expand Up @@ -104,7 +104,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"]
)
Expand All @@ -118,7 +119,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"]
)
Expand Down
Loading