Skip to main content

cratestack_sqlx/delegate/
scoped_delete.rs

1//! Single-row + predicate-driven bulk DELETE wrappers bound to a
2//! `CoolContext`.
3
4use cratestack_core::{CoolContext, CoolError};
5
6use crate::{DeleteMany, DeleteRecord, Filter, FilterExpr, sqlx};
7
8#[derive(Debug, Clone)]
9pub struct ScopedDeleteRecord<'a, M: 'static, PK: 'static> {
10    request: DeleteRecord<'a, M, PK>,
11    ctx: CoolContext,
12}
13
14impl<'a, M: 'static, PK: 'static> ScopedDeleteRecord<'a, M, PK> {
15    pub(super) fn new(request: DeleteRecord<'a, M, PK>, ctx: CoolContext) -> Self {
16        Self { request, ctx }
17    }
18
19    pub fn preview_sql(&self) -> String {
20        self.request.preview_sql()
21    }
22
23    pub async fn run(self) -> Result<M, CoolError>
24    where
25        for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
26        PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
27    {
28        self.request.run(&self.ctx).await
29    }
30
31    pub async fn run_in_tx<'tx>(
32        self,
33        tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
34    ) -> Result<M, CoolError>
35    where
36        for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
37        PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
38    {
39        self.request.run_in_tx(tx, &self.ctx).await
40    }
41}
42
43#[derive(Debug, Clone)]
44pub struct ScopedDeleteMany<'a, M: 'static, PK: 'static> {
45    request: DeleteMany<'a, M, PK>,
46    ctx: CoolContext,
47}
48
49impl<'a, M: 'static, PK: 'static> ScopedDeleteMany<'a, M, PK> {
50    pub(super) fn new(request: DeleteMany<'a, M, PK>, ctx: CoolContext) -> Self {
51        Self { request, ctx }
52    }
53
54    pub fn where_(mut self, filter: Filter) -> Self {
55        self.request = self.request.where_(filter);
56        self
57    }
58
59    pub fn where_expr(mut self, filter: FilterExpr) -> Self {
60        self.request = self.request.where_expr(filter);
61        self
62    }
63
64    pub fn where_any(mut self, filters: impl IntoIterator<Item = FilterExpr>) -> Self {
65        self.request = self.request.where_any(filters);
66        self
67    }
68
69    /// See [`DeleteMany::where_optional`].
70    pub fn where_optional<F>(mut self, filter: Option<F>) -> Self
71    where
72        F: Into<FilterExpr>,
73    {
74        self.request = self.request.where_optional(filter);
75        self
76    }
77
78    pub fn preview_sql(&self) -> String {
79        self.request.preview_sql()
80    }
81
82    pub async fn run(self) -> Result<cratestack_core::BatchSummary, CoolError>
83    where
84        for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
85    {
86        self.request.run(&self.ctx).await
87    }
88
89    pub async fn run_in_tx<'tx>(
90        self,
91        tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
92    ) -> Result<cratestack_core::BatchSummary, CoolError>
93    where
94        for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
95    {
96        self.request.run_in_tx(tx, &self.ctx).await
97    }
98}