Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions sqlx-core/src/postgres/types/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ impl Type<Postgres> for PgRange<bigdecimal::BigDecimal> {
}
}

#[cfg(feature = "decimal")]
impl Type<Postgres> for PgRange<rust_decimal::Decimal> {
fn type_info() -> PgTypeInfo {
PgTypeInfo::NUM_RANGE
}

fn compatible(ty: &PgTypeInfo) -> bool {
range_compatible::<rust_decimal::Decimal>(ty)
}
}

#[cfg(feature = "chrono")]
impl Type<Postgres> for PgRange<chrono::NaiveDate> {
fn type_info() -> PgTypeInfo {
Expand Down Expand Up @@ -227,6 +238,13 @@ impl Type<Postgres> for [PgRange<bigdecimal::BigDecimal>] {
}
}

#[cfg(feature = "decimal")]
impl Type<Postgres> for [PgRange<rust_decimal::Decimal>] {
fn type_info() -> PgTypeInfo {
PgTypeInfo::NUM_RANGE_ARRAY
}
}

#[cfg(feature = "chrono")]
impl Type<Postgres> for [PgRange<chrono::NaiveDate>] {
fn type_info() -> PgTypeInfo {
Expand Down Expand Up @@ -288,6 +306,13 @@ impl Type<Postgres> for Vec<PgRange<bigdecimal::BigDecimal>> {
}
}

#[cfg(feature = "decimal")]
impl Type<Postgres> for Vec<PgRange<rust_decimal::Decimal>> {
fn type_info() -> PgTypeInfo {
PgTypeInfo::NUM_RANGE_ARRAY
}
}

#[cfg(feature = "chrono")]
impl Type<Postgres> for Vec<PgRange<chrono::NaiveDate>> {
fn type_info() -> PgTypeInfo {
Expand Down
14 changes: 14 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ test_type!(bigdecimal<sqlx::types::BigDecimal>(Postgres,
"12345.6789::numeric" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(),
));

#[cfg(feature = "bigdecimal")]
test_type!(numrange_bigdecimal<PgRange<sqlx::types::BigDecimal>>(Postgres,
"'(1.3,2.4)'::numrange" == PgRange::from(
(Bound::Excluded("1.3".parse::<sqlx::types::BigDecimal>().unwrap()),
Bound::Excluded("2.4".parse::<sqlx::types::BigDecimal>().unwrap())))
));

#[cfg(feature = "decimal")]
test_type!(decimal<sqlx::types::Decimal>(Postgres,
"0::numeric" == sqlx::types::Decimal::from_str("0").unwrap(),
Expand All @@ -436,6 +443,13 @@ test_type!(decimal<sqlx::types::Decimal>(Postgres,
"12345.6789::numeric" == sqlx::types::Decimal::from_str("12345.6789").unwrap(),
));

#[cfg(feature = "decimal")]
test_type!(numrange_decimal<PgRange<sqlx::types::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<i32> = Bound::Excluded(2);
const EXC3: Bound<i32> = Bound::Excluded(3);
const INC1: Bound<i32> = Bound::Included(1);
Expand Down