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
use backend::Backend;
use expression::subselect::Subselect;
use expression::*;
use query_builder::*;
use result::QueryResult;
use sql_types::Bool;
#[derive(Debug, Copy, Clone, QueryId)]
pub struct In<T, U> {
left: T,
values: U,
}
#[derive(Debug, Copy, Clone, QueryId)]
pub struct NotIn<T, U> {
left: T,
values: U,
}
impl<T, U> In<T, U> {
pub fn new(left: T, values: U) -> Self {
In {
left: left,
values: values,
}
}
}
impl<T, U> NotIn<T, U> {
pub fn new(left: T, values: U) -> Self {
NotIn {
left: left,
values: values,
}
}
}
impl<T, U> Expression for In<T, U>
where
T: Expression,
U: Expression<SqlType = T::SqlType>,
{
type SqlType = Bool;
}
impl<T, U> Expression for NotIn<T, U>
where
T: Expression,
U: Expression<SqlType = T::SqlType>,
{
type SqlType = Bool;
}
impl<T, U> NonAggregate for In<T, U> where In<T, U>: Expression {}
impl<T, U> NonAggregate for NotIn<T, U> where NotIn<T, U>: Expression {}
impl<T, U, DB> QueryFragment<DB> for In<T, U>
where
DB: Backend,
T: QueryFragment<DB>,
U: QueryFragment<DB> + MaybeEmpty,
{
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
if self.values.is_empty() {
out.push_sql("1=0");
} else {
self.left.walk_ast(out.reborrow())?;
out.push_sql(" IN (");
self.values.walk_ast(out.reborrow())?;
out.push_sql(")");
}
Ok(())
}
}
impl<T, U, DB> QueryFragment<DB> for NotIn<T, U>
where
DB: Backend,
T: QueryFragment<DB>,
U: QueryFragment<DB> + MaybeEmpty,
{
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
if self.values.is_empty() {
out.push_sql("1=1");
} else {
self.left.walk_ast(out.reborrow())?;
out.push_sql(" NOT IN (");
self.values.walk_ast(out.reborrow())?;
out.push_sql(")");
}
Ok(())
}
}
impl_selectable_expression!(In<T, U>);
impl_selectable_expression!(NotIn<T, U>);
use query_builder::{BoxedSelectStatement, SelectStatement};
pub trait AsInExpression<T> {
type InExpression: MaybeEmpty + Expression<SqlType = T>;
fn as_in_expression(self) -> Self::InExpression;
}
impl<I, T, ST> AsInExpression<ST> for I
where
I: IntoIterator<Item = T>,
T: AsExpression<ST>,
{
type InExpression = Many<T::Expression>;
fn as_in_expression(self) -> Self::InExpression {
let expressions = self.into_iter().map(AsExpression::as_expression).collect();
Many(expressions)
}
}
pub trait MaybeEmpty {
fn is_empty(&self) -> bool;
}
impl<ST, S, F, W, O, L, Of, G, LC> AsInExpression<ST> for SelectStatement<S, F, W, O, L, Of, G, LC>
where
Subselect<Self, ST>: Expression<SqlType = ST>,
Self: SelectQuery<SqlType = ST>,
{
type InExpression = Subselect<Self, ST>;
fn as_in_expression(self) -> Self::InExpression {
Subselect::new(self)
}
}
impl<'a, ST, QS, DB> AsInExpression<ST> for BoxedSelectStatement<'a, ST, QS, DB>
where
Subselect<BoxedSelectStatement<'a, ST, QS, DB>, ST>: Expression<SqlType = ST>,
{
type InExpression = Subselect<Self, ST>;
fn as_in_expression(self) -> Self::InExpression {
Subselect::new(self)
}
}
#[derive(Debug, Clone)]
pub struct Many<T>(Vec<T>);
impl<T: Expression> Expression for Many<T> {
type SqlType = T::SqlType;
}
impl<T> MaybeEmpty for Many<T> {
fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<T, QS> SelectableExpression<QS> for Many<T>
where
Many<T>: AppearsOnTable<QS>,
T: SelectableExpression<QS>,
{
}
impl<T, QS> AppearsOnTable<QS> for Many<T>
where
Many<T>: Expression,
T: AppearsOnTable<QS>,
{
}
impl<T, DB> QueryFragment<DB> for Many<T>
where
DB: Backend,
T: QueryFragment<DB>,
{
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
out.unsafe_to_cache_prepared();
let mut first = true;
for value in &self.0 {
if first {
first = false;
} else {
out.push_sql(", ");
}
value.walk_ast(out.reborrow())?;
}
Ok(())
}
}
impl<T> QueryId for Many<T> {
type QueryId = ();
const HAS_STATIC_QUERY_ID: bool = false;
}