43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from typing import Optional
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.engine import Engine
|
|
|
|
from actions.config.config import Config
|
|
|
|
|
|
class Firebird:
|
|
_engine: Optional[Engine] = None
|
|
|
|
@classmethod
|
|
def get_engine(cls) -> Engine:
|
|
# Obtem as configurações do arquivo JSON
|
|
database = Config.get('database/firebird.json')
|
|
|
|
# Cria a engine apenas uma vez
|
|
if cls._engine is None:
|
|
|
|
# DSN para o SQLAlchemy usando firebird-driver
|
|
dsn = (
|
|
f"firebird+firebird://{database.user}:"
|
|
f"{database.password}@"
|
|
f"{database.host}:"
|
|
f"{database.port}/"
|
|
f"{database.name}"
|
|
)
|
|
|
|
# Criação da engine SQLAlchemy
|
|
cls._engine = create_engine(
|
|
dsn,
|
|
connect_args={"charset": database.charset},
|
|
pool_pre_ping=bool(database.pool.pre_ping),
|
|
pool_size=database.pool.size,
|
|
max_overflow=database.pool.max_overflow,
|
|
)
|
|
|
|
return cls._engine
|
|
|
|
@classmethod
|
|
def dispose(cls):
|
|
if cls._engine:
|
|
cls._engine.dispose()
|
|
cls._engine = None
|