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.
This commit is contained in:
parent
0a3caebf12
commit
7567453a97
1 changed files with 9 additions and 17 deletions
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue