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

Usability improvements to Arguments, IntoArguments traits #3586

Open
zdenek-crha opened this issue Oct 31, 2024 · 0 comments
Open

Usability improvements to Arguments, IntoArguments traits #3586

zdenek-crha opened this issue Oct 31, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@zdenek-crha
Copy link

zdenek-crha commented Oct 31, 2024

I'm trying to create 'query builder function' that is generic over database type and need to convert input struct into query binds. Having impl IntoArguments for Args trait and adding trait bound is a solution, but implementing the trait without panicking is problem:

  • IntoArguments is infallible and can't propagate errors to caller
  • Arguments::add() method is fallilble and does not have infallible alternative
  • code has to use unwrap() and panic on Encode error to compile
pub struct Args {
    pub id: i32,
    // ... many more fields ...
}

impl<'q> sqlx::IntoArguments<'q, Postgres> for Args {
    fn into_arguments(self) -> PgArguments {
        let mut args = PgArguments::default();
        args.add(self.id).unwrap();
        // ... more fields ...
        args
    }
}

async fn make_query<D: Database>(args: Args) -> QueryAs<D, Data, Args>
    Args: sqlx::IntoArguments<'q, D>,
    Data: for<'r> sqlx::FromRow<'r, D::Row>,
{
    sqlx::query_as_with(r#"SQL"#, args)
}

Describe the solution you'd like

I see several ways the usability could be improved. I don't see deep enough into sqlx code to say what is possible though:

  1. Store encoding errors in add() function and defer their handling to query execution. The Query already does something like that to allow bind() method to be infallible.

  2. Have TryIntoArgumentds trait and integrate it into sqlx query API. With blanked impl for anything implementing IntoArguments, it might (not sure though) even be drop-in replacement for query_*() functions trait bounds.

Describe alternatives you've considered

I've tried to prototype TryIntoArguments trait as temporary workaround and got it looks like it might work. But it does have few paper-cuts:

It does not integrate into rest of the sqlx query API, like sqlx::query*() functions.

It needs 'magic looking' trait bound linking Database::Arguments and IntoArguments trait that is seemingly not related to the function, not necessary but still required to compile.

pub trait TryIntoArguments<'q, D: Database>: Sized + Send {
    fn try_into_arguments(self) -> Result<D::Arguments<'q>, BoxError>;
}

async fn try_make_query_with<'q, D: Database>(args: Args) -> Result<QueryAs<'q, D, Data, D::Arguments<'q>>, BoxError>
where
    D::Arguments<'q>: sqlx::IntoArguments<'q, D>, // magic trait bound
    Args: TryIntoArguments<'q, D>,
    Data: for<'r> sqlx::FromRow<'r, D::Row>,
{
    let args = args.try_into_arguments()?;
    Ok(sqlx::query_as_with(r#"SQL"#, args))
}

The 'magic trait' bound is most likely needed because neither of following is true. But I can be completely mistaken, sqlx trait bounds often leave me scratching my head :-D

  • IntoArguments impl is not part of Database::Arguments type trait bounds
  • there is no blanket impl linking types implementing Arguments<Database=D> with IntoArguments<DB>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant