I am trying to abstract a query over 2 backends (postgres and sqlite). I understand why the following does not work
async fn my_query<T>(p: sqlx::Pool<T>, bar: &Uuid) -> Result<(), Error>
where
T: sqlx::Database,
{
sqlx::query_as::<_, (i64,)>("select id from foo where bar = ?")
.bind(bar)
.fetch_one(&p)
.await?;
}
However, I would like to say something like:
where
T: sqlx::Sqlite AND sqlx::Postgres
In other words, can I express that I will obey the most restrictive implementation? Obviously Sqlite
and Postgres
are not traits, but the concept I want to phrase in Rust is that the restriction should apply to the most restrictive thing, whatever that may be.
I was able to find something which recommended I define a db, executor,d(encoded) value bounds, but then I also have to include the bounds for every type I want to use in the function body:
Generic function that accepts a SQLx PgPool or MySqlPool
I'm hoping rust has some nice way of expressing this, but I may be out of luck, and have to dive into a fairly messy looking implementation.