Skip to content

Commit 3b4e3d4

Browse files
committed
consolidate excess SELECT in load()
1 parent 4ee4169 commit 3b4e3d4

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

beets/library.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,7 @@ def load(self):
410410
"""Refresh the object's metadata from the library database.
411411
"""
412412
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)
423414
self.update(dict(stored_obj))
424415
self.clear_dirty()
425416

@@ -1508,24 +1499,23 @@ def move(self, item, copy=False, basedir=None,
15081499

15091500
# Querying.
15101501

1511-
def _fetch(self, model_cls, order_by, query):
1502+
def _fetch(self, model_cls, query, order_by=None):
15121503
"""Fetch the objects of type `model_cls` matching the given
15131504
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.
15151507
"""
15161508
query = get_query(query, model_cls)
1517-
15181509
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)
15191517
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)
15291519

15301520
return Results(model_cls, rows, self, None if where else query)
15311521

@@ -1535,23 +1525,29 @@ def albums(self, query=None):
15351525
order = '{0}, album'.format(
15361526
_orelse("albumartist_sort", "albumartist")
15371527
)
1538-
return self._fetch(Album, order, query)
1528+
return self._fetch(Album, query, order)
15391529

15401530
def items(self, query=None):
15411531
"""Get a sorted list of Item objects matching the given query.
15421532
"""
15431533
order = '{0}, album'.format(
15441534
_orelse("artist_sort", "artist")
15451535
)
1546-
return self._fetch(Item, order, query)
1536+
return self._fetch(Item, query, order)
15471537

15481538

15491539
# Convenience accessors.
15501540

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+
15511547
def get_item(self, id):
15521548
"""Fetch an Item by its ID. Returns None if no match is found.
15531549
"""
1554-
return self.items(MatchQuery('id', id)).get()
1550+
return self._get(Item, id)
15551551

15561552
def get_album(self, item_or_id):
15571553
"""Given an album ID or an item associated with an album,
@@ -1564,17 +1560,15 @@ def get_album(self, item_or_id):
15641560
album_id = item_or_id.album_id
15651561
if album_id is None:
15661562
return None
1567-
1568-
return self.albums(MatchQuery('id', album_id)).get()
1563+
return self._get(Album, album_id)
15691564

15701565
def add_album(self, items):
15711566
"""Create a new album in the database with metadata derived
15721567
from its items. The items are added to the database if they
15731568
don't yet have an ID. Returns an Album object.
15741569
"""
15751570
# 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)
15781572

15791573
# When adding an album and its items for the first time, the
15801574
# items do not yet have a timestamp.

0 commit comments

Comments
 (0)