Skip to content

Make sqlx::Type object safe by taking &self #1461

@tqwewe

Description

@tqwewe

The trait sqlx::Type is not object safe, as it does not take &self in it's two methods fn type_info() -> TypeInfo and fn compatible(ty: TypeInfo) -> bool.

This is a major issue in a project I'm working on (sea-query & sea-orm), as in my case I am passing values as dyn Trait objects, but cannot include sqlx::Type in the trait objects.

For example, my trait has:

trait QueryValue: sqlx::Encode<'static, sqlx::MySql> + sqlx::Type<sqlx::MySql> {
    fn query_value(&self, query_builder: &dyn QueryBuilder) -> String;
}

But this does not work as sqlx::Type cannot be made into a trait object.

Another example would be if you had an enum containing many database types:

enum Value {
    Bool(Option<bool>),
    TinyInt(Option<i8>),
    SmallInt(Option<i16>),
    String(Option<Box<String>>),
    ...
    Custom(Box<dyn QueryValue>),
}

This cannot be done without adding a generic to the Value enum, which in my case needs to be avoided.

Is there any kind of workaround possible for this situation? My main use for QueryValue is to pass it to .bind(value) for use in queries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions