Use Item.field_query for queries that receive user input

This commit is contained in:
Šarūnas Nejus 2024-12-07 16:27:31 +00:00
parent 4650f6513b
commit a8ad7df064
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
2 changed files with 17 additions and 10 deletions

View file

@ -186,7 +186,9 @@ class AURADocument:
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)
@ -318,13 +320,12 @@ class AURADocument:
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

View file

@ -26,6 +26,7 @@ import sys
import time
import traceback
from string import Template
from typing import TYPE_CHECKING
import beets
import beets.ui
@ -34,6 +35,9 @@ from beets.library import Item
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
@ -1402,7 +1406,7 @@ class Server(BaseServer):
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):
@ -1417,7 +1421,7 @@ class Server(BaseServer):
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()
@ -1480,7 +1484,9 @@ class Server(BaseServer):
_, 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)