cratestack_sqlx/delegate/
scoped_find_many_projected.rs1use cratestack_core::{CratestackContext, CratestackError};
4
5use crate::{Filter, FilterExpr, OrderClause, sqlx};
6
7#[derive(Clone)]
8pub struct ScopedProjectedFindMany<'a, M: 'static, PK: 'static> {
9 pub(super) request: crate::ProjectedFindMany<'a, M, PK>,
10 pub(super) ctx: CratestackContext,
11}
12
13impl<'a, M: 'static, PK: 'static> ScopedProjectedFindMany<'a, M, PK> {
14 pub(super) fn new(
15 request: crate::ProjectedFindMany<'a, M, PK>,
16 ctx: CratestackContext,
17 ) -> Self {
18 Self { request, ctx }
19 }
20
21 pub fn where_(mut self, filter: Filter) -> Self {
22 self.request = self.request.where_(filter);
23 self
24 }
25
26 pub fn where_expr(mut self, filter: FilterExpr) -> Self {
27 self.request = self.request.where_expr(filter);
28 self
29 }
30
31 pub fn where_any(mut self, filters: impl IntoIterator<Item = FilterExpr>) -> Self {
32 self.request = self.request.where_any(filters);
33 self
34 }
35
36 pub fn where_optional<F>(mut self, filter: Option<F>) -> Self
37 where
38 F: Into<FilterExpr>,
39 {
40 self.request = self.request.where_optional(filter);
41 self
42 }
43
44 pub fn order_by(mut self, clause: OrderClause) -> Self {
45 self.request = self.request.order_by(clause);
46 self
47 }
48
49 pub fn limit(mut self, limit: i64) -> Self {
50 self.request = self.request.limit(limit);
51 self
52 }
53
54 pub fn offset(mut self, offset: i64) -> Self {
55 self.request = self.request.offset(offset);
56 self
57 }
58
59 pub fn for_update(mut self) -> Self {
60 self.request = self.request.for_update();
61 self
62 }
63
64 pub async fn run(self) -> Result<Vec<cratestack_sql::Projection<M>>, CratestackError>
65 where
66 M: crate::FromPartialPgRow,
67 {
68 self.request.run(&self.ctx).await
69 }
70
71 pub async fn run_in_tx<'tx>(
72 self,
73 tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
74 ) -> Result<Vec<cratestack_sql::Projection<M>>, CratestackError>
75 where
76 M: crate::FromPartialPgRow,
77 {
78 self.request.run_in_tx(tx, &self.ctx).await
79 }
80}