steel script working perfectly well

This commit is contained in:
filipriec
2025-03-17 12:28:09 +01:00
parent 65b254f4c2
commit 0887b46782
4 changed files with 55 additions and 43 deletions

View File

@@ -7,6 +7,8 @@ use crate::steel::server::syntax_parser::SyntaxParser;
const SYSTEM_COLUMNS: &[&str] = &["id", "deleted", "created_at"];
/// Validates the target column and ensures it is not a system column.
/// Returns the column type if valid.
fn validate_target_column(
table_name: &str,
target: &str,
@@ -16,9 +18,11 @@ fn validate_target_column(
return Err(format!("Cannot override system column: {}", target));
}
// Parse the columns JSON into a vector of strings
let columns: Vec<String> = serde_json::from_value(table_columns.clone())
.map_err(|e| format!("Invalid column data: {}", e))?;
// Extract column names and types
let column_info: Vec<(&str, &str)> = columns
.iter()
.filter_map(|c| {
@@ -29,19 +33,20 @@ fn validate_target_column(
})
.collect();
let column_type = column_info
// Find the target column and return its 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(column_type.to_string())
.map(|(_, dt)| dt.to_string())
.ok_or_else(|| format!("Target column '{}' not defined in table '{}'", target, table_name))
}
/// Handles the creation of a new table script.
pub async fn post_table_script(
db_pool: &PgPool,
request: PostTableScriptRequest,
) -> Result<TableScriptResponse, Status> {
// Fetch the table definition
let table_def = sqlx::query!(
r#"SELECT id, table_name, columns, profile_id
FROM table_definitions WHERE id = $1"#,
@@ -49,10 +54,12 @@ pub async fn post_table_script(
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Database error: {}", e)))?
.map_err(|e| {
Status::internal(format!("Failed to fetch table definition: {}", e))
})?
.ok_or_else(|| Status::not_found("Table definition not found"))?;
// Validate target column and get its type
// Validate the target column and get its type
let column_type = validate_target_column(
&table_def.table_name,
&request.target_column,
@@ -60,14 +67,14 @@ pub async fn post_table_script(
)
.map_err(|e| Status::invalid_argument(e))?;
// Parse and transform the script
// Parse and transform the script using the syntax parser
let parser = SyntaxParser::new();
let parsed_script = parser.parse(&request.script, &table_def.table_name);
// Store script in database with automatic target_table
// Insert the script into the database
let script_record = sqlx::query!(
r#"INSERT INTO table_scripts
(table_definitions_id, target_table, target_column,
(table_definitions_id, target_table, target_column,
target_column_type, script, description, profile_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id"#,
@@ -85,11 +92,12 @@ pub async fn post_table_script(
SqlxError::Database(db_err) if db_err.constraint() == Some("table_scripts_table_definitions_id_target_column_key") => {
Status::already_exists("Script already exists for this column")
}
_ => Status::internal(format!("Database error: {}", e)),
_ => Status::internal(format!("Failed to insert script: {}", e)),
})?;
// Return the response with the new script ID
Ok(TableScriptResponse {
id: script_record.id,
warnings: String::new(),
warnings: String::new(), // No warnings for now
})
}