diesel/query_dsl/
locking_dsl.rs

1use crate::expression::TypedExpressionType;
2use crate::expression::ValidGrouping;
3use crate::query_builder::AsQuery;
4use crate::query_builder::FromClause;
5use crate::query_builder::SelectStatement;
6use crate::query_source::Table;
7use crate::Expression;
8
9/// Methods related to locking select statements
10///
11/// This trait should not be relied on directly by most apps. Its behavior is
12/// provided by [`QueryDsl`]. However, you may need a where clause on this trait
13/// to call `for_update` from generic code.
14///
15/// [`QueryDsl`]: crate::QueryDsl
16#[diagnostic::on_unimplemented(
17    note = "a `LOCKING` clause is incompatible with various other clauses like a `DISTINCT` clause"
18)]
19pub trait LockingDsl<Lock> {
20    /// The type returned by `set_lock`. See [`dsl::ForUpdate`] and friends for
21    /// convenient access to this type.
22    ///
23    /// [`dsl::ForUpdate`]: crate::dsl::ForUpdate
24    type Output;
25
26    /// See the trait level documentation
27    fn with_lock(self, lock: Lock) -> Self::Output;
28}
29
30#[diagnostic::do_not_recommend]
31impl<T, Lock> LockingDsl<Lock> for T
32where
33    T: Table + AsQuery<Query = SelectStatement<FromClause<T>>>,
34    T::DefaultSelection: Expression<SqlType = T::SqlType> + ValidGrouping<()>,
35    T::SqlType: TypedExpressionType,
36{
37    type Output = <SelectStatement<FromClause<T>> as LockingDsl<Lock>>::Output;
38
39    fn with_lock(self, lock: Lock) -> Self::Output {
40        self.as_query().with_lock(lock)
41    }
42}
43
44/// Methods related to modifiers on locking select statements
45///
46/// This trait should not be relied on directly by most apps. Its behavior is
47/// provided by [`QueryDsl`]. However, you may need a where clause on this trait
48/// to call `skip_locked` from generic code.
49///
50/// [`QueryDsl`]: crate::QueryDsl
51pub trait ModifyLockDsl<Modifier> {
52    /// The type returned by `modify_lock`. See [`dsl::SkipLocked`] and friends
53    /// for convenient access to this type.
54    ///
55    /// [`dsl::SkipLocked`]: crate::dsl::SkipLocked
56    type Output;
57
58    /// See the trait level documentation
59    fn modify_lock(self, modifier: Modifier) -> Self::Output;
60}