# database manager functions to interact with database
from sqlmodel.main import SQLModel
from sqlmodel import Session

from app.src.db.engine import get_db, get_engine
from app.src.logger import logger


# create table
def create_table() -> bool:
    # Database Engine
    db_engine = get_db()
    try:
        # for table in objects:
        #     new_table = table
        #     logger.debug(f"Creating new table: {new_table}")
        SQLModel.metadata.create_all(db_engine)
        return True
    except Exception as message:
        logger.error(
            "Impossible to create the table for the object: {new_table.__class__.__name__}"
        )
        logger.exception(message)
        raise Exception(message)


# Destroy metadata table
def destroy_metadata_table() -> bool:
    # Database Engine
    db_engine = get_db()
    try:
        # for table in objects:
        #     new_table = table
        #     logger.debug(f"Creating new table: {new_table}")
        SQLModel.metadata.drop_all(db_engine)
        return True
    except Exception as message:
        logger.error(
            "Impossible to create the table for the object: {new_table.__class__.__name__}"
        )
        logger.exception(message)
        raise Exception(message)


# check table
def check_table():
    pass


# insert new table with data
def insert_data(your_object: object) -> bool:
    try:
        engine = get_engine()
        with Session(engine) as session:
            session.add(your_object)
            session.commit()
        return True
    except Exception as message:
        logger.error("Impossible to create the table for the object")
        logger.exception(message)
        raise Exception(message)


# refresh the schema and data
def refresh_data(your_object: object) -> bool:

    try:
        engine = get_engine()
        with Session(engine) as session:
            session.refresh(your_object)
        return True
    except Exception as message:
        logger.error(
            "Impossible to refresh the object: {your_object.__class__.__name__}"
        )
        logger.exception(message)
        raise Exception(message)


# get schema
# get info (select)
# get all (select *)
# update single row
# delete single row

app.src.db.engine.get_db sqlmodel.Session sqlmodel.main.SQLModel.metadata.create_all app.src.logger.logger.exception app.src.logger.logger.error sqlmodel.main.SQLModel.metadata.drop_all app.src.db.engine.get_engine
[((286, 294), 'app.src.db.engine.get_db', 'get_db', ([], {}), '()\n', (292, 294), False, 'from app.src.db.engine import get_db, get_engine\n'), ((826, 834), 'app.src.db.engine.get_db', 'get_db', ([], {}), '()\n', (832, 834), False, 'from app.src.db.engine import get_db, get_engine\n'), ((439, 478), 'sqlmodel.main.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['db_engine'], {}), '(db_engine)\n', (467, 478), False, 'from sqlmodel.main import SQLModel\n'), ((979, 1016), 'sqlmodel.main.SQLModel.metadata.drop_all', 'SQLModel.metadata.drop_all', (['db_engine'], {}), '(db_engine)\n', (1005, 1016), False, 'from sqlmodel.main import SQLModel\n'), ((1408, 1420), 'app.src.db.engine.get_engine', 'get_engine', ([], {}), '()\n', (1418, 1420), False, 'from app.src.db.engine import get_db, get_engine\n'), ((1824, 1836), 'app.src.db.engine.get_engine', 'get_engine', ([], {}), '()\n', (1834, 1836), False, 'from app.src.db.engine import get_db, get_engine\n'), ((540, 643), 'app.src.logger.logger.error', 'logger.error', (['"""Impossible to create the table for the object: {new_table.__class__.__name__}"""'], {}), "(\n    'Impossible to create the table for the object: {new_table.__class__.__name__}'\n    )\n", (552, 643), False, 'from app.src.logger import logger\n'), ((664, 689), 'app.src.logger.logger.exception', 'logger.exception', (['message'], {}), '(message)\n', (680, 689), False, 'from app.src.logger import logger\n'), ((1078, 1181), 'app.src.logger.logger.error', 'logger.error', (['"""Impossible to create the table for the object: {new_table.__class__.__name__}"""'], {}), "(\n    'Impossible to create the table for the object: {new_table.__class__.__name__}'\n    )\n", (1090, 1181), False, 'from app.src.logger import logger\n'), ((1202, 1227), 'app.src.logger.logger.exception', 'logger.exception', (['message'], {}), '(message)\n', (1218, 1227), False, 'from app.src.logger import logger\n'), ((1434, 1449), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1441, 1449), False, 'from sqlmodel import Session\n'), ((1589, 1650), 'app.src.logger.logger.error', 'logger.error', (['"""Impossible to create the table for the object"""'], {}), "('Impossible to create the table for the object')\n", (1601, 1650), False, 'from app.src.logger import logger\n'), ((1659, 1684), 'app.src.logger.logger.exception', 'logger.exception', (['message'], {}), '(message)\n', (1675, 1684), False, 'from app.src.logger import logger\n'), ((1850, 1865), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1857, 1865), False, 'from sqlmodel import Session\n'), ((1980, 2067), 'app.src.logger.logger.error', 'logger.error', (['"""Impossible to refresh the object: {your_object.__class__.__name__}"""'], {}), "(\n    'Impossible to refresh the object: {your_object.__class__.__name__}')\n", (1992, 2067), False, 'from app.src.logger import logger\n'), ((2093, 2118), 'app.src.logger.logger.exception', 'logger.exception', (['message'], {}), '(message)\n', (2109, 2118), False, 'from app.src.logger import logger\n')]