Having issue with passing numeric type in postgres rust

       let json_input = r#"[{"type":"integer","value":1}, {"type":"string","value":"hello"}, {"type":"numeric","value":3}, {"type":"boolean","value":true}]"#;
                let mut query_params: Vec<Box<dyn postgres::types::ToSql + Sync>> = Vec::new(); // Change the type to hold owned values
                if let Ok(data) = serde_json::from_str::<Value>(json_input) {
                    if let Some(json_array) = data.as_array() {
                        for json_object in json_array {
                            if let Some(json_type) = json_object.get("type") {
                                match json_type.as_str() {
                                    Some("integer") => {
                                        // Handle the case where "type" is "integer"
                                        if let Some(json_value) = json_object.get("value") {
                                            if let Some(integer_value) = json_value.as_i64() {
                                                query_params.push(Box::new(integer_value as i32)); 
                                                println!("Integer Value: {}", integer_value);
                                            }
                                        }
                                    }
                                    Some("string") => {
                                        // Handle the case where "type" is "string"
                                        if let Some(json_value) = json_object.get("value") {
                                            if let Some(string_value) = json_value.as_str() {
                                                query_params.push(Box::new(string_value.to_string()));
                                                println!("String Value: {}", string_value);
                                            }
                                        }
                                    }
                                    Some("numeric") => {
                                        // Handle the case where "type" is "float"
                                        if let Some(json_value) = json_object.get("value") {
                                            if let Some(float_value) = json_value.as_i64() {
                                               
                                                query_params.push(Box::new(float_value));
                                                println!("Numeric Value: {}", float_value);
                                            }
                                        }
                                    }
                                    Some("boolean") => {
                                        // Handle the case where "type" is "boolean"
                                        if let Some(json_value) = json_object.get("value") {
                                            if let Some(bool_value) = json_value.as_bool() {
                                                query_params.push(Box::new(bool_value));
                                                println!("Boolean Value: {}", bool_value);
                                            }
                                        }
                                    }
                                    _ => {
                                        // Handle other cases here
                                    }
                                }
                            }
                        }
                    }
                    let database_url = "postgresql://postgres:postgres@localhost:5432/db";
                    let mut client = Client::connect(&database_url, NoTls)?;
                    let query_param_refs: Vec<&(dyn postgres::types::ToSql + Sync)> = query_params.iter().map(AsRef::as_ref).collect();
                    let query_param_slice = &query_param_refs[..];

                    let result = client.query("SELECT $1::integer as param1, $2::text as param2, $3::numeric as param3, $4::boolean as param4", query_param_slice);

                    match result {
                        Ok(rows) => {
                            for row in rows {
                                println!("{:?}", row);
                                // Process the row data here
                            }
                        }
                        Err(err) => {
                            // Log the error
                            eprintln!("Error executing query: {:?}", err);
                        }
                    }
                } else {
                    eprintln!("Failed to parse JSON.");
                }

error i'm receiving
Error executing query: Error { kind: ToSql(2), cause: Some(WrongType { postgres: Numeric, rust: "i64" }) }

Not sure without spinning up Postgres itself, but isn't Numeric a floating point type, i.e. Non-integer? And your 3rd value is set as an i64. You might need to change one of those to match the other.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.