diff --git a/beets/dbcore/__init__.py b/beets/dbcore/__init__.py index 913351630..6924be32a 100644 --- a/beets/dbcore/__init__.py +++ b/beets/dbcore/__init__.py @@ -15,4 +15,5 @@ """DBCore is an abstract database package that forms the basis for beets' Library. """ -from .db import Type, Model, Database, Query, FieldQuery, MatchQuery +from .db import Type, Model, Database +from .query import Query, FieldQuery, MatchQuery diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index c155c2239..c7c84054c 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -23,6 +23,7 @@ import contextlib import beets from beets.util.functemplate import Template +from .query import MatchQuery # Path element formatting for templating. @@ -404,73 +405,6 @@ class Model(object): -# Basic query classes. - - -class Query(object): - """An abstract class representing a query into the item database. - """ - def clause(self): - """Generate an SQLite expression implementing the query. - Return a clause string, a sequence of substitution values for - the clause, and a Query object representing the "remainder" - Returns (clause, subvals) where clause is a valid sqlite - WHERE clause implementing the query and subvals is a list of - items to be substituted for ?s in the clause. - """ - return None, () - - def match(self, item): - """Check whether this query matches a given Item. Can be used to - perform queries on arbitrary sets of Items. - """ - raise NotImplementedError - - -class FieldQuery(Query): - """An abstract query that searches in a specific field for a - pattern. Subclasses must provide a `value_match` class method, which - determines whether a certain pattern string matches a certain value - string. Subclasses may also provide `col_clause` to implement the - same matching functionality in SQLite. - """ - def __init__(self, field, pattern, fast=True): - self.field = field - self.pattern = pattern - self.fast = fast - - def col_clause(self): - return None, () - - def clause(self): - if self.fast: - return self.col_clause() - else: - # Matching a flexattr. This is a slow query. - return None, () - - @classmethod - def value_match(cls, pattern, value): - """Determine whether the value matches the pattern. Both - arguments are strings. - """ - raise NotImplementedError() - - def match(self, item): - return self.value_match(self.pattern, item.get(self.field)) - - -class MatchQuery(FieldQuery): - """A query that looks for exact matches in an item field.""" - def col_clause(self): - return self.field + " = ?", [self.pattern] - - @classmethod - def value_match(cls, pattern, value): - return pattern == value - - - # Database controller and supporting interfaces. diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py new file mode 100644 index 000000000..de8ed2f15 --- /dev/null +++ b/beets/dbcore/query.py @@ -0,0 +1,77 @@ +# This file is part of beets. +# Copyright 2014, Adrian Sampson. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. + +"""The Query type hierarchy for DBCore. +""" +class Query(object): + """An abstract class representing a query into the item database. + """ + def clause(self): + """Generate an SQLite expression implementing the query. + Return a clause string, a sequence of substitution values for + the clause, and a Query object representing the "remainder" + Returns (clause, subvals) where clause is a valid sqlite + WHERE clause implementing the query and subvals is a list of + items to be substituted for ?s in the clause. + """ + return None, () + + def match(self, item): + """Check whether this query matches a given Item. Can be used to + perform queries on arbitrary sets of Items. + """ + raise NotImplementedError + + +class FieldQuery(Query): + """An abstract query that searches in a specific field for a + pattern. Subclasses must provide a `value_match` class method, which + determines whether a certain pattern string matches a certain value + string. Subclasses may also provide `col_clause` to implement the + same matching functionality in SQLite. + """ + def __init__(self, field, pattern, fast=True): + self.field = field + self.pattern = pattern + self.fast = fast + + def col_clause(self): + return None, () + + def clause(self): + if self.fast: + return self.col_clause() + else: + # Matching a flexattr. This is a slow query. + return None, () + + @classmethod + def value_match(cls, pattern, value): + """Determine whether the value matches the pattern. Both + arguments are strings. + """ + raise NotImplementedError() + + def match(self, item): + return self.value_match(self.pattern, item.get(self.field)) + + +class MatchQuery(FieldQuery): + """A query that looks for exact matches in an item field.""" + def col_clause(self): + return self.field + " = ?", [self.pattern] + + @classmethod + def value_match(cls, pattern, value): + return pattern == value