post table data validating now steel

This commit is contained in:
filipriec
2025-03-16 17:47:02 +01:00
parent 587f122275
commit 3ecfcfee02
3 changed files with 47 additions and 20 deletions

View File

@@ -28,8 +28,11 @@ pub fn execute_script(
script: String,
target_type: &str,
_db_pool: Arc<PgPool>, // Passed to the SteelContext
context: SteelContext, // Contains row_data, profile_id, etc.
mut context: SteelContext, // Make mutable to inject runtime
) -> Result<Value, ExecutionError> {
// Inject the current runtime handle
context.runtime = tokio::runtime::Handle::current();
let mut vm = Engine::new();
let context = Arc::new(context);

View File

@@ -5,6 +5,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;
use sqlx::Row;
use tokio::runtime::Handle;
#[derive(Debug, Error)]
pub enum FunctionError {
@@ -18,11 +19,13 @@ pub enum FunctionError {
DatabaseError(String),
}
#[derive(Clone)]
pub struct SteelContext {
pub current_table: String,
pub profile_id: i64,
pub row_data: HashMap<String, String>,
pub db_pool: Arc<PgPool>,
pub runtime: Handle, // Add runtime handle
}
impl SteelContext {
@@ -51,20 +54,20 @@ impl SteelContext {
let base_name = table.split_once('_')
.map(|(_, rest)| rest)
.unwrap_or(table);
let fk_column = format!("{}_id", base_name);
let fk_value = self.row_data.get(&fk_column)
.ok_or_else(|| SteelVal::StringV(format!("Foreign key {} not found", fk_column).into()))?;
// Convert to async block for database access
let result = tokio::runtime::Runtime::new().unwrap().block_on(async {
// Use the injected runtime handle
let result = self.runtime.block_on(async {
let actual_table = self.get_related_table_name(base_name).await
.map_err(|e| SteelVal::StringV(e.to_string().into()))?;
sqlx::query_scalar::<_, String>(
&format!("SELECT {} FROM {} WHERE id = $1", column, actual_table)
)
.bind(fk_value.parse::<i64>().map_err(|_|
.bind(fk_value.parse::<i64>().map_err(|_|
SteelVal::StringV("Invalid foreign key format".into()))?)
.fetch_one(&*self.db_pool)
.await
@@ -100,7 +103,7 @@ impl SteelContext {
}
let pool = self.db_pool.clone();
let result = tokio::runtime::Runtime::new().unwrap().block_on(async {
let result = self.runtime.block_on(async {
// Execute and get first column of all rows as strings
let rows = sqlx::query(query)
.fetch_all(&*pool)
@@ -113,10 +116,10 @@ impl SteelContext {
.map_err(|e| SteelVal::StringV(e.to_string().into()))?;
results.push(val);
}
Ok(results.join(","))
});
result.map(|s| SteelVal::StringV(s.into()))
}
}