Skip to main content

diesel/pg/query_builder/
limit_offset.rs

1use crate::pg::Pg;
2use crate::query_builder::limit_offset_clause::{
3    BoxedCloneLimitOffsetClause, BoxedLimitOffsetClause, LimitOffsetClause,
4};
5use crate::query_builder::{AstPass, IntoBoxedClause, IntoBoxedCloneClause, QueryFragment};
6use crate::result::QueryResult;
7use alloc::sync::Arc;
8
9impl<'a, L, O> IntoBoxedCloneClause<'a, Pg> for LimitOffsetClause<L, O>
10where
11    L: QueryFragment<Pg> + Send + Sync + 'a,
12    O: QueryFragment<Pg> + Send + Sync + 'a,
13{
14    type BoxedCloneClause = BoxedCloneLimitOffsetClause<'a, Pg>;
15
16    fn into_boxed_clone(self) -> Self::BoxedCloneClause {
17        BoxedCloneLimitOffsetClause {
18            limit: Some(Arc::new(self.limit_clause)),
19            offset: Some(Arc::new(self.offset_clause)),
20        }
21    }
22}
23
24/// Walk the limit and the offset of a boxed limit/offset clause, whichever pointer holds them.
25fn walk_boxed_limit_offset<'b, P>(
26    limit: Option<&'b P>,
27    offset: Option<&'b P>,
28    mut out: AstPass<'_, 'b, Pg>,
29) -> QueryResult<()>
30where
31    P: QueryFragment<Pg>,
32{
33    if let Some(limit) = limit {
34        limit.walk_ast(out.reborrow())?;
35    }
36    if let Some(offset) = offset {
37        offset.walk_ast(out.reborrow())?;
38    }
39    Ok(())
40}
41
42impl QueryFragment<Pg> for BoxedCloneLimitOffsetClause<'_, Pg> {
43    fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
44        walk_boxed_limit_offset(self.limit.as_ref(), self.offset.as_ref(), out)
45    }
46}
47
48impl<'a, L, O> IntoBoxedClause<'a, Pg> for LimitOffsetClause<L, O>
49where
50    L: QueryFragment<Pg> + Send + 'a,
51    O: QueryFragment<Pg> + Send + 'a,
52{
53    type BoxedClause = BoxedLimitOffsetClause<'a, Pg>;
54
55    fn into_boxed(self) -> Self::BoxedClause {
56        BoxedLimitOffsetClause {
57            limit: Some(Box::new(self.limit_clause)),
58            offset: Some(Box::new(self.offset_clause)),
59        }
60    }
61}
62
63impl QueryFragment<Pg> for BoxedLimitOffsetClause<'_, Pg> {
64    fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
65        walk_boxed_limit_offset(self.limit.as_ref(), self.offset.as_ref(), out)
66    }
67}
68
69impl<L, O> QueryFragment<Pg> for LimitOffsetClause<L, O>
70where
71    L: QueryFragment<Pg>,
72    O: QueryFragment<Pg>,
73{
74    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
75        self.limit_clause.walk_ast(out.reborrow())?;
76        self.offset_clause.walk_ast(out.reborrow())?;
77        Ok(())
78    }
79}