try to make db genericerer

This commit is contained in:
2023-09-15 18:30:19 +02:00
parent 33373006e6
commit a2ff43cd05
4 changed files with 52 additions and 36 deletions

View File

@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "\n DELETE FROM trip_todos\n WHERE\n id = ?\n AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?)\n ",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "182db3fb919cf345f2b05f1c1325cc939b54336c66890892859c2c0f97930c63"
}

View File

@@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "\n DELETE FROM trip_todos\n WHERE \n id = ?\n AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?)\n ",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "d47bf74e8aaecdeb3730edab4d267f61ab03356497e777e55493c4f188e14a25"
}

View File

@@ -52,29 +52,41 @@ pub mod crud {
} }
#[async_trait] #[async_trait]
pub trait Delete: Sized { pub trait Delete<'c>: Sized {
type Id; type Id;
type Filter; type Filter;
async fn delete( async fn delete<T>(
ctx: &Context, ctx: &Context,
pool: impl sqlx::Acquire, db: T,
filter: Self::Filter, filter: Self::Filter,
id: Self::Id, id: Self::Id,
) -> Result<bool, Error>; ) -> Result<bool, Error>
where
// we require something that allows us to get something that implements
// executor from a Sqlite database
//
// in practice, this will either be a pool or a transaction
//
// * A pool will let us begin() a new transaction directly and then
// acquire() a new conncetion
//
// * A transaction will begin() (a noop) and then acquire() a new connection
T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug;
async fn delete_all( // async fn delete_all(
ctx: &Context, // ctx: &Context,
pool: &sqlite::Pool, // pool: &sqlite::Pool,
filter: Self::Filter, // filter: Self::Filter,
ids: Vec<Self::Id>, // ids: Vec<Self::Id>,
) -> Result<bool, Error> { // ) -> Result<bool, Error> {
let mut transaction = pool.begin().await?; // let mut transaction = pool.begin().await?;
for id in ids { // for id in ids {
Self::delete(ctx, &mut transaction, filter, id).await?; // Self::delete(ctx, Box::new(&mut *transaction), filter, id).await?;
} // }
} // unimplemented!()
// }
} }
} }

View File

@@ -294,29 +294,33 @@ impl crud::Update for Todo {
} }
#[async_trait] #[async_trait]
impl crud::Delete for Todo { impl<'c> crud::Delete<'c> for Todo {
type Id = Uuid; type Id = Uuid;
type Filter = TodoFilter; type Filter = TodoFilter;
#[tracing::instrument] #[tracing::instrument]
async fn delete( async fn delete<T>(ctx: &Context, db: T, filter: TodoFilter, id: Uuid) -> Result<bool, Error>
ctx: &Context, where
pool: &sqlite::Pool, T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug,
filter: TodoFilter, {
id: Uuid, use sqlx::Acquire;
) -> Result<bool, Error> {
let id_param = id.to_string(); let id_param = id.to_string();
let user_id = ctx.user.id.to_string(); let user_id = ctx.user.id.to_string();
let trip_id_param = filter.trip_id.to_string(); let trip_id_param = filter.trip_id.to_string();
let mut transaction = db.begin().await?;
let conn = transaction.acquire().await?;
let results = crate::execute!( let results = crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
query_type: sqlite::QueryType::Delete, query_type: sqlite::QueryType::Delete,
component: sqlite::Component::Todo, component: sqlite::Component::Todo,
}, },
pool, conn,
r#" r#"
DELETE FROM trip_todos DELETE FROM trip_todos
WHERE WHERE
id = ? id = ?
AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?) AND EXISTS (SELECT 1 FROM trips WHERE trip_id = ? AND user_id = ?)
"#, "#,