redesing of a steel usage completely
This commit is contained in:
@@ -2,19 +2,15 @@
|
||||
use tonic::Status;
|
||||
use sqlx::{PgPool, Error as SqlxError};
|
||||
use common::proto::multieko2::table_script::{PostTableScriptRequest, TableScriptResponse};
|
||||
use crate::steel::server::script::validate_script;
|
||||
use serde_json::Value;
|
||||
|
||||
// Add these imports for the execution module and ScriptOperation
|
||||
use crate::steel::server::execution::{self, ScriptOperation};
|
||||
|
||||
const SYSTEM_COLUMNS: &[&str] = &["id", "deleted", "created_at"];
|
||||
|
||||
fn validate_target_column(
|
||||
table_name: &str,
|
||||
target: &str,
|
||||
table_columns: &Value,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<String, String> {
|
||||
if SYSTEM_COLUMNS.contains(&target) {
|
||||
return Err(format!("Cannot override system column: {}", target));
|
||||
}
|
||||
@@ -22,28 +18,31 @@ fn validate_target_column(
|
||||
let columns: Vec<String> = serde_json::from_value(table_columns.clone())
|
||||
.map_err(|e| format!("Invalid column data: {}", e))?;
|
||||
|
||||
// Extract column names from the format "\"column_name\" TYPE"
|
||||
let column_names: Vec<&str> = columns
|
||||
// Extract column name and type
|
||||
let column_info: Vec<(&str, &str)> = columns
|
||||
.iter()
|
||||
.filter_map(|c| {
|
||||
c.split_whitespace()
|
||||
.next()
|
||||
.map(|s| s.trim_matches('"'))
|
||||
let mut parts = c.split_whitespace();
|
||||
let name = parts.next()?.trim_matches('"');
|
||||
let data_type = parts.next()?;
|
||||
Some((name, data_type))
|
||||
})
|
||||
.collect();
|
||||
|
||||
if !column_names.contains(&target) {
|
||||
return Err(format!("Target column {} not defined in table {}", target, table_name));
|
||||
}
|
||||
// Find the target column
|
||||
let column_type = column_info
|
||||
.iter()
|
||||
.find(|(name, _)| *name == target)
|
||||
.map(|(_, dt)| *dt)
|
||||
.ok_or_else(|| format!("Target column {} not defined in table {}", target, table_name))?;
|
||||
|
||||
Ok(())
|
||||
Ok(column_type.to_string())
|
||||
}
|
||||
|
||||
pub async fn post_table_script(
|
||||
db_pool: &PgPool,
|
||||
request: PostTableScriptRequest,
|
||||
) -> Result<TableScriptResponse, Status> {
|
||||
// Basic validation
|
||||
let table_def = sqlx::query!(
|
||||
r#"SELECT id, table_name, columns, profile_id
|
||||
FROM table_definitions WHERE id = $1"#,
|
||||
@@ -54,36 +53,25 @@ pub async fn post_table_script(
|
||||
.map_err(|e| Status::internal(format!("Database error: {}", e)))?
|
||||
.ok_or_else(|| Status::not_found("Table definition not found"))?;
|
||||
|
||||
// Use the full path to parse_script
|
||||
let operation = execution::parse_script(&request.script, &request.target_column)
|
||||
.map_err(|e| Status::invalid_argument(e.to_string()))?;
|
||||
// Validate target column and get its type
|
||||
let column_type = validate_target_column(
|
||||
&table_def.table_name,
|
||||
&request.target_column,
|
||||
&table_def.columns,
|
||||
)
|
||||
.map_err(|e| Status::invalid_argument(e))?;
|
||||
|
||||
// Ensure the operation is valid (additional checks if needed)
|
||||
match operation {
|
||||
ScriptOperation::SetToLocalColumn { .. } => {},
|
||||
ScriptOperation::SetToExternalColumn { .. } => {},
|
||||
}
|
||||
|
||||
// Call validation functions
|
||||
validate_script(&request.script)
|
||||
.map_err(|e| Status::invalid_argument(e.to_string()))?;
|
||||
|
||||
validate_target_column(&table_def.table_name, &request.target_column, &table_def.columns)
|
||||
.map_err(|e| Status::invalid_argument(e))?;
|
||||
|
||||
// Handle optional description
|
||||
let description = request.description;
|
||||
|
||||
// Store script in database
|
||||
// Store script in database with column type
|
||||
let script_record = sqlx::query!(
|
||||
r#"INSERT INTO table_scripts
|
||||
(table_definitions_id, target_column, script, description)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
(table_definitions_id, target_column, target_column_type, script, description)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id"#,
|
||||
request.table_definition_id,
|
||||
request.target_column,
|
||||
column_type,
|
||||
request.script,
|
||||
description
|
||||
request.description
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user