You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
pubstructArgs{pubid:i32,// ... many more fields ...}impl<'q> sqlx::IntoArguments<'q,Postgres>forArgs{fninto_arguments(self) -> PgArguments{letmut args = PgArguments::default();
args.add(self.id).unwrap();// ... more fields ...
args
}}asyncfn 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:
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.
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.
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>
The text was updated successfully, but these errors were encountered:
I'm trying to create 'query builder function' that is generic over database type and need to convert input
struct
into query binds. Havingimpl 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 callerArguments::add()
method is fallilble and does not have infallible alternativeunwrap()
and panic onEncode
error to compileDescribe 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:
Store encoding errors in
add()
function and defer their handling to query execution. TheQuery
already does something like that to allowbind()
method to be infallible.Have
TryIntoArgumentds
trait and integrate it into sqlx query API. With blanked impl for anything implementingIntoArguments
, it might (not sure though) even be drop-in replacement forquery_*()
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, likesqlx::query*()
functions.It needs 'magic looking' trait bound linking
Database::Arguments
andIntoArguments
trait that is seemingly not related to the function, not necessary but still required to compile.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 :-DIntoArguments
impl is not part ofDatabase::Arguments
type trait boundsArguments<Database=D>
withIntoArguments<DB>
The text was updated successfully, but these errors were encountered: