use sqlx::{ AssertSqlSafe, FromRow, Postgres, Transaction, postgres::{PgArguments, PgQueryResult, PgRow}, }; pub struct AppTransaction<'a> { pub inner: Transaction<'a, Postgres>, } impl<'a> AppTransaction<'a> { pub fn inner_mut(&mut self) -> &mut Transaction<'a, Postgres> { &mut self.inner } pub async fn commit(self) -> Result<(), sqlx::Error> { self.inner.commit().await } pub async fn rollback(self) -> Result<(), sqlx::Error> { self.inner.rollback().await } pub async fn execute( &mut self, sql: &str, ) -> Result { self.execute_with_args(sql, PgArguments::default()).await } pub async fn execute_with_args( &mut self, sql: &str, args: PgArguments, ) -> Result { sqlx::query_with(AssertSqlSafe(sql.to_owned()), args) .execute(&mut *self.inner) .await } pub async fn fetch_one(&mut self, sql: &str) -> Result where for<'r> T: FromRow<'r, PgRow> + Send + Unpin, { self.fetch_one_with_args(sql, PgArguments::default()).await } pub async fn fetch_one_with_args( &mut self, sql: &str, args: PgArguments, ) -> Result where for<'r> T: FromRow<'r, PgRow> + Send + Unpin, { sqlx::query_as_with::<_, T, _>(AssertSqlSafe(sql.to_owned()), args) .fetch_one(&mut *self.inner) .await } pub async fn fetch_optional( &mut self, sql: &str, ) -> Result, sqlx::Error> where for<'r> T: FromRow<'r, PgRow> + Send + Unpin, { self.fetch_optional_with_args(sql, PgArguments::default()) .await } pub async fn fetch_optional_with_args( &mut self, sql: &str, args: PgArguments, ) -> Result, sqlx::Error> where for<'r> T: FromRow<'r, PgRow> + Send + Unpin, { sqlx::query_as_with::<_, T, _>(AssertSqlSafe(sql.to_owned()), args) .fetch_optional(&mut *self.inner) .await } pub async fn fetch_all( &mut self, sql: &str, ) -> Result, sqlx::Error> where for<'r> T: FromRow<'r, PgRow> + Send + Unpin, { self.fetch_all_with_args(sql, PgArguments::default()).await } pub async fn fetch_all_with_args( &mut self, sql: &str, args: PgArguments, ) -> Result, sqlx::Error> where for<'r> T: FromRow<'r, PgRow> + Send + Unpin, { sqlx::query_as_with::<_, T, _>(AssertSqlSafe(sql.to_owned()), args) .fetch_all(&mut *self.inner) .await } }