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

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

Closed
tqwewe opened this issue Sep 30, 2021 · 2 comments
Closed

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

tqwewe opened this issue Sep 30, 2021 · 2 comments

Comments

@tqwewe
Copy link

tqwewe commented Sep 30, 2021

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.

@ikrivosheev
Copy link

Hello! Any updates?)

@abonander
Copy link
Collaborator

The Type trait is not intended to be object-safe. I suggest working around it another way.

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

3 participants