now working with the gen schema in the database

This commit is contained in:
filipriec
2025-06-02 12:39:23 +02:00
parent 59ed52814e
commit 6b5cbe854b
8 changed files with 229 additions and 75 deletions

View File

@@ -3,6 +3,7 @@ use tonic::Status;
use sqlx::{PgPool, Row};
use std::collections::HashMap;
use common::proto::multieko2::tables_data::{GetTableDataRequest, GetTableDataResponse};
use crate::shared::schema_qualifier::qualify_table_name_for_data; // Import schema qualifier
pub async fn get_table_data(
db_pool: &PgPool,
@@ -69,20 +70,36 @@ pub async fn get_table_data(
.collect::<Vec<_>>()
.join(", ");
// Qualify table name with schema
let qualified_table = qualify_table_name_for_data(&table_name)?;
let sql = format!(
"SELECT {} FROM \"{}\" WHERE id = $1 AND deleted = false",
columns_clause, table_name
"SELECT {} FROM {} WHERE id = $1 AND deleted = false",
columns_clause, qualified_table
);
// Execute query
let row = sqlx::query(&sql)
// Execute query with enhanced error handling
let row_result = sqlx::query(&sql)
.bind(record_id)
.fetch_one(db_pool)
.await
.map_err(|e| match e {
sqlx::Error::RowNotFound => Status::not_found("Record not found"),
_ => Status::internal(format!("Database error: {}", e)),
})?;
.await;
let row = match row_result {
Ok(row) => row,
Err(sqlx::Error::RowNotFound) => return Err(Status::not_found("Record not found")),
Err(e) => {
// Handle "relation does not exist" error specifically
if let Some(db_err) = e.as_database_error() {
if db_err.code() == Some(std::borrow::Cow::Borrowed("42P01")) {
return Err(Status::internal(format!(
"Table '{}' is defined but does not physically exist in the database as {}",
table_name, qualified_table
)));
}
}
return Err(Status::internal(format!("Database error: {}", e)));
}
};
// Build response data
let mut data = HashMap::new();