cleaned up code
This commit is contained in:
@@ -18,6 +18,13 @@ use crate::indexer::{IndexCommand, IndexCommandData};
|
|||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
/// Inserts new record into a table with validation through associated scripts.
|
||||||
|
///
|
||||||
|
/// This function handles creating new records in dynamically defined tables while:
|
||||||
|
/// - Validating permissions and column existence
|
||||||
|
/// - Executing validation scripts for computed columns
|
||||||
|
/// - Performing type-safe database insertions
|
||||||
|
/// - Maintaining search index consistency
|
||||||
pub async fn post_table_data(
|
pub async fn post_table_data(
|
||||||
db_pool: &PgPool,
|
db_pool: &PgPool,
|
||||||
request: PostTableDataRequest,
|
request: PostTableDataRequest,
|
||||||
@@ -26,6 +33,7 @@ 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;
|
||||||
|
|
||||||
|
// Fetch schema and table metadata
|
||||||
let schema = sqlx::query!(
|
let schema = sqlx::query!(
|
||||||
"SELECT id FROM schemas WHERE name = $1",
|
"SELECT id FROM schemas WHERE name = $1",
|
||||||
profile_name
|
profile_name
|
||||||
@@ -48,6 +56,7 @@ pub async fn post_table_data(
|
|||||||
|
|
||||||
let table_def = table_def.ok_or_else(|| Status::not_found("Table not found"))?;
|
let table_def = table_def.ok_or_else(|| Status::not_found("Table not found"))?;
|
||||||
|
|
||||||
|
// Parse column definitions from JSON format
|
||||||
let columns_json: Vec<String> = serde_json::from_value(table_def.columns.clone())
|
let columns_json: Vec<String> = serde_json::from_value(table_def.columns.clone())
|
||||||
.map_err(|e| Status::internal(format!("Column parsing error: {}", e)))?;
|
.map_err(|e| Status::internal(format!("Column parsing error: {}", e)))?;
|
||||||
|
|
||||||
@@ -62,6 +71,7 @@ pub async fn post_table_data(
|
|||||||
columns.push((name, sql_type));
|
columns.push((name, sql_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build list of valid system columns (foreign keys and special columns)
|
||||||
let fk_columns = sqlx::query!(
|
let fk_columns = sqlx::query!(
|
||||||
r#"SELECT ltd.table_name
|
r#"SELECT ltd.table_name
|
||||||
FROM table_definition_links tdl
|
FROM table_definition_links tdl
|
||||||
@@ -80,6 +90,7 @@ pub async fn post_table_data(
|
|||||||
|
|
||||||
let system_columns_set: std::collections::HashSet<_> = system_columns.iter().map(|s| s.as_str()).collect();
|
let system_columns_set: std::collections::HashSet<_> = system_columns.iter().map(|s| s.as_str()).collect();
|
||||||
|
|
||||||
|
// Validate all requested columns exist and are insertable
|
||||||
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() {
|
for key in request.data.keys() {
|
||||||
if !system_columns_set.contains(key.as_str()) &&
|
if !system_columns_set.contains(key.as_str()) &&
|
||||||
@@ -88,6 +99,7 @@ pub async fn post_table_data(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert proto values to strings for script execution context
|
||||||
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 {
|
||||||
@@ -108,6 +120,7 @@ pub async fn post_table_data(
|
|||||||
string_data_for_scripts.insert(key.clone(), str_val);
|
string_data_for_scripts.insert(key.clone(), str_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute validation scripts for computed columns
|
||||||
let scripts = sqlx::query!(
|
let scripts = sqlx::query!(
|
||||||
"SELECT target_column, script FROM table_scripts WHERE table_definitions_id = $1",
|
"SELECT target_column, script FROM table_scripts WHERE table_definitions_id = $1",
|
||||||
table_def.id
|
table_def.id
|
||||||
@@ -119,6 +132,7 @@ 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;
|
||||||
|
|
||||||
|
// All script target columns are required for INSERT operations
|
||||||
let user_value = string_data_for_scripts.get(&target_column)
|
let user_value = string_data_for_scripts.get(&target_column)
|
||||||
.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)
|
||||||
@@ -132,6 +146,7 @@ pub async fn post_table_data(
|
|||||||
db_pool: Arc::new(db_pool.clone()),
|
db_pool: Arc::new(db_pool.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Execute script to calculate expected value
|
||||||
let script_result = execution::execute_script(
|
let script_result = execution::execute_script(
|
||||||
script_record.script,
|
script_record.script,
|
||||||
"STRINGS",
|
"STRINGS",
|
||||||
@@ -153,8 +168,8 @@ pub async fn post_table_data(
|
|||||||
let expected_value = script_output.pop()
|
let expected_value = script_output.pop()
|
||||||
.ok_or_else(|| Status::internal("Script returned no values"))?;
|
.ok_or_else(|| Status::internal("Script returned no values"))?;
|
||||||
|
|
||||||
// FIX: Compare as Decimal numbers, not strings!
|
// Perform numeric comparison for decimal values to handle equivalent representations
|
||||||
// Parse the user's value into a Decimal
|
// (e.g., "76.5" should equal "76.50")
|
||||||
let user_decimal = Decimal::from_str(user_value).map_err(|_| {
|
let user_decimal = Decimal::from_str(user_value).map_err(|_| {
|
||||||
Status::invalid_argument(format!(
|
Status::invalid_argument(format!(
|
||||||
"Invalid decimal format provided for column '{}': {}",
|
"Invalid decimal format provided for column '{}': {}",
|
||||||
@@ -162,7 +177,6 @@ pub async fn post_table_data(
|
|||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Parse the script's calculated value into a Decimal
|
|
||||||
let expected_decimal = Decimal::from_str(&expected_value).map_err(|_| {
|
let expected_decimal = Decimal::from_str(&expected_value).map_err(|_| {
|
||||||
Status::internal(format!(
|
Status::internal(format!(
|
||||||
"Script for column '{}' produced an invalid decimal format: {}",
|
"Script for column '{}' produced an invalid decimal format: {}",
|
||||||
@@ -170,7 +184,6 @@ pub async fn post_table_data(
|
|||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Now compare the actual numbers - this correctly sees that 76.5 == 76.50
|
|
||||||
if user_decimal != expected_decimal {
|
if user_decimal != expected_decimal {
|
||||||
return Err(Status::invalid_argument(format!(
|
return Err(Status::invalid_argument(format!(
|
||||||
"Validation failed for column '{}': Script calculated '{}', but user provided '{}'",
|
"Validation failed for column '{}': Script calculated '{}', but user provided '{}'",
|
||||||
@@ -179,12 +192,14 @@ pub async fn post_table_data(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build parameterized INSERT query with type-safe parameter binding
|
||||||
let mut params = PgArguments::default();
|
let mut params = PgArguments::default();
|
||||||
let mut columns_list = Vec::new();
|
let mut columns_list = Vec::new();
|
||||||
let mut placeholders = Vec::new();
|
let mut placeholders = Vec::new();
|
||||||
let mut param_idx = 1;
|
let mut param_idx = 1;
|
||||||
|
|
||||||
for (col, proto_value) in request.data {
|
for (col, proto_value) in request.data {
|
||||||
|
// Determine SQL type for proper parameter binding
|
||||||
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() {
|
||||||
"deleted" => "BOOLEAN",
|
"deleted" => "BOOLEAN",
|
||||||
@@ -198,6 +213,7 @@ 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)))?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Handle NULL values first
|
||||||
let kind = match &proto_value.kind {
|
let kind = match &proto_value.kind {
|
||||||
None | Some(Kind::NullValue(_)) => {
|
None | Some(Kind::NullValue(_)) => {
|
||||||
match sql_type {
|
match sql_type {
|
||||||
@@ -218,102 +234,107 @@ pub async fn post_table_data(
|
|||||||
Some(k) => k,
|
Some(k) => k,
|
||||||
};
|
};
|
||||||
|
|
||||||
if sql_type == "TEXT" {
|
// Handle typed values with validation
|
||||||
if let Kind::StringValue(value) = kind {
|
match sql_type {
|
||||||
let trimmed_value = value.trim();
|
"TEXT" => {
|
||||||
|
if let Kind::StringValue(value) = kind {
|
||||||
|
let trimmed_value = value.trim();
|
||||||
|
|
||||||
if trimmed_value.is_empty() {
|
if trimmed_value.is_empty() {
|
||||||
params.add(None::<String>).map_err(|e| Status::internal(format!("Failed to add null parameter for {}: {}", col, e)))?;
|
params.add(None::<String>).map_err(|e| Status::internal(format!("Failed to add null parameter for {}: {}", col, e)))?;
|
||||||
} else {
|
|
||||||
if col == "telefon" && trimmed_value.len() > 15 {
|
|
||||||
return Err(Status::internal(format!("Value too long for {}", col)));
|
|
||||||
}
|
|
||||||
params.add(trimmed_value).map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected string for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "BOOLEAN" {
|
|
||||||
if let Kind::BoolValue(val) = kind {
|
|
||||||
params.add(val).map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected boolean for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "TIMESTAMPTZ" {
|
|
||||||
if let Kind::StringValue(value) = kind {
|
|
||||||
let dt = DateTime::parse_from_rfc3339(value).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)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected ISO 8601 string for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "BIGINT" {
|
|
||||||
if let Kind::NumberValue(val) = kind {
|
|
||||||
if val.fract() != 0.0 {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simple universal check: try the conversion and verify it's reversible
|
|
||||||
// This handles ALL edge cases: infinity, NaN, overflow, underflow, precision loss
|
|
||||||
let as_i64 = *val as i64;
|
|
||||||
if (as_i64 as f64) != *val {
|
|
||||||
return Err(Status::invalid_argument(format!("Integer value out of range for BIGINT column '{}'", col)));
|
|
||||||
}
|
|
||||||
|
|
||||||
params.add(as_i64).map_err(|e| Status::invalid_argument(format!("Failed to add bigint parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "INTEGER" {
|
|
||||||
if let Kind::NumberValue(val) = kind {
|
|
||||||
if val.fract() != 0.0 {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simple universal check: try the conversion and verify it's reversible
|
|
||||||
// This handles ALL edge cases: infinity, NaN, overflow, underflow, precision loss
|
|
||||||
let as_i32 = *val as i32;
|
|
||||||
if (as_i32 as f64) != *val {
|
|
||||||
return Err(Status::invalid_argument(format!("Integer value out of range for INTEGER column '{}'", col)));
|
|
||||||
}
|
|
||||||
|
|
||||||
params.add(as_i32).map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type.starts_with("NUMERIC") {
|
|
||||||
// MODIFIED: This block is now stricter.
|
|
||||||
let decimal_val = match kind {
|
|
||||||
Kind::StringValue(s) => {
|
|
||||||
let trimmed = s.trim();
|
|
||||||
if trimmed.is_empty() {
|
|
||||||
None // Treat empty string as NULL
|
|
||||||
} else {
|
} else {
|
||||||
// This is the only valid path: parse from a string.
|
// Apply business rule validation (e.g., phone number length)
|
||||||
Some(Decimal::from_str(trimmed).map_err(|_| {
|
if col == "telefon" && trimmed_value.len() > 15 {
|
||||||
Status::invalid_argument(format!(
|
return Err(Status::internal(format!("Value too long for {}", col)));
|
||||||
"Invalid decimal string format for column '{}': {}",
|
}
|
||||||
col, s
|
params.add(trimmed_value).map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
||||||
))
|
|
||||||
})?)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected string for column '{}'", col)));
|
||||||
}
|
}
|
||||||
// CATCH-ALL: Reject NumberValue, BoolValue, etc. for NUMERIC fields.
|
},
|
||||||
_ => {
|
"BOOLEAN" => {
|
||||||
return Err(Status::invalid_argument(format!(
|
if let Kind::BoolValue(val) = kind {
|
||||||
"Expected a string representation for decimal column '{}', but received a different type.",
|
params.add(val).map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
||||||
col
|
} else {
|
||||||
)));
|
return Err(Status::invalid_argument(format!("Expected boolean for column '{}'", col)));
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
|
"TIMESTAMPTZ" => {
|
||||||
|
if let Kind::StringValue(value) = kind {
|
||||||
|
let dt = DateTime::parse_from_rfc3339(value).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)))?;
|
||||||
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected ISO 8601 string for column '{}'", col)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"BIGINT" => {
|
||||||
|
if let Kind::NumberValue(val) = kind {
|
||||||
|
if val.fract() != 0.0 {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
||||||
|
}
|
||||||
|
|
||||||
params.add(decimal_val).map_err(|e| {
|
// Validate conversion is reversible to catch overflow, underflow, and precision loss
|
||||||
Status::invalid_argument(format!(
|
let as_i64 = *val as i64;
|
||||||
"Failed to add decimal parameter for {}: {}",
|
if (as_i64 as f64) != *val {
|
||||||
col, e
|
return Err(Status::invalid_argument(format!("Integer value out of range for BIGINT column '{}'", col)));
|
||||||
))
|
}
|
||||||
})?;
|
|
||||||
} else {
|
params.add(as_i64).map_err(|e| Status::invalid_argument(format!("Failed to add bigint parameter for {}: {}", col, e)))?;
|
||||||
return Err(Status::invalid_argument(format!("Unsupported type {}", sql_type)));
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"INTEGER" => {
|
||||||
|
if let Kind::NumberValue(val) = kind {
|
||||||
|
if val.fract() != 0.0 {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate conversion is reversible to catch overflow, underflow, and precision loss
|
||||||
|
let as_i32 = *val as i32;
|
||||||
|
if (as_i32 as f64) != *val {
|
||||||
|
return Err(Status::invalid_argument(format!("Integer value out of range for INTEGER column '{}'", col)));
|
||||||
|
}
|
||||||
|
|
||||||
|
params.add(as_i32).map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
||||||
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
s if s.starts_with("NUMERIC") => {
|
||||||
|
// Decimal values must be provided as strings for precise parsing
|
||||||
|
let decimal_val = match kind {
|
||||||
|
Kind::StringValue(s) => {
|
||||||
|
let trimmed = s.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
None // Treat empty string as NULL
|
||||||
|
} else {
|
||||||
|
Some(Decimal::from_str(trimmed).map_err(|_| {
|
||||||
|
Status::invalid_argument(format!(
|
||||||
|
"Invalid decimal string format for column '{}': {}",
|
||||||
|
col, s
|
||||||
|
))
|
||||||
|
})?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Reject other types to ensure precise decimal handling
|
||||||
|
_ => {
|
||||||
|
return Err(Status::invalid_argument(format!(
|
||||||
|
"Expected a string representation for decimal column '{}', but received a different type.",
|
||||||
|
col
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
params.add(decimal_val).map_err(|e| {
|
||||||
|
Status::invalid_argument(format!(
|
||||||
|
"Failed to add decimal parameter for {}: {}",
|
||||||
|
col, e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
},
|
||||||
|
_ => return Err(Status::invalid_argument(format!("Unsupported type {}", sql_type))),
|
||||||
}
|
}
|
||||||
|
|
||||||
columns_list.push(format!("\"{}\"", col));
|
columns_list.push(format!("\"{}\"", col));
|
||||||
@@ -325,6 +346,7 @@ pub async fn post_table_data(
|
|||||||
return Err(Status::invalid_argument("No valid columns to insert"));
|
return Err(Status::invalid_argument("No valid columns to insert"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute the INSERT query
|
||||||
let qualified_table = crate::shared::schema_qualifier::qualify_table_name_for_data(
|
let qualified_table = crate::shared::schema_qualifier::qualify_table_name_for_data(
|
||||||
db_pool,
|
db_pool,
|
||||||
&profile_name,
|
&profile_name,
|
||||||
@@ -346,6 +368,7 @@ pub async fn post_table_data(
|
|||||||
let inserted_id = match result {
|
let inserted_id = match result {
|
||||||
Ok(id) => id,
|
Ok(id) => id,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
// Handle specific database errors with user-friendly messages
|
||||||
if let Some(db_err) = e.as_database_error() {
|
if let Some(db_err) = e.as_database_error() {
|
||||||
if db_err.code() == Some(std::borrow::Cow::Borrowed("22P02")) ||
|
if db_err.code() == Some(std::borrow::Cow::Borrowed("22P02")) ||
|
||||||
db_err.code() == Some(std::borrow::Cow::Borrowed("22003")) {
|
db_err.code() == Some(std::borrow::Cow::Borrowed("22003")) {
|
||||||
@@ -364,6 +387,7 @@ pub async fn post_table_data(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Queue record for search index update
|
||||||
let command = IndexCommand::AddOrUpdate(IndexCommandData {
|
let command = IndexCommand::AddOrUpdate(IndexCommandData {
|
||||||
table_name: table_name.clone(),
|
table_name: table_name.clone(),
|
||||||
row_id: inserted_id,
|
row_id: inserted_id,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// src/tables_data/handlers/put_table_data.rs
|
// src/tables_data/handlers/put_table_data.rs
|
||||||
// TODO WORK ON SCRIPTS INPUT OUTPUT HAS TO BE CHECKED
|
|
||||||
|
|
||||||
use tonic::Status;
|
use tonic::Status;
|
||||||
use sqlx::{PgPool, Arguments, Row};
|
use sqlx::{PgPool, Arguments, Row};
|
||||||
@@ -17,22 +16,22 @@ use crate::indexer::{IndexCommand, IndexCommandData};
|
|||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
/// Updates table data with validation through associated scripts.
|
||||||
|
///
|
||||||
|
/// This function handles updating records in dynamically defined tables while:
|
||||||
|
/// - Validating permissions and column existence
|
||||||
|
/// - Executing validation scripts for computed columns
|
||||||
|
/// - Performing type-safe database updates
|
||||||
|
/// - Maintaining search index consistency
|
||||||
pub async fn put_table_data(
|
pub async fn put_table_data(
|
||||||
db_pool: &PgPool,
|
db_pool: &PgPool,
|
||||||
request: PutTableDataRequest,
|
request: PutTableDataRequest,
|
||||||
indexer_tx: &mpsc::Sender<IndexCommand>,
|
indexer_tx: &mpsc::Sender<IndexCommand>,
|
||||||
) -> Result<PutTableDataResponse, Status> {
|
) -> Result<PutTableDataResponse, Status> {
|
||||||
eprintln!("🔥🔥🔥 PUT_TABLE_DATA FUNCTION CALLED 🔥🔥🔥");
|
|
||||||
eprintln!("Request: {:?}", request);
|
|
||||||
|
|
||||||
let profile_name = request.profile_name;
|
let profile_name = request.profile_name;
|
||||||
let table_name = request.table_name;
|
let table_name = request.table_name;
|
||||||
let record_id = request.id;
|
let record_id = request.id;
|
||||||
|
|
||||||
eprintln!("=== PUT TABLE DATA START ===");
|
|
||||||
eprintln!("Profile: {}, Table: {}, Record ID: {}", profile_name, table_name, record_id);
|
|
||||||
eprintln!("Request data: {:?}", request.data);
|
|
||||||
|
|
||||||
if request.data.is_empty() {
|
if request.data.is_empty() {
|
||||||
return Ok(PutTableDataResponse {
|
return Ok(PutTableDataResponse {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -41,7 +40,7 @@ pub async fn put_table_data(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Metadata Fetching ---
|
// Fetch schema and table metadata
|
||||||
let schema = sqlx::query!("SELECT id FROM schemas WHERE name = $1", profile_name)
|
let schema = sqlx::query!("SELECT id FROM schemas WHERE name = $1", profile_name)
|
||||||
.fetch_optional(db_pool).await
|
.fetch_optional(db_pool).await
|
||||||
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?
|
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?
|
||||||
@@ -56,6 +55,7 @@ pub async fn put_table_data(
|
|||||||
.map_err(|e| Status::internal(format!("Table lookup error: {}", e)))?
|
.map_err(|e| Status::internal(format!("Table lookup error: {}", e)))?
|
||||||
.ok_or_else(|| Status::not_found("Table not found"))?;
|
.ok_or_else(|| Status::not_found("Table not found"))?;
|
||||||
|
|
||||||
|
// Parse column definitions from JSON format
|
||||||
let columns_json: Vec<String> = serde_json::from_value(table_def.columns.clone())
|
let columns_json: Vec<String> = serde_json::from_value(table_def.columns.clone())
|
||||||
.map_err(|e| Status::internal(format!("Column parsing error: {}", e)))?;
|
.map_err(|e| Status::internal(format!("Column parsing error: {}", e)))?;
|
||||||
|
|
||||||
@@ -70,9 +70,7 @@ pub async fn put_table_data(
|
|||||||
columns.push((name, sql_type));
|
columns.push((name, sql_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Table columns: {:?}", columns);
|
// Build list of valid system columns (foreign keys and special columns)
|
||||||
|
|
||||||
// --- Validate Column Permissions ---
|
|
||||||
let fk_columns = sqlx::query!(
|
let fk_columns = sqlx::query!(
|
||||||
r#"SELECT ltd.table_name
|
r#"SELECT ltd.table_name
|
||||||
FROM table_definition_links tdl
|
FROM table_definition_links tdl
|
||||||
@@ -89,6 +87,7 @@ pub async fn put_table_data(
|
|||||||
system_columns.push(format!("{}_id", fk.table_name));
|
system_columns.push(format!("{}_id", fk.table_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate all requested columns exist and are editable
|
||||||
let system_columns_set: std::collections::HashSet<_> = system_columns.iter().map(|s| s.as_str()).collect();
|
let system_columns_set: std::collections::HashSet<_> = system_columns.iter().map(|s| s.as_str()).collect();
|
||||||
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
||||||
|
|
||||||
@@ -99,16 +98,12 @@ pub async fn put_table_data(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Smart Data Fetching using script_dependencies table ---
|
// Fetch validation scripts for this table
|
||||||
let scripts = sqlx::query!("SELECT id, target_column, script FROM table_scripts WHERE table_definitions_id = $1", table_def.id)
|
let scripts = sqlx::query!("SELECT id, target_column, script FROM table_scripts WHERE table_definitions_id = $1", table_def.id)
|
||||||
.fetch_all(db_pool).await
|
.fetch_all(db_pool).await
|
||||||
.map_err(|e| Status::internal(format!("Failed to fetch scripts: {}", e)))?;
|
.map_err(|e| Status::internal(format!("Failed to fetch scripts: {}", e)))?;
|
||||||
|
|
||||||
println!("Found {} scripts for table", scripts.len());
|
// Determine minimal set of columns needed for script execution context
|
||||||
for script in &scripts {
|
|
||||||
println!("Script ID {}: target_column='{}', script='{}'", script.id, script.target_column, script.script);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut required_columns = std::collections::HashSet::new();
|
let mut required_columns = std::collections::HashSet::new();
|
||||||
|
|
||||||
// Always need: id, target columns of scripts, and columns being updated
|
// Always need: id, target columns of scripts, and columns being updated
|
||||||
@@ -120,7 +115,7 @@ pub async fn put_table_data(
|
|||||||
required_columns.insert(key.clone());
|
required_columns.insert(key.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use pre-computed dependencies from script_dependencies table
|
// Add columns referenced by scripts based on pre-computed dependencies
|
||||||
if !scripts.is_empty() {
|
if !scripts.is_empty() {
|
||||||
let script_ids: Vec<i64> = scripts.iter().map(|s| s.id).collect();
|
let script_ids: Vec<i64> = scripts.iter().map(|s| s.id).collect();
|
||||||
|
|
||||||
@@ -136,8 +131,8 @@ pub async fn put_table_data(
|
|||||||
.map_err(|e| Status::internal(format!("Failed to fetch script dependencies: {}", e)))?;
|
.map_err(|e| Status::internal(format!("Failed to fetch script dependencies: {}", e)))?;
|
||||||
|
|
||||||
for dep in dependencies {
|
for dep in dependencies {
|
||||||
// If it references this table, add the columns it uses
|
|
||||||
if dep.target_table == table_name {
|
if dep.target_table == table_name {
|
||||||
|
// Script references this table - add specific columns it uses
|
||||||
match dep.dependency_type.as_str() {
|
match dep.dependency_type.as_str() {
|
||||||
"column_access" | "indexed_access" => {
|
"column_access" | "indexed_access" => {
|
||||||
if let Some(context) = dep.context_info {
|
if let Some(context) = dep.context_info {
|
||||||
@@ -148,18 +143,15 @@ pub async fn put_table_data(
|
|||||||
}
|
}
|
||||||
_ => {} // SQL queries handled differently
|
_ => {} // SQL queries handled differently
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
// If it references linked tables, add their foreign key columns
|
// Script references linked table - add foreign key column
|
||||||
else {
|
|
||||||
let fk_column = format!("{}_id", dep.target_table.split('_').last().unwrap_or(&dep.target_table));
|
let fk_column = format!("{}_id", dep.target_table.split('_').last().unwrap_or(&dep.target_table));
|
||||||
required_columns.insert(fk_column);
|
required_columns.insert(fk_column);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Required columns for context: {:?}", required_columns);
|
// Fetch current record state with only required columns for efficiency
|
||||||
|
|
||||||
// Build optimized SELECT query with only required columns
|
|
||||||
let qualified_table = crate::shared::schema_qualifier::qualify_table_name_for_data(db_pool, &profile_name, &table_name).await?;
|
let qualified_table = crate::shared::schema_qualifier::qualify_table_name_for_data(db_pool, &profile_name, &table_name).await?;
|
||||||
|
|
||||||
let columns_clause = required_columns
|
let columns_clause = required_columns
|
||||||
@@ -169,7 +161,6 @@ pub async fn put_table_data(
|
|||||||
.join(", ");
|
.join(", ");
|
||||||
|
|
||||||
let select_sql = format!("SELECT {} FROM {} WHERE id = $1", columns_clause, qualified_table);
|
let select_sql = format!("SELECT {} FROM {} WHERE id = $1", columns_clause, qualified_table);
|
||||||
println!("SELECT SQL: {}", select_sql);
|
|
||||||
|
|
||||||
let current_row = sqlx::query(&select_sql).bind(record_id).fetch_optional(db_pool).await
|
let current_row = sqlx::query(&select_sql).bind(record_id).fetch_optional(db_pool).await
|
||||||
.map_err(|e| Status::internal(format!("Failed to fetch current row state: {}", e)))?
|
.map_err(|e| Status::internal(format!("Failed to fetch current row state: {}", e)))?
|
||||||
@@ -181,11 +172,7 @@ pub async fn put_table_data(
|
|||||||
current_row_data.insert(col_name.clone(), value);
|
current_row_data.insert(col_name.clone(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("=== CURRENT ROW DATA FROM DB ===");
|
// Extract and normalize update data from request
|
||||||
println!("{:?}", current_row_data);
|
|
||||||
|
|
||||||
// --- Data Merging Logic ---
|
|
||||||
eprintln!("🚨🚨🚨 EXTRACTING UPDATE DATA FROM REQUEST 🚨🚨🚨");
|
|
||||||
let mut update_data = HashMap::new();
|
let mut update_data = 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 {
|
||||||
@@ -195,33 +182,25 @@ pub async fn put_table_data(
|
|||||||
Some(Kind::NullValue(_)) | None => String::new(),
|
Some(Kind::NullValue(_)) | None => String::new(),
|
||||||
_ => return Err(Status::invalid_argument(format!("Unsupported type for column '{}'", key))),
|
_ => return Err(Status::invalid_argument(format!("Unsupported type for column '{}'", key))),
|
||||||
};
|
};
|
||||||
eprintln!("🔥 UPDATE_DATA[{}] = '{}'", key, str_val);
|
|
||||||
update_data.insert(key.clone(), str_val);
|
update_data.insert(key.clone(), str_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("🚨 UPDATE DATA EXTRACTED: {:?}", update_data);
|
// Create final context by merging current data with updates
|
||||||
|
|
||||||
let mut final_context_data = current_row_data.clone();
|
let mut final_context_data = current_row_data.clone();
|
||||||
eprintln!("🚨 BEFORE EXTEND - final_context_data: {:?}", final_context_data);
|
|
||||||
final_context_data.extend(update_data.clone());
|
final_context_data.extend(update_data.clone());
|
||||||
eprintln!("🚨 AFTER EXTEND - final_context_data: {:?}", final_context_data);
|
|
||||||
|
|
||||||
// Script validation with type-aware comparison
|
// Execute and validate scripts for computed columns
|
||||||
for script_record in scripts {
|
for script_record in scripts {
|
||||||
let target_column = script_record.target_column;
|
let target_column = script_record.target_column;
|
||||||
|
|
||||||
println!("=== PROCESSING SCRIPT FOR COLUMN: {} ===", target_column);
|
// Determine SQL type for type-aware validation
|
||||||
|
|
||||||
// Find the SQL type for this target column
|
|
||||||
let target_sql_type = if let Some((_, stype)) = columns.iter().find(|(name, _)| name == &target_column) {
|
let target_sql_type = if let Some((_, stype)) = columns.iter().find(|(name, _)| name == &target_column) {
|
||||||
stype.as_str()
|
stype.as_str()
|
||||||
} else {
|
} else {
|
||||||
"TEXT" // Default fallback for system columns
|
"TEXT" // Default fallback for system columns
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("Target column SQL type: {}", target_sql_type);
|
// Execute script with current context
|
||||||
println!("Executing script: {}", script_record.script);
|
|
||||||
|
|
||||||
let script_result = execution::execute_script(
|
let script_result = execution::execute_script(
|
||||||
script_record.script.clone(),
|
script_record.script.clone(),
|
||||||
"STRINGS",
|
"STRINGS",
|
||||||
@@ -242,91 +221,69 @@ pub async fn put_table_data(
|
|||||||
};
|
};
|
||||||
let script_output = script_output_vec.pop().ok_or_else(|| Status::internal("Script returned no values"))?;
|
let script_output = script_output_vec.pop().ok_or_else(|| Status::internal("Script returned no values"))?;
|
||||||
|
|
||||||
println!("Script output: '{}'", script_output);
|
|
||||||
|
|
||||||
if update_data.contains_key(&target_column) {
|
if update_data.contains_key(&target_column) {
|
||||||
// Case A: Column is being updated. Validate user input against script.
|
// Case A: Column is being updated - validate user input against script output
|
||||||
let user_value = update_data.get(&target_column).unwrap();
|
let user_value = update_data.get(&target_column).unwrap();
|
||||||
println!("Case A: Validating user value '{}' against script output '{}'", user_value, script_output);
|
|
||||||
|
|
||||||
// TYPE-AWARE COMPARISON based on SQL type
|
// Perform type-aware comparison based on SQL column type
|
||||||
let values_match = match target_sql_type {
|
let values_match = match target_sql_type {
|
||||||
s if s.starts_with("NUMERIC") => {
|
s if s.starts_with("NUMERIC") => {
|
||||||
let user_decimal = Decimal::from_str(user_value).map_err(|_| Status::invalid_argument(format!("Invalid decimal format for column '{}'", target_column)))?;
|
let user_decimal = Decimal::from_str(user_value).map_err(|_| Status::invalid_argument(format!("Invalid decimal format for column '{}'", target_column)))?;
|
||||||
let script_decimal = Decimal::from_str(&script_output).map_err(|_| Status::internal(format!("Script for '{}' produced invalid decimal", target_column)))?;
|
let script_decimal = Decimal::from_str(&script_output).map_err(|_| Status::internal(format!("Script for '{}' produced invalid decimal", target_column)))?;
|
||||||
println!("Decimal comparison: user={}, script={}", user_decimal, script_decimal);
|
|
||||||
user_decimal == script_decimal
|
user_decimal == script_decimal
|
||||||
},
|
},
|
||||||
"INTEGER" | "BIGINT" => {
|
"INTEGER" | "BIGINT" => {
|
||||||
let user_int: i64 = user_value.parse().map_err(|_| Status::invalid_argument(format!("Invalid integer format for column '{}'", target_column)))?;
|
let user_int: i64 = user_value.parse().map_err(|_| Status::invalid_argument(format!("Invalid integer format for column '{}'", target_column)))?;
|
||||||
let script_int: i64 = script_output.parse().map_err(|_| Status::internal(format!("Script for '{}' produced invalid integer", target_column)))?;
|
let script_int: i64 = script_output.parse().map_err(|_| Status::internal(format!("Script for '{}' produced invalid integer", target_column)))?;
|
||||||
println!("Integer comparison: user={}, script={}", user_int, script_int);
|
|
||||||
user_int == script_int
|
user_int == script_int
|
||||||
},
|
},
|
||||||
"BOOLEAN" => {
|
"BOOLEAN" => {
|
||||||
let user_bool: bool = user_value.parse().map_err(|_| Status::invalid_argument(format!("Invalid boolean format for column '{}'", target_column)))?;
|
let user_bool: bool = user_value.parse().map_err(|_| Status::invalid_argument(format!("Invalid boolean format for column '{}'", target_column)))?;
|
||||||
let script_bool: bool = script_output.parse().map_err(|_| Status::internal(format!("Script for '{}' produced invalid boolean", target_column)))?;
|
let script_bool: bool = script_output.parse().map_err(|_| Status::internal(format!("Script for '{}' produced invalid boolean", target_column)))?;
|
||||||
println!("Boolean comparison: user={}, script={}", user_bool, script_bool);
|
|
||||||
user_bool == script_bool
|
user_bool == script_bool
|
||||||
},
|
},
|
||||||
_ => {
|
_ => user_value == &script_output
|
||||||
println!("String comparison: user='{}', script='{}'", user_value, script_output);
|
|
||||||
user_value == &script_output
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("Values match: {}", values_match);
|
|
||||||
|
|
||||||
if !values_match {
|
if !values_match {
|
||||||
return Err(Status::invalid_argument(format!("Validation failed for column '{}': Script calculated '{}', but user provided '{}'", target_column, script_output, user_value)));
|
return Err(Status::invalid_argument(format!("Validation failed for column '{}': Script calculated '{}', but user provided '{}'", target_column, script_output, user_value)));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Case B: Column is NOT being updated. Prevent unauthorized changes.
|
// Case B: Column is NOT being updated - prevent unauthorized script-triggered changes
|
||||||
let current_value = current_row_data.get(&target_column).cloned().unwrap_or_default();
|
let current_value = current_row_data.get(&target_column).cloned().unwrap_or_default();
|
||||||
println!("Case B: Checking if script would change current value '{}' to '{}'", current_value, script_output);
|
|
||||||
|
|
||||||
let values_match = match target_sql_type {
|
let values_match = match target_sql_type {
|
||||||
s if s.starts_with("NUMERIC") => {
|
s if s.starts_with("NUMERIC") => {
|
||||||
let current_decimal = Decimal::from_str(¤t_value).unwrap_or_default();
|
let current_decimal = Decimal::from_str(¤t_value).unwrap_or_default();
|
||||||
let script_decimal = Decimal::from_str(&script_output).unwrap_or_default();
|
let script_decimal = Decimal::from_str(&script_output).unwrap_or_default();
|
||||||
println!("Decimal comparison: current={}, script={}", current_decimal, script_decimal);
|
|
||||||
current_decimal == script_decimal
|
current_decimal == script_decimal
|
||||||
},
|
},
|
||||||
"INTEGER" | "BIGINT" => {
|
"INTEGER" | "BIGINT" => {
|
||||||
let current_int: i64 = current_value.parse().unwrap_or_default();
|
let current_int: i64 = current_value.parse().unwrap_or_default();
|
||||||
let script_int: i64 = script_output.parse().unwrap_or_default();
|
let script_int: i64 = script_output.parse().unwrap_or_default();
|
||||||
println!("Integer comparison: current={}, script={}", current_int, script_int);
|
|
||||||
current_int == script_int
|
current_int == script_int
|
||||||
},
|
},
|
||||||
"BOOLEAN" => {
|
"BOOLEAN" => {
|
||||||
let current_bool: bool = current_value.parse().unwrap_or(false);
|
let current_bool: bool = current_value.parse().unwrap_or(false);
|
||||||
let script_bool: bool = script_output.parse().unwrap_or(false);
|
let script_bool: bool = script_output.parse().unwrap_or(false);
|
||||||
println!("Boolean comparison: current={}, script={}", current_bool, script_bool);
|
|
||||||
current_bool == script_bool
|
current_bool == script_bool
|
||||||
},
|
},
|
||||||
_ => {
|
_ => current_value == script_output
|
||||||
println!("String comparison: current='{}', script='{}'", current_value, script_output);
|
|
||||||
current_value == script_output
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("Values match: {}", values_match);
|
|
||||||
|
|
||||||
if !values_match {
|
if !values_match {
|
||||||
return Err(Status::failed_precondition(format!("Script for column '{}' was triggered and would change its value from '{}' to '{}'. To apply this change, please include '{}' in your update request.", target_column, current_value, script_output, target_column)));
|
return Err(Status::failed_precondition(format!("Script for column '{}' was triggered and would change its value from '{}' to '{}'. To apply this change, please include '{}' in your update request.", target_column, current_value, script_output, target_column)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("=== END PROCESSING SCRIPT FOR COLUMN: {} ===", target_column);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Database Update ---
|
// Build parameterized UPDATE query with type-safe parameter binding
|
||||||
let mut params = PgArguments::default();
|
let mut params = PgArguments::default();
|
||||||
let mut set_clauses = Vec::new();
|
let mut set_clauses = Vec::new();
|
||||||
let mut param_idx = 1;
|
let mut param_idx = 1;
|
||||||
|
|
||||||
println!("=== BUILDING UPDATE QUERY ===");
|
|
||||||
|
|
||||||
for (col, proto_value) in request.data {
|
for (col, proto_value) in request.data {
|
||||||
|
// Determine SQL type for proper parameter binding
|
||||||
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() {
|
||||||
"deleted" => "BOOLEAN",
|
"deleted" => "BOOLEAN",
|
||||||
@@ -340,6 +297,7 @@ pub async fn put_table_data(
|
|||||||
.ok_or_else(|| Status::invalid_argument(format!("Column not found: {}", col)))?
|
.ok_or_else(|| Status::invalid_argument(format!("Column not found: {}", col)))?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Handle NULL values first
|
||||||
let kind = match &proto_value.kind {
|
let kind = match &proto_value.kind {
|
||||||
None | Some(Kind::NullValue(_)) => {
|
None | Some(Kind::NullValue(_)) => {
|
||||||
match sql_type {
|
match sql_type {
|
||||||
@@ -359,89 +317,97 @@ pub async fn put_table_data(
|
|||||||
Some(k) => k,
|
Some(k) => k,
|
||||||
};
|
};
|
||||||
|
|
||||||
if sql_type == "TEXT" {
|
// Handle typed values with validation
|
||||||
if let Kind::StringValue(value) = kind {
|
match sql_type {
|
||||||
let trimmed_value = value.trim();
|
"TEXT" => {
|
||||||
if trimmed_value.is_empty() {
|
if let Kind::StringValue(value) = kind {
|
||||||
params.add(None::<String>).map_err(|e| Status::internal(format!("Failed to add null parameter for {}: {}", col, e)))?;
|
let trimmed_value = value.trim();
|
||||||
} else {
|
if trimmed_value.is_empty() {
|
||||||
if col == "telefon" && trimmed_value.len() > 15 {
|
params.add(None::<String>).map_err(|e| Status::internal(format!("Failed to add null parameter for {}: {}", col, e)))?;
|
||||||
return Err(Status::internal(format!("Value too long for {}", col)));
|
|
||||||
}
|
|
||||||
params.add(trimmed_value).map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected string for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "BOOLEAN" {
|
|
||||||
if let Kind::BoolValue(val) = kind {
|
|
||||||
params.add(val).map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected boolean for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "TIMESTAMPTZ" {
|
|
||||||
if let Kind::StringValue(value) = kind {
|
|
||||||
let dt = DateTime::parse_from_rfc3339(value).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)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected ISO 8601 string for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "BIGINT" {
|
|
||||||
if let Kind::NumberValue(val) = kind {
|
|
||||||
if val.fract() != 0.0 {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
|
||||||
}
|
|
||||||
let as_i64 = *val as i64;
|
|
||||||
if (as_i64 as f64) != *val {
|
|
||||||
return Err(Status::invalid_argument(format!("Integer value out of range for BIGINT column '{}'", col)));
|
|
||||||
}
|
|
||||||
params.add(as_i64).map_err(|e| Status::invalid_argument(format!("Failed to add bigint parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type == "INTEGER" {
|
|
||||||
if let Kind::NumberValue(val) = kind {
|
|
||||||
if val.fract() != 0.0 {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
|
||||||
}
|
|
||||||
let as_i32 = *val as i32;
|
|
||||||
if (as_i32 as f64) != *val {
|
|
||||||
return Err(Status::invalid_argument(format!("Integer value out of range for INTEGER column '{}'", col)));
|
|
||||||
}
|
|
||||||
params.add(as_i32).map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
|
||||||
} else {
|
|
||||||
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
|
||||||
}
|
|
||||||
} else if sql_type.starts_with("NUMERIC") {
|
|
||||||
let decimal_val = match kind {
|
|
||||||
Kind::StringValue(s) => {
|
|
||||||
let trimmed = s.trim();
|
|
||||||
if trimmed.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
} else {
|
||||||
Some(Decimal::from_str(trimmed).map_err(|_| {
|
// Apply business rule validation (e.g., phone number length)
|
||||||
Status::invalid_argument(format!(
|
if col == "telefon" && trimmed_value.len() > 15 {
|
||||||
"Invalid decimal string format for column '{}': {}",
|
return Err(Status::internal(format!("Value too long for {}", col)));
|
||||||
col, s
|
}
|
||||||
))
|
params.add(trimmed_value).map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
||||||
})?)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected string for column '{}'", col)));
|
||||||
}
|
}
|
||||||
_ => {
|
},
|
||||||
return Err(Status::invalid_argument(format!(
|
"BOOLEAN" => {
|
||||||
"Expected a string representation for decimal column '{}', but received a different type.",
|
if let Kind::BoolValue(val) = kind {
|
||||||
col
|
params.add(val).map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
||||||
)));
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected boolean for column '{}'", col)));
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
params.add(decimal_val).map_err(|e| {
|
"TIMESTAMPTZ" => {
|
||||||
Status::invalid_argument(format!(
|
if let Kind::StringValue(value) = kind {
|
||||||
"Failed to add decimal parameter for {}: {}",
|
let dt = DateTime::parse_from_rfc3339(value).map_err(|_| Status::invalid_argument(format!("Invalid timestamp for {}", col)))?;
|
||||||
col, e
|
params.add(dt.with_timezone(&Utc)).map_err(|e| Status::invalid_argument(format!("Failed to add timestamp parameter for {}: {}", col, e)))?;
|
||||||
))
|
} else {
|
||||||
})?;
|
return Err(Status::invalid_argument(format!("Expected ISO 8601 string for column '{}'", col)));
|
||||||
} else {
|
}
|
||||||
return Err(Status::invalid_argument(format!("Unsupported type {}", sql_type)));
|
},
|
||||||
|
"BIGINT" => {
|
||||||
|
if let Kind::NumberValue(val) = kind {
|
||||||
|
if val.fract() != 0.0 {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
||||||
|
}
|
||||||
|
let as_i64 = *val as i64;
|
||||||
|
if (as_i64 as f64) != *val {
|
||||||
|
return Err(Status::invalid_argument(format!("Integer value out of range for BIGINT column '{}'", col)));
|
||||||
|
}
|
||||||
|
params.add(as_i64).map_err(|e| Status::invalid_argument(format!("Failed to add bigint parameter for {}: {}", col, e)))?;
|
||||||
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"INTEGER" => {
|
||||||
|
if let Kind::NumberValue(val) = kind {
|
||||||
|
if val.fract() != 0.0 {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected integer for column '{}', but got a float", col)));
|
||||||
|
}
|
||||||
|
let as_i32 = *val as i32;
|
||||||
|
if (as_i32 as f64) != *val {
|
||||||
|
return Err(Status::invalid_argument(format!("Integer value out of range for INTEGER column '{}'", col)));
|
||||||
|
}
|
||||||
|
params.add(as_i32).map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
||||||
|
} else {
|
||||||
|
return Err(Status::invalid_argument(format!("Expected number for column '{}'", col)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
s if s.starts_with("NUMERIC") => {
|
||||||
|
let decimal_val = match kind {
|
||||||
|
Kind::StringValue(s) => {
|
||||||
|
let trimmed = s.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(Decimal::from_str(trimmed).map_err(|_| {
|
||||||
|
Status::invalid_argument(format!(
|
||||||
|
"Invalid decimal string format for column '{}': {}",
|
||||||
|
col, s
|
||||||
|
))
|
||||||
|
})?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(Status::invalid_argument(format!(
|
||||||
|
"Expected a string representation for decimal column '{}', but received a different type.",
|
||||||
|
col
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
params.add(decimal_val).map_err(|e| {
|
||||||
|
Status::invalid_argument(format!(
|
||||||
|
"Failed to add decimal parameter for {}: {}",
|
||||||
|
col, e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
},
|
||||||
|
_ => return Err(Status::invalid_argument(format!("Unsupported type {}", sql_type))),
|
||||||
}
|
}
|
||||||
|
|
||||||
set_clauses.push(format!("\"{}\" = ${}", col, param_idx));
|
set_clauses.push(format!("\"{}\" = ${}", col, param_idx));
|
||||||
@@ -456,6 +422,7 @@ pub async fn put_table_data(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute the UPDATE query
|
||||||
let set_clause = set_clauses.join(", ");
|
let set_clause = set_clauses.join(", ");
|
||||||
let sql = format!(
|
let sql = format!(
|
||||||
"UPDATE {} SET {} WHERE id = ${} RETURNING id",
|
"UPDATE {} SET {} WHERE id = ${} RETURNING id",
|
||||||
@@ -464,8 +431,6 @@ pub async fn put_table_data(
|
|||||||
param_idx
|
param_idx
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("UPDATE SQL: {}", sql);
|
|
||||||
|
|
||||||
params.add(record_id).map_err(|e| Status::internal(format!("Failed to add record_id parameter: {}", e)))?;
|
params.add(record_id).map_err(|e| Status::internal(format!("Failed to add record_id parameter: {}", e)))?;
|
||||||
|
|
||||||
let result = sqlx::query_scalar_with::<_, i64, _>(&sql, params)
|
let result = sqlx::query_scalar_with::<_, i64, _>(&sql, params)
|
||||||
@@ -476,6 +441,7 @@ pub async fn put_table_data(
|
|||||||
Ok(Some(id)) => id,
|
Ok(Some(id)) => id,
|
||||||
Ok(None) => return Err(Status::not_found("Record not found")),
|
Ok(None) => return Err(Status::not_found("Record not found")),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
// Handle specific database errors with user-friendly messages
|
||||||
if let Some(db_err) = e.as_database_error() {
|
if let Some(db_err) = e.as_database_error() {
|
||||||
if db_err.code() == Some(std::borrow::Cow::Borrowed("22P02")) ||
|
if db_err.code() == Some(std::borrow::Cow::Borrowed("22P02")) ||
|
||||||
db_err.code() == Some(std::borrow::Cow::Borrowed("22003")) {
|
db_err.code() == Some(std::borrow::Cow::Borrowed("22003")) {
|
||||||
@@ -488,7 +454,7 @@ pub async fn put_table_data(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Indexer Command ---
|
// Queue record for search index update
|
||||||
let command = IndexCommand::AddOrUpdate(IndexCommandData {
|
let command = IndexCommand::AddOrUpdate(IndexCommandData {
|
||||||
table_name: table_name.clone(),
|
table_name: table_name.clone(),
|
||||||
row_id: updated_id,
|
row_id: updated_id,
|
||||||
@@ -501,9 +467,6 @@ pub async fn put_table_data(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("=== PUT TABLE DATA SUCCESS ===");
|
|
||||||
println!("Updated record ID: {}", updated_id);
|
|
||||||
|
|
||||||
Ok(PutTableDataResponse {
|
Ok(PutTableDataResponse {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Data updated successfully".into(),
|
message: "Data updated successfully".into(),
|
||||||
|
|||||||
Reference in New Issue
Block a user