athena_rs 3.26.3

Hyper performant polyglot Database driver
//! `/schema/tables` response orchestration helpers.
//!
//! This module composes table loader rows with presentation/error mapping.

use actix_web::HttpResponse;
use serde_json::json;
use sqlx::{Pool, Postgres};

use super::super::catalog_errors::schema_internal_fetch_error;
use super::super::catalog_service_loader::load_schema_table_rows;
use super::super::presentation::schema_tables as build_schema_tables_payload;
use super::super::schema_backend::SchemaBackend;

/// Builds a `/schema/tables` response for a resolved schema client pool.
pub(in super::super) async fn schema_tables_response(
    backend: &SchemaBackend,
    app_state: &crate::AppState,
) -> HttpResponse {
    let relations = match backend {
        SchemaBackend::Postgres { pool } => match load_schema_table_rows(pool).await {
            Ok(rows) => rows,
            Err(err) => return schema_internal_fetch_error("tables", &err),
        },
        SchemaBackend::D1 { connection_info } => {
            match super::super::schema_d1::load_schema_table_rows(
                app_state,
                connection_info,
                "public",
            )
            .await
            {
                Ok(rows) => rows,
                Err(resp) => return resp,
            }
        }
    };

    HttpResponse::Ok().json(json!({ "tables": build_schema_tables_payload(relations) }))
}