diff --git a/db/test.db b/db/test.db index 939858e8e..ae57c8508 100644 Binary files a/db/test.db and b/db/test.db differ diff --git a/src/connector/postgres/conversion.rs b/src/connector/postgres/conversion.rs index e8da0e564..8cb43c94d 100644 --- a/src/connector/postgres/conversion.rs +++ b/src/connector/postgres/conversion.rs @@ -511,36 +511,28 @@ impl<'a> ToSql for Value<'a> { (Value::Integer(integer), &PostgresType::OID) => integer.map(|integer| (integer as u32).to_sql(ty, out)), (Value::Integer(integer), _) => integer.map(|integer| (integer as i64).to_sql(ty, out)), (Value::Float(float), &PostgresType::FLOAT8) => float.map(|float| (float as f64).to_sql(ty, out)), - #[cfg(feature = "bigdecimal")] - (Value::Float(float), &PostgresType::NUMERIC) => float - .map(|float| BigDecimal::from_f32(float).unwrap()) - .map(DecimalWrapper) - .map(|dw| dw.to_sql(ty, out)), + (Value::Float(_), &PostgresType::NUMERIC) => { + let kind = ErrorKind::conversion(format!( + "Writing a float to a {} column is unstable.", + PostgresType::NUMERIC + )); + Err(Error::builder(kind).build())? + } (Value::Float(float), _) => float.map(|float| float.to_sql(ty, out)), (Value::Double(double), &PostgresType::FLOAT4) => double.map(|double| (double as f32).to_sql(ty, out)), - #[cfg(feature = "bigdecimal")] - (Value::Double(double), &PostgresType::NUMERIC) => double - .map(|double| BigDecimal::from_f64(double).unwrap()) - .map(DecimalWrapper) - .map(|dw| dw.to_sql(ty, out)), + (Value::Double(_), &PostgresType::NUMERIC) => { + let kind = ErrorKind::conversion(format!( + "Writing a double to a {} column is unstable.", + PostgresType::NUMERIC + )); + Err(Error::builder(kind).build())? + } (Value::Double(double), _) => double.map(|double| double.to_sql(ty, out)), - #[cfg(feature = "bigdecimal")] - (Value::Numeric(decimal), &PostgresType::FLOAT4) => decimal.as_ref().map(|decimal| { - let f = decimal.to_string().parse::().expect("decimal to f32 conversion"); - f.to_sql(ty, out) - }), - #[cfg(feature = "bigdecimal")] - (Value::Numeric(decimal), &PostgresType::FLOAT8) => decimal.as_ref().map(|decimal| { - let f = decimal.to_string().parse::().expect("decimal to f64 conversion"); - f.to_sql(ty, out) - }), - #[cfg(feature = "bigdecimal")] (Value::Array(values), &PostgresType::FLOAT4_ARRAY) => values.as_ref().map(|values| { let mut floats = Vec::with_capacity(values.len()); for value in values.iter() { let float = match value { - Value::Numeric(n) => n.as_ref().and_then(|n| n.to_string().parse::().ok()), Value::Float(f) => *f, Value::Double(d) => d.map(|d| d as f32), v => { @@ -558,13 +550,11 @@ impl<'a> ToSql for Value<'a> { floats.to_sql(ty, out) }), - #[cfg(feature = "bigdecimal")] (Value::Array(values), &PostgresType::FLOAT8_ARRAY) => values.as_ref().map(|values| { let mut floats = Vec::with_capacity(values.len()); for value in values.iter() { let float = match value { - Value::Numeric(n) => n.as_ref().and_then(|n| n.to_string().parse::().ok()), Value::Float(f) => f.map(|f| f as f64), Value::Double(d) => *d, v => { @@ -598,9 +588,10 @@ impl<'a> ToSql for Value<'a> { .as_ref() .map(|decimal| DecimalWrapper(decimal.clone()).to_sql(ty, out)), #[cfg(feature = "bigdecimal")] - (Value::Numeric(float), _) => float - .as_ref() - .map(|float| DecimalWrapper(float.clone()).to_sql(ty, out)), + (Value::Numeric(_), typ) => { + let kind = ErrorKind::conversion(format!("Writing a decimal to a {} column is unstable.", typ)); + Err(Error::builder(kind).build())? + } #[cfg(feature = "uuid")] (Value::Text(string), &PostgresType::UUID) => string.as_ref().map(|string| { let parsed_uuid: Uuid = string.parse()?; diff --git a/src/tests/query.rs b/src/tests/query.rs index 769877b1c..3848efd10 100644 --- a/src/tests/query.rs +++ b/src/tests/query.rs @@ -1896,7 +1896,6 @@ async fn ints_read_write_to_numeric(api: &mut dyn TestApi) -> crate::Result<()> let table = api.create_table("id int, value numeric(12,2)").await?; let insert = Insert::multi_into(&table, &["id", "value"]) - .values(vec![Value::integer(1), Value::double(1234.5)]) .values(vec![Value::integer(2), Value::integer(1234)]) .values(vec![Value::integer(3), Value::integer(12345)]); @@ -1907,37 +1906,10 @@ async fn ints_read_write_to_numeric(api: &mut dyn TestApi) -> crate::Result<()> for (i, row) in rows.into_iter().enumerate() { match i { - 0 => assert_eq!(Value::numeric(BigDecimal::from_str("1234.5").unwrap()), row["value"]), - 1 => assert_eq!(Value::numeric(BigDecimal::from_str("1234.0").unwrap()), row["value"]), + 0 => assert_eq!(Value::numeric(BigDecimal::from_str("1234.0").unwrap()), row["value"]), _ => assert_eq!(Value::numeric(BigDecimal::from_str("12345.0").unwrap()), row["value"]), } } Ok(()) } - -#[cfg(feature = "bigdecimal")] -#[test_each_connector(tags("postgresql"))] -async fn bigdecimal_read_write_to_floating(api: &mut dyn TestApi) -> crate::Result<()> { - use bigdecimal::BigDecimal; - use std::str::FromStr; - - let table = api.create_table("id int, a float4, b float8").await?; - let val = BigDecimal::from_str("0.1").unwrap(); - - let insert = Insert::multi_into(&table, &["id", "a", "b"]).values(vec![ - Value::integer(1), - Value::numeric(val.clone()), - Value::numeric(val.clone()), - ]); - - api.conn().execute(insert.into()).await?; - - let select = Select::from_table(&table); - let row = api.conn().select(select).await?.into_single()?; - - assert_eq!(Value::float(0.1), row["a"]); - assert_eq!(Value::double(0.1), row["b"]); - - Ok(()) -} diff --git a/src/tests/types/postgres/bigdecimal.rs b/src/tests/types/postgres/bigdecimal.rs index ca99857fd..2cbc1c9e0 100644 --- a/src/tests/types/postgres/bigdecimal.rs +++ b/src/tests/types/postgres/bigdecimal.rs @@ -186,23 +186,3 @@ test_type!(money_array( Value::Array(None), Value::array(vec![BigDecimal::from_str("1.12")?, BigDecimal::from_str("1.12")?]) )); - -test_type!(float4( - postgresql, - "float4", - (Value::Numeric(None), Value::Float(None)), - ( - Value::numeric(BigDecimal::from_str("1.123456")?), - Value::float(1.123456) - ) -)); - -test_type!(float8( - postgresql, - "float8", - (Value::Numeric(None), Value::Double(None)), - ( - Value::numeric(BigDecimal::from_str("1.123456")?), - Value::double(1.123456) - ) -));