from dataclasses import dataclass
from modules.connector import connection

#-Context Manager Class
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
class DatabaseConnectionManager:
    def __init__(self, db_target=None):
        self.conn = None
        self.db_target = db_target

    def __enter__(self):
        # Contract: return a valid sqlite3.Connection, or let the original
        # connection exception propagate. Never swallow it and bind None --
        # that turned a DB failure into a misleading AttributeError at the
        # call site and lost the sqlite cause.
        self.conn = connection(self.db_target)
        return self.conn

    def __exit__(self, exc_type, exc_value, exc_traceback):
        if self.conn:
            self.conn.close()
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
