Skip to content

better api for passing parameters to *_raw methods #684

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

Merged
merged 5 commits into from
Oct 27, 2020

Conversation

vemoo
Copy link
Contributor

@vemoo vemoo commented Oct 27, 2020

This enables passing iterators of &dyn ToSql or T: ToSql as the params argument to *_raw methods.

Adds the a helper trait BorrowToSql that is only implemented for both &dyn ToSql and T: ToSql. This trait shouldn't be implemented by users.

Maybe there's a better name for the trait, I chose it because I works as Borrow<dyn ToSql> if that was possible.

Fixes #683

@sfackler sfackler merged commit 1fcd04d into sfackler:master Oct 27, 2020
@Stargateur
Copy link

Stargateur commented Aug 13, 2021

this make impossible to use query_raw with mixed slice type.

On a second though maybe not but query_raw API is strange I need to send a concrete implementation and then the function transform it to dyn ToSql I don't really see the point.

@Stargateur
Copy link

Stargateur commented Aug 13, 2021

no I confirm this make the api way more limited to use

here a small example of what I try:

let params: &[&(dyn ToSql)] = &[&last_one, &(MAX as i64)];
client
    .query_raw(&statement, params)
    .await
    .context(PostgreSQLError)?

I explicitly add the type cause that wasn't compiling, now here the error:

error[E0277]: the size for values of type `dyn ToSql` cannot be known at compilation time
  --> src\accounts.rs:69:36
   |
69 |             .query_raw(&statement, params.iter())
   |                                    ^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn ToSql`
   = note: required because of the requirements on the impl of `ToSql` for `&dyn ToSql`
   = note: 1 redundant requirements hidden
   = note: required because of the requirements on the impl of `ToSql` for `&&dyn ToSql`
   = note: required because of the requirements on the impl of `BorrowToSql` for `&&dyn ToSql`

@Stargateur
Copy link

in the end I need to not use query_raw or implement a small enum that wrap my two type that will be them converted to dynamic dispatch... we finally do a static dynamic dispatch !

@Stargateur
Copy link

is there any actual test that prove this can be used with &dyn ToSql ?

@Stargateur
Copy link

Stargateur commented Aug 13, 2021

seem this is what is needed:

let params: [&dyn ToSql; 2] = [&last_one, &(MAX as i64)];
client
    .query_raw(&statement, params)
    .await
    .context(PostgreSQLError)?

my real problem seem to be unrelated to this, nevermind I need to dig more.

@Stargateur
Copy link

Stargateur commented Aug 13, 2021

still doesn't work I run into Sync issue and I'm unable to create a MCVE

@Stargateur
Copy link

I end up copied

fn slice_iter<'a>(
and it's work. I have literally zero clue why.

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

Successfully merging this pull request may close these issues.

limitation in how parameters are passed to *_raw methods and ToSql
3 participants