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

Support smartstring #1998

Closed
gitmalong opened this issue Jul 27, 2022 · 4 comments
Closed

Support smartstring #1998

gitmalong opened this issue Jul 27, 2022 · 4 comments

Comments

@gitmalong
Copy link

gitmalong commented Jul 27, 2022

I tried using the https://docs.rs/smartstring/1.0.1/smartstring/ crate together with sqlx but that string type is not supported when binding variables. Are there any plans to support such small string crates as optional feature?

@xfbs
Copy link

xfbs commented Jul 27, 2022

Hey @gitmalong, if I am not mistaken, you should still be able to use the smartstring crate with sqlx. As you can see from their documentation, SmartString implements AsRef<str>, which means that it dereferences into, you guessed it, the native Rust string slice type &str.

How is that useful to you? Well, it means that when you write a query, you are able to still use SmartStrings in places where sqlx expects a string. For example:

query("INSERT INTO shitty_code_examples(author, code) VALUES ('xfbs', ?)")
    .bind(&your_smart_string_here)
    .execute(&pool)
    .await?;

I hope this makes sense. If this didn't answer your question, feel free to give more context and/or show me what code you are trying to run.

@gitmalong
Copy link
Author

Hi @xfbs . Thanks for your explanation. I'll give that a try tomorrow and report back!

@abonander
Copy link
Collaborator

My response here is the same as #1922 (comment)

Every external crate we add is another semver hazard, so it needs to be very compelling to make it worthwhile. smartstring may be at 1.0 now, but what happens if/when the author releases a 2.0?

@abonander abonander closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2022
@lcmgh
Copy link

lcmgh commented Aug 1, 2022

I just gave CompactString a try

  sqlx::query(
        "
            INSERT INTO public.my_table(value, current_value) 
            SELECT *, $4 FROM UNNEST($1::text[], $2::decimal[])
        ",
    )
    .bind(data.values) # of type: Vec<CompactString, Global>
    .bind(data.numbers)
    .execute(pool)
    .await

Error:

the trait bound `for<'a> &'a [CompactString]: Encode<'_, sqlx::Postgres>` is not satisfied
the following other types implement trait `Encode<'q, DB>`:
  <&[T] as Encode<'q, sqlx::Postgres>>
  <&[u8] as Encode<'_, sqlx::Postgres>>
required because of the requirements on the impl of `Encode<'_, sqlx::Postgres>` for `Vec<CompactString>`rustc[E0277](https://doc.rust-lang.org/error-index.html#E0277)
dao.rs(23, 6): required by a bound introduced by this call
query.rs(79, 32): required by a bound in `sqlx::query::Query::<'q, DB, <DB as HasArguments<'q>>::Arguments>::bind`

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

4 participants