# Infrastruttura Async / DB

## Scopo

Questo diagramma descrive il flusso comune usato da tutti i moduli GUI quando
eseguono una query sul database.

## Flusso trasversale

```{mermaid}
flowchart TD
    A["Evento UI (click / selezione / ricerca)"] --> B["Metodo finestra"]
    B --> C["AsyncRunner.run(awaitable)"]
    C --> D["Coroutines sul loop globale"]
    D --> E["AsyncMSSQLClient.query_json() / exec()"]
    E --> F["SQL Server"]
    F --> G["Risultato query"]
    G --> H["Future completata"]
    H --> I["Callback _ok / _err su thread Tk"]
    I --> J["Aggiornamento widget"]
```

## Relazioni principali

```{mermaid}
flowchart LR
    Main["main.py"] --> Loop["get_global_loop()"]
    Main --> DB["AsyncMSSQLClient"]
    Windows["Moduli GUI"] --> Runner["AsyncRunner"]
    Runner --> Loop
    Runner --> DB
    DB --> SQL["SQL Server Mediseawall"]
```

## Note

- Il loop asincrono è condiviso tra tutte le finestre.
- Il client DB è condiviso e creato una sola volta nel launcher.
- I callback che aggiornano la UI rientrano sempre sul thread Tk.
