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

How to use sqlx::Type with more complex scalar types? #1493

Closed
thomaseizinger opened this issue Oct 14, 2021 · 4 comments
Closed

How to use sqlx::Type with more complex scalar types? #1493

thomaseizinger opened this issue Oct 14, 2021 · 4 comments

Comments

@thomaseizinger
Copy link

I have a Rust type that consists of multiple fields, in particular it looks like this:

pub struct BitMexPriceEventId {
    /// The timestamp this price event refers to.
    timestamp: OffsetDateTime,
    digits: usize,
}

This type has a Display and FromStr implementation and hence can represent itself as a single scalar value. I would like to store this value in the database using its string representation AND use this type directly within SQL queries (which means sqlx needs to know how to handle it).

What is the best way of supporting this? Perhaps this would work?

#[derive(sqlx::Type)]
#[sqlx(display_fromstr)] // name subject to bike-shedding
pub struct BitMexPriceEventId {
    /// The timestamp this price event refers to.
    timestamp: OffsetDateTime,
    digits: usize,
}
@thomaseizinger thomaseizinger changed the title How to use sqlx::Type with more complex scalar types How to use sqlx::Type with more complex scalar types? Oct 14, 2021
@jplatte
Copy link
Contributor

jplatte commented Oct 19, 2021

This seems more appropriate for the Q&A section of the discussions than an issue.

@thomaseizinger
Copy link
Author

This seems more appropriate for the Q&A section of the discussions than an issue.

Makes sense, can someone with mod rights move it please?

@jplatte
Copy link
Contributor

jplatte commented Oct 19, 2021

"Moving" it only copy-pastes it and and closes the issue, so you can do that too ;)

@thomaseizinger
Copy link
Author

"Moving" it only copy-pastes it and and closes the issue, so you can do that too ;)

Does it? I swear I've been redirected to discussions before that were originally issues.

In any case, I can re-create this as a discussion.

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

No branches or pull requests

2 participants