From 62b57f021faa0792e939e5c2eda7bd4d9d66bb73 Mon Sep 17 00:00:00 2001 From: meh Date: Wed, 3 Nov 2021 20:28:39 +0100 Subject: [PATCH] fix(postgres): allow rust_decimal::Decimal in PgRange (#1523) * fix(postgres): allow rust_decimal::Decimal in PgRange * test(postgres): add tests for BigDecimal and Decimal in ranges --- sqlx-core/src/postgres/types/range.rs | 25 +++++++++++++++++++++++++ tests/postgres/types.rs | 14 ++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/sqlx-core/src/postgres/types/range.rs b/sqlx-core/src/postgres/types/range.rs index 760249f79c..59f689d9c0 100644 --- a/sqlx-core/src/postgres/types/range.rs +++ b/sqlx-core/src/postgres/types/range.rs @@ -142,6 +142,17 @@ impl Type for PgRange { } } +#[cfg(feature = "decimal")] +impl Type for PgRange { + fn type_info() -> PgTypeInfo { + PgTypeInfo::NUM_RANGE + } + + fn compatible(ty: &PgTypeInfo) -> bool { + range_compatible::(ty) + } +} + #[cfg(feature = "chrono")] impl Type for PgRange { fn type_info() -> PgTypeInfo { @@ -227,6 +238,13 @@ impl Type for [PgRange] { } } +#[cfg(feature = "decimal")] +impl Type for [PgRange] { + fn type_info() -> PgTypeInfo { + PgTypeInfo::NUM_RANGE_ARRAY + } +} + #[cfg(feature = "chrono")] impl Type for [PgRange] { fn type_info() -> PgTypeInfo { @@ -288,6 +306,13 @@ impl Type for Vec> { } } +#[cfg(feature = "decimal")] +impl Type for Vec> { + fn type_info() -> PgTypeInfo { + PgTypeInfo::NUM_RANGE_ARRAY + } +} + #[cfg(feature = "chrono")] impl Type for Vec> { fn type_info() -> PgTypeInfo { diff --git a/tests/postgres/types.rs b/tests/postgres/types.rs index 932f6b81f3..7b39de8596 100644 --- a/tests/postgres/types.rs +++ b/tests/postgres/types.rs @@ -425,6 +425,13 @@ test_type!(bigdecimal(Postgres, "12345.6789::numeric" == "12345.6789".parse::().unwrap(), )); +#[cfg(feature = "bigdecimal")] +test_type!(numrange_bigdecimal>(Postgres, + "'(1.3,2.4)'::numrange" == PgRange::from( + (Bound::Excluded("1.3".parse::().unwrap()), + Bound::Excluded("2.4".parse::().unwrap()))) +)); + #[cfg(feature = "decimal")] test_type!(decimal(Postgres, "0::numeric" == sqlx::types::Decimal::from_str("0").unwrap(), @@ -436,6 +443,13 @@ test_type!(decimal(Postgres, "12345.6789::numeric" == sqlx::types::Decimal::from_str("12345.6789").unwrap(), )); +#[cfg(feature = "decimal")] +test_type!(numrange_decimal>(Postgres, + "'(1.3,2.4)'::numrange" == PgRange::from( + (Bound::Excluded(sqlx::types::Decimal::from_str("1.3").unwrap()), + Bound::Excluded(sqlx::types::Decimal::from_str("2.4").unwrap()))), +)); + const EXC2: Bound = Bound::Excluded(2); const EXC3: Bound = Bound::Excluded(3); const INC1: Bound = Bound::Included(1);