Skip to main content

diesel/sqlite/query_builder/
limit_offset.rs

1use crate::query_builder::limit_clause::{LimitClause, NoLimitClause};
2use crate::query_builder::limit_offset_clause::{
3    BoxedCloneLimitOffsetClause, BoxedLimitOffsetClause, LimitOffsetClause,
4};
5use crate::query_builder::offset_clause::{NoOffsetClause, OffsetClause};
6use crate::query_builder::{AstPass, IntoBoxedClause, IntoBoxedCloneClause, QueryFragment};
7use crate::result::QueryResult;
8use crate::sqlite::Sqlite;
9use alloc::boxed::Box;
10use alloc::sync::Arc;
11
12impl QueryFragment<Sqlite> for LimitOffsetClause<NoLimitClause, NoOffsetClause> {
13    fn walk_ast<'b>(&'b self, _out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
14        Ok(())
15    }
16}
17
18impl<L> QueryFragment<Sqlite> for LimitOffsetClause<LimitClause<L>, NoOffsetClause>
19where
20    LimitClause<L>: QueryFragment<Sqlite>,
21{
22    fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
23        self.limit_clause.walk_ast(out)?;
24        Ok(())
25    }
26}
27
28impl<O> QueryFragment<Sqlite> for LimitOffsetClause<NoLimitClause, OffsetClause<O>>
29where
30    OffsetClause<O>: QueryFragment<Sqlite>,
31{
32    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
33        // Sqlite requires a limit clause in front of any offset clause
34        // using `LIMIT -1` is the same as not having any limit clause
35        // https://sqlite.org/lang_select.html
36        out.push_sql(" LIMIT -1 ");
37        self.offset_clause.walk_ast(out)?;
38        Ok(())
39    }
40}
41
42impl<L, O> QueryFragment<Sqlite> for LimitOffsetClause<LimitClause<L>, OffsetClause<O>>
43where
44    LimitClause<L>: QueryFragment<Sqlite>,
45    OffsetClause<O>: QueryFragment<Sqlite>,
46{
47    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
48        self.limit_clause.walk_ast(out.reborrow())?;
49        self.offset_clause.walk_ast(out.reborrow())?;
50        Ok(())
51    }
52}
53
54/// Walk the limit and the offset of a boxed limit/offset clause, whichever pointer holds them.
55fn walk_boxed_limit_offset<'b, P>(
56    limit: Option<&'b P>,
57    offset: Option<&'b P>,
58    mut out: AstPass<'_, 'b, Sqlite>,
59) -> QueryResult<()>
60where
61    P: QueryFragment<Sqlite>,
62{
63    match (limit, offset) {
64        (Some(limit), Some(offset)) => {
65            limit.walk_ast(out.reborrow())?;
66            offset.walk_ast(out.reborrow())?;
67        }
68        (Some(limit), None) => {
69            limit.walk_ast(out.reborrow())?;
70        }
71        (None, Some(offset)) => {
72            // See the `QueryFragment` implementation for `LimitOffsetClause` for details.
73            out.push_sql(" LIMIT -1 ");
74            offset.walk_ast(out.reborrow())?;
75        }
76        (None, None) => {}
77    }
78    Ok(())
79}
80
81impl QueryFragment<Sqlite> for BoxedLimitOffsetClause<'_, Sqlite> {
82    fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
83        walk_boxed_limit_offset(self.limit.as_ref(), self.offset.as_ref(), out)
84    }
85}
86
87impl QueryFragment<Sqlite> for BoxedCloneLimitOffsetClause<'_, Sqlite> {
88    fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
89        walk_boxed_limit_offset(self.limit.as_ref(), self.offset.as_ref(), out)
90    }
91}
92
93// Have explicit impls here because we need to set `Some`/`None` for the clauses
94// correspondingly, otherwise we cannot match on it in the `QueryFragment` impl
95// above
96impl<'a> IntoBoxedClause<'a, Sqlite> for LimitOffsetClause<NoLimitClause, NoOffsetClause> {
97    type BoxedClause = BoxedLimitOffsetClause<'a, Sqlite>;
98
99    fn into_boxed(self) -> Self::BoxedClause {
100        BoxedLimitOffsetClause {
101            limit: None,
102            offset: None,
103        }
104    }
105}
106
107impl<'a, L> IntoBoxedClause<'a, Sqlite> for LimitOffsetClause<LimitClause<L>, NoOffsetClause>
108where
109    L: QueryFragment<Sqlite> + Send + 'a,
110{
111    type BoxedClause = BoxedLimitOffsetClause<'a, Sqlite>;
112
113    fn into_boxed(self) -> Self::BoxedClause {
114        BoxedLimitOffsetClause {
115            limit: Some(Box::new(self.limit_clause)),
116            offset: None,
117        }
118    }
119}
120
121impl<'a, O> IntoBoxedClause<'a, Sqlite> for LimitOffsetClause<NoLimitClause, OffsetClause<O>>
122where
123    O: QueryFragment<Sqlite> + Send + 'a,
124{
125    type BoxedClause = BoxedLimitOffsetClause<'a, Sqlite>;
126
127    fn into_boxed(self) -> Self::BoxedClause {
128        BoxedLimitOffsetClause {
129            limit: None,
130            offset: Some(Box::new(self.offset_clause)),
131        }
132    }
133}
134
135impl<'a, L, O> IntoBoxedClause<'a, Sqlite> for LimitOffsetClause<LimitClause<L>, OffsetClause<O>>
136where
137    L: QueryFragment<Sqlite> + Send + 'a,
138    O: QueryFragment<Sqlite> + Send + 'a,
139{
140    type BoxedClause = BoxedLimitOffsetClause<'a, Sqlite>;
141
142    fn into_boxed(self) -> Self::BoxedClause {
143        BoxedLimitOffsetClause {
144            limit: Some(Box::new(self.limit_clause)),
145            offset: Some(Box::new(self.offset_clause)),
146        }
147    }
148}
149
150impl<'a> IntoBoxedCloneClause<'a, Sqlite> for LimitOffsetClause<NoLimitClause, NoOffsetClause> {
151    type BoxedCloneClause = BoxedCloneLimitOffsetClause<'a, Sqlite>;
152
153    fn into_boxed_clone(self) -> Self::BoxedCloneClause {
154        BoxedCloneLimitOffsetClause {
155            limit: None,
156            offset: None,
157        }
158    }
159}
160
161impl<'a, L> IntoBoxedCloneClause<'a, Sqlite> for LimitOffsetClause<LimitClause<L>, NoOffsetClause>
162where
163    L: QueryFragment<Sqlite> + Send + Sync + 'a,
164{
165    type BoxedCloneClause = BoxedCloneLimitOffsetClause<'a, Sqlite>;
166
167    fn into_boxed_clone(self) -> Self::BoxedCloneClause {
168        BoxedCloneLimitOffsetClause {
169            limit: Some(Arc::new(self.limit_clause)),
170            offset: None,
171        }
172    }
173}
174
175impl<'a, O> IntoBoxedCloneClause<'a, Sqlite> for LimitOffsetClause<NoLimitClause, OffsetClause<O>>
176where
177    O: QueryFragment<Sqlite> + Send + Sync + 'a,
178{
179    type BoxedCloneClause = BoxedCloneLimitOffsetClause<'a, Sqlite>;
180
181    fn into_boxed_clone(self) -> Self::BoxedCloneClause {
182        BoxedCloneLimitOffsetClause {
183            limit: None,
184            offset: Some(Arc::new(self.offset_clause)),
185        }
186    }
187}
188
189impl<'a, L, O> IntoBoxedCloneClause<'a, Sqlite>
190    for LimitOffsetClause<LimitClause<L>, OffsetClause<O>>
191where
192    L: QueryFragment<Sqlite> + Send + Sync + 'a,
193    O: QueryFragment<Sqlite> + Send + Sync + 'a,
194{
195    type BoxedCloneClause = BoxedCloneLimitOffsetClause<'a, Sqlite>;
196
197    fn into_boxed_clone(self) -> Self::BoxedCloneClause {
198        BoxedCloneLimitOffsetClause {
199            limit: Some(Arc::new(self.limit_clause)),
200            offset: Some(Arc::new(self.offset_clause)),
201        }
202    }
203}