Skip to content
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

sqlite date macro support #2491

Merged
merged 6 commits into from
Jun 30, 2023
Merged

sqlite date macro support #2491

merged 6 commits into from
Jun 30, 2023

Conversation

Arcayr
Copy link
Contributor

@Arcayr Arcayr commented May 6, 2023

Adds DATE pseudo-type support for sqlite into the macros for both chrono and time.

Just adding the few lines mentioned in #2026 (comment).

I'm aware sqlite has no actual support for the types and simply treats them as strings, however given we already kinda support it in sqlx I figured it may as well be integrated.

…lite library.

Presently this only functions for the `time` feature, because I don't use the `chrono` feature.
Add chrono date support.
@abonander
Copy link
Collaborator

@Arcayr please address CI failures.

@Arcayr
Copy link
Contributor Author

Arcayr commented May 14, 2023

All checks passed now. Thanks!

@Arcayr
Copy link
Contributor Author

Arcayr commented May 28, 2023

I've actually noticed that this breaks applications relying on serialising time, and probably in a couple of other ways, due to the compiler trying to turn all time types from OffsetDateTime into PrimitiveDateTime.

Switching the entries around in the file modified in the original PR fixes it. I haven't poked deep enough into the sqlx macro generation to work out why. If anyone has a pointer for a fix I can get it incorporated.

@abonander
Copy link
Collaborator

@Arcayr it's pretty dumb: it just tries the types in order and checks https://docs.rs/sqlx/latest/sqlx/trait.Type.html#method.compatible

So if you place the new entry before another compatible type, it will take precedent.

Since SQLite doesn't have dedicated data types for dates and times, we use the string of the declared column type:

"date" => DataType::Date,

@Arcayr
Copy link
Contributor Author

Arcayr commented May 29, 2023

@Arcayr it's pretty dumb: it just tries the types in order and checks https://docs.rs/sqlx/latest/sqlx/trait.Type.html#method.compatible

I thought that was the case but didn't want to presume, I haven't been around Rust code long enough yet. :)

So if you place the new entry before another compatible type, it will take precedent.
Since SQLite doesn't have dedicated data types for dates and times, we use the string of the declared column type:

"date" => DataType::Date,

In this case, I imagine it'd be fine just using OffsetDateTime instead of PrimitiveDateTime, as you get conversion issues the other way. I'll update the PR accordingly and rebase if you agree.

(One alternative would perhaps be to use something like datetimetz, similar to Postgres's timestamptz, but that'd be departing even further from the fact that SQLite doesn't have native date/time support.)

Switch order of time::OffsetDateTime and time::PrimitiveDateTime.
@Arcayr
Copy link
Contributor Author

Arcayr commented Jun 19, 2023

I ended up switching them, as OffsetDateTime is a superset of PrimitiveDateTime in all senses anyway. This should be ready to merge if you're happy with it. Thanks!

@abonander abonander merged commit 4b254ea into launchbadge:main Jun 30, 2023
53 checks passed
@abonander
Copy link
Collaborator

Changing the order of existing declarations is technically a breaking change but in this case it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants