Skip to main content

cratestack_sqlx/delegate/
scoped_find_unique.rs

1//! `ScopedFindUnique` + the projected exit `ScopedProjectedFindUnique`.
2
3use cratestack_core::{CoolContext, CoolError};
4
5use crate::{FindUnique, sqlx};
6
7#[derive(Debug, Clone)]
8pub struct ScopedFindUnique<'a, M: 'static, PK: 'static> {
9    pub(super) request: FindUnique<'a, M, PK>,
10    pub(super) ctx: CoolContext,
11}
12
13impl<'a, M: 'static, PK: 'static> ScopedFindUnique<'a, M, PK> {
14    pub(super) fn new(request: FindUnique<'a, M, PK>, ctx: CoolContext) -> Self {
15        Self { request, ctx }
16    }
17
18    /// See [`FindUnique::for_update`].
19    pub fn for_update(mut self) -> Self {
20        self.request = self.request.for_update();
21        self
22    }
23
24    /// See [`FindUnique::as_detail`].
25    pub fn as_detail(mut self) -> Self {
26        self.request = self.request.as_detail();
27        self
28    }
29
30    /// See [`FindUnique::as_list`].
31    pub fn as_list(mut self) -> Self {
32        self.request = self.request.as_list();
33        self
34    }
35
36    pub fn preview_sql(&self) -> String {
37        self.request.preview_sql()
38    }
39
40    pub fn preview_scoped_sql(&self) -> String {
41        self.request.preview_scoped_sql(&self.ctx)
42    }
43
44    pub async fn run(self) -> Result<Option<M>, CoolError>
45    where
46        for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow>,
47        PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
48    {
49        self.request.run(&self.ctx).await
50    }
51
52    pub async fn run_in_tx<'tx>(
53        self,
54        tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
55    ) -> Result<Option<M>, CoolError>
56    where
57        for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow>,
58        PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
59    {
60        self.request.run_in_tx(tx, &self.ctx).await
61    }
62
63    /// See [`FindUnique::select`].
64    pub fn select<I, C>(self, columns: I) -> ScopedProjectedFindUnique<'a, M, PK>
65    where
66        I: IntoIterator<Item = C>,
67        C: cratestack_sql::IntoColumnName,
68    {
69        ScopedProjectedFindUnique {
70            request: self.request.select(columns),
71            ctx: self.ctx,
72        }
73    }
74}
75
76#[derive(Debug, Clone)]
77pub struct ScopedProjectedFindUnique<'a, M: 'static, PK: 'static> {
78    request: crate::ProjectedFindUnique<'a, M, PK>,
79    ctx: CoolContext,
80}
81
82impl<'a, M: 'static, PK: 'static> ScopedProjectedFindUnique<'a, M, PK> {
83    pub fn as_detail(mut self) -> Self {
84        self.request = self.request.as_detail();
85        self
86    }
87
88    pub fn as_list(mut self) -> Self {
89        self.request = self.request.as_list();
90        self
91    }
92
93    pub fn for_update(mut self) -> Self {
94        self.request = self.request.for_update();
95        self
96    }
97
98    pub async fn run(self) -> Result<Option<cratestack_sql::Projection<M>>, CoolError>
99    where
100        M: crate::FromPartialPgRow,
101        PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
102    {
103        self.request.run(&self.ctx).await
104    }
105
106    pub async fn run_in_tx<'tx>(
107        self,
108        tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
109    ) -> Result<Option<cratestack_sql::Projection<M>>, CoolError>
110    where
111        M: crate::FromPartialPgRow,
112        PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
113    {
114        self.request.run_in_tx(tx, &self.ctx).await
115    }
116}