1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![allow(missing_docs)]
mod boxed;
mod dsl_impls;
pub use self::boxed::BoxedSelectStatement;
use super::distinct_clause::NoDistinctClause;
use super::group_by_clause::NoGroupByClause;
use super::limit_clause::NoLimitClause;
use super::locking_clause::NoLockingClause;
use super::offset_clause::NoOffsetClause;
use super::order_clause::NoOrderClause;
use super::select_clause::*;
use super::where_clause::*;
use super::{AstPass, Query, QueryFragment};
use backend::Backend;
use expression::subselect::ValidSubselect;
use expression::*;
use query_builder::SelectQuery;
use query_source::joins::{AppendSelection, Inner, Join};
use query_source::*;
use result::QueryResult;
#[derive(Debug, Clone, Copy, QueryId)]
#[doc(hidden)]
#[must_use = "Queries are only executed when calling `load`, `get_result` or similar."]
pub struct SelectStatement<
From,
Select = DefaultSelectClause,
Distinct = NoDistinctClause,
Where = NoWhereClause,
Order = NoOrderClause,
Limit = NoLimitClause,
Offset = NoOffsetClause,
GroupBy = NoGroupByClause,
Locking = NoLockingClause,
> {
pub(crate) select: Select,
pub(crate) from: From,
pub(crate) distinct: Distinct,
pub(crate) where_clause: Where,
pub(crate) order: Order,
pub(crate) limit: Limit,
pub(crate) offset: Offset,
pub(crate) group_by: GroupBy,
pub(crate) locking: Locking,
}
impl<F, S, D, W, O, L, Of, G, LC> SelectStatement<F, S, D, W, O, L, Of, G, LC> {
#[allow(clippy::too_many_arguments)]
pub fn new(
select: S,
from: F,
distinct: D,
where_clause: W,
order: O,
limit: L,
offset: Of,
group_by: G,
locking: LC,
) -> Self {
SelectStatement {
select: select,
from: from,
distinct: distinct,
where_clause: where_clause,
order: order,
limit: limit,
offset: offset,
group_by: group_by,
locking: locking,
}
}
}
impl<F> SelectStatement<F> {
pub fn simple(from: F) -> Self {
SelectStatement::new(
DefaultSelectClause,
from,
NoDistinctClause,
NoWhereClause,
NoOrderClause,
NoLimitClause,
NoOffsetClause,
NoGroupByClause,
NoLockingClause,
)
}
}
impl<F, S, D, W, O, L, Of, G, LC> Query for SelectStatement<F, S, D, W, O, L, Of, G, LC>
where
S: SelectClauseExpression<F>,
W: ValidWhereClause<F>,
{
type SqlType = S::SelectClauseSqlType;
}
impl<F, S, D, W, O, L, Of, G, LC> SelectQuery for SelectStatement<F, S, D, W, O, L, Of, G, LC>
where
S: SelectClauseExpression<F>,
{
type SqlType = S::SelectClauseSqlType;
}
impl<F, S, D, W, O, L, Of, G, LC, DB> QueryFragment<DB>
for SelectStatement<F, S, D, W, O, L, Of, G, LC>
where
DB: Backend,
S: SelectClauseQueryFragment<F, DB>,
F: QuerySource,
F::FromClause: QueryFragment<DB>,
D: QueryFragment<DB>,
W: QueryFragment<DB>,
O: QueryFragment<DB>,
L: QueryFragment<DB>,
Of: QueryFragment<DB>,
G: QueryFragment<DB>,
LC: QueryFragment<DB>,
{
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
out.push_sql("SELECT ");
self.distinct.walk_ast(out.reborrow())?;
self.select.walk_ast(&self.from, out.reborrow())?;
out.push_sql(" FROM ");
self.from.from_clause().walk_ast(out.reborrow())?;
self.where_clause.walk_ast(out.reborrow())?;
self.group_by.walk_ast(out.reborrow())?;
self.order.walk_ast(out.reborrow())?;
self.limit.walk_ast(out.reborrow())?;
self.offset.walk_ast(out.reborrow())?;
self.locking.walk_ast(out.reborrow())?;
Ok(())
}
}
impl<S, D, W, O, L, Of, G, LC, DB> QueryFragment<DB>
for SelectStatement<(), S, D, W, O, L, Of, G, LC>
where
DB: Backend,
S: SelectClauseQueryFragment<(), DB>,
D: QueryFragment<DB>,
W: QueryFragment<DB>,
O: QueryFragment<DB>,
L: QueryFragment<DB>,
Of: QueryFragment<DB>,
G: QueryFragment<DB>,
LC: QueryFragment<DB>,
{
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
out.push_sql("SELECT ");
self.distinct.walk_ast(out.reborrow())?;
self.select.walk_ast(&(), out.reborrow())?;
self.where_clause.walk_ast(out.reborrow())?;
self.group_by.walk_ast(out.reborrow())?;
self.order.walk_ast(out.reborrow())?;
self.limit.walk_ast(out.reborrow())?;
self.offset.walk_ast(out.reborrow())?;
self.locking.walk_ast(out.reborrow())?;
Ok(())
}
}
impl<S, F, D, W, O, L, Of, G, LC, QS> ValidSubselect<QS>
for SelectStatement<F, S, D, W, O, L, Of, LC, G>
where
Self: SelectQuery,
W: ValidWhereClause<Join<F, QS, Inner>>,
{
}
impl<From, T> AppearsInFromClause<T> for SelectStatement<From>
where
From: AppearsInFromClause<T>,
{
type Count = From::Count;
}
impl<From> QuerySource for SelectStatement<From>
where
From: QuerySource,
From::DefaultSelection: SelectableExpression<Self>,
{
type FromClause = From::FromClause;
type DefaultSelection = From::DefaultSelection;
fn from_clause(&self) -> Self::FromClause {
self.from.from_clause()
}
fn default_selection(&self) -> Self::DefaultSelection {
self.from.default_selection()
}
}
impl<From, Selection> AppendSelection<Selection> for SelectStatement<From>
where
From: AppendSelection<Selection>,
{
type Output = From::Output;
fn append_selection(&self, selection: Selection) -> Self::Output {
self.from.append_selection(selection)
}
}