fixing serialization and deserialization in the data insertion
This commit is contained in:
@@ -5,22 +5,7 @@ use sqlx::{PgPool, Transaction, Postgres};
|
||||
use serde_json::json;
|
||||
use common::proto::komp_ac::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
||||
use common::proto::komp_ac::table_definition::ColumnDefinition;
|
||||
|
||||
// TODO CRITICAL add decimal with optional precision"
|
||||
const PREDEFINED_FIELD_TYPES: &[(&str, &str)] = &[
|
||||
("text", "TEXT"),
|
||||
("string", "TEXT"),
|
||||
("boolean", "BOOLEAN"),
|
||||
("timestamp", "TIMESTAMPTZ"),
|
||||
("timestamptz", "TIMESTAMPTZ"),
|
||||
("time", "TIMESTAMPTZ"),
|
||||
("money", "NUMERIC(14, 4)"),
|
||||
("integer", "INTEGER"),
|
||||
("int", "INTEGER"),
|
||||
("biginteger", "BIGINT"),
|
||||
("bigint", "BIGINT"),
|
||||
("date", "DATE"),
|
||||
];
|
||||
use crate::table_definition::models::map_field_type;
|
||||
|
||||
// NEW: Helper function to provide detailed error messages
|
||||
fn validate_identifier_format(s: &str, identifier_type: &str) -> Result<(), Status> {
|
||||
@@ -59,116 +44,6 @@ fn validate_identifier_format(s: &str, identifier_type: &str) -> Result<(), Stat
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_decimal_number_format(num_str: &str, param_name: &str) -> Result<(), Status> {
|
||||
if num_str.is_empty() {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} cannot be empty",
|
||||
param_name
|
||||
)));
|
||||
}
|
||||
|
||||
// Check for explicit signs
|
||||
if num_str.starts_with('+') || num_str.starts_with('-') {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} cannot have explicit positive or negative signs",
|
||||
param_name
|
||||
)));
|
||||
}
|
||||
|
||||
// Check for decimal points
|
||||
if num_str.contains('.') {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} must be a whole number (no decimal points)",
|
||||
param_name
|
||||
)));
|
||||
}
|
||||
|
||||
// Check for leading zeros (but allow "0" itself)
|
||||
if num_str.len() > 1 && num_str.starts_with('0') {
|
||||
let trimmed = num_str.trim_start_matches('0');
|
||||
let suggestion = if trimmed.is_empty() { "0" } else { trimmed };
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} cannot have leading zeros (use '{}' instead of '{}')",
|
||||
param_name,
|
||||
suggestion,
|
||||
num_str
|
||||
)));
|
||||
}
|
||||
|
||||
// Check that all characters are digits
|
||||
if !num_str.chars().all(|c| c.is_ascii_digit()) {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} contains invalid characters. Only digits 0-9 are allowed",
|
||||
param_name
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn map_field_type(field_type: &str) -> Result<String, Status> {
|
||||
let lower_field_type = field_type.to_lowercase();
|
||||
|
||||
// Special handling for "decimal(precision, scale)"
|
||||
if lower_field_type.starts_with("decimal(") && lower_field_type.ends_with(')') {
|
||||
// Extract the part inside the parentheses, e.g., "10, 2"
|
||||
let args = lower_field_type
|
||||
.strip_prefix("decimal(")
|
||||
.and_then(|s| s.strip_suffix(')'))
|
||||
.unwrap_or(""); // Should always succeed due to the checks above
|
||||
|
||||
// Split into precision and scale parts
|
||||
if let Some((p_str, s_str)) = args.split_once(',') {
|
||||
let precision_str = p_str.trim();
|
||||
let scale_str = s_str.trim();
|
||||
|
||||
// NEW: Validate format BEFORE parsing
|
||||
validate_decimal_number_format(precision_str, "precision")?;
|
||||
validate_decimal_number_format(scale_str, "scale")?;
|
||||
|
||||
// Parse precision, returning an error if it's not a valid number
|
||||
let precision = precision_str.parse::<u32>().map_err(|_| {
|
||||
Status::invalid_argument("Invalid precision in decimal type")
|
||||
})?;
|
||||
|
||||
// Parse scale, returning an error if it's not a valid number
|
||||
let scale = scale_str.parse::<u32>().map_err(|_| {
|
||||
Status::invalid_argument("Invalid scale in decimal type")
|
||||
})?;
|
||||
|
||||
// Add validation based on PostgreSQL rules
|
||||
if precision < 1 {
|
||||
return Err(Status::invalid_argument("Precision must be at least 1"));
|
||||
}
|
||||
if scale > precision {
|
||||
return Err(Status::invalid_argument(
|
||||
"Scale cannot be greater than precision",
|
||||
));
|
||||
}
|
||||
|
||||
// If everything is valid, build and return the NUMERIC type string
|
||||
return Ok(format!("NUMERIC({}, {})", precision, scale));
|
||||
} else {
|
||||
// The format was wrong, e.g., "decimal(10)" or "decimal()"
|
||||
return Err(Status::invalid_argument(
|
||||
"Invalid decimal format. Expected: decimal(precision, scale)",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// If not a decimal, fall back to the predefined list
|
||||
PREDEFINED_FIELD_TYPES
|
||||
.iter()
|
||||
.find(|(key, _)| *key == lower_field_type.as_str())
|
||||
.map(|(_, sql_type)| sql_type.to_string()) // Convert to an owned String
|
||||
.ok_or_else(|| {
|
||||
Status::invalid_argument(format!(
|
||||
"Invalid field type: {}",
|
||||
field_type
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
fn is_invalid_table_name(table_name: &str) -> bool {
|
||||
table_name.ends_with("_id") ||
|
||||
table_name == "id" ||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// src/table_definition/models.rs
|
||||
use tonic::Status;
|
||||
|
||||
/// Predefined static field mappings
|
||||
// TODO CRITICAL add decimal with optional precision"
|
||||
pub const PREDEFINED_FIELD_TYPES: &[(&str, &str)] = &[
|
||||
("text", "TEXT"),
|
||||
("string", "TEXT"),
|
||||
("boolean", "BOOLEAN"),
|
||||
("timestamp", "TIMESTAMPTZ"),
|
||||
("timestamptz", "TIMESTAMPTZ"),
|
||||
("time", "TIMESTAMPTZ"),
|
||||
("money", "NUMERIC(14, 4)"),
|
||||
("integer", "INTEGER"),
|
||||
("int", "INTEGER"),
|
||||
("biginteger", "BIGINT"),
|
||||
("bigint", "BIGINT"),
|
||||
("date", "DATE"),
|
||||
];
|
||||
|
||||
/// reusable decimal number validation
|
||||
pub fn validate_decimal_number_format(num_str: &str, param_name: &str) -> Result<(), Status> {
|
||||
if num_str.is_empty() {
|
||||
return Err(Status::invalid_argument(format!("{} cannot be empty", param_name)));
|
||||
}
|
||||
if num_str.starts_with('+') || num_str.starts_with('-') {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} cannot have explicit positive/negative signs", param_name
|
||||
)));
|
||||
}
|
||||
if num_str.contains('.') {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} must be a whole number (no decimal point)", param_name
|
||||
)));
|
||||
}
|
||||
if num_str.len() > 1 && num_str.starts_with('0') {
|
||||
let trimmed = num_str.trim_start_matches('0');
|
||||
let suggestion = if trimmed.is_empty() { "0" } else { trimmed };
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} cannot have leading zeros (use '{}' instead of '{}')",
|
||||
param_name, suggestion, num_str
|
||||
)));
|
||||
}
|
||||
if !num_str.chars().all(|c| c.is_ascii_digit()) {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"{} contains invalid characters. Only digits allowed", param_name
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// reusable field type mapper
|
||||
pub fn map_field_type(field_type: &str) -> Result<String, Status> {
|
||||
let lower_field_type = field_type.to_lowercase();
|
||||
|
||||
if lower_field_type.starts_with("decimal(") && lower_field_type.ends_with(')') {
|
||||
let args = lower_field_type.strip_prefix("decimal(").unwrap()
|
||||
.strip_suffix(')').unwrap();
|
||||
|
||||
if let Some((p_str, s_str)) = args.split_once(',') {
|
||||
let precision_str = p_str.trim();
|
||||
let scale_str = s_str.trim();
|
||||
|
||||
validate_decimal_number_format(precision_str, "precision")?;
|
||||
validate_decimal_number_format(scale_str, "scale")?;
|
||||
|
||||
let precision = precision_str.parse::<u32>()
|
||||
.map_err(|_| Status::invalid_argument("Invalid precision"))?;
|
||||
let scale = scale_str.parse::<u32>()
|
||||
.map_err(|_| Status::invalid_argument("Invalid scale"))?;
|
||||
|
||||
if precision < 1 {
|
||||
return Err(Status::invalid_argument("Precision must be >= 1"));
|
||||
}
|
||||
if scale > precision {
|
||||
return Err(Status::invalid_argument("Scale cannot be > precision"));
|
||||
}
|
||||
return Ok(format!("NUMERIC({}, {})", precision, scale));
|
||||
} else {
|
||||
return Err(Status::invalid_argument(
|
||||
"Invalid decimal format. Expected decimal(precision, scale)"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
PREDEFINED_FIELD_TYPES
|
||||
.iter()
|
||||
.find(|(key, _)| *key == lower_field_type.as_str())
|
||||
.map(|(_, sql_type)| sql_type.to_string())
|
||||
.ok_or_else(|| Status::invalid_argument(format!("Invalid field type: {}", field_type)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user