From: CMDR furrycat Date: Wed, 5 Oct 2016 10:49:45 +0000 (+0100) Subject: Reopen DB for each operation. X-Git-Url: http://git.furryclan.net/?a=commitdiff_plain;h=eccc05d3e33b909fde06c70534ab8470d33d0925;p=furrycat%2Fcatbot.git Reopen DB for each operation. --- diff --git a/db.py b/db.py index 509de0f..0d66847 100644 --- a/db.py +++ b/db.py @@ -8,10 +8,12 @@ log = logging.getLogger('db') class DBConnection(object): def __init__(self, filename = 'bot.sqlite'): + self.filename = filename self.ONLINE_AVATAR = 'avatar_online.png' self.IDLE_AVATAR = 'avatar_idle.png' - self.open_db(filename) + self.open_db() self.create_tables() + self.close_db() def log_level(self, level): log.setLevel(level) @@ -22,7 +24,9 @@ class DBConnection(object): def now(self): return int(time.time()) - def open_db(self, filename): + def open_db(self, filename = None): + if filename is None: + filename = self.filename self.dbh = sqlite3.connect(filename) self.dbh.row_factory = sqlite3.Row @@ -31,10 +35,14 @@ class DBConnection(object): cursor = self.query('create table if not exists announcements (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, member_id varchar(32) not null, channel_id varchar(32) not null, voice_id varchar(32), sound varchar(128), mention varchar(32), start_date datetime, end_date datetime, interval int, probability float not null default 1.0, last_spoke datetime, message text, digest char(56))') self.dbh.commit() - def close(self): - self.dbh.close() + def close_db(self): + if self.dbh is not None: + self.dbh.close() + self.dbh = None def query(self, sql, params = ()): + if self.dbh is None: + self.open_db() log.debug(sql, params) cursor = self.dbh.cursor() cursor.execute(sql, params) @@ -43,6 +51,7 @@ class DBConnection(object): def get_state(self, client): cursor = self.query('select avatar, idle, last_spoke from state where bot_id=?', [client.user.id]) row = cursor.fetchone() + self.close_db() if row is None: return None return dict(row) @@ -59,6 +68,7 @@ class DBConnection(object): if not cursor.rowcount: cursor = self.query('insert into state (id, bot_id) values (?, ?)', [self.uuid(), client.user.id]) cursor = self.query(sql, params) + self.close_db() self.dbh.commit() def get_all_announcements(self, client, servers = []): @@ -71,31 +81,38 @@ class DBConnection(object): cursor = self.query(sql, params) for row in cursor.fetchall(): yield dict(row) + self.close_db() def get_announcement(self, client, id): cursor = self.query('select * from announcements where bot_id=? and id=?', [client.user.id, id]) row = cursor.fetchone() if row: - return dict(row) + announcement = dict(row) else: - return None + announcement = None + self.close_db() + return annoucement def get_announcements(self, client): now = self.now() cursor = self.query('select id, server_id, channel_id, voice_id, sound, mention, start_date, end_date, interval, probability, last_spoke, message, digest from announcements where (start_date is null or start_date <= ?) and (end_date is null or end_date > ? or end_date=start_date) and (last_spoke is null or last_spoke < ? - interval) and bot_id=?', [now, now, now, client.user.id]) for row in cursor.fetchall(): yield dict(row) + self.close_db() def set_announcement(self, client, id, digest): now = self.now() cursor = self.query('update announcements set last_spoke=?, digest=? where id=?', [now, digest, id]) self.dbh.commit() + self.close_db() def delete_announcement(self, client, id): cursor = self.query('delete from announcements where bot_id=? and id=?', [client.user.id, id]) - if not cursor.rowcount: + if cursor.rowcount: + self.dbh.commit() + ret = True + else: log.error('No such announcement {} for {}'.format(id, client.user.name)) - self.dbh.rollback() - return False - self.dbh.commit() - return True + ret = False + self.close_db() + return ret