from crossmod.environments.consts import CrossmodConsts
from crossmod.db.base import Base
from crossmod.db.tables import DataTable
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import scoped_session
import datetime


class CrossmodDB:
    def __init__(self, database_uri = 'sqlite:///' + CrossmodConsts.DB_PATH):
        self.database_uri = database_uri
        # Connection timeout parameter for SQLite: https://stackoverflow.com/questions/15065037/how-to-increase-connection-timeout-using-sqlalchemy-with-sqlite-in-python
        self.database = create_engine(self.database_uri, connect_args={'timeout': 15})
        Base.metadata.bind = self.database
        Base.metadata.create_all(self.database)
        self.database_session = scoped_session(sessionmaker(bind = self.database))
        
    def write(self, table_type, commit = True, **kwargs):
        try:
            table_data_entry = table_type(**kwargs)
            self.database_session.add(table_data_entry)
            
            if commit:
                self.database_session.commit()
        except Exception as e:
            print("Could not write data:")
            print(e)
            return