Skip to content

Commit 62b57f0

Browse files
authored
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
1 parent 1efbbca commit 62b57f0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

sqlx-core/src/postgres/types/range.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ impl Type<Postgres> for PgRange<bigdecimal::BigDecimal> {
142142
}
143143
}
144144

145+
#[cfg(feature = "decimal")]
146+
impl Type<Postgres> for PgRange<rust_decimal::Decimal> {
147+
fn type_info() -> PgTypeInfo {
148+
PgTypeInfo::NUM_RANGE
149+
}
150+
151+
fn compatible(ty: &PgTypeInfo) -> bool {
152+
range_compatible::<rust_decimal::Decimal>(ty)
153+
}
154+
}
155+
145156
#[cfg(feature = "chrono")]
146157
impl Type<Postgres> for PgRange<chrono::NaiveDate> {
147158
fn type_info() -> PgTypeInfo {
@@ -227,6 +238,13 @@ impl Type<Postgres> for [PgRange<bigdecimal::BigDecimal>] {
227238
}
228239
}
229240

241+
#[cfg(feature = "decimal")]
242+
impl Type<Postgres> for [PgRange<rust_decimal::Decimal>] {
243+
fn type_info() -> PgTypeInfo {
244+
PgTypeInfo::NUM_RANGE_ARRAY
245+
}
246+
}
247+
230248
#[cfg(feature = "chrono")]
231249
impl Type<Postgres> for [PgRange<chrono::NaiveDate>] {
232250
fn type_info() -> PgTypeInfo {
@@ -288,6 +306,13 @@ impl Type<Postgres> for Vec<PgRange<bigdecimal::BigDecimal>> {
288306
}
289307
}
290308

309+
#[cfg(feature = "decimal")]
310+
impl Type<Postgres> for Vec<PgRange<rust_decimal::Decimal>> {
311+
fn type_info() -> PgTypeInfo {
312+
PgTypeInfo::NUM_RANGE_ARRAY
313+
}
314+
}
315+
291316
#[cfg(feature = "chrono")]
292317
impl Type<Postgres> for Vec<PgRange<chrono::NaiveDate>> {
293318
fn type_info() -> PgTypeInfo {

tests/postgres/types.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ test_type!(bigdecimal<sqlx::types::BigDecimal>(Postgres,
425425
"12345.6789::numeric" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(),
426426
));
427427

428+
#[cfg(feature = "bigdecimal")]
429+
test_type!(numrange_bigdecimal<PgRange<sqlx::types::BigDecimal>>(Postgres,
430+
"'(1.3,2.4)'::numrange" == PgRange::from(
431+
(Bound::Excluded("1.3".parse::<sqlx::types::BigDecimal>().unwrap()),
432+
Bound::Excluded("2.4".parse::<sqlx::types::BigDecimal>().unwrap())))
433+
));
434+
428435
#[cfg(feature = "decimal")]
429436
test_type!(decimal<sqlx::types::Decimal>(Postgres,
430437
"0::numeric" == sqlx::types::Decimal::from_str("0").unwrap(),
@@ -436,6 +443,13 @@ test_type!(decimal<sqlx::types::Decimal>(Postgres,
436443
"12345.6789::numeric" == sqlx::types::Decimal::from_str("12345.6789").unwrap(),
437444
));
438445

446+
#[cfg(feature = "decimal")]
447+
test_type!(numrange_decimal<PgRange<sqlx::types::Decimal>>(Postgres,
448+
"'(1.3,2.4)'::numrange" == PgRange::from(
449+
(Bound::Excluded(sqlx::types::Decimal::from_str("1.3").unwrap()),
450+
Bound::Excluded(sqlx::types::Decimal::from_str("2.4").unwrap()))),
451+
));
452+
439453
const EXC2: Bound<i32> = Bound::Excluded(2);
440454
const EXC3: Bound<i32> = Bound::Excluded(3);
441455
const INC1: Bound<i32> = Bound::Included(1);

0 commit comments

Comments
 (0)