accpeting now null in the post table data as nothing
This commit is contained in:
@@ -25,12 +25,6 @@ pub async fn post_table_data(
|
|||||||
let profile_name = request.profile_name;
|
let profile_name = request.profile_name;
|
||||||
let table_name = request.table_name;
|
let table_name = request.table_name;
|
||||||
|
|
||||||
// REMOVED: The old data conversion loop. We will process request.data directly.
|
|
||||||
// let mut data = HashMap::new();
|
|
||||||
// for (key, value) in request.data {
|
|
||||||
// data.insert(key, value.trim().to_string());
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Lookup profile
|
// Lookup profile
|
||||||
let profile = sqlx::query!(
|
let profile = sqlx::query!(
|
||||||
"SELECT id FROM profiles WHERE name = $1",
|
"SELECT id FROM profiles WHERE name = $1",
|
||||||
@@ -94,22 +88,26 @@ pub async fn post_table_data(
|
|||||||
|
|
||||||
// Validate all data columns
|
// Validate all data columns
|
||||||
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
||||||
for key in request.data.keys() { // CHANGED: Use request.data
|
for key in request.data.keys() {
|
||||||
if !system_columns_set.contains(key.as_str()) &&
|
if !system_columns_set.contains(key.as_str()) &&
|
||||||
!user_columns.contains(&&key.to_string()) {
|
!user_columns.contains(&&key.to_string()) {
|
||||||
return Err(Status::invalid_argument(format!("Invalid column: {}", key)));
|
return Err(Status::invalid_argument(format!("Invalid column: {}", key)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NEW: Create a string-based map for backwards compatibility with Steel scripts.
|
// ========================================================================
|
||||||
|
// FIX #1: SCRIPT VALIDATION LOOP
|
||||||
|
// This loop now correctly handles JSON `null` (which becomes `None`).
|
||||||
|
// ========================================================================
|
||||||
let mut string_data_for_scripts = HashMap::new();
|
let mut string_data_for_scripts = HashMap::new();
|
||||||
for (key, proto_value) in &request.data {
|
for (key, proto_value) in &request.data {
|
||||||
let str_val = match &proto_value.kind {
|
let str_val = match &proto_value.kind {
|
||||||
Some(Kind::StringValue(s)) => s.clone(),
|
Some(Kind::StringValue(s)) => s.clone(),
|
||||||
Some(Kind::NumberValue(n)) => n.to_string(),
|
Some(Kind::NumberValue(n)) => n.to_string(),
|
||||||
Some(Kind::BoolValue(b)) => b.to_string(),
|
Some(Kind::BoolValue(b)) => b.to_string(),
|
||||||
Some(Kind::NullValue(_)) => String::new(),
|
// This now correctly skips both protobuf `NULL` and JSON `null`.
|
||||||
Some(Kind::StructValue(_)) | Some(Kind::ListValue(_)) | None => {
|
Some(Kind::NullValue(_)) | None => continue,
|
||||||
|
Some(Kind::StructValue(_)) | Some(Kind::ListValue(_)) => {
|
||||||
return Err(Status::invalid_argument(format!("Unsupported type for script validation in column '{}'", key)));
|
return Err(Status::invalid_argument(format!("Unsupported type for script validation in column '{}'", key)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -128,21 +126,18 @@ pub async fn post_table_data(
|
|||||||
for script_record in scripts {
|
for script_record in scripts {
|
||||||
let target_column = script_record.target_column;
|
let target_column = script_record.target_column;
|
||||||
|
|
||||||
// Ensure target column exists in submitted data
|
let user_value = string_data_for_scripts.get(&target_column)
|
||||||
let user_value = string_data_for_scripts.get(&target_column) // CHANGED: Use the new string map
|
|
||||||
.ok_or_else(|| Status::invalid_argument(
|
.ok_or_else(|| Status::invalid_argument(
|
||||||
format!("Script target column '{}' is required", target_column)
|
format!("Script target column '{}' is required", target_column)
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
// Create execution context
|
|
||||||
let context = SteelContext {
|
let context = SteelContext {
|
||||||
current_table: table_name.clone(),
|
current_table: table_name.clone(),
|
||||||
profile_id,
|
profile_id,
|
||||||
row_data: string_data_for_scripts.clone(), // CHANGED: Use the new string map
|
row_data: string_data_for_scripts.clone(),
|
||||||
db_pool: Arc::new(db_pool.clone()),
|
db_pool: Arc::new(db_pool.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
// ... (rest of script execution is the same)
|
|
||||||
let script_result = execution::execute_script(
|
let script_result = execution::execute_script(
|
||||||
script_record.script,
|
script_record.script,
|
||||||
"STRINGS",
|
"STRINGS",
|
||||||
@@ -174,7 +169,11 @@ pub async fn post_table_data(
|
|||||||
let mut placeholders = Vec::new();
|
let mut placeholders = Vec::new();
|
||||||
let mut param_idx = 1;
|
let mut param_idx = 1;
|
||||||
|
|
||||||
// CHANGED: This entire loop is rewritten to handle prost_types::Value
|
// ========================================================================
|
||||||
|
// FIX #2: DATABASE INSERTION LOOP
|
||||||
|
// This loop now correctly handles JSON `null` (which becomes `None`)
|
||||||
|
// without crashing and correctly inserts a SQL NULL.
|
||||||
|
// ========================================================================
|
||||||
for (col, proto_value) in request.data {
|
for (col, proto_value) in request.data {
|
||||||
let sql_type = if system_columns_set.contains(col.as_str()) {
|
let sql_type = if system_columns_set.contains(col.as_str()) {
|
||||||
match col.as_str() {
|
match col.as_str() {
|
||||||
@@ -189,41 +188,52 @@ pub async fn post_table_data(
|
|||||||
.ok_or_else(|| Status::invalid_argument(format!("Column not found: {}", col)))?
|
.ok_or_else(|| Status::invalid_argument(format!("Column not found: {}", col)))?
|
||||||
};
|
};
|
||||||
|
|
||||||
let kind = proto_value.kind.ok_or_else(|| {
|
// Check for `None` (from JSON null) or `Some(NullValue)` first.
|
||||||
Status::invalid_argument(format!("Value for column '{}' cannot be null", col))
|
let kind = match &proto_value.kind {
|
||||||
})?;
|
None | Some(Kind::NullValue(_)) => {
|
||||||
|
// It's a null value. Add the correct SQL NULL type and continue.
|
||||||
|
match sql_type {
|
||||||
|
"BOOLEAN" => params.add(None::<bool>),
|
||||||
|
"TEXT" | "VARCHAR(15)" | "VARCHAR(255)" => params.add(None::<String>),
|
||||||
|
"TIMESTAMPTZ" => params.add(None::<DateTime<Utc>>),
|
||||||
|
"BIGINT" => params.add(None::<i64>),
|
||||||
|
_ => return Err(Status::invalid_argument(format!("Unsupported type for null value: {}", sql_type))),
|
||||||
|
}.map_err(|e| Status::internal(format!("Failed to add null parameter for {}: {}", col, e)))?;
|
||||||
|
|
||||||
|
columns_list.push(format!("\"{}\"", col));
|
||||||
|
placeholders.push(format!("${}", param_idx));
|
||||||
|
param_idx += 1;
|
||||||
|
continue; // Skip to the next column in the loop
|
||||||
|
}
|
||||||
|
// If it's not null, just pass the inner `Kind` through.
|
||||||
|
Some(k) => k,
|
||||||
|
};
|
||||||
|
|
||||||
|
// From here, we know `kind` is not a null type.
|
||||||
match sql_type {
|
match sql_type {
|
||||||
"TEXT" | "VARCHAR(15)" | "VARCHAR(255)" => {
|
"TEXT" | "VARCHAR(15)" | "VARCHAR(255)" => {
|
||||||
if let Kind::StringValue(value) = kind {
|
if let Kind::StringValue(value) = kind {
|
||||||
if let Some(max_len) = sql_type.strip_prefix("VARCHAR(")
|
if let Some(max_len) = sql_type.strip_prefix("VARCHAR(").and_then(|s| s.strip_suffix(')')).and_then(|s| s.parse::<usize>().ok()) {
|
||||||
.and_then(|s| s.strip_suffix(')'))
|
|
||||||
.and_then(|s| s.parse::<usize>().ok())
|
|
||||||
{
|
|
||||||
if value.len() > max_len {
|
if value.len() > max_len {
|
||||||
return Err(Status::internal(format!("Value too long for {}", col)));
|
return Err(Status::internal(format!("Value too long for {}", col)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.add(value)
|
params.add(value).map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
||||||
.map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
} else {
|
||||||
return Err(Status::invalid_argument(format!("Expected string for column '{}'", col)));
|
return Err(Status::invalid_argument(format!("Expected string for column '{}'", col)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"BOOLEAN" => {
|
"BOOLEAN" => {
|
||||||
if let Kind::BoolValue(val) = kind {
|
if let Kind::BoolValue(val) = kind {
|
||||||
params.add(val)
|
params.add(val).map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
||||||
.map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
} else {
|
||||||
return Err(Status::invalid_argument(format!("Expected boolean for column '{}'", col)));
|
return Err(Status::invalid_argument(format!("Expected boolean for column '{}'", col)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"TIMESTAMPTZ" => {
|
"TIMESTAMPTZ" => {
|
||||||
if let Kind::StringValue(value) = kind {
|
if let Kind::StringValue(value) = kind {
|
||||||
let dt = DateTime::parse_from_rfc3339(&value)
|
let dt = DateTime::parse_from_rfc3339(value).map_err(|_| Status::invalid_argument(format!("Invalid timestamp for {}", col)))?;
|
||||||
.map_err(|_| Status::invalid_argument(format!("Invalid timestamp for {}", col)))?;
|
params.add(dt.with_timezone(&Utc)).map_err(|e| Status::invalid_argument(format!("Failed to add timestamp parameter for {}: {}", col, e)))?;
|
||||||
params.add(dt.with_timezone(&Utc))
|
|
||||||
.map_err(|e| Status::invalid_argument(format!("Failed to add timestamp parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
} else {
|
||||||
return Err(Status::invalid_argument(format!("Expected ISO 8601 string for column '{}'", col)));
|
return Err(Status::invalid_argument(format!("Expected ISO 8601 string for column '{}'", col)));
|
||||||
}
|
}
|
||||||
@@ -233,8 +243,7 @@ pub async fn post_table_data(
|
|||||||
if val.fract() != 0.0 {
|
if val.fract() != 0.0 {
|
||||||
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
||||||
}
|
}
|
||||||
params.add(val as i64)
|
params.add(*val as i64).map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
||||||
.map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
} else {
|
||||||
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
||||||
}
|
}
|
||||||
@@ -261,7 +270,6 @@ pub async fn post_table_data(
|
|||||||
placeholders.join(", ")
|
placeholders.join(", ")
|
||||||
);
|
);
|
||||||
|
|
||||||
// ... (rest of the function is unchanged)
|
|
||||||
let result = sqlx::query_scalar_with::<_, i64, _>(&sql, params)
|
let result = sqlx::query_scalar_with::<_, i64, _>(&sql, params)
|
||||||
.fetch_one(db_pool)
|
.fetch_one(db_pool)
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
Reference in New Issue
Block a user