Skip to content

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

Discussion options

You must be logged in to vote

Found a way that works:

macro_rules! impl_sqlx_type_display_from_str {
    ($ty:ty) => {
        impl sqlx::Type<sqlx::Sqlite> for $ty {
            fn type_info() -> sqlx::sqlite::SqliteTypeInfo {
                String::type_info()
            }
        }

        impl<'q> sqlx::Encode<'q, sqlx::Sqlite> for $ty {
            fn encode_by_ref(
                &self,
                args: &mut Vec<sqlx::sqlite::SqliteArgumentValue<'q>>,
            ) -> sqlx::encode::IsNull {
                self.to_string().encode_by_ref(args)
            }
        }

        impl<'r> sqlx::Decode<'r, sqlx::Sqlite> for $ty {
            fn decode(
                value: sqlx::sqlite::SqliteValueRef<'r>,
…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by thomaseizinger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant