@@ -1508,58 +1508,42 @@ def move(self, item, copy=False, basedir=None,
1508
1508
1509
1509
# Querying.
1510
1510
1511
- def albums (self , query = None , artist = None ):
1512
- """Returns a sorted list of Album objects, possibly filtered
1513
- by an artist name or an arbitrary query. Unqualified query
1514
- string terms only match fields that apply at an album
1515
- granularity: artist, album, and genre.
1511
+ def _fetch (self , model_cls , order_by , query ):
1512
+ """Fetch the objects of type `model_cls` matching the given
1513
+ query. The query may be given as a string, string sequence, a
1514
+ Query object, or None (to fetch everything).
1516
1515
"""
1517
- query = get_query (query , True )
1518
- if artist is not None :
1519
- # "Add" the artist to the query.
1520
- query = AndQuery ((query , MatchQuery ('albumartist' , artist )))
1516
+ query = get_query (query , model_cls is Album )
1521
1517
1522
1518
where , subvals = query .clause ()
1523
1519
with self .transaction () as tx :
1524
1520
rows = tx .query (
1525
- "SELECT * FROM albums WHERE {0} "
1526
- "ORDER BY {1}, album" .format (
1527
- where or '1' ,
1528
- _orelse ("albumartist_sort" , "albumartist" ),
1521
+ "SELECT * FROM {table} WHERE {where} "
1522
+ "ORDER BY {order_by}" .format (
1523
+ table = model_cls ._table ,
1524
+ where = where or '1' ,
1525
+ order_by = order_by
1529
1526
),
1530
- subvals ,
1527
+ subvals
1531
1528
)
1532
1529
1533
- return Results (Album , rows , self , None if where else query )
1534
-
1535
- def items (self , query = None , artist = None , album = None , title = None ):
1536
- """Returns a sequence of the items matching the given artist,
1537
- album, title, and query (if present). Sorts in such a way as to
1538
- group albums appropriately. Unqualified query string terms only
1539
- match intuitively relevant fields: artist, album, genre, title,
1540
- and comments.
1541
- """
1542
- queries = [get_query (query , False )]
1543
- if artist is not None :
1544
- queries .append (MatchQuery ('artist' , artist ))
1545
- if album is not None :
1546
- queries .append (MatchQuery ('album' , album ))
1547
- if title is not None :
1548
- queries .append (MatchQuery ('title' , title ))
1549
- query = AndQuery (queries )
1550
- where , subvals = query .clause ()
1530
+ return Results (model_cls , rows , self , None if where else query )
1551
1531
1552
- with self .transaction () as tx :
1553
- rows = tx .query (
1554
- "SELECT * FROM items WHERE {0} "
1555
- "ORDER BY {1}, album, disc, track" .format (
1556
- where or '1' ,
1557
- _orelse ("artist_sort" , "artist" ),
1558
- ),
1559
- subvals
1560
- )
1532
+ def albums (self , query = None ):
1533
+ """Get a sorted list of Album objects matching the given query.
1534
+ """
1535
+ order = '{0}, album' .format (
1536
+ _orelse ("albumartist_sort" , "albumartist" )
1537
+ )
1538
+ return self ._fetch (Album , order , query )
1561
1539
1562
- return Results (Item , rows , self , None if where else query )
1540
+ def items (self , query = None ):
1541
+ """Get a sorted list of Item objects matching the given query.
1542
+ """
1543
+ order = '{0}, album' .format (
1544
+ _orelse ("artist_sort" , "artist" )
1545
+ )
1546
+ return self ._fetch (Item , order , query )
1563
1547
1564
1548
1565
1549
# Convenience accessors.
@@ -1932,7 +1916,7 @@ def tmpl_aunique(self, keys=None, disam=None):
1932
1916
for key in keys :
1933
1917
value = getattr (album , key )
1934
1918
subqueries .append (MatchQuery (key , value ))
1935
- albums = self .lib .albums (query = AndQuery (subqueries ))
1919
+ albums = self .lib .albums (AndQuery (subqueries ))
1936
1920
1937
1921
# If there's only one album to matching these details, then do
1938
1922
# nothing.
0 commit comments