Skip to content
Open
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
45 changes: 45 additions & 0 deletions sqlx-postgres/src/types/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ pub struct PgInterval {
pub microseconds: i64,
}

impl PgInterval {
pub const INFINITY: Self = Self {
months: i32::MAX,
days: i32::MAX,
microseconds: i64::MAX,
};

pub const NEG_INFINITY: Self = Self {
months: i32::MIN,
days: i32::MIN,
microseconds: i64::MIN,
};

pub fn is_infinite(&self) -> bool {
*self == Self::INFINITY || *self == Self::NEG_INFINITY
}

pub fn is_positive_infinity(&self) -> bool {
*self == Self::INFINITY
}

pub fn is_negative_infinity(&self) -> bool {
*self == Self::NEG_INFINITY
}
}

impl Type<Postgres> for PgInterval {
fn type_info() -> PgTypeInfo {
PgTypeInfo::INTERVAL
Expand Down Expand Up @@ -330,6 +356,25 @@ fn test_pginterval_std() {
assert!(PgInterval::try_from(std::time::Duration::from_secs(20_000_000_000_000)).is_err());
}

#[test]
fn test_pginterval_infinity() {
assert!(PgInterval::INFINITY.is_infinite());
assert!(PgInterval::NEG_INFINITY.is_infinite());
assert!(PgInterval::INFINITY.is_positive_infinity());
assert!(!PgInterval::NEG_INFINITY.is_positive_infinity());
assert!(!PgInterval::INFINITY.is_negative_infinity());
assert!(PgInterval::NEG_INFINITY.is_negative_infinity());
assert!(!PgInterval::default().is_infinite());

// verify values match what PostgreSQL expects
assert_eq!(PgInterval::INFINITY.microseconds, i64::MAX);
assert_eq!(PgInterval::INFINITY.days, i32::MAX);
assert_eq!(PgInterval::INFINITY.months, i32::MAX);
assert_eq!(PgInterval::NEG_INFINITY.microseconds, i64::MIN);
assert_eq!(PgInterval::NEG_INFINITY.days, i32::MIN);
assert_eq!(PgInterval::NEG_INFINITY.months, i32::MIN);
}

#[test]
#[cfg(feature = "chrono")]
fn test_pginterval_chrono() {
Expand Down
4 changes: 4 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ test_prepared_type!(interval<PgInterval>(
days: 0,
microseconds: (3 * 3_600 + 10 * 60 + 20) * 1_000_000 + 116100
},
"INTERVAL 'infinity'"
== PgInterval::INFINITY,
"INTERVAL '-infinity'"
== PgInterval::NEG_INFINITY,
));
Comment on lines +668 to 672

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have cfg-flags for gating tests like this:

Suggested change
"INTERVAL 'infinity'"
== PgInterval::INFINITY,
"INTERVAL '-infinity'"
== PgInterval::NEG_INFINITY,
));
));
// Not supported before Postgres 17
#[cfg(any(postgres = "17", postgres = "18"))]
test_prepared_type!(interval_infinity<PgInterval>(
Postgres,
"INTERVAL 'infinity'"
== PgInterval::INFINITY,
"INTERVAL '-infinity'"
== PgInterval::NEG_INFINITY,
));


test_prepared_type!(money<PgMoney>(Postgres, "123.45::money" == PgMoney(12345)));
Expand Down
Loading