Skip to content

Commit

Permalink
Use Item.field_query for queries that receive user input
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Dec 7, 2024
1 parent 94cbccb commit 003afb2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
15 changes: 8 additions & 7 deletions beetsplug/aura.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def translate_filters(self):
value = converter(value)
# Add exact match query to list
# Use a slow query so it works with all fields
queries.append(MatchQuery(beets_attr, value, fast=False))
queries.append(
self.model_cls.field_query(beets_attr, value, MatchQuery)
)
# NOTE: AURA doesn't officially support multiple queries
return AndQuery(queries)

Expand Down Expand Up @@ -317,13 +319,12 @@ def all_resources(self):
sort = self.translate_sorts(sort_arg)
# For each sort field add a query which ensures all results
# have a non-empty, non-zero value for that field.
for s in sort.sorts:
query.subqueries.append(
NotQuery(
# Match empty fields (^$) or zero fields, (^0$)
RegexpQuery(s.field, "(^$|^0$)", fast=False)
)
query.subqueries.extend(
NotQuery(
self.model_cls.field_query(s.field, "(^$|^0$)", RegexpQuery)
)
for s in sort.sorts
)
else:
sort = None
# Get information from the library
Expand Down
15 changes: 11 additions & 4 deletions beetsplug/bpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use of the wide range of MPD clients.
"""

from __future__ import annotations

import inspect
import math
import random
Expand All @@ -26,7 +28,7 @@
import time
import traceback
from string import Template
from typing import List
from typing import TYPE_CHECKING, List

import beets
import beets.ui
Expand All @@ -35,6 +37,9 @@
from beets.plugins import BeetsPlugin
from beets.util import bluelet

if TYPE_CHECKING:
from beets.dbcore.query import Query

PROTOCOL_VERSION = "0.16.0"
BUFSIZE = 1024

Expand Down Expand Up @@ -1403,7 +1408,7 @@ def _metadata_query(self, query_type, kv, allow_any_query: bool = False):
type "any"; if None, then an error is thrown.
"""
if kv: # At least one key-value pair.
queries = []
queries: list[Query] = []
# Iterate pairwise over the arguments.
it = iter(kv)
for tag, value in zip(it, it):
Expand All @@ -1418,7 +1423,7 @@ def _metadata_query(self, query_type, kv, allow_any_query: bool = False):
raise BPDError(ERROR_UNKNOWN, "no such tagtype")
else:
_, key = self._tagtype_lookup(tag)
queries.append(query_type(key, value))
queries.append(Item.field_query(key, value, query_type))
return dbcore.query.AndQuery(queries)
else: # No key-value pairs.
return dbcore.query.TrueQuery()
Expand Down Expand Up @@ -1481,7 +1486,9 @@ def cmd_count(self, conn, tag, value):
_, key = self._tagtype_lookup(tag)
songs = 0
playtime = 0.0
for item in self.lib.items(dbcore.query.MatchQuery(key, value)):
for item in self.lib.items(
Item.field_query(key, value, dbcore.query.MatchQuery)
):
songs += 1
playtime += item.length
yield "songs: " + str(songs)
Expand Down

0 comments on commit 003afb2

Please sign in to comment.