compiled, needs further progress

This commit is contained in:
filipriec
2025-03-08 12:49:54 +01:00
parent 9a87940311
commit 6281702575
4 changed files with 133 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ use sqlx::{PgPool, Arguments};
use sqlx::postgres::PgArguments;
use chrono::{DateTime, Utc};
use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse};
use crate::steel::handlers::execution::{self, ScriptOperation};
use std::collections::HashMap;
pub async fn post_table_data(
@@ -100,6 +101,56 @@ pub async fn post_table_data(
}
}
// Validate Steel scripts
let scripts = sqlx::query!(
"SELECT target_column, script FROM table_scripts WHERE table_definitions_id = $1",
table_def.id
)
.fetch_all(db_pool)
.await
.map_err(|e| Status::internal(format!("Failed to fetch scripts: {}", e)))?;
for script_record in scripts {
let target_column = script_record.target_column;
// Check if target column is present in data
if !data.contains_key(&target_column) {
return Err(Status::invalid_argument(
format!("Column '{}' is required due to an associated script", target_column)
));
}
// Parse the script
let operation = execution::parse_script(&script_record.script, &target_column)
.map_err(|e| Status::invalid_argument(e.to_string()))?;
// Get source column from operation
let source_column = match operation {
ScriptOperation::SetToColumn { source } => source,
};
// Check source column presence
let source_value = data.get(&source_column)
.ok_or_else(|| Status::invalid_argument(
format!("Source column '{}' required by script for '{}' is missing", source_column, target_column)
))?;
// Get target value
let target_value = data.get(&target_column)
.ok_or_else(|| Status::invalid_argument(
format!("Target column '{}' is missing in data", target_column)
))?;
// Validate value match
if target_value != source_value {
return Err(Status::invalid_argument(
format!("Value for '{}' must match '{}' as per script. Expected '{}', got '{}'",
target_column, source_column, source_value, target_value)
));
}
}
// Prepare SQL parameters
let mut params = PgArguments::default();
let mut columns_list = Vec::new();