import mariadb
import sys
from configparser import ConfigParser as CP
class DBConnection():
    
    def __init__(self, conf:CP) :
        self.name:str = conf['db']['name']
        self.user:str = conf['db']['user']
        self.password:str = conf['db']['password']
        self.host:str = conf['db']['host']
        try : 
            self.port:int = int(conf['db']['port'])
        except:
            msg = f"Le numéro de port indiqué dans le fichier de configuration  ne semble pas être un nombre entier: {conf['db']['port']}. Merci de corriger et de relancer le programme."
            print(msg)
            sys.exit(1)
        
        self.connect_mariadb()


    def connect_mariadb(self):
        try:
            self.conn = mariadb.connect(
                user=self.user,
                password=self.password,
                host=self.host,
                port=self.port,
                database=self.name

            )
        except mariadb.Error as e:
            print(f"Error connecting to MariaDB Platform: {e}")
            sys.exit(1)


    def get_conn(self):
        return self.conn
    

    def close(self):
        self.conn.close()