redesing of a steel usage completely

This commit is contained in:
filipriec
2025-03-09 23:37:19 +01:00
parent 693ac54544
commit e152a2006b
6 changed files with 38 additions and 152 deletions

View File

@@ -1,92 +1,16 @@
// src/steel/server/data_access.rs
use crate::steel::server::{
rvals::{Custom, SteelVal},
SteelError,
};
use sqlx::{PgPool, Row};
use std::collections::HashMap;
// With this implementation
#[derive(Debug)]
pub struct SteelError(String);
pub struct TableAccess {
db_pool: PgPool,
profile_id: i64,
current_table: String,
local_data: HashMap<String, String>,
}
impl Custom for TableAccess {}
impl TableAccess {
pub fn new(
db_pool: PgPool,
profile_id: i64,
current_table: &str,
local_data: HashMap<String, String>,
) -> Self {
Self {
db_pool,
profile_id,
current_table: current_table.to_string(),
local_data,
}
}
async fn validate_link(&self, table: &str) -> Result<(), SteelError> {
let is_linked: bool = sqlx::query_scalar!(
r#"SELECT EXISTS(
SELECT 1 FROM table_definition_links tdl
JOIN table_definitions src ON tdl.source_table_id = src.id
JOIN table_definitions dst ON tdl.linked_table_id = dst.id
WHERE src.table_name = $1 AND dst.table_name = $2 AND src.profile_id = $3
)"#,
self.current_table,
table,
self.profile_id
)
.fetch_one(&self.db_pool)
.await
.map_err(|e| SteelError::new(e.to_string()))?
.unwrap_or(false);
if !is_linked {
return Err(SteelError::new(format!(
"Table '{}' is not linked to '{}'",
table, self.current_table
)));
}
Ok(())
}
async fn get_external_value(&self, table: &str, column: &str) -> Result<SteelVal, SteelError> {
self.validate_link(table).await?;
let firma = self.local_data.get("firma")
.ok_or_else(|| SteelError::new("Missing 'firma' in local data"))?;
let query = format!(
"SELECT {} FROM \"{}\" WHERE firma = $1 AND profile_id = $2",
column, table
);
let value: Option<String> = sqlx::query(&query)
.bind(firma)
.bind(self.profile_id)
.fetch_optional(&self.db_pool)
.await
.map_err(|e| SteelError::new(e.to_string()))?
.and_then(|row| row.try_get(0).ok());
value
.map(SteelVal::StringV)
.ok_or_else(|| SteelError::new(format!("No value found for {}.{}", table, column)))
impl SteelError {
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}
fn table_access_module(
db_pool: PgPool,
profile_id: i64,
current_table: String,
local_data: HashMap<String, String>,
) -> Result<impl Custom, SteelError> {
Ok(TableAccess::new(db_pool, profile_id, &current_table, local_data))
impl std::fmt::Display for SteelError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

View File

@@ -1,5 +1,4 @@
// src/steel/server/mod.rs
pub mod rvals;
pub mod data_access;
pub mod execution;
pub mod script;
@@ -7,4 +6,3 @@ pub mod script;
pub use data_access::*;
pub use execution::*;
pub use script::*;
pub use rvals::*;

View File

@@ -1,25 +0,0 @@
// src/steel/server/rvals.rs
use std::fmt;
#[derive(Debug)]
pub struct SteelError {
pub message: String,
}
impl SteelError {
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into() }
}
}
impl fmt::Display for SteelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
pub trait Custom {}
pub enum SteelVal {
StringV(String),
// Add other variants as needed
}