Adrian Sampson avatar Adrian Sampson committed a0f4546

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.

Comments (0)

Files changed (1)

     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):
     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):
         """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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.