From 7567453a978ddf8660f780fa83e6b4e6cd5461df Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Tue, 14 Oct 2008 23:01:20 +0200 Subject: [PATCH] Simplify database purging code for single channels Make the purge() function in dbsqlite simpler by only allowing a single channel to be purged at a time. This reduces the amounts of SQL queries. --- src/gpodder/dbsqlite.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/gpodder/dbsqlite.py b/src/gpodder/dbsqlite.py index 5a93a1d7..c25aba79 100644 --- a/src/gpodder/dbsqlite.py +++ b/src/gpodder/dbsqlite.py @@ -65,30 +65,22 @@ class Storage(object): except TypeError, e: log('Exception in log(): %s: %s', e, message, sender=self) - def purge(self, max_episodes, channel_id=None): + def purge(self, max_episodes, channel_id): """ Deletes old episodes. Should be called before adding new episodes to a channel. """ cur = self.cursor(lock=True) - if channel_id is None: - cur.execute("SELECT channel_id, COUNT(*) AS count FROM episodes GROUP BY channel_id HAVING count > ?", (max_episodes, )) - else: - cur.execute("SELECT channel_id, COUNT(*) AS count FROM episodes WHERE channel_id = ? GROUP BY channel_id HAVING count > ?", (channel_id, max_episodes, )) - self.log("purge(%s)", channel_id) - - for row in cur.fetchall(): - self.log("purge() -- deleting episodes in %d", row[0]) - sql = """ - DELETE FROM episodes - WHERE channel_id = %d - AND state <> %d - AND id NOT IN - (SELECT id FROM episodes WHERE channel_id = %d - ORDER BY pubDate DESC LIMIT %d)""" % (row[0], self.STATE_DOWNLOADED, row[0], max_episodes) - cur.execute(sql) + sql = """ + DELETE FROM episodes + WHERE channel_id = ? + AND state <> ? + AND id NOT IN + (SELECT id FROM episodes WHERE channel_id = ? + ORDER BY pubDate DESC LIMIT ?)""" + cur.execute(sql, channel_id, self.STATE_DOWNLOADED, channel_id, max_episodes) cur.close() self.lock.release()