I want to be able to instantiate Db with both sqlx::Pool<sqlx::Postgres> and sqlx::Transaction<'_, sqlx::Postgres>, so that I can run the same queries either directly or multiple within a transaction.
And from my understanding both should implement the trait sqlx::PgExecutor, but when I try to instantiate Db with sqlx::Transaction<'_, sqlx::Postgres> I get the following error:
expected `Pool<Postgres>`, found `Transaction<'_, Postgres>`
Full code:
#[derive(Clone)]
pub struct Db<T>
where
for<'a> &'a T: sqlx::PgExecutor<'a>,
{
pub executor: T,
}
impl<T> Db<T>
where
for<'a> &'a T: sqlx::PgExecutor<'a>,
{
pub fn new(executor: T) -> Self {
Self { executor }
}
}
impl<T> Db<T>
where
for<'a> &'a T: sqlx::PgExecutor<'a>,
{
pub async fn get_user(&self) -> Result<i64, sqlx::Error> {
sqlx::query_scalar(r#"SELECT "id" FROM "user""#)
.fetch_one(&self.executor)
.await
}
}
pub async fn test() {
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(100)
.connect("postgres://postgres:postgres@localhost:5432/db")
.await
.expect("Could not initialize PgPool");
//let db = Db::new(pool);
let tx = match pool.begin().await {
Ok(value) => value,
Err(..) => return,
};
let db = Db::new(tx);
}
I saw this thread earlier, as it's related to a problem I had in the project that motivated the reply you linked to! Thanks for your solution - I like what you've done here and it gives me a couple of ideas.