saas_api/database/firebird.py

43 lines
1.4 KiB
Python

from typing import Optional
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from actions.env.env_config_loader import EnvConfigLoader
class Firebird:
_engine: Optional[Engine] = None
@classmethod
def get_engine(cls) -> Engine:
# Instancia o loader com o prefixo correto
env_database = EnvConfigLoader(".env")
# Cria a engine apenas uma vez
if cls._engine is None:
# DSN para o SQLAlchemy usando firebird-driver
dsn = (
f"firebird+firebird://{env_database.ORIUS_API_FDB_USER}:"
f"{env_database.ORIUS_API_FDB_PASSWORD}@"
f"{env_database.ORIUS_API_FDB_HOST}:"
f"{env_database.ORIUS_API_FDB_PORT}/"
f"{env_database.ORIUS_API_FDB_NAME}"
)
# Criação da engine SQLAlchemy
cls._engine = create_engine(
dsn,
connect_args={"charset": env_database.ORIUS_API_FDB_CHARSET},
pool_pre_ping=bool(env_database.ORIUS_API_FDB_POOL_PRE_PING),
pool_size=int(env_database.ORIUS_API_FDB_POOL_SIZE),
max_overflow=int(env_database.ORIUS_API_FDB_POOL_MAX_OVERFLOW),
)
return cls._engine
@classmethod
def dispose(cls):
if cls._engine:
cls._engine.dispose()
cls._engine = None