From 573b3e8a347aa6cc36fa3fe89788d9730868bb3b Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 18 Nov 2016 17:28:07 +0000 Subject: [PATCH] Added commit() and rollback(). --- db.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/db.py b/db.py index 5dd09d1..c62ec29 100644 --- a/db.py +++ b/db.py @@ -40,7 +40,7 @@ class DBConnection(object): cursor = self.query('create table if not exists admins (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, user_id varchar(32))') cursor = self.query('create unique index if not exists admins_id on admins (id)') cursor = self.query('create index if not exists admins_bot_id on admins (bot_id)') - self.dbh.commit() + self.commit() self.close_db() def close_db(self): @@ -56,6 +56,12 @@ class DBConnection(object): cursor.execute(sql, params) return cursor + def commit(self): + return self.dbh.commit() + + def rollback(self): + return self.dbh.rollback() + def get_state(self, client): cursor = self.query('select avatar, idle, last_spoke from state where bot_id=?', [client.user.id]) row = cursor.fetchone() @@ -76,7 +82,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.dbh.commit() + self.commit() self.close_db() def get_all_from_table(self, client, table, servers = []): @@ -107,7 +113,7 @@ class DBConnection(object): def delete_from_table(self, client, table, id): cursor = self.query('delete from {} where bot_id=? and id=?'.format(table), [client.user.id, id]) if cursor.rowcount: - self.dbh.commit() + self.commit() ret = True else: log.error('No such entry {} in {} table for {}'.format(id, table, client.user.name)) @@ -126,11 +132,11 @@ class DBConnection(object): sql = 'insert into {} (id, bot_id, {}) values (?, ?, {})'.format(table, ', '.join(values), ', '.join(['?'] * len(values))) cursor = self.query(sql, params) if cursor.rowcount: - self.dbh.commit() + self.commit() ret = id else: log.error('Failed to create entry {} in table {}: {}'.format(id, table, args)) - self.dbh.rollback() + self.rollback() ret = None self.close_db() return ret @@ -146,11 +152,11 @@ class DBConnection(object): sql = 'update {} set {} where bot_id=? and id=?'.format(table, ', '.join(['{}=?'.format(v) for v in values])) cursor = self.query(sql, params) if cursor.rowcount: - self.dbh.commit() + self.commit() ret = True else: log.error('No such entry {} in {} table for {}'.format(id, table, client.user.name)) - self.dbh.rollback() + self.rollback() ret = False self.close_db() return ret -- 2.7.4