athena_rs 3.26.4

Hyper performant polyglot Database driver
Documentation
//! `/schema/constraints` response orchestration helpers.
//!
//! This module composes validated constraint-query input with loader rows and
//! response payload 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_query::load_schema_constraint_rows;
use super::super::presentation::schema_constraints as build_schema_constraints_payload;
use super::super::request_params::NormalizedConstraintQuery;
use super::super::schema_backend::SchemaBackend;

/// Builds a `/schema/constraints` response for validated table/schema query input.
pub(in super::super) async fn schema_constraints_response(
    backend: &SchemaBackend,
    app_state: &crate::AppState,
    query: NormalizedConstraintQuery,
) -> HttpResponse {
    let rows = match backend {
        SchemaBackend::Postgres { pool } => match load_schema_constraint_rows(pool, &query).await {
            Ok(rows) => rows,
            Err(err) => return schema_internal_fetch_error("constraints", &err),
        },
        SchemaBackend::D1 { connection_info } => {
            match super::super::schema_d1::load_schema_constraint_rows(
                app_state,
                connection_info,
                query.table_name.as_str(),
            )
            .await
            {
                Ok(rows) => rows,
                Err(resp) => return resp,
            }
        }
    };

    HttpResponse::Ok().json(json!({ "constraints": build_schema_constraints_payload(rows) }))
}