from sqlite3 import Connection

from messageprocessor import MessageProcessorCallback


class SqliteConnectionTransactionCallback(MessageProcessorCallback):
    def __init__(self, conn: Connection):
        self.conn = conn

    def process_begun(self, event_name, request_id, **kwargs):
        return

    def input_parsed(self, event_name, request_id, input_obj):
        return

    def business_called(self, event_name, request_id, input_obj, output_obj):
        self.conn.commit()

    def process_finished(self, event_name, request_id, input_obj, output_obj):
        return

    def parse_exception(self, event_name, request_id, data, exception):
        return

    def unhandled_exception(self, event_name, request_id, input_obj, exception):
        self.conn.rollback()
