cratestack_sqlx/delegate/
scoped_update_many.rs1use cratestack_core::{CoolContext, CoolError};
5
6use crate::{Filter, FilterExpr, UpdateMany, UpdateManySet, UpdateModelInput, sqlx};
7
8#[derive(Debug, Clone)]
9pub struct ScopedUpdateMany<'a, M: 'static, PK: 'static> {
10 request: UpdateMany<'a, M, PK>,
11 ctx: CoolContext,
12}
13
14impl<'a, M: 'static, PK: 'static> ScopedUpdateMany<'a, M, PK> {
15 pub(super) fn new(request: UpdateMany<'a, M, PK>, ctx: CoolContext) -> Self {
16 Self { request, ctx }
17 }
18
19 pub fn where_(mut self, filter: Filter) -> Self {
20 self.request = self.request.where_(filter);
21 self
22 }
23
24 pub fn where_expr(mut self, filter: FilterExpr) -> Self {
25 self.request = self.request.where_expr(filter);
26 self
27 }
28
29 pub fn where_any(mut self, filters: impl IntoIterator<Item = FilterExpr>) -> Self {
30 self.request = self.request.where_any(filters);
31 self
32 }
33
34 pub fn where_optional<F>(mut self, filter: Option<F>) -> Self
36 where
37 F: Into<FilterExpr>,
38 {
39 self.request = self.request.where_optional(filter);
40 self
41 }
42
43 pub fn set<I>(self, input: I) -> ScopedUpdateManySet<'a, M, PK, I> {
44 ScopedUpdateManySet {
45 request: self.request.set(input),
46 ctx: self.ctx,
47 }
48 }
49}
50
51#[derive(Debug, Clone)]
52pub struct ScopedUpdateManySet<'a, M: 'static, PK: 'static, I> {
53 request: UpdateManySet<'a, M, PK, I>,
54 ctx: CoolContext,
55}
56
57impl<'a, M: 'static, PK: 'static, I> ScopedUpdateManySet<'a, M, PK, I>
58where
59 I: UpdateModelInput<M>,
60{
61 pub fn preview_sql(&self) -> String {
62 self.request.preview_sql()
63 }
64
65 pub async fn run(self) -> Result<cratestack_core::BatchSummary, CoolError>
66 where
67 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
68 {
69 self.request.run(&self.ctx).await
70 }
71
72 pub async fn run_in_tx<'tx>(
73 self,
74 tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
75 ) -> Result<cratestack_core::BatchSummary, CoolError>
76 where
77 for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
78 {
79 self.request.run_in_tx(tx, &self.ctx).await
80 }
81}