163
163
}
164
164
SQLITE_KEY_TYPE = 'INTEGER PRIMARY KEY'
165
165
166
- # Default search fields for various granularities.
167
- ARTIST_DEFAULT_FIELDS = ('artist' ,)
166
+ # Default search fields for each model.
168
167
ALBUM_DEFAULT_FIELDS = ('album' , 'albumartist' , 'genre' )
169
- ITEM_DEFAULT_FIELDS = ARTIST_DEFAULT_FIELDS + ALBUM_DEFAULT_FIELDS + \
170
- ('title' , 'comments' )
168
+ ITEM_DEFAULT_FIELDS = ALBUM_DEFAULT_FIELDS + ('artist' , 'title' , 'comments' )
171
169
172
170
# Special path format key.
173
171
PF_KEY_DEFAULT = 'default'
@@ -350,6 +348,11 @@ class LibModel(FlexModel):
350
348
strings.
351
349
"""
352
350
351
+ _search_fields = ()
352
+ """The fields that should be queried by default by unqualified query
353
+ terms.
354
+ """
355
+
353
356
def __init__ (self , lib = None , ** values ):
354
357
self ._lib = lib
355
358
super (LibModel , self ).__init__ (** values )
@@ -424,6 +427,7 @@ class Item(LibModel):
424
427
_fields = ITEM_KEYS
425
428
_table = 'items'
426
429
_flex_table = 'item_attributes'
430
+ _search_fields = ITEM_DEFAULT_FIELDS
427
431
428
432
@classmethod
429
433
def from_path (cls , path ):
@@ -610,7 +614,7 @@ class Query(object):
610
614
def clause (self ):
611
615
"""Generate an SQLite expression implementing the query.
612
616
Return a clause string, a sequence of substitution values for
613
- the clause, and a Query object representing the "remainder"
617
+ the clause, and a Query object representing the "remainder"
614
618
Returns (clause, subvals) where clause is a valid sqlite
615
619
WHERE clause implementing the query and subvals is a list of
616
620
items to be substituted for ?s in the clause.
@@ -1101,18 +1105,13 @@ def construct_query_part(query_part, default_fields, all_keys):
1101
1105
else :
1102
1106
return query_class (key .lower (), pattern , key in all_keys )
1103
1107
1104
- def get_query (val , album = False ):
1108
+ def get_query (val , model_cls ):
1105
1109
"""Takes a value which may be None, a query string, a query string
1106
- list, or a Query object, and returns a suitable Query object. album
1107
- determines whether the query is to match items or albums.
1110
+ list, or a Query object, and returns a suitable Query object.
1111
+ `model_cls` is the subclass of LibModel indicating which entity this
1112
+ is a query for (i.e., Album or Item) and is used to determine which
1113
+ fields are searched.
1108
1114
"""
1109
- if album :
1110
- default_fields = ALBUM_DEFAULT_FIELDS
1111
- all_keys = ALBUM_KEYS
1112
- else :
1113
- default_fields = ITEM_DEFAULT_FIELDS
1114
- all_keys = ITEM_KEYS
1115
-
1116
1115
# Convert a single string into a list of space-separated
1117
1116
# criteria.
1118
1117
if isinstance (val , basestring ):
@@ -1121,7 +1120,8 @@ def get_query(val, album=False):
1121
1120
if val is None :
1122
1121
return TrueQuery ()
1123
1122
elif isinstance (val , list ) or isinstance (val , tuple ):
1124
- return AndQuery .from_strings (val , default_fields , all_keys )
1123
+ return AndQuery .from_strings (val , model_cls ._search_fields ,
1124
+ model_cls ._fields )
1125
1125
elif isinstance (val , Query ):
1126
1126
return val
1127
1127
else :
@@ -1513,7 +1513,7 @@ def _fetch(self, model_cls, order_by, query):
1513
1513
query. The query may be given as a string, string sequence, a
1514
1514
Query object, or None (to fetch everything).
1515
1515
"""
1516
- query = get_query (query , model_cls is Album )
1516
+ query = get_query (query , model_cls )
1517
1517
1518
1518
where , subvals = query .clause ()
1519
1519
with self .transaction () as tx :
@@ -1608,6 +1608,7 @@ class Album(LibModel):
1608
1608
_fields = ALBUM_KEYS
1609
1609
_table = 'albums'
1610
1610
_flex_table = 'album_attributes'
1611
+ _search_fields = ALBUM_DEFAULT_FIELDS
1611
1612
1612
1613
def __setitem__ (self , key , value ):
1613
1614
"""Set the value of an album attribute."""
0 commit comments