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]
pub trait Delete: Sized {
pub trait Delete<'c>: Sized {
type Id;
type Filter;
async fn delete(
async fn delete<T>(
ctx: &Context,
pool: impl sqlx::Acquire,
db: T,
filter: Self::Filter,
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(
ctx: &Context,
pool: &sqlite::Pool,
filter: Self::Filter,
ids: Vec<Self::Id>,
) -> Result<bool, Error> {
let mut transaction = pool.begin().await?;
// async fn delete_all(
// ctx: &Context,
// pool: &sqlite::Pool,
// filter: Self::Filter,
// ids: Vec<Self::Id>,
// ) -> Result<bool, Error> {
// let mut transaction = pool.begin().await?;
for id in ids {
Self::delete(ctx, &mut transaction, filter, id).await?;
}
}
// for id in ids {
// Self::delete(ctx, Box::new(&mut *transaction), filter, id).await?;
// }
// unimplemented!()
// }
}
}

View File

@@ -294,26 +294,30 @@ impl crud::Update for Todo {
}
#[async_trait]
impl crud::Delete for Todo {
impl<'c> crud::Delete<'c> for Todo {
type Id = Uuid;
type Filter = TodoFilter;
#[tracing::instrument]
async fn delete(
ctx: &Context,
pool: &sqlite::Pool,
filter: TodoFilter,
id: Uuid,
) -> Result<bool, Error> {
async fn delete<T>(ctx: &Context, db: T, filter: TodoFilter, id: Uuid) -> Result<bool, Error>
where
T: sqlx::Acquire<'c, Database = sqlx::Sqlite> + Send + std::fmt::Debug,
{
use sqlx::Acquire;
let id_param = id.to_string();
let user_id = ctx.user.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!(
&sqlite::QueryClassification {
query_type: sqlite::QueryType::Delete,
component: sqlite::Component::Todo,
},
pool,
conn,
r#"
DELETE FROM trip_todos
WHERE