-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
postgres: Create PgInterval type #271
Conversation
Can you add some tests to |
@@ -235,6 +236,38 @@ fn postgres_epoch() -> DateTime<Utc> { | |||
Utc.ymd(2000, 1, 1).and_hms(0, 0, 0) | |||
} | |||
|
|||
impl TryInto<Duration> for PgInterval { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reasonably sure that you want to impl TryFrom<PgInterval> for Duration
instead.
} | ||
|
||
impl PgInterval { | ||
pub fn new(months: i32, days: i32, microseconds: i64) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are making the fields of PgInterval
public, there isn't much point to have the new
here.
sqlx-core/src/postgres/types/time.rs
Outdated
@@ -252,6 +256,40 @@ impl Encode<Postgres> for OffsetDateTime { | |||
} | |||
} | |||
|
|||
impl TryInto<Duration> for PgInterval { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The chrono and time files are already pretty big. This impls might be better in interval.rs
with the appropriate cfg
flags. Just a minor thought.
This may actually cause a lot of errors if people are inserting |
That is a good idea, I think that replacing the TryFrom behavior by silencing this type of issue could generate even more bugs that go unnoticed. So I would think that, as you said, adding a new constructor would be better. And I should add a bit of documentation to highlight the difference between the two. I have one question however, I not that seasoned in Rust and I'm not sure how to create a function Thank you |
I've created two functions I'm not sure if it is the right thing to do. Please let me know how I can improve that. |
@dimtion Sorry for the delay, would you mind providing impls for As for
|
I don't think this impl is a good idea. As the Interval type is a proper interval and not just a duration, attempting to treat a Duration as such is error-prone at best. In the future, the time crate will provide for a Period type that could be properly mapped to the Interval appropriately. There's no timeframe on this, but I thought it would be best to make my thoughts known before this potentially lands. |
@jhpratt what you says makes a lot of sense, and is indeed the core of the issue I faced. An interval/period is not the same duration, and converting between the two must not be an implicit thing. @abonander do you think creating a PR only providing the |
To be clear, I don't think converting a Obviously you're free to do as you wish, but that's my perspective from the time crate. |
I have to agree with @jhpratt here about the
|
Ok, so I've rebased this branch with master:
Let me know if this implementation is better. |
Looking at the overall diff, it looks good to me. I just checked the bits relevant to the time crate, though. One but, I believe it would be more efficient to check if the nanos % 1000 == 0 instead of performing more complicated arithmetic. |
@dimtion Thank you ❤️ for sticking with this. I merged it and narrowed down the public API further. We can always add methods to it if it becomes a pattern people want ( the @jhpratt Thank you as well for reviewing this as it moved along. |
This PR creates a new type binding for PostgreSQL INTERVAL type.
Fixes #197
Some notes:
sqlx::postgres::types::PgInterval
which is fully compatible with PG INTERVAL typeTryFrom
forchrono::Duration
andtime::Duration
, theses conversions fail if there is an overflow, if using incompatible fields (PG days and month fields) or if there is loss of precision by using nanoseconds.