server table structure response is now generalized
This commit is contained in:
@@ -1,181 +1,134 @@
|
||||
// src/table_structure/handlers/table_structure.rs
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use common::proto::multieko2::{
|
||||
table_structure::{TableStructureResponse, TableColumn},
|
||||
common::Empty
|
||||
use common::proto::multieko2::table_structure::{
|
||||
GetTableStructureRequest, TableColumn, TableStructureResponse,
|
||||
};
|
||||
use sqlx::{PgPool, Row};
|
||||
use tonic::Status;
|
||||
|
||||
pub async fn get_adresar_table_structure(
|
||||
_db_pool: &PgPool,
|
||||
_request: Empty,
|
||||
) -> Result<TableStructureResponse, Status> {
|
||||
let columns = vec![
|
||||
TableColumn {
|
||||
name: "firma".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "kz".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "drc".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "ulica".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "psc".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "mesto".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "stat".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "banka".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "ucet".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "skladm".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "ico".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "kontakt".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "telefon".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "skladu".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "fax".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
];
|
||||
Ok(TableStructureResponse { columns })
|
||||
// Helper struct to map query results
|
||||
#[derive(sqlx::FromRow, Debug)]
|
||||
struct DbColumnInfo {
|
||||
column_name: String,
|
||||
formatted_data_type: String,
|
||||
is_nullable: bool,
|
||||
is_primary_key: bool,
|
||||
}
|
||||
|
||||
pub async fn get_uctovnictvo_table_structure(
|
||||
_db_pool: &PgPool,
|
||||
_request: Empty,
|
||||
pub async fn get_table_structure(
|
||||
db_pool: &PgPool,
|
||||
request: GetTableStructureRequest,
|
||||
) -> Result<TableStructureResponse, Status> {
|
||||
let columns = vec![
|
||||
TableColumn {
|
||||
name: "adresar_id".to_string(),
|
||||
data_type: "BIGINT".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "c_dokladu".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "datum".to_string(),
|
||||
data_type: "DATE".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "c_faktury".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "obsah".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "stredisko".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "c_uctu".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "md".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "identif".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "poznanka".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
},
|
||||
TableColumn {
|
||||
name: "firma".to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: false,
|
||||
},
|
||||
];
|
||||
let profile_name = request.profile_name;
|
||||
let table_name = request.table_name; // This should be the full table name, e.g., "2025_adresar6"
|
||||
let table_schema = "public"; // Assuming tables are in the 'public' schema
|
||||
|
||||
// 1. Validate Profile
|
||||
let profile = sqlx::query!(
|
||||
"SELECT id FROM profiles WHERE name = $1",
|
||||
profile_name
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
Status::internal(format!(
|
||||
"Failed to query profile '{}': {}",
|
||||
profile_name, e
|
||||
))
|
||||
})?;
|
||||
|
||||
let profile_id = match profile {
|
||||
Some(p) => p.id,
|
||||
None => {
|
||||
return Err(Status::not_found(format!(
|
||||
"Profile '{}' not found",
|
||||
profile_name
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
// 2. Validate Table within Profile
|
||||
sqlx::query!(
|
||||
"SELECT id FROM table_definitions WHERE profile_id = $1 AND table_name = $2",
|
||||
profile_id,
|
||||
table_name
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Failed to query table_definitions: {}", e)))?
|
||||
.ok_or_else(|| Status::not_found(format!(
|
||||
"Table '{}' not found in profile '{}'",
|
||||
table_name,
|
||||
profile_name
|
||||
)))?;
|
||||
|
||||
// 3. Query information_schema for column details
|
||||
let query_str = r#"
|
||||
SELECT
|
||||
c.column_name,
|
||||
CASE
|
||||
WHEN c.udt_name = 'varchar' AND c.character_maximum_length IS NOT NULL THEN
|
||||
'VARCHAR(' || c.character_maximum_length || ')'
|
||||
WHEN c.udt_name = 'bpchar' AND c.character_maximum_length IS NOT NULL THEN
|
||||
'CHAR(' || c.character_maximum_length || ')'
|
||||
WHEN c.udt_name = 'numeric' AND c.numeric_precision IS NOT NULL AND c.numeric_scale IS NOT NULL THEN
|
||||
'NUMERIC(' || c.numeric_precision || ',' || c.numeric_scale || ')'
|
||||
WHEN c.udt_name = 'numeric' AND c.numeric_precision IS NOT NULL THEN
|
||||
'NUMERIC(' || c.numeric_precision || ')'
|
||||
WHEN STARTS_WITH(c.udt_name, '_') THEN
|
||||
UPPER(SUBSTRING(c.udt_name FROM 2)) || '[]'
|
||||
ELSE
|
||||
UPPER(c.udt_name)
|
||||
END AS formatted_data_type,
|
||||
c.is_nullable = 'YES' AS is_nullable,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.key_column_usage kcu
|
||||
JOIN information_schema.table_constraints tc
|
||||
ON kcu.constraint_name = tc.constraint_name
|
||||
AND kcu.table_schema = tc.table_schema
|
||||
AND kcu.table_name = tc.table_name
|
||||
WHERE tc.table_schema = c.table_schema
|
||||
AND tc.table_name = c.table_name
|
||||
AND tc.constraint_type = 'PRIMARY KEY'
|
||||
AND kcu.column_name = c.column_name
|
||||
) AS is_primary_key
|
||||
FROM
|
||||
information_schema.columns c
|
||||
WHERE
|
||||
c.table_schema = $1
|
||||
AND c.table_name = $2
|
||||
ORDER BY
|
||||
c.ordinal_position;
|
||||
"#;
|
||||
|
||||
let db_columns = sqlx::query_as::<_, DbColumnInfo>(query_str)
|
||||
.bind(table_schema)
|
||||
.bind(&table_name) // Use the validated table_name
|
||||
.fetch_all(db_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
Status::internal(format!(
|
||||
"Failed to query column information for table '{}': {}",
|
||||
table_name, e
|
||||
))
|
||||
})?;
|
||||
|
||||
if db_columns.is_empty() {
|
||||
// This could mean the table exists in table_definitions but not in information_schema,
|
||||
// or it has no columns. The latter is unlikely for a created table.
|
||||
// Depending on desired behavior, you could return an error or an empty list.
|
||||
// For now, returning an empty list if the table was validated.
|
||||
}
|
||||
|
||||
let columns = db_columns
|
||||
.into_iter()
|
||||
.map(|db_col| TableColumn {
|
||||
name: db_col.column_name,
|
||||
data_type: db_col.formatted_data_type,
|
||||
is_nullable: db_col.is_nullable,
|
||||
is_primary_key: db_col.is_primary_key,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(TableStructureResponse { columns })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user