this miracle compiled, idk if it even does what i want but should run custom sql queries from steel script. Da heck if this works, my gosh
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
// src/steel/server/execution.rs
|
||||
use steel::steel_vm::engine::Engine;
|
||||
use steel::steel_vm::register_fn::RegisterFn;
|
||||
use steel::rvals::SteelVal;
|
||||
use thiserror::Error;
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ExecutionError {
|
||||
@@ -21,9 +26,68 @@ pub enum Value {
|
||||
pub fn execute_script(
|
||||
script: String,
|
||||
target_type: &str,
|
||||
current_data: HashMap<String, String>,
|
||||
) -> Result<Value, ExecutionError> {
|
||||
let mut vm = Engine::new();
|
||||
|
||||
let data = current_data.clone();
|
||||
vm.register_fn("get_current_column", move |column: String| {
|
||||
data.get(&column)
|
||||
.map(|s| SteelVal::StringV(s.clone().into()))
|
||||
.ok_or_else(|| SteelVal::StringV(format!("Column {} not found", column).into()))
|
||||
});
|
||||
|
||||
let results = vm.compile_and_run_raw_program(script)
|
||||
.map_err(|e| ExecutionError::RuntimeError(e.to_string()))?;
|
||||
|
||||
let last_result = results.last()
|
||||
.ok_or_else(|| ExecutionError::TypeConversionError("Script returned no values".to_string()))?;
|
||||
|
||||
match target_type {
|
||||
"STRING" => {
|
||||
if let SteelVal::StringV(s) = last_result {
|
||||
Ok(Value::String(s.to_string()))
|
||||
} else {
|
||||
Err(ExecutionError::TypeConversionError(
|
||||
format!("Expected string, got {:?}", last_result)
|
||||
))
|
||||
}
|
||||
}
|
||||
_ => Err(ExecutionError::UnsupportedType(target_type.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute_script_with_sql(
|
||||
script: String,
|
||||
target_type: &str,
|
||||
db_pool: Arc<PgPool>,
|
||||
current_data: HashMap<String, String>,
|
||||
) -> Result<Value, ExecutionError> {
|
||||
let mut vm = Engine::new();
|
||||
let handle = Handle::current();
|
||||
|
||||
let data = current_data.clone();
|
||||
vm.register_fn("get_current_column", move |column: String| {
|
||||
data.get(&column)
|
||||
.map(|s| SteelVal::StringV(s.clone().into()))
|
||||
.ok_or_else(|| SteelVal::StringV(format!("Column {} not found", column).into()))
|
||||
});
|
||||
|
||||
let pool = db_pool.clone();
|
||||
vm.register_fn("query_sql_from_steel", move |query: String| {
|
||||
let pool = pool.clone();
|
||||
let query = query.clone();
|
||||
let handle = handle.clone();
|
||||
|
||||
handle.block_on(async move {
|
||||
sqlx::query_scalar::<_, i64>(&query)
|
||||
.fetch_one(&*pool)
|
||||
.await
|
||||
.map(|v| SteelVal::IntV(v as isize))
|
||||
.map_err(|e| SteelVal::StringV(format!("SQL error: {}", e).into()))
|
||||
})
|
||||
});
|
||||
|
||||
let results = vm.compile_and_run_raw_program(script)
|
||||
.map_err(|e| ExecutionError::RuntimeError(e.to_string()))?;
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// src/steel/server/mod.rs
|
||||
pub mod execution;
|
||||
pub mod syntax_parser;
|
||||
|
||||
pub use execution::*;
|
||||
pub use syntax_parser::*;
|
||||
|
||||
65
server/src/steel/server/syntax_parser.rs
Normal file
65
server/src/steel/server/syntax_parser.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
// src/steel/server/syntax_parser.rs
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub struct SyntaxParser {
|
||||
column_access_re: Regex,
|
||||
relationship_re: Regex,
|
||||
indexed_access_re: Regex,
|
||||
sql_integration_re: Regex,
|
||||
}
|
||||
|
||||
impl SyntaxParser {
|
||||
pub fn new() -> Self {
|
||||
SyntaxParser {
|
||||
column_access_re: Regex::new(r"@(\w+)").unwrap(),
|
||||
relationship_re: Regex::new(r"@(\w+)\.(\w+)").unwrap(),
|
||||
indexed_access_re: Regex::new(r"@(\w+)\[(\d+)\]\.(\w+)").unwrap(),
|
||||
sql_integration_re: Regex::new(r#"@sql\((["'])(.*?)\1\)"#).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(&self, script: &str) -> String {
|
||||
let mut transformed = script.to_string();
|
||||
|
||||
// Process indexed access first to avoid overlap with relationship matches
|
||||
transformed = self.indexed_access_re.replace_all(&transformed, |caps: ®ex::Captures| {
|
||||
format!("(get_related_index \"{}\" {} \"{}\")",
|
||||
&caps[1], &caps[2], &caps[3])
|
||||
}).to_string();
|
||||
|
||||
// Process relationships
|
||||
transformed = self.relationship_re.replace_all(&transformed, |caps: ®ex::Captures| {
|
||||
format!("(get_related_value \"{}\" \"{}\")", &caps[1], &caps[2])
|
||||
}).to_string();
|
||||
|
||||
// Process basic column access
|
||||
transformed = self.column_access_re.replace_all(&transformed, |caps: ®ex::Captures| {
|
||||
format!("(get_current_column \"{}\")", &caps[1])
|
||||
}).to_string();
|
||||
|
||||
// Process SQL integration
|
||||
transformed = self.sql_integration_re.replace_all(&transformed, |caps: ®ex::Captures| {
|
||||
format!("(query_sql_from_steel \"{}\")", &caps[2])
|
||||
}).to_string();
|
||||
|
||||
transformed
|
||||
}
|
||||
|
||||
pub fn extract_dependencies(&self, script: &str) -> (HashSet<String>, HashSet<String>) {
|
||||
let mut tables = HashSet::new();
|
||||
let mut columns = HashSet::new();
|
||||
|
||||
for cap in self.relationship_re.captures_iter(script) {
|
||||
tables.insert(cap[1].to_string());
|
||||
columns.insert(cap[2].to_string());
|
||||
}
|
||||
|
||||
for cap in self.indexed_access_re.captures_iter(script) {
|
||||
tables.insert(cap[1].to_string());
|
||||
columns.insert(cap[3].to_string());
|
||||
}
|
||||
|
||||
(tables, columns)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user