steel script working perfectly well
This commit is contained in:
@@ -28,11 +28,8 @@ pub fn execute_script(
|
||||
script: String,
|
||||
target_type: &str,
|
||||
_db_pool: Arc<PgPool>, // Passed to the SteelContext
|
||||
mut context: SteelContext, // Make mutable to inject runtime
|
||||
context: SteelContext,
|
||||
) -> Result<Value, ExecutionError> {
|
||||
// Inject the current runtime handle
|
||||
context.runtime = tokio::runtime::Handle::current();
|
||||
|
||||
let mut vm = Engine::new();
|
||||
let context = Arc::new(context);
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use thiserror::Error;
|
||||
use sqlx::Row;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FunctionError {
|
||||
@@ -25,7 +24,6 @@ pub struct SteelContext {
|
||||
pub profile_id: i64,
|
||||
pub row_data: HashMap<String, String>,
|
||||
pub db_pool: Arc<PgPool>,
|
||||
pub runtime: Handle, // Add runtime handle
|
||||
}
|
||||
|
||||
impl SteelContext {
|
||||
@@ -59,19 +57,23 @@ impl SteelContext {
|
||||
let fk_value = self.row_data.get(&fk_column)
|
||||
.ok_or_else(|| SteelVal::StringV(format!("Foreign key {} not found", fk_column).into()))?;
|
||||
|
||||
// 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()))?;
|
||||
// Use `tokio::task::block_in_place` to safely block the thread
|
||||
let result = tokio::task::block_in_place(|| {
|
||||
let handle = tokio::runtime::Handle::current();
|
||||
handle.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(|_|
|
||||
SteelVal::StringV("Invalid foreign key format".into()))?)
|
||||
.fetch_one(&*self.db_pool)
|
||||
.await
|
||||
.map_err(|e| SteelVal::StringV(e.to_string().into()))
|
||||
// Add quotes around the table name
|
||||
sqlx::query_scalar::<_, String>(
|
||||
&format!("SELECT {} FROM \"{}\" WHERE id = $1", column, actual_table)
|
||||
)
|
||||
.bind(fk_value.parse::<i64>().map_err(|_|
|
||||
SteelVal::StringV("Invalid foreign key format".into()))?)
|
||||
.fetch_one(&*self.db_pool)
|
||||
.await
|
||||
.map_err(|e| SteelVal::StringV(e.to_string().into()))
|
||||
})
|
||||
});
|
||||
|
||||
result.map(|v| SteelVal::StringV(v.into()))
|
||||
@@ -103,21 +105,26 @@ impl SteelContext {
|
||||
}
|
||||
|
||||
let pool = self.db_pool.clone();
|
||||
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)
|
||||
.await
|
||||
.map_err(|e| SteelVal::StringV(e.to_string().into()))?;
|
||||
|
||||
let mut results = Vec::new();
|
||||
for row in rows {
|
||||
let val: String = row.try_get(0)
|
||||
// Use `tokio::task::block_in_place` to safely block the thread
|
||||
let result = tokio::task::block_in_place(|| {
|
||||
let handle = tokio::runtime::Handle::current();
|
||||
handle.block_on(async {
|
||||
// Execute and get first column of all rows as strings
|
||||
let rows = sqlx::query(query)
|
||||
.fetch_all(&*pool)
|
||||
.await
|
||||
.map_err(|e| SteelVal::StringV(e.to_string().into()))?;
|
||||
results.push(val);
|
||||
}
|
||||
|
||||
Ok(results.join(","))
|
||||
let mut results = Vec::new();
|
||||
for row in rows {
|
||||
let val: String = row.try_get(0)
|
||||
.map_err(|e| SteelVal::StringV(e.to_string().into()))?;
|
||||
results.push(val);
|
||||
}
|
||||
|
||||
Ok(results.join(","))
|
||||
})
|
||||
});
|
||||
|
||||
result.map(|s| SteelVal::StringV(s.into()))
|
||||
|
||||
Reference in New Issue
Block a user