now working with the gen schema in the database
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user