@@ -410,16 +410,7 @@ def load(self):
410
410
"""Refresh the object's metadata from the library database.
411
411
"""
412
412
self ._check_db ()
413
-
414
- # Get a fresh copy of this object from the DB.
415
- with self ._lib .transaction () as tx :
416
- rows = tx .query (
417
- 'SELECT * FROM {0} WHERE id=?;' .format (self ._table ),
418
- (self .id ,)
419
- )
420
- results = Results (type (self ), rows , self ._lib )
421
- stored_obj = results .get ()
422
-
413
+ stored_obj = self ._lib ._get (type (self ), self .id )
423
414
self .update (dict (stored_obj ))
424
415
self .clear_dirty ()
425
416
@@ -1508,24 +1499,23 @@ def move(self, item, copy=False, basedir=None,
1508
1499
1509
1500
# Querying.
1510
1501
1511
- def _fetch (self , model_cls , order_by , query ):
1502
+ def _fetch (self , model_cls , query , order_by = None ):
1512
1503
"""Fetch the objects of type `model_cls` matching the given
1513
1504
query. The query may be given as a string, string sequence, a
1514
- Query object, or None (to fetch everything).
1505
+ Query object, or None (to fetch everything). If provided,
1506
+ `order_by` is a SQLite ORDER BY clause for sorting.
1515
1507
"""
1516
1508
query = get_query (query , model_cls )
1517
-
1518
1509
where , subvals = query .clause ()
1510
+
1511
+ sql = "SELECT * FROM {0} WHERE {1}" .format (
1512
+ model_cls ._table ,
1513
+ where or '1' ,
1514
+ )
1515
+ if order_by :
1516
+ sql += " ORDER BY {0}" .format (order_by )
1519
1517
with self .transaction () as tx :
1520
- rows = tx .query (
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
1526
- ),
1527
- subvals
1528
- )
1518
+ rows = tx .query (sql , subvals )
1529
1519
1530
1520
return Results (model_cls , rows , self , None if where else query )
1531
1521
@@ -1535,23 +1525,29 @@ def albums(self, query=None):
1535
1525
order = '{0}, album' .format (
1536
1526
_orelse ("albumartist_sort" , "albumartist" )
1537
1527
)
1538
- return self ._fetch (Album , order , query )
1528
+ return self ._fetch (Album , query , order )
1539
1529
1540
1530
def items (self , query = None ):
1541
1531
"""Get a sorted list of Item objects matching the given query.
1542
1532
"""
1543
1533
order = '{0}, album' .format (
1544
1534
_orelse ("artist_sort" , "artist" )
1545
1535
)
1546
- return self ._fetch (Item , order , query )
1536
+ return self ._fetch (Item , query , order )
1547
1537
1548
1538
1549
1539
# Convenience accessors.
1550
1540
1541
+ def _get (self , model_cls , id ):
1542
+ """Get a LibModel object by its id or None if the id does not
1543
+ exist.
1544
+ """
1545
+ return self ._fetch (model_cls , MatchQuery ('id' , id )).get ()
1546
+
1551
1547
def get_item (self , id ):
1552
1548
"""Fetch an Item by its ID. Returns None if no match is found.
1553
1549
"""
1554
- return self .items ( MatchQuery ( 'id' , id )). get ( )
1550
+ return self ._get ( Item , id )
1555
1551
1556
1552
def get_album (self , item_or_id ):
1557
1553
"""Given an album ID or an item associated with an album,
@@ -1564,17 +1560,15 @@ def get_album(self, item_or_id):
1564
1560
album_id = item_or_id .album_id
1565
1561
if album_id is None :
1566
1562
return None
1567
-
1568
- return self .albums (MatchQuery ('id' , album_id )).get ()
1563
+ return self ._get (Album , album_id )
1569
1564
1570
1565
def add_album (self , items ):
1571
1566
"""Create a new album in the database with metadata derived
1572
1567
from its items. The items are added to the database if they
1573
1568
don't yet have an ID. Returns an Album object.
1574
1569
"""
1575
1570
# Set the metadata from the first item.
1576
- album_values = dict (
1577
- (key , getattr (items [0 ], key )) for key in ALBUM_KEYS_ITEM )
1571
+ album_values = dict ((key , items [0 ][key ]) for key in ALBUM_KEYS_ITEM )
1578
1572
1579
1573
# When adding an album and its items for the first time, the
1580
1574
# items do not yet have a timestamp.
0 commit comments