From 4c85dc8f7269c3e399bea4ae7b2bb9917a963e53 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 16 Mar 2013 12:50:24 -0700 Subject: [PATCH] reduce redundancy in item queries Now the SELECT query for items only appears in Library.items(). All other functions that retrieve items go through this method. This should make it easier to change the way flexattrs are picked up. --- beets/library.py | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/beets/library.py b/beets/library.py index 441554494..2eb8c6bf1 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1314,13 +1314,8 @@ class Library(BaseLibrary): def load(self, item, load_id=None): if load_id is None: load_id = item.id - - with self.transaction() as tx: - rows = tx.query('SELECT * FROM items WHERE id=?', (load_id,)) - flex_rows = tx.query( - 'SELECT * FROM item_attributes WHERE entity_id=?', (load_id,) - ) - item._fill_record(rows[0], get_flexattrs(flex_rows)) + stored_item = self.get_item(load_id) + item._fill_record(stored_item.record, stored_item.flexattrs) item._clear_dirty() def store(self, item, store_id=None, store_all=False): @@ -1468,14 +1463,10 @@ class Library(BaseLibrary): def get_item(self, id): """Fetch an Item by its ID. Returns None if no match is found. """ - with self.transaction() as tx: - rows = tx.query("SELECT * FROM items WHERE id=?", (id,)) - flex_rows = tx.query( - 'SELECT * FROM item_attributes WHERE entity_id=?', (id,) - ) - if rows: - return Item(rows, get_flexattrs(flex_rows)) - else: + items = self.items(MatchQuery('id', id)) + try: + return items.next() + except StopIteration: return None def get_album(self, item_or_id): @@ -1590,12 +1581,7 @@ class Album(BaseAlbum): """Returns an iterable over the items associated with this album. """ - with self._library.transaction() as tx: - rows = tx.query( - 'SELECT * FROM items WHERE album_id=?', - (self.id,) - ) - return ResultIterator(rows, self._library) + return self._library.items(MatchQuery('album_id', self.id)) def remove(self, delete=False, with_items=True): """Removes this album and all its associated items from the